Skip to content

Instantly share code, notes, and snippets.

@MrSunshyne
Last active March 3, 2022 09:20
Show Gist options
  • Save MrSunshyne/cccc1ecb8a17aeeb5f2b530fc5cb6cc7 to your computer and use it in GitHub Desktop.
Save MrSunshyne/cccc1ecb8a17aeeb5f2b530fc5cb6cc7 to your computer and use it in GitHub Desktop.
Audio Switcher for Pulse Audio

Audio Switcher for Pulse Audio

Quickly switch between your audio devices.

Script uses pactl - Pulse Audio Control. It uses the ID of your audio sinks (line 4, 5)

Steps :

  1. Move the script to your home directory e.g ~/Scripts/audio_switch.sh
  2. Give it excecutable permission : chmod +x audio_switcher.sh
  3. Bind the script to a hotkey using Settings > Keyboards > Shortcuts > Custom Shortcuts

Tested on Pop_OS 21.10

Based on this thread : https://superuser.com/questions/919033/quickly-change-audio-device-in-kde

#!/bin/bash
# Define Audio sinks
HEADSET=$(pactl list short sinks | grep analog | awk {'print $1'})
SPEAKERS=$(pactl list short sinks | grep hdmi | awk {'print $1'})
#echo "Headset $HEADSET"
#echo "Speakers $SPEAKERS"
# Current audio sink
CURRENTDEV=$(pactl list short sinks | grep RUNNING | awk {'print $1'})
#echo "Current $CURRENTDEV"
# Next audio sync.
if [[ "$CURRENTDEV" == "$HEADSET" ]] ; then
NEXTDEV=$SPEAKERS
else
NEXTDEV=$HEADSET
fi
# Set default device
#echo "Switching to $NEXTDEV"
pactl set-default-sink $NEXTDEV
# Move current streams (dont check for null, if null you wont see heads up display of audio change)
INPUTS=($(pacmd list-sink-inputs | grep index | awk '{print $2}'))
for i in ${INPUTS[*]}; do pacmd move-sink-input $i $NEXTDEV &> /dev/null; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment