From a99da5ec6a77386865f73967644494ad0f25a6b7 Mon Sep 17 00:00:00 2001 From: Andrew Lorimer Date: Wed, 14 Jul 2021 23:20:43 +1000 Subject: [PATCH] add pactl-switch.sh --- pactl-switch.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 pactl-switch.sh diff --git a/pactl-switch.sh b/pactl-switch.sh new file mode 100755 index 0000000..49ef175 --- /dev/null +++ b/pactl-switch.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +declare -i sinks_count=`pacmd list-sinks | grep -c index:[[:space:]][[:digit:]]` + +if [ $sinks_count -eq 0 ] ; then + exit +fi + +declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'` + +active_index_position_found=0 +let next_sink_index=-1 +while read index ; +do + declare -i ind=($(echo $index | tr -dc '[0-9]+')) + if [ $next_sink_index -lt 0 ] ; then + export next_sink_index=$ind + fi + if [ $active_index_position_found -eq 1 ] ; then + export next_sink_index=$ind + break; + fi + if [ $active_sink_index -eq $ind ] ; then + export active_index_position_found=1 + fi +done < <(pacmd list-sinks | grep index:[[:space:]][[:digit:]]) + +#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 [ $next_sink_index -eq $ndx ] ; then + #notify-send -i notification-audio-volume-high "Sound output switched to" "$line" + notify-send "Sound output switched" "$line" + exit + fi + ndx+=1 +done; -- 2.43.2