Ubuntu On Sony Vaio FW Series

Tips & Tricks related to Sony Vaio FW series laptops

HDMI sound output switcher for PulseAudio

with 9 comments

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&#91;email sign&#93;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.


Advertisements

Written by egaistek

July 8, 2009 at 13:22

Posted in Vaio

Tagged with , , , , , , ,

9 Responses

Subscribe to comments with RSS.

  1. 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

  2. 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

  3. 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

  4. 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

  5. […] […]

  6. […] I’ve been trying to find a solution to this problem as well. As of Ubuntu 11.04 this does not seem to be possible directly, I only found this guide to add both HDMI and Analog as separate outputs, so pacmd shows 3 sinks now. Then it is possible to switch between sinks using the above commands. (see also) […]

  7. […] might, however, want to have a look at this blog post describing how to write a script to change the output device to HDMI. I don’t know of a way […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: