HDMI sound output switcher for PulseAudio
Here is another script which you can link for example to AV Mode button, to switch sound output to HDMI and back. First I thought I can use Alsa, but it works only with one application, other application can not play sound while another one is playing. So then I did it with PulseAudio which works correctly. This switcher works for all native Gnome/gstreamer sound applications, and some applications e.g. SMplayer refuse to switch their sound regardles what default output set in PulseAudio. If you have playing sound and then switched to HDMI, it will still play through old way and you need to restart your sound e.g. push stop/play button to point sound through new way.
First of all you need to prepare PulseAudio configuration. For some point PA does not add HDMI output to devices itself. We should add it and rename internal sound card to something readable. Open /etc/pulse/defauld.pa in editor with root privileges (e.g. sudo nano /etc/pulse/default.pa in terminal) locate line “#load-module module-pipe-sink” there. Open terminal and run this command there: aplay -l | grep -o -e "card [0-9]:.*[0-9]:" | sed -e "s/card \([0-9]\): \(.*\) \[.*device \([0-9]\).*/load-module module-alsa-sink device=hw:\1,\3 sink_name=\2/"
Command returns two lines which we need to add to default.pa file after that line you located earlier. In my case this lines look like:
#load-module module-null-sink #load-module module-pipe-sink load-module module-alsa-sink device=hw:0,0 sink_name=Intel load-module module-alsa-sink device=hw:1,3 sink_name=HDMI ### Automatically load driver modules depending on the hardware available
Then you should restart PulseAudio (sudo /etc/init.d/pulseaudio restart) to changes make effect. You can check that devices added correctly by running pacmd list-sinks in terminal, you should see “name: <HDMI>” and “name <Intel>” if you have got the same names as in my case, theese names must be the same as after “sink_name” in lines you added to default.pa.
Ok, now to switcher:
#!/bin/bash # # Sound output switcher using Gnome,gconf,PulseAudio,Alsa,libnotify # # by Anton Veretenenko <anton[email sign]veretenenko.ru> notify_show=1 notify_delay=500 hdmi_card_num=1 internal_card_name=Intel hdmi_card_name=HDMI is_hdmi_on=`amixer -c $hdmi_card_num cget numid=4,iface=MIXER,name='IEC958 Playback Switch' | grep -i -c -P -e "values=on"` if [ "$is_hdmi_on" == "1" ] then amixer -c $hdmi_card_num cset numid=4,iface=MIXER,name='IEC958 Playback Switch' off > /dev/null gconftool --set "/system/gstreamer/0.10/default/musicaudiosink" --type=string "pulsesink device=\"$internal_card_name\"" gconftool --set "/system/gstreamer/0.10/default/audiosink" --type=string "pulsesink device=\"$internal_card_name\"" gconftool --set "/system/gstreamer/0.10/default/chataudiosink" --type=string "pulsesink device=\"$internal_card_name\"" pacmd "set-default-sink "$internal_card_name"" if [ $notify_show -eq 1 ] then notify-send -u normal -t $notify_delay -i gnome-sound-properties "HDMI Off" "Sound output switched to $internal_card_name" fi else amixer -c $hdmi_card_num cset numid=4,iface=MIXER,name='IEC958 Playback Switch' on > /dev/null gconftool --set "/system/gstreamer/0.10/default/musicaudiosink" --type=string "pulsesink device=\"$hdmi_card_name\"" gconftool --set "/system/gstreamer/0.10/default/audiosink" --type=string "pulsesink device=\"$hdmi_card_name\"" gconftool --set "/system/gstreamer/0.10/default/chataudiosink" --type=string "pulsesink device=\"$hdmi_card_name\"" pacmd "set-default-sink "$hdmi_card_name"" if [ $notify_show -eq 1 ] then notify-send -u normal -t $notify_delay -i gnome-sound-properties "HDMI On" "Sound output switched to $hdmi_card_name" fi fi
Here you need to change hdmi_card_num to number of your HDMI sound card (you can get it by aplay -l in terminal) and internal_card_name to name you found earlier and hdmi_card_name to hdmi card name which you saw after “sink_name” parameter.
This switcher will show a popup notification, like you see when changing brightness
P.S. How to link this script to AV Mode button (XF86Launch2) read in post about headphones switcher script.
Great script, but it has some issues:
mplayer (or pulse) gets “stuck” with HDMI output. I have ao=pulse,alsa in mplayer.conf
When I switch back to Intel output, it doesn’t affect mplayer, it still outputs to HDMI… I have to explicitly use -ao alsa on it.
Any ideas?
Peppo
July 16, 2009 at 20:13
Yea, as I wrote, apps which does not use gnome/gstreamer libs will probably still output through old resource. This script set HDMI/Intel to default output device (Default Sink) while switching. You can check this by running “paman”. I don’t know why mplayer does not catch this. I guess it should, but it does not. Maybe you have some thoughts?
egaistek
July 16, 2009 at 21:13
Now it has locked epiphany-gecko (web browser) to HDMI… all Flash is outputting to HDMI :/
Peppo
July 17, 2009 at 01:19
This is superb blog since I am ubuntu user and I want to connect it with hdmi.
This helps me a lot.
fuß
September 12, 2009 at 15:27
First of all, great work!
Notice: your terminal command work only if local language is English, for example in Italian we need to change “card” with “scheda” and after sed “device” with “dispositivo” so it become:
aplay -l | grep -o -e “scheda [0-9]:.*[0-9]:” | sed -e “s/scheda \([0-9]\): \(.*\) \[.*dispositivo \([0-9]\).*/load-module module-alsa-sink device=hw:\1,\3 sink_name=\2/”
Samuele Catuzzi
February 4, 2010 at 17:14
Dude, GREAT WORK!
I’m running kubuntu 11.04, kde 4.6 on a Dell Studio 15. I was stuck with the hdmi thing until i found your tip.
I’ve just had to add the result of aplay command to my default.pa
Thank you!
Bruno
May 6, 2011 at 18:55
[...] [...]
HDMI - pulseaudio - Linux: Linux-Forum
September 25, 2011 at 03:01