Skip to content

Instantly share code, notes, and snippets.

@BOSSoNe0013
Created May 12, 2023 01:03
Show Gist options
  • Save BOSSoNe0013/885e1e12191378f72e476b9b773da696 to your computer and use it in GitHub Desktop.
Save BOSSoNe0013/885e1e12191378f72e476b9b773da696 to your computer and use it in GitHub Desktop.
I've made this script linked to a keyboard shortcut to easily switch the audio output device used by Linux desktop manager
#!/bin/bash
##########################################################################################################################################
# _____ .___.__ ________ __ __ _________ .__ __ .__ #
# / _ \ __ __ __| _/|__| ____ \_____ \ __ ___/ |_______ __ ___/ |_ / _____/_ _ _|__|/ |_ ____ | |__ ___________ #
# / /_\ \| | \/ __ | | |/ _ \ / | \| | \ __\____ \| | \ __\ \_____ \\ \/ \/ / \ __\/ ___\| | \_/ __ \_ __ \ #
# / | \ | / /_/ | | ( <_> ) / | \ | /| | | |_> > | /| | / \\ /| || | \ \___| Y \ ___/| | \/ #
# \____|__ /____/\____ | |__|\____/ \_______ /____/ |__| | __/|____/ |__| /_______ / \/\_/ |__||__| \___ >___| /\___ >__| #
# \/ \/ \/ |__| \/ \/ \/ \/ #
# #
# Copyright (C) 2023 Cyril Bosselut #
# #
# paswitch is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# paswitch is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with paswitch If not, see <http://www.gnu.org/licenses/>. #
# #
##########################################################################################################################################
##
# First we check if the bridge sink exists
##
bridgeId=`pactl list short sinks | grep "bridge" | cut -f1`
if [ -z "$bridgeId" ]; then
##
# Bridge does not exist so we set the volume for all sound cards
# then we create it by loading the null-sink and the loopback modules.
# The bridge will be the loopback input.
# We set the bridge as the default sink so all programs will use it as output.
##
echo "Setting default volume for all sound cards"
for s in `pactl list short sinks | cut -f2`; do
pactl set-sink-volume $s 75%
done
echo "Creating bridge sink"
pactl load-module module-null-sink sink_name=bridge format=s32le channels=2 rate=96000 sink_properties="device.description='Bridge Sink'"
pactl load-module module-loopback source="bridge.monitor"
pactl set-default-sink bridge
pactl set-sink-volume bridge 50%
pactl set-sink-mute bridge 0
else
echo "Bridge sink exists #$bridgeId"
fi
##
# We retrieve available output sinks IDs, current output ID and loopback ID
##
availableSinksString=`pactl list short sinks | grep "alsa_output" | cut -f1`
currentOutput=`pactl list short sink-inputs | grep "PipeWire" | grep "-" | cut -f2`
loopback=`pactl list short sink-inputs | grep "PipeWire" | grep "-" | cut -f1`
##
# We search the next output sink ID
##
shoudSelectNext=0
found=0
i=0
echo "Current sink: #${currentOutput}"
for sink in $availableSinksString; do
availableSinks[$i]=$sink
if [ $shoudSelectNext == 1 ]; then
currentOutput=$sink
found=1
shoudSelectNext=0
fi
if [ $found == 0 ]; then
if [ "$currentOutput" = "${sink}" ]; then
# echo "Should select next sink"
shoudSelectNext=1
else
shoudSelectNext=0
fi
fi
i=$i+1
done
echo "Available sinks: ${availableSinks[@]}"
if [ $found == 0 ]; then
##
# Selected sink was the last so we select the first
##
currentOutput=${availableSinks[0]}
fi
##
# We set the selected sink as loopback output
##
pactl move-sink-input $loopback $currentOutput
##
# We retrieve sound card info and display a notification
##
card=`pactl list sinks | grep -A29 "Destination #$currentOutput" | grep 'alsa.card_name = ".*"$' | cut -d'"' -f2`
notify-send -a paswitch -c device -i org.kde.plasma.audiodeviceswitcher "🎧 Audio output switcher" "Audio output switched to card :\n$card" &
echo "Audio output switched to card #$currentOutput [$card]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment