Page 1 of 4 123 ... LastLast
Results 1 to 10 of 34

Thread: Audio device switch

  1. #1
    Join Date
    Jan 2005
    Location
    Bulgaria
    Beans
    9
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Audio device switch

    I have a system with a graphic card with HDMI output and I'm often using my TV to watch movies and listen music though HDMI. Unfortunately Ubuntu doesn't switch the sound output automatically upon HDMI device connection and I had to do it manually everytime. After some research and experiments I made a little bash script that switches the output audio devices.

    The script assumes that pulseaudio is used and was tested with Karmic. It is pretty generic and actually has nothing to do with any HDMI device – it simply iterates over the sound devices and switches the output to the next one.

    Here is how to make it work:
    1. # sudo gedit /usr/local/bin/audio-device-switch.sh
    2. Copy and paste the source code bellow in the gedit editor
    3. Save it and close gedit
    4. # sudo chmod 755 /usr/local/bin/audio-device-switch.sh
    5. System -> Preferences -> Keyboard Shortcuts
    6. Press „Add“ and enter „Switch between audio devices“ as name and audio-device-switch.sh as command and press „Apply“.
    7. Select the newly added shortcut row and click on the „shortcut“ column. Choose a shortcut combination – e.g. Win + F12.
    8. That's all - now you can plug in your plug in your HDMI device and switch the audio output by pressing the chosen shortcut combination.

    Code:
    #!/bin/bash
    
    declare -i sinks_count=`pacmd list-sinks | grep -c index:[[:space:]][[:digit:]]`
    declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
    declare -i major_sink_index=$sinks_count-1
    declare -i next_sink_index=0
    
    if [ $active_sink_index -ne $major_sink_index ] ; then
    	next_sink_index=active_sink_index+1
    fi
    
    #change the default sink
    pacmd "set-default-sink ${next_sink_index}"
    
    #move all inputs to the new sink
    for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
    do
    	pacmd "move-sink-input $app $next_sink_index"
    done
    
    #display notification
    declare -i ndx=0
    pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
    do
    	if [ $next_sink_index -eq $ndx ] ; then
    		notify-send -i notification-audio-volume-high "Sound output switched to" "$line"
    		exit
    	fi
    	ndx+=1
    done;
    Hope that this is helpful.

  2. #2
    Join Date
    Apr 2008
    Beans
    13

    Re: Audio device switch

    Hi,
    That was exactly what I was looking for. I now have a desktop-starter linked to the script, and I can move audio with just one click. That was exactly what I was looking for

  3. #3
    Join Date
    Sep 2009
    Beans
    50

    Re: Audio device switch

    Simply brillant ....
    I was looking for this for a while !!!



    THANK YOU

  4. #4
    Join Date
    Mar 2006
    Beans
    1

    Re: Audio device switch

    I, too, was looking all over the place for some kind of script/command that would accomplish this simple task... thanks a lot, it works like a charm!

  5. #5
    Join Date
    Aug 2010
    Beans
    6
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Audio device switch

    Fantastic! Thanks for this script. It works great to switch audio outputs on-the-fly for a Logitech USB headset for Skype.

  6. #6
    Join Date
    Aug 2010
    Location
    Canada
    Beans
    1
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Audio device switch

    I modified tsvetan's script slightly to allow for an (odd ?) behaviour on my machine where sinks often do not get assigned consecutive index numbers (e.g. I would get indexes 2 and 4 rather than 0 and 1, as the script expected).

    I'll include it here in case someone else has the same issue.

    Code:
    #!/bin/bash
    
    declare -i sinks=(`pacmd list-sinks | sed -n -e 's/\**[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`)
    declare -i sinks_count=${#sinks[*]}
    declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
    declare -i next_sink_index=${sinks[0]}
    
    #find the next sink (not always the next index number)
    declare -i ord=0
    while [ $ord -lt $sinks_count ];
    do
        echo ${sinks[$ord]}
        if [ ${sinks[$ord]} -gt $active_sink_index ] ; then
            next_sink_index=${sinks[$ord]}
            break
        fi
        let ord++
    done
    
    #change the default sink
    pacmd "set-default-sink ${next_sink_index}"
    
    #move all inputs to the new sink
    for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
    do
        pacmd "move-sink-input $app $next_sink_index"
    done
    
    #display notification
    declare -i ndx=0
    pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
    do
        if [ $(( $ord % $sinks_count )) -eq $ndx ] ; then
            notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-synchronous: "Sound output switched to" "$line"
            exit
        fi
        let ndx++
    done;
    Credit to tsvetan for creating the initial script and posting instructions on how to easily switch audio device! Thank you!

  7. #7
    Join Date
    Sep 2010
    Beans
    5

    Re: Audio device switch

    Nice script, it is very useful to change my audio output when I want watch film in TV and use 2nd sound card for this

  8. #8
    Join Date
    May 2010
    Beans
    20

    Re: Audio device switch

    Thanks, works perfect for me too, but does anyone know how to alter the script to change the sound input too? would be nice if it could change the output and input all in one go for use with my bluetooth headset and skype.

  9. #9
    Join Date
    Jul 2008
    Beans
    2

    Re: Audio device switch

    I've found the script very Useful and I used it with "xrandr" to enable/disable TV output.
    But now it's stopped working!

    Default sink changes, but inputs created after the switch aren't affected by this change!
    Someone else got this behaviour? Any solution? I haven't found any command to refresh or something similar.

    The Cool thing is it seems to play something on one sink while playing something else on other sink...

    However, Thanks a lot for the script..
    Last edited by Takky; December 11th, 2010 at 08:49 AM.

  10. #10
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    70
    Distro
    Ubuntu

    Re: Audio device switch

    can anyone help me with my problem here?
    http://ubuntuforums.org/showthread.p...9#post10244899
    I want to change only the sound input

    Thanks

Page 1 of 4 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •