Script to change audio output defaults

I have reconfigured my system (do not remember exactly how) to make HDMI as the default sound output. I am using my laptop nowadays detached from the HDMI monitor I used formerly, so I would like my default to be P2 plug now. Since pavucontrol does not save the last config as the default, one needs to reconfigure it every reboot. I would like a script to change the audio output, so when I am using an HDMI monitor, I would type “snd-default-output hdmi” and when I am using my laptop unplugged, I would use “snd-default-output p2”. Thanks in advance for your help.

Please post more information about your PulseAudio output sinks

pactl list short sinks
pacmd dump | grep default-sink

If the default-sink is set to the hdmi output, audio will try to use it for audio output
But if the monitor is unplugged and hdmi output not available PulseAudio will use a fallback sink

[P2 HEADPHONES - ANALOG STEREO OUTPUT]

pactl list short sinks 

1 combined module-combine-sink.c s16le 2ch 16624Hz IDLE
3 ladspa_output.mbeq_1197.mbeq module-ladspa-sink.c float32le 2ch 16624Hz IDLE
4 alsa_output.pci-0000_00_1b.0.analog-stereo module-alsa-card.c s16le 2ch 44100Hz RUNNING

[HDMI]

pactl list short sinks

1 combined module-combine-sink.c s16le 2ch 16624Hz RUNNING
2 alsa_output.pci-0000_00_1b.0.hdmi-stereo module-alsa-card.c s16le 2ch 32000Hz RUNNING
3 ladspa_output.mbeq_1197.mbeq module-ladspa-sink.c float32le 2ch 16624Hz IDLE

[BOTH]

pacmd dump | grep default-sink

set-default-sink ladspa_output.mbeq_1197.mbeq

You have 4 sinks for audio output not just the 2 I expected
and the default-sink ladspa_output.mbeq_1197.mbeq is pulseaudio-ladspa-equalizer

If you want to continue using the equalizer, leave the default-sink set to ladspa_output.mbeq_1197.mbeq
Changing the default-sink will bypass the equalizer

If you are no longer using HDMI you should remove module-combine-sink
That module may have been loaded by enabling the setting for simultaneous output in paprefs
Or it may have been added to the PulseAudio configuration file default.pa

With your help, I was able to write this script. Thanks.

#!/bin/bash

#no-argument
if [ "$1" == "" ]; then
    echo "No argument provided. Please inform an option: [hdmi|analog]"
    exit 1
fi

#hdmi
if [ "$1" == "hdmi" ]; then
    PARAM_REGEX='s/output:analog-stereo/output:hdmi-stereo/'
    PARAM_PATH=/etc/pulse/default.pa
    sed -i $PARAM_REGEX $PARAM_PATH
    echo "done"
    exit 0
fi

#analog
if [ "$1" == "analog" ]; then
    PARAM_REGEX='s/output:hdmi-stereo/output:analog-stereo/'
    PARAM_PATH=/etc/pulse/default.pa
    sed -i $PARAM_REGEX $PARAM_PATH
    echo "done"
    exit 0
fi

#invalid-argument
echo "Invalid argument provided. Please inform an option: [hdmi|analog]"
exit 1

To use it, just type:

sudo snd-default-output hdmi

or

sudo snd-default-output analog

This way, one can travel some weeks with your laptop and use the analog output. Coming back home, one can use the hdmi monitor.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.