Set default sound output card dynamically using PulseAudio.
pactl list short sources
returns:
1 alsa_output.pci-0000_0b_00.4.analog-stereo.monitor module-alsa-card.c s16le 2ch 48000Hz IDLE 4 alsa_output.pci-0000_09_00.1.hdmi-stereo-extra1.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
pactl list short sinks
returns:
1 alsa_output.pci-0000_0b_00.4.analog-stereo module-alsa-card.c s16le 2ch 48000Hz RUNNING 3 alsa_output.pci-0000_09_00.1.hdmi-stereo-extra1 module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
pactl set-default-source alsa_output.pci-0000_0b_00.4.analog-stereo pactl set-default-sink alsa_output.pci-0000_0b_00.4.analog-stereo
!/bin/bash declare -i sinks_count=`pacmd list-sinks | grep -c index:[[:space:]][[:digit:]]` declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'` declare -i major_sink_index=$sinks_count-1 declare -i next_sink_index=0 if [ $active_sink_index -ne $major_sink_index ] ; then next_sink_index=active_sink_index+1 fi #change the default sink pacmd "set-default-sink ${next_sink_index}" #move all inputs to the new sink for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p'); do pacmd "move-sink-input $app $next_sink_index" done #display notification declare -i ndx=0 pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line; do if [ $next_sink_index -eq $ndx ] ; then notify-send -i notification-audio-volume-high "Sound output switched to" "$line" exit
NOTE:
Before running the script:
sudo chmod 755 /usr/local/bin/audio-device-switch.sh
To allocate to a shortcut key:
man pactl