Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

9 Answers

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).
share|improve this answer
add comment

Well, you can install pavucontrol, it shows apps with sound device selection on the first tab, it'll save you some effort. But what you really want I think is to mark you USB headset as default device, then every time you plug it in all sound will be redirected to it, and when you plug it out - it'll be back to speakers. Effortless, really.

share|improve this answer
2  
How do you set the default device in Ubuntu Precise? –  blueyed Jun 2 '12 at 21:17
add comment

The question is quite old but my answer may still be useful for GNOME 2 users. I used PulseAudio Mixer Applet for the exact problem described here. You can change input and output devices right in the panel menu. It's the most convenient way I found.

share|improve this answer
add comment

Thanks for the scripts! I added a part for input (microphone) as well in case anyone wants to use it:

while read line ; do
  case $line in 
    *\*\ index*) x=1 ;;
    index*) x=0 ;;
    name*) 
      source=$(echo $line | sed -e "s/.*<//;s/>.*//")
      if [[ $x = "0" ]] ; then
        pacmd set-default-source $source &> /dev/null
      fi ;;
  esac
done < <(pacmd list-sources | grep -e alsa_input -e index)
share|improve this answer
add comment

I was looking for a way to do this to switch between the internal speakers on my laptop and an apple airport base station which is plugged into my stereo. I decided to assign hotkeys to switch. I wanted the audio preferences window to pop up for a second to give a visual confirmation of the switch and used wmctrl to close the window afterwards.

(I used ctrl+F8 and ctrl+F9 as the hotkeys since the Fn function of these keys on my laptop is to increase and decrease the volume so it seemed obvious to have the ctrl functions change the audio output.)

I used the following 2 (almost identical) scripts:

#! /bin/bash
gnome-volume-control -p output &
sleep .7 
pacmd set-default-sink 1
sleep .8 
wmctrl -c "Sound Preferences"
exit

#! /bin/bash
gnome-volume-control -p output &
sleep .7 
pacmd set-default-sink 0
sleep .8 
wmctrl -c "Sound Preferences"
exit

The first changes to the apple base station (which I assigned to ctrl+F9) and the second one switches back to internal speakers (ctrl+F8). This assumes that these are the only 2 output devices installed.

share|improve this answer
add comment

Of course switching devices by using the Applet is also working fine. But the cool thing about writing a script, is that it is super fast. I put mine for example on the keyboard shortcut alt+s. thus when I want to switch from headphones to speakers, I only need to press alt+s.

Anyway. Andrew said:

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.

That's what I did. I share it, in case someone has troubles with it:

#!/usr/bin/env bash

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

#find active sink
active=0
for i in ${sinks[*]}
do
    if [ $i -eq 0 ]
        then active=$((active+1))
        else break
    fi
done

#switch to next sink
swap=$(((active+1)%${#sinks[@]}))

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

What I did differently is a) find the active sink in a for loop. And b) switch to the next sink by increase the index by 1. Then I modulo the result by the number of sinks. That assures that e.g. when having 3 sinks, (2+1)%3=0. Thus from sink index 2 we would switch to sink index 0.

In this way the switch allows to move upwards through the available sinks.

share|improve this answer
add comment

I built upon Andrews script to make it use notify-osd to announce what it just did. Edit the lines with the names after your needs.

#!/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 

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

if [ "$swap" = "0" ]; then
notify-send -u normal -i audio-volume-medium-symbolic "Sound output changed. Now using: Corsair 2.1 Speakers!" 

else
notify-send -u normal -i audio-volume-medium-symbolic "Sound output changed. Now using: Logitech G930 Headset!" 

fi
share|improve this answer
add comment

I had the same problem and wrote a small indicator applet for the Ubuntu Unity desktop:

https://github.com/lkettenb/sound-output-switcher

http://www.capslockblog.com/images/misc/Screenshot.png

share|improve this answer
add comment

To cycle through your list of sound devices, use the following script:

#!/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;

All credit goes to tsvetan from the Ubuntu forum.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.