Skip to content

Instantly share code, notes, and snippets.

@tomjnixon
Last active November 28, 2020 13:39
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tomjnixon/1348383 to your computer and use it in GitHub Desktop.
Save tomjnixon/1348383 to your computer and use it in GitHub Desktop.
PulseAudio output switcher script
#!/bin/bash
# Script to switch all programs to a specific sink. Why this isn't easy, i
# don't know.
# This script takes a single argument, the sink name, index, or alias (defined
# below).
# To make this work, you will need to:
# - In /etc/pulse/default.pa:
# change "load-module module-stream-restore"
# to "load-module module-stream-restore restore_device=false"
# This makes all streams use the default device when they start, rather
# than the last device.
# - Set up the sink_names array below.
# PulseAudio sink names are usually rather obtuse, and the indices can
# change, so you can declare short names for sinks in the array below.
#
# The format is '[alias]=device_name'. The sink names can be found with:
# $ pacmd list-sinks | grep '^\s*name'
# , and are the monstrosities between the angle brackets.
declare -A sink_names=(
[headphones]=alsa_output.usb-Burr-Brown_from_TI_USB_Audio_DAC-00-DAC.analog-stereo
[speakers]=alsa_output.pci-0000_00_1b.0.analog-stereo
)
sink=${sink_names[$1]:-$1}
(
echo set-default-sink $sink
pacmd list-sink-inputs |
grep -E '^\s*index:' |
grep -oE '[0-9]+' |
while read input
do
echo move-sink-input $input $sink
done
) | pacmd > /dev/null
@buntwo
Copy link

buntwo commented Mar 29, 2014

thank you...

@riicchhaarrd
Copy link

appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment