How to change sound device from CLI?

Similar to this post I want to find a command to toggle between 2 audio sinks.

When using my laptop as TV, it shows a ton of audio sinks, and I switch from “Analog Stereo Duplex” to “Digital Stereo (HDMI)-Output + Analog Stereo-Input”.

I tried to use dbus-monitor but no keyword gave any info.

In the end I would convert this into a script to toggle between these 2 sinks, so that I can make it a single button press in KDE connect.

Alternatively, I would like to auto-switch sinks when connecting that HDMI device?

KDE Plasma 6.3
Fedora 41
Pipewire

If sing pulseaudio/pipewire you can use pactl command

List sinks pactl list sinks | grep -E 'Name|Desc|State' will return something like

State: SUSPENDED
Name: alsa_output.pci-0000_00_1f.3.analog-stereo-headphones
Description: Built-in Audio Headphones

State: RUNNING
Name: alsa_output.pci-0000_00_1f.3.analog-stereo-speaker
Description: Built-in Audio Speakers

To switch sink use pactl set-default-sink SINK_NAME

pactl set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo-headphones

To get the current sink use pactl get-default-sink

Automatic switch to HDMI is apparently broken Auto switching audio to HDMI back and forth does not work anymore (#629) · Issues · PipeWire / wireplumber · GitLab but you could use an udev rule with a script GitHub - gladykov/hdmi-audio-auto-switch-on-arch: HDMI auto audio switch for XFCE on Endevour OS using Udev rule with some edits, haven’t tried this one as I don’t have audio capable monitor.

I am not using Pulseaudio

Apparently missed the Pipewire in the comment. His line for detection works fine for me here using the Pipewire.

zeus@9600k:~$ pactl list sinks | grep -E 'Name|Desc|State'
	State: SUSPENDED
	Name: alsa_output.usb-0d8c_USB_Sound_Device-00.analog-surround-51
	Description: CM106 Like Sound Device Analog Surround 5.1
	State: SUSPENDED
	Name: alsa_output.usb-Griffin_Technology__Inc_iMic_USB_audio_system-00.iec958-stereo
	Description: iMic Digital Stereo (IEC958)
zeus@9600k:~$ pactl info| grep -E 'Server'
Server String: /run/user/1000/pulse/native
Server Protocol Version: 35
Server Name: PulseAudio (on PipeWire 1.2.7)
Server Version: 15.0.0

Using the Pipewire-pulse replacement for the PulseAudio something I would suspect you had to do to use it.

1 Like

Hmm, interesting.

~ ❯❯❯ pactl set-default-sink alsa_output.pci-0000_00_1f.3.hdmi-stereo
~ ❯❯❯ pactl set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo

These are the 2 sinks and they seems to have the same name.

But the other sink is not available, probably because pulseaudio just sits on top of pipewire and doesnt actually control anything

1 Like

Yes, I think it’s not going to work that way. It’s a bit frustrating that you can’t choose your sinks, label them, and then easily switch with a command.

When I listen to podcasts (Bluetooth from the phone) I want to switch to Stereo Duplex analog (basically using 4 speakers to spread the sound), and then switch back to Analog 5.1 when I’m done - but it’s not so simple. I usually just leave the Audio settings window open ready for when I’m finished to switch back.

1 Like