PulseAudio

Saturday, 11 May 2019

Date
May 11, 2019
tags

linux, technical

summary

Broadcasting or recording audio from only select applications in a Linux desktop.

Sometimes, I stream a recording of part of my desktop to strangers on the internet for some reason.

Often, the video captures just one monitor or one application and so I only want to stream audio from one or two applications. PulseAudio has a bunch of modules that let us configure it. Using these modules, we can send audio from selected applications to a dedicated sink, use a monitor of the sink as an audio source for the stream, and also listen to the sink so that we still hear the audio being streamed.


To start, load the null-sink module, creating a new sink to send sound to.

pactl load-module module-null-sink \
   sink_name=strim \
   sink_properties=device.description="StrimSink"

On success, that’ll output the module index for the sink. (This can be used to remove it with pactl unload-module.)

If we open pavucontrol, we should see our sink under “Output Devices” and a monitor for it under the “Input Devices” tab which we’ll user later. The device.description we gave when we loaded the module is what things tend to show when referring to our sink. If we didn’t set one, it’ll show up as “Null Output” or something.

At this point we can configure things to play to or record from this sink.

If we’re using OBS Studio, we can configure it to capture audio from our sink.
If we have an already running program connected to PulseAudio, we can change the sink it outputs to in the “Playback” tab of pavucontrol.

You can start a program that will output audio to that sink by setting the PULSE_SINK environment variable:

env PULSE_SINK=strim paplay /usr/share/sounds/freedesktop/stereo/bell.oga

At this point we can’t hear programs playing sound into the sink. But we should at least see a short blip on the volume monitor for the sink under the “Output Devices” tab of pavucontrol.

Technically, this is all we need to separate sound from our applications. But usually when I’m streaming audio I also want to be able to hear it. PulseAudio was kind enough to make a monitor of our sink (visible under “Input Devices”). We can use a loopback module to listen to it and specify that monitor as the input source. We could also specify a destination sink but, if we omit that, it should use the normal sink that applications play sound to by default; which is probably what we want.

pactl load-module module-loopback source=strim.monitor latency_msec=1

On success, we should be able to hear things that play into our null sink–like that paplay command mentioned earlier. This loopback module has its own volume controls and can be muted; it shows up under the “Recording” tab in pavucontrol.

Scribbled in crayon on Saturday, 11 May 2019

Tagged with linux and technical.

Categorized as “