Results 1 to 10 of 34

Thread: Audio device switch

Hybrid View

  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
    Dec 2012
    Beans
    1

    Re: Audio device switch

    Here is the code for switching audio INPUTS.

    Code:
    #!/bin/bash
    ## This code is for switching audio inputs.
    
    declare -i sources=(`pacmd list-sources | sed -n -e 's/\**[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`)
    declare -i sources_count=${#sources[*]}
    declare -i active_source_index=`pacmd list-sources | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
    declare -i next_source_index=${sources[0]}
    
    
    #find the next source (not always the next index number)
    declare -i ord=0
    while [ $ord -lt $sources_count ];
    do
        echo ${sources[$ord]}
        if [ ${sources[$ord]} -gt $active_source_index ] ; then
            next_source_index=${sources[$ord]}
            break
        fi
        let ord++
    done
    
    
    #change the default source
    pacmd "set-default-source ${next_source_index}"
    
    
    #move all outputs to the new source
    for app in $(pacmd list-source-outputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
    do
        pacmd "move-source-output $app $next_source_index"
    done
    
    
    #display notification
    declare -i ndx=0
    pacmd list-sources | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
    do
        if [ $(( $ord % $sources_count )) -eq $ndx ] ; then
            notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-synchronous: "Sound input switched to" "$line"
            exit
        fi
        let ndx++
    done;
    Basically it's the same code as from the original, just using pacmd list-sources (for input devices) instead of pacmd list-sinks (for output devices).
    Last edited by alesr; April 23rd, 2013 at 01:52 PM.

  8. #8
    Join Date
    Jan 2007
    Beans
    1,634
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Audio device switch

    This is my output from
    Code:
    pacmd list-cards
    Code:
    2 card(s) available.
        index: 0
    	name: <alsa_card.pci-0000_01_05.1>
    	driver: <module-alsa-card.c>
    	owner module: 4
    	properties:
    		alsa.card = "1"
    		alsa.card_name = "HDA ATI HDMI"
    		alsa.long_card_name = "HDA ATI HDMI at 0xd0410000 irq 19"
    		alsa.driver_name = "snd_hda_intel"
    		device.bus_path = "pci-0000:01:05.1"
    		sysfs.path = "/devices/pci0000:00/0000:00:01.0/0000:01:05.1/sound/card1"
    		device.bus = "pci"
    		device.vendor.id = "1002"
    		device.vendor.name = "Advanced Micro Devices [AMD] nee ATI"
    		device.product.name = "RS880 HDMI Audio [Radeon HD 4200 Series]"
    		device.string = "1"
    		device.description = "RS880 HDMI Audio [Radeon HD 4200 Series]"
    		module-udev-detect.discovered = "1"
    		device.icon_name = "audio-card-pci"
    	profiles:
    		output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400)
    		off: Off (priority 0)
    	active profile: <output:hdmi-stereo>
    	sinks:
    		alsa_output.pci-0000_01_05.1.hdmi-stereo/#0: RS880 HDMI Audio [Radeon HD 4200 Series] Digital Stereo (HDMI)
    	sources:
    		alsa_output.pci-0000_01_05.1.hdmi-stereo.monitor/#0: Monitor of RS880 HDMI Audio [Radeon HD 4200 Series] Digital Stereo (HDMI)
    	ports:
    		hdmi-output-0: HDMI / DisplayPort (priority 5900, available: unknown)
    			properties:
    				
        index: 1
    	name: <alsa_card.pci-0000_00_14.2>
    	driver: <module-alsa-card.c>
    	owner module: 5
    	properties:
    		alsa.card = "0"
    		alsa.card_name = "HDA ATI SB"
    		alsa.long_card_name = "HDA ATI SB at 0xd0a00000 irq 16"
    		alsa.driver_name = "snd_hda_intel"
    		device.bus_path = "pci-0000:00:14.2"
    		sysfs.path = "/devices/pci0000:00/0000:00:14.2/sound/card0"
    		device.bus = "pci"
    		device.vendor.id = "1002"
    		device.vendor.name = "Advanced Micro Devices [AMD] nee ATI"
    		device.product.name = "SBx00 Azalia (Intel HDA)"
    		device.form_factor = "internal"
    		device.string = "0"
    		device.description = "Built-in Audio"
    		module-udev-detect.discovered = "1"
    		device.icon_name = "audio-card-pci"
    	profiles:
    		output:analog-stereo: Analog Stereo Output (priority 6000)
    		output:analog-stereo+input:analog-stereo: Analog Stereo Duplex (priority 6060)
    		input:analog-stereo: Analog Stereo Input (priority 60)
    		off: Off (priority 0)
    	active profile: <output:analog-stereo+input:analog-stereo>
    	sinks:
    		alsa_output.pci-0000_00_14.2.analog-stereo/#1: Built-in Audio Analog Stereo
    	sources:
    		alsa_output.pci-0000_00_14.2.analog-stereo.monitor/#1: Monitor of Built-in Audio Analog Stereo
    		alsa_input.pci-0000_00_14.2.analog-stereo/#2: Built-in Audio Analog Stereo
    	ports:
    		analog-output-speaker: Speakers (priority 10000, available: unknown)
    			properties:
    				
    		analog-output-headphones: Headphones (priority 9000, available: no)
    			properties:
    				
    		analog-input-microphone: Microphone (priority 8700, available: no)
    			properties:
    I can't quite figure out how to work with this. The card at index 0 is my ATI graphics card (ugh), which is apparently responsible for sound via HDMI out. The card at index 1 is my actual sound card.
    Don't forget to give thanks and mark your thread as solved

  9. #9
    Join Date
    Oct 2008
    Beans
    2

    Re: Audio device switch

    I found this thread very helpful, but for my needs, I just set up this simple script that switches between my monitor+desktop audio and HDMI tv+HDMI audio.
    Since I mostly use my TV with XBMC, I included that in the script, and mapped [Super+Enter (keypad)] to run it.
    Now everything switches to my TV when I want to watch something, and switches back when I exit XBMC.

    Code:
    #!/bin/bash
    
    if xrandr | grep -q ', current 1440'
    
    	then
    		xrandr --output HDMI-0 --mode 1920x1080 --output DVI-0 --off
    		xbmc
    		pacmd set-default-sink 0
    
    	while pgrep -u root xbmc > /dev/null; do sleep 1; done
    		xrandr --output DVI-0 --mode 1440x900 --output HDMI-0 --off
    		pacmd set-default-sink 1
    
    	else
    		xrandr --output DVI-0 --mode 1440x900 --output HDMI-0 --off
    		pacmd set-default-sink 1
    
    fi
    Now if I could only get Igor Cesko's COM receiver to work with lirc...

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
  •