Skip to content

Instantly share code, notes, and snippets.

@mikesmullin
Last active September 23, 2020 10:51
Show Gist options
  • Save mikesmullin/2838899 to your computer and use it in GitHub Desktop.
Save mikesmullin/2838899 to your computer and use it in GitHub Desktop.
linux pulseaudio headphone speaker toggle switch
#!/bin/bash
# see original thread discussion:
# http://ubuntuforums.org/showthread.php?t=1370383
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment