add pactl-switch.sh
authorAndrew Lorimer <andrew@lorimer.id.au>
Wed, 14 Jul 2021 13:20:43 +0000 (23:20 +1000)
committerAndrew Lorimer <andrew@lorimer.id.au>
Wed, 14 Jul 2021 13:20:43 +0000 (23:20 +1000)
pactl-switch.sh [new file with mode: 0755]
diff --git a/pactl-switch.sh b/pactl-switch.sh
new file mode 100755 (executable)
index 0000000..49ef175
--- /dev/null
@@ -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;