Route application’s audio output to multiple playback devices

alsaaudiopulseaudio

I have an application that I need to record audio coming from. For that purpose I am using pavucontrol to set the audio input for the recording software to the monitor of the audio output. Now my issue is that most of the times, I do not want to hear the output. For that purpose I created a dummy output device using modprobe snd-aloop. This works great, but sometimes I want to listen in on the live audio stream, while still recording. That means I always have to change both the audio output of the sound application and the audio input of the recording application, which can get really tiring. Plus, that will result in the recording application to record everything I hear, not just the specific application.

I found this answer, but it's not exactly what I want since I don't want the sound output on all devices. Just the dummy loop and another output of my choosing. My system is arch-linux using gnome3-shell. I do NOT want this to be permanently so I need a solution that I can change on-the-fly.

Bonus question: I would love to have a shortcut that enabled/disables "live listening" (adding a second audio output to the application's audio stream), for that reason a CLI command would be great.

To be perfectly clear, I want to be able to switch between the following audio routings:

[playback app] --> [dummy aloop] --> [monitor of aloop] --> [recording app]

and

[playback app] --> [dummy aloop] --> [monitor of aloop] --> [recording app]
               \-> [headphones] (simultanously)

Best Answer

If you are using Pulseaudio anyway, have a look at Pulseaudio's modules, in particular module-loopback and module-null-sink. In my experience, snd-aloop is a bit of a pain to use, and as Pulseaudio mostly deals with sound stream transport, it's much easier in Pulseaudio.

So if the use case is "record output of one app in the background, use loadspeakers in the meantime for other stuff, occasionally listen in on the app", I'd create an appropriately named null sink, connect the app to it with pavucontrol (this will be remembered by Pulseaudio), record from the monitor source of this sink (again, this will be remembered, if necessary with command line options for the recording app to set the app name for Pulseaudio). When you want to listen in, make a loopback connection from the monitor source to your loudspeaker sink. Or make this loopback permanent, and mute/unmute it as required.

You can use pacmd or pactl to try all this out on the commandline. With the same commands you can also script your shortcuts. Make it permanent by using the Pulseaudio startup scripts.

So something along the lines of

pactl load-module module-null-sink sink_name=recsink
pactl load-module module-loopback source=recsink.monitor sink=your_loadspeaker_sink

Use pactl list short sinks to see your sink names. pactl help and pacmd help for more commands.

Related Question