Last active
May 2, 2023 17:06
-
-
Save domdomegg/ebef7b1808e1cca7879517c83a46ea9c to your computer and use it in GitHub Desktop.
PulseAudio output switcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a simple adaption for pactl (for use with pipewire)