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]

2
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.