Skip to content

Instantly share code, notes, and snippets.

@raidzero
Created March 3, 2014 00:08
Show Gist options
  • Save raidzero/9316043 to your computer and use it in GitHub Desktop.
Save raidzero/9316043 to your computer and use it in GitHub Desktop.
Pulseaudio: switch all apps playing sound to play on a specific device
#!/bin/sh
# switch all apps playing sound to a given device.
# usage: paswitch.sh [HEADPHONES|SPEAKERS] (really meant to be bound a key shortcut)
# these are the sink names of the devices we want to use
HEADPHONES="alsa_output.usb-FiiO_DigiHug_USB_Audio-01-Audio.analog-stereo"
SPEAKERS="alsa_output.pci-0000_00_1b.0.analog-stereo"
# get a list of ID's of everything playing sound
INPUTS=`pacmd list-sink-inputs | grep index | cut -d ':' -f2 | sed 's/^ *//g'`
if [ -z "$INPUTS" ]; then
# nothing is playing
exit
fi
case $1 in
SPEAKERS)
INPUT=$SPEAKERS;;
HEADPHONES)
INPUT=$HEADPHONES;;
esac
# get the index of the desired sink
INDEX_LINE=`echo $(pacmd list-sinks | nl | grep "$INPUT" | awk '{print$1}') - 1 | bc`
INDEX=`pacmd list-sinks | nl | grep "^ *$INDEX_LINE" | head -n 1 | grep -oE "[0-9]+$"`
# change every input to use the desired sink
for i in `echo "$INPUTS" | sed 's/ /\n/g'`; do
pacmd move-sink-input $i $INDEX
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment