User Tools

Site Tools


ubuntu:sound:pulseaudio:set_default_sound_output_card

Ubuntu - Sound - PulseAudio - Set default sound output card

Set default sound output card dynamically using PulseAudio.


Get list of Sources

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

Get list of Sinks

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


Set Defaults

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

Use a BASH Script

!/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:

  • Set Permissions:
    sudo chmod 755 /usr/local/bin/audio-device-switch.sh

To allocate to a shortcut key:

  • Navigate to System → Preferences → Keyboard Shortcuts.
  • Click Add.
    • Name: Switch between audio devices.
    • Command: audio-device-switch.sh.
    • Press Apply.
    • Select the newly added shortcut row and click on the shortcut column.
    • Choose a shortcut combination – e.g.WIN + F12.

References

man pactl

ubuntu/sound/pulseaudio/set_default_sound_output_card.txt · Last modified: 2020/12/13 12:23 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki