Thursday 24 October 2013

Concurrent audio outputs with PulseAudio in Fedora (playing through multiple speakers at the same time)

pacmd is the dogs bollocks. 

I wanted to be able to talk to my friend on skype with one of my headphones in, whilst playing a movie over HDMI on my tv from my laptop, and searching for this was not proving easy on google. Whilst searching how to fix another issue i fell upon pacmd. With pacmd, you can manipulate your audio inputs and outputs to a unexpectedly awesome amount.

Below are a few examples but first things first, if you get stuck, RTFM. I have added a few bits of grep and awkage just to make it easier to read, I hope you don't mind.



What sinks (devices..ish) do we have

[adam@localhost ~]$ pacmd list-sinks | grep index -A 1
    index: 0
    name: <alsa_output.pci-0000_00_03.0.hdmi-stereo-extra1>
--
  * index: 1
    name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
[adam@localhost ~]$ 


I have two different sinks: my HDMI out and my standard audio card, index 0 and 1 respectively. Dropping the grep gives you a bunch more useful information which I am sure if of use to lots of you.



Redirecting audio output

If you want to just swap audio to another output its pretty simple, you can just change the default sink:

[adam@localhost ~]$ pacmd set-default-sink 0

So now if I load up another application, the audio will be outputted to sink 0, which in my case is my HDMI.

This will not however redirect the audio of current applications. To list the applications which are currently linked to a sink, run the following:

[adam@localhost ~]$ pacmd list-sink-inputs | grep -Ei "(available|sink|index|application\.name)" | sed s/^[[:space:]]*//g 

>>> 1 sink input(s) available.
index: 39
sink: 1 <alsa_output.pci-0000_00_1b.0.analog-stereo>
application.name = "ALSA plug-in [plugin-container.#prelink#.QLz5gf (deleted)]"

This was a crappy youtube video that I had paused at the time, but it makes a good example. If i want to instantly redirect the output of the application all we have to do is move the index to another sink:

[adam@localhost ~]$ pacmd move-sink-input 39 0 > /dev/null
[adam@localhost ~]$ pacmd list-sink-inputs | grep -Ei "(available|sink|index|application\.name)" | sed s/^[[:space:]]*//g
 

>>> 1 sink input(s) available.
index: 39
sink: 0 <alsa_output.pci-0000_00_03.0.hdmi-stereo-extra1>
application.name = "ALSA plug-in [plugin-container.#prelink#.QLz5gf (deleted)]"


Tadaaa, it's now playing over HDMI without affecting any other application.



Playing over multiple speakers

For this we need a module, yes you read right pacmd is modular, it's just that awesome.

[adam@localhost ~]$ pacmd load-module module-combine-sink sink_name="Combined" slaves=0,1 adjust_time=5

[adam@localhost ~]$ pacmd list-sinks | grep index -A 1
    index: 0
    name: <alsa_output.pci-0000_00_03.0.hdmi-stereo-extra1>
--
  * index: 1
    name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
--
    index: 2
    name: <Combined>

[adam@localhost ~]$ pacmd set-default-sink 2



What this did was create a new sink called "Combined" joining together my HDMI card and my standard audio card together with a 5 second re-sample rate to keep things in sync. If you have issues with the speakers becoming out of time, play with the adjust_time option. By omitting the slave option, all cards will be joined.



Too quiet?

My laptop speakers aren't half bad however some web players are just SO quiet. To fix it, we can set the volume to 100%+. The following sets it to 150%

[adam@localhost ~]$ pacmd set-sink-volume 1 0x15000



Else

You can of course do other magical things, i just have not had the time to look into them further.

No comments:

Post a Comment