Skip to content

Instantly share code, notes, and snippets.

@domdomegg
Last active May 2, 2023 17:06
Show Gist options
  • Save domdomegg/ebef7b1808e1cca7879517c83a46ea9c to your computer and use it in GitHub Desktop.
Save domdomegg/ebef7b1808e1cca7879517c83a46ea9c to your computer and use it in GitHub Desktop.
PulseAudio output switcher
#!/bin/bash
# Installs switchaudio.sh in current directory
# Tested on Ubuntu 20.04.1 LTS with GNOME 3.36.3 and X11
# Uninstall is manual: delete the script file and remove the shortcut from the keyboard shortcut preferences
set -e
curl -s https://gist.githubusercontent.com/domdomegg/ebef7b1808e1cca7879517c83a46ea9c/raw/8a408d85d0270841787186bacfa2d72c700aca61/switchaudiooutput.sh --output switchaudio.sh
chmod +x switchaudio.sh
shortcut="<Super>o"
current=$(gsettings get org.gnome.settings-daemon.plugins.media-keys custom-keybindings)
if ! [[ $current == *"'/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/switchaudio/'"* ]]; then
gsettings set org.gnome.settings-daemon.plugins.media-keys custom-keybindings "${current%]*}, '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/switchaudio/']"
fi
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/switchaudio/ name 'Switch audio output device'
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/switchaudio/ command "$(pwd)/switchaudio.sh"
gsettings set org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/switchaudio/ binding $shortcut
echo "Installation complete"
#!/bin/bash
# Adapted from https://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
echo $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"
echo "hi"
done
#change the default sink
pacmd "set-default-sink ${next_sink_index}"
#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
line=$(echo ${line% Analog Stereo} | tr '[:upper:]' '[:lower:]')
line=${line//hdmi/HDMI}
notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-synchronous: "Sound output switched" "Now using $line"
exit
fi
let ndx++
done;
@vgropp
Copy link

vgropp commented May 2, 2023

a simple adaption for pactl (for use with pipewire)

#!/bin/bash

default_sink=$(pactl get-default-sink)
declare -i sink_count=$(pactl list sinks short | awk '{print $2}' | wc -l)
declare -i active_sink_index=$(pactl list sinks short | awk '{print $2}' | grep -n $default_sink | cut -f1 -d:)
declare -i next_sink_index=1

if [ $active_sink_index -ne $sink_count ] ; then
   next_sink_index=active_sink_index+1
fi

next_sink=$(pactl list sinks short | awk '{print $2}' | sed "${next_sink_index}q;d")

#change the default sink
pactl set-default-sink $next_sink

#move all inputs to the new sink
for app in $(pactl list sink-inputs short | cut -f1 -d:);
do
   pactl move-sink-input $app $next_sink
done

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