=> ./ code | ../ Retry/Abort

Changing pulseaudio default sink and moving current inputs

Having more than one audio device on a computer is not an uncommon situation: the computer generally have its own onboard audio interface, and if you are a musician, or a gamer, chances are that you connected another, better quality, audio interface, via a USB port for example.

Pulsaudio is today the de facto standard for sound management at the desktop level. It allows interface sharing between audio streams produced by different applications.

But sometimes, pulseaudio can be a very capricious piece of software. In particular, it sometimes changes the default audio sink. A sink, in pulseaudio terminology, is an endpoint where programs write audio data. A sink can be attached to a physical output (a soundcard) or a software output (for example, this kind of software output is used to redirect pulseaudio output to a jack server).

Moreover, even if you change the default sink to whatever suits your needs, running processes which write audio continue to use the previous sink.

The following small script relies on pulseaudio control commands to change the default sink and migrate existing audio processes to this new default sink. It uses zenity to select a sink among available sinks.

Writing this script was a kind of challenge : use only shell and common unix utilities. This script relies on bash, sed, awk, and of course pacmd for pulseaudio handling and zenity for the gui part.

#!/bin/bash
# pa-select 2021-06-12 by Éric Würbel 
# Inspired by paswitch 2011-02-02 by Ng Oon-Ee .
#   Select a pulseaudio sink. All streams are moved to the new default sink.

# $totalsc: Number of sound cards available
totalsc=$(pacmd "list-sinks" | grep card: | wc -l) # total of sound cards: $totalsc
if [ $totalsc -le 1 ]; then # Check whether there are actually multiple cards available
    notify-send -u critical -t 5000 "Nothing to switch, system only has one sound card."
    exit
fi
# $cards: an array Pulseaudio sink indexes
cards=( $(pacmd list-sinks | sed 's|*||' | awk '$1 == "index:" {print $2}') )
# $descriptions : an array of Pulseaudio sink descriptions
mapfile -t descriptions  < <(pacmd list-sinks | awk '$1 == "device.description" {$1=$2=""; print $0}' | sed 's/\"//g;s/^ *//')

upper=$((${#cards[@]} - 1))
selection=$(for i in $(seq 0 $upper); do
                echo ${cards[$i]}
                echo "\"${descriptions[$i]}\""
            done | zenity --list --column "idx" --column "card name" 2>/dev/null)
if "X$selection" == "X" ; then
    exit 0
fi
# set default sink to selected soundcard
pacmd "set-default-sink $selection"
# list of currently playing inputs
inputs=$(pacmd list-sink-inputs | awk '$1 == "index:" {print $2}')
for inp in $inputs; do # Move all current inputs to the new default sound card
    pacmd move-sink-input $inp $selection
done
Proxy Information
Original URL
gemini://retry-abort.org/code/2021-06-14-switching-pulseaudio-sink.gmi
Status Code
Success (20)
Meta
text/gemini; lang=fr
Capsule Response Time
204.188838 milliseconds
Gemini-to-HTML Time
0.908099 milliseconds

This content has been proxied by September (3851b).