Ubuntu – Audio output device, fast switch

10.04soundusb

Depending on the situation, I use either my speakers or my headset for audio output. Given that my headset is an USB headset it behaves as its own audio device.

Currently I switch between audio output devices by clicking on the speaker icon in the upper right tray, where I select Sound settings, goes to the Output tab and there choses the device I want.

What I wonder is if there might be some easier/quicker way to switch back and forth to my USB headset? Perhaps a dedicated tray icon, a key mapping, or so?

I am running Ubuntu 10.04, with the default Gnome desktop.

Best Answer

In order to change the default audio output device from the command line, you can use the pacmd Pulse Audio command-line utility.

I found the need to do similarly today, wanting to switch between headphones and speakers, both plugged into separate audio cards. Here's the shell script I wrote to do so:

#!/usr/bin/env bash

sinks=($(pacmd list-sinks | grep index | \
    awk '{ if ($1 == "*") print "1",$3; else print "0",$2 }'))
inputs=($(pacmd list-sink-inputs | grep index | awk '{print $2}'))

[[ ${sinks[0]} = 0 ]] && swap=${sinks[1]} || swap=${sinks[3]}

pacmd set-default-sink $swap &> /dev/null
for i in ${inputs[*]}; do pacmd move-sink-input $i $swap &> /dev/null; done

Notes:

  • This swaps between the first two audio output devices that the pacmd utility lists. If you have more than two audio devices and want to swap to a different one, you'll need to replace the logic on line 7 with some conditionals.
  • Just swapping the default sink device does not do anything for the applications currently running — they will continue to output to the previous device. This script also moves the sink destination for all existing inputs. I.e., if you run this script with music playing on one device, it will instantly swap to the other. If you'd like the existing applications to continue on the previous device, comment out the last line (and line 5, if you'd like).
Related Question