Skip to content

Instantly share code, notes, and snippets.

@miyl
Last active April 1, 2024 19:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miyl/f6668df14a9309909613da5dc26cdb6d to your computer and use it in GitHub Desktop.
Save miyl/f6668df14a9309909613da5dc26cdb6d to your computer and use it in GitHub Desktop.
Pipewire default source (input) switching (Pulseaudio pactl)
#!/usr/bin/env sh
# This script switches between whatever sources exist.
# Removing pulseaudio (but not libpulse) means removing pacmd, so this is an attempt at switching the default via pactl instead.
# Cycles through all sources and sets whatever is the next one as the default.
# Pass -g to just print the current default source
CUR_DEFAULT="$(pactl info | grep 'Default Source' | cut -d':' -f 2)"
SOURCES="$(pactl list short sources | cut -d' ' -f 2)"
[ -n "$1" ] && [ "$1" = "-g" ] && echo "$CUR_DEFAULT" && exit
NEXT=0
for S in $SOURCES ; do
# Store the first value for later, in case of the special case happening, where the current default is the last one.
# AFAIK one can't always assume it's 0
[ -z "$FIRST_SOURCE" ] && FIRST_SOURCE=$S
# If the loop doesn't end we reach this step, get the number of the next source and set it to default
[ $NEXT -eq 1 ] && NEW_DEFAULT_SOURCE=$S && break
#echo "NDS: $NEW_DEFAULT_SOURCE"
# If the current value is the default, the next one should be the new one. Flag that - AFTER the check
# Either The space before $S has to be there, or I need to trim it in CUR_DEFAULT, fx. with | xargs. I figure this is clearly better computationally
[ " $S" = "$CUR_DEFAULT" ] && NEXT=1
done
[ -z "$NEW_DEFAULT_SOURCE" ] && NEW_DEFAULT_SOURCE=$FIRST_SOURCE
# Set default sink for new audio recordings
pactl set-default-source "$NEW_DEFAULT_SOURCE"
notify-send --expire-time 1350 "Switched default input source to: $NEW_DEFAULT_SOURCE"
@h0lylag
Copy link

h0lylag commented Nov 30, 2021

You're awesome. I was about to write my own script to do this and you nailed it. Nice and simple, works flawless for me.

@miyl
Copy link
Author

miyl commented Feb 4, 2022

Very late response, but glad it helped out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment