4

I want to set a unique hotkey to be able to alternate the system sound between analog and HDMI outputs. How can I do this?

This behavior could be observed in play shortcut (from org.gnome.settings-daemon.plugins.media-keys) that play and pause the media alternatively.

I could find answered here that the command to set the sound-output is pactl set-card-profile 0 output:[card-profile]

artu-hnrq
  • 705
  • 1
    See if https://stackoverflow.com/q/28588147 or https://stackoverflow.com/q/27857085 help. (I searched the 'net for bash toggle.) – DK Bose Aug 25 '19 at 02:03
  • 1
    You need to find a way to read the current status. This may help: https://unix.stackexchange.com/questions/65246/change-pulseaudio-input-output-from-shell – vanadium Aug 25 '19 at 06:19

1 Answers1

1

Well, reading the topics that DK Bose and vanadium suggested and having done some more research to learning about bash script and Linux PulseAudio sound server, I came up with one solution:

We need to save the following script in a file (preferably in /usr/local/bin/, as explained here), mark it as executable (by run chmod +x over it) and then assigned it to a keyboard shortcut (optionally as explained here).

#!/bin/bash
sound_output=$(pacmd list-cards | grep "active")

if [[ $sound_output = *"analog-stereo"* ]]; then
  sound_output=hdmi-stereo
else
  sound_output=analog-stereo
fi

pactl set-card-profile 0 output:"$sound_output"
echo -e "PulseAudio sound server output set to $sound_output"

Explaning:

  • #!/bin/bash ensures that the interpreter for this script is bash, enabling extended syntax such as [[ ]].
  • As cited here, the syntax $( ) is "command substitution". This runs a command and use it's output to compose the line action. This way we save the return of pacmd list-cards | grep "active" in sound_output variable.
  • As explained here, the use of * in the condition turn it into a substring check.
  • As answered here, pactl set-card-profile 0 output: [card-profile] is the command to switch the sound server output, as wished.
artu-hnrq
  • 705