5

I have a Xonar DGX card, and I can change the audio output with alsamixer by going to the Analog Output option and switching between Stereo Headphones and Stereo Headphones FP. How can I do the same by using the command line?

I've looked up and found about sinks and how they can be switched with pacmd, but from what I understand my both outputs are within the same sink, which is my Xonar card.

  • 1
    If both outputs are within the same sink in PA, they probably will have different associated profiles. – muru Jul 30 '18 at 00:02
  • 1
    amixer is the commandline version of alsamixer – Ipor Sircer Jul 30 '18 at 00:09
  • Possible duplicate - https://unix.stackexchange.com/questions/175930/change-default-port-for-pulseaudio-line-out-not-headphones. – slm Jul 30 '18 at 01:54

4 Answers4

6

I believe you can do this using a number of tools, such as amixer, or pamd, or pactl.

NOTE: Your assumption is how I understand things as well. Sinks I equate to actual soundcards (output), and the outputs on them are called ports.

The actual definition from this guide titled PulseAudio under the hood:

Sink

A sink is an output device. It is an active unit that consumes samples.

Sink usually runs a thread with its own event loop, peeks sample chunks from connected sink inputs, and mixes them. It also implements clocking and maintains latency. The rest of the world usually communicates with a sink using messages.

The typical sink represents an output sound device, e.g. headphones connected to a sound card line output or on a Bluetooth headset. PulseAudio automatically creates a sink for every detected output device.

Example

Here's an example showing how to use pactl:

$ pactl list sinks |& grep -E "Sink|Ports|analog-ou"
Sink #0
    Ports:
        analog-output-lineout: Line Out (priority: 9900, not available)
        analog-output-headphones: Headphones (priority: 9000, not available)
    Active Port: analog-output-lineout

Above you can see my Active Port: is currently my soundcard's lineout. Let's change that to the headphones.

$ pactl set-sink-port 0 analog-output-headphones

And if we check again:

$ pactl list sinks |& grep -E "Sink|Ports|analog-ou"
X11 connection rejected because of wrong authentication.
Sink #0
    Ports:
        analog-output-lineout: Line Out (priority: 9900, not available)
        analog-output-headphones: Headphones (priority: 9000, not available)
    Active Port: analog-output-headphones

From man pactl:

   set-sink-port SINK PORT
          Set the specified sink (identified by its symbolic name or
          numerical index) to the specified port (identified by its symbolic 
          name).

What if audio device disappears?

In rare cases I've noticed that the audio output device mysteriously disappears from the list of devices. If you find this happens you can easily resolve this by telling Pulse Audio to reload its modules:

$ pactl load-module module-detect

References

slm
  • 369,824
  • Why do you use |&? And what is the reasoning behund analog-ou instead of analog-output? Not that it changes anything, but the output colors look better that way. I also included |Name: in the grep-Expression to be able to see more easily which Sink is which. You could also add iec958-stereo-output and [Out] alongside the analog one for a more complete output including HDMI and digital outputs. My full command would then be pactl list sinks |& grep -E "Sink |Ports:|Name:|\[Out\] |analog-output:|iec958-stereo-output:". Should I post a separate answer including all the details? – Cadoiz Nov 07 '22 at 07:50
  • Is is possible that you used this answer to change ports, but no to change sinks? – Cadoiz Nov 07 '22 at 07:57
2

I used pacmd to auto switch sound from pc speakers to hdmi:

if grep '^connected$' /sys/class/drm/card0/card0-HDMI*/status ;then             
  sleep 2
  pacmd set-card-profile 0 output:hdmi-stereo
else
  sleep 2
  pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo
fi

To get the active sound profile:

pacmd list |grep 'active profile'
        active profile: <output:analog-stereo+input:analog-stereo>

To list all the available output sound profiles supported:

pacmd list |grep 'output:'
2

All of the other answers here didn't solve my problems as they don't change necessary parts of the configuration or don't move active streams. This Q/A provides some answers which are fairly complicated. But note that today (as of Jul 15, 2022 on on Ubuntu 22.04), it is much easier than described in slm's answer or the other ones on that related askubuntu Q/A.

Especially this answer there made me consider an answer here too.


You can just use

  • pactl list short sinks and
  • pactl list short sources

to get the list of available sinks and sources

(from that answer). After that, you can switch with

pactl set-default-sink '<the name of the sink from the list>'

The mentioned answer also provides a script to switch/toggle between two different devices.


slm mentioned a "number of tools, such as amixer, or pamd, or pactl". These can be found in different answers. Here are some references using

Cadoiz
  • 276
0

You can do it with amixer.

amixer -c 0 cset name='Analog Output Playback Enum' 0

Where 0 in -c 0 represents the card number. The last parameter is as follows:

0 - 'Stereo Headphones'
1 - 'Stereo Headphones FP'
2 - 'Multichannel'
  • Unfortunately gives me amixer: Cannot find the given element from control sysdefault:0. I used amixer -c 0 to display all the different options. – Cadoiz Nov 07 '22 at 08:01