1. Overview

When it comes to system sound, we sometimes need to switch between audio outputs. For example, this can be the case when connecting a PC to an external monitor using an HDMI cable. Even though we can use the graphical user interface (GUI), it’s often quicker to automate this process using the Linux command line interface (CLI) for this.

In this tutorial, we’ll be using the pacmd command to switch between audio outputs. First, we’ll look at the command installation. After that, we’ll focus on how to get the audio profile name. Finally, we’ll create aliases to speed up the switching.

2. Install the pacmd Package

The pacmd tool interacts with the PulseAudio sound server which is a high-level audio configuration software built on top of the Advanced Linux Sound Architecture (ALSA).

We may need to install the PulseAudio utility package to be able to use the pacmd command. For example, in Debian-based systems, we can use the apt command for that:

$ sudo apt install pulseaudio-utils

Once installed, we should be ready to switch between audio interfaces.

3. Switch Between Audio Output

To switch between audio outputs, we might need to change the card profile by using the following command:

$ pacmd set-card-profile <cardindex> <profilename>

Here, the card index, and the profile name are the parameters needed for each audio output.

Let’s find the card indices by using the list-cards option:

$ pacmd list-cards
1 card(s) available.
    index: 0
	name: <alsa_card.pci-0000_00_1f.3>
	driver: <module-alsa-card.c>
	owner module: 8
	properties:
                ...
	profiles:
		input:analog-stereo: Analogue Stereo Input (priority 32833, available: unknown)
		output:analog-stereo: Analogue Stereo Output (priority 39268, available: unknown)
		output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 38668, available: unknown)
		off: Off (priority 0, available: unknown)
        active profile: <output:analog-stereo+input:analog-stereo>
        ...

Thus, we can see that we have one audio card available with the index 0 and with the output profile names output:analog-stereo and output:hdmi-stereo.

Now, we can use the index 0 and the output:hdmi-stereo profile to switch the audio output to the HDMI output:

$ pacmd set-card-profile 0 output:hdmi-stereo

If no error occurred, the audio output is now changed.

If we’d like to set the sound input too, we can add the input profile after the + sign:

$ pacmd set-card-profile 0 output:hdmi-stereo+input:analog-stereo

This uses the hdmi-stereo output and analog-stereo input.

Finally, to switch back to the PC speakers, we can set the analog-stereo output:

$ pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo

At this point, the audio output should be back to the analog stereo.

4. Use Aliases to Speed Up the Switch

Additionally, we can use aliases to speed up the audio output switch.

For that, we can add the following lines to the .bashrc or equivalent shell RC file:

alias audio-pc='pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo'
alias audio-hdmi='pacmd set-card-profile 0 output:hdmi-stereo+input:analog-stereo'

Now, all we need to type is audio-pc to switch to the PC output, and audio-hdmi to switch to the HDMI output. This way, we can include the commands within a script as well.

5. Conclusion

In this article, we looked at how to switch between audio outputs using the CLI.

Initially, we learned how to install and use the pacmd command. After that, we found the index number and profile names of various audio devices. Next, we changed the audio output according to our needs. Finally, we created aliases to speed up the audio output switch.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.