From 5cc5009bb0467591863481d924ac8338db150c42 Mon Sep 17 00:00:00 2001 From: Epic NaN Date: Fri, 10 May 2024 19:58:07 +0200 Subject: [PATCH] added pactl-next-output-dev.sh --- sound/pactl-next-output-dev.sh | 81 ++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 sound/pactl-next-output-dev.sh diff --git a/sound/pactl-next-output-dev.sh b/sound/pactl-next-output-dev.sh new file mode 100755 index 0000000..249abf9 --- /dev/null +++ b/sound/pactl-next-output-dev.sh @@ -0,0 +1,81 @@ +#/bin/bash + +# +# author: arian +# date: 2024-05-06 +# +# purpose: +# use this script on a keyboard shortcut to automatically +# change your output device to the next one using 'pactl' +# +# usage: +# ./pactl-next-output-dev.sh /path/to/log +# + +#region run in debug if $DEBUG +[ "$DEBUG" = 'true' ] && set -x +#endregion + +#region globals +CYAN='\e[0;36m' +PURPLE='\e[0;35m' +NORMAL='\e[0m' +LOGFILE=${1:-'/var/log/arian/pactl-next-output-dev.sh.log'} +LOGDIR="$(dirname "${LOGFILE}")" +#endregion + +#region functions +function log() { + # logs strings to both the terminal and a file + # usage: log [1-3] 'String' + # 1 => INFORMATION + # 2 => ERROR + # 3 => DEBUG + + case $1 in + 1) + msg_type="INFORMATION" + ;; + + 2) + msg_type="ERROR" + ;; + + 3) + msg_type="DEBUG" + ;; + + *) + echo "Wrong input; exiting" + exit 1 + esac + + msg="[${PURPLE}$(date -Ins)${NORMAL}] [${CYAN}${msg_type}${NORMAL}]: $2" + # tee command from https://unix.stackexchange.com/questions/694671/leave-color-in-stdout-but-remove-from-tee + # (omits coloring in logfile) + echo -e "${msg}" | tee >(sed $'s/\033[[][^A-Za-z]*[A-Za-z]//g' >> ${LOGFILE}) +} +#endregion + +#region main +[ -d "${LOGDIR}" ] || mkdir -p "${LOGDIR}" + +ALL_SINKS=($(pactl list short sinks | awk '{ print $2 }')) +CURR_SINK=$(pactl get-default-sink) +CURR_INDEX=-1 + +log 1 "Using current sink [${CURR_SINK}]" + +for i in "${!ALL_SINKS[@]}"; do + if [[ "${ALL_SINKS[$i]}" = "${CURR_SINK}" ]]; then + CURR_INDEX="$i" + fi +done + +NEXT_INDEX=$(($((CURR_INDEX + 1))%(${#ALL_SINKS[@]}))) + +NEXT_SINK="${ALL_SINKS[$NEXT_INDEX]}" +log 1 "Switching to sink [${NEXT_SINK}]" +pactl set-default-sink "${ALL_SINKS[$NEXT_INDEX]}" + +#endregion