Audio output device, fast switch?

24,531

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Solution 5

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:
24,531

Related videos on Youtube

William Brendel
Author by

William Brendel

Updated on September 17, 2022

Comments

  • William Brendel
    William Brendel over 1 year

    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.

  • blueyed
    blueyed almost 12 years
    How do you set the default device in Ubuntu Precise?