[pulseaudio-discuss] pactl command to switch sound from speakers to headphone

Tanu Kaskinen tanuk at iki.fi
Sat Mar 10 17:57:39 UTC 2018


On Fri, 2018-03-09 at 23:30 +0000, Wolfram Volpi wrote:
> I need to quickly switch from speakers to headphone when my PC receives an incoming phone call.
> The speakers and headphones are always plugged in.
> I am on Fedora 27, Gnome 3.26.2
> 
> So I want to write a script that calls a few lines of pactl.
> But the pactl commands I tried aren't switching sound from speakers to headphone.
> 
> This is what I have tried from the command line:
>     $ pactl set-sink-port 1 "analog-output-headphones"
> But it didn't effect my speaker or headphone behavior.
> 
> I also tried:
>     $ pactl set-sink-port 0 "iec958-stereo-output"
>     Failure: No such entity
> 
> Here are the pactl sinks on my system:
>     $ pactl list sinks
>     Sink #0
>      State: SUSPENDED
>      Name: alsa_output.usb-1130_USB_AUDIO-00.iec958-stereo
>      Description: USB  AUDIO   Digital Stereo (IEC958)
>      Driver: module-alsa-card.c
>      Sample Specification: s16le 2ch 48000Hz
>      Channel Map: front-left,front-right
>      Owner Module: 7
>      Mute: no
>      Volume: front-left: 15176 /  23% / -38.12 dB,   front-right: 15176 /  23% / -38.12 dB
>             balance 0.00
>      Base Volume: 65536 / 100% / 0.00 dB
>      Monitor Source: alsa_output.usb-1130_USB_AUDIO-00.iec958-stereo.monitor
>      Latency: 0 usec, configured 0 usec
>      Flags: HARDWARE DECIBEL_VOLUME LATENCY SET_FORMATS 
>      Properties:
>      alsa.resolution_bits = "16"
>     device.api = "alsa"
>      device.class = "sound"
>     alsa.class = "generic"
>      alsa.subclass = "generic-mix"
>     alsa.name = "USB Audio"
>      alsa.id = "USB Audio"
>     alsa.subdevice = "0"
>      alsa.subdevice_name = "subdevice #0"
>     alsa.device = "0"
>      alsa.card = "0"
>     alsa.card_name = "USB  AUDIO"
>      alsa.long_card_name = "USB  AUDIO at usb-0000:00:14.0-3, full speed"
>     alsa.driver_name = "snd_usb_audio"
>      device.bus_path = "pci-0000:00:14.0-usb-0:3:1.0"
>     sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3:1.0/sound/card0"
>      udev.id = "usb-1130_USB_AUDIO-00"
>     device.bus = "usb"
>      device.vendor.id = "1130"
>     device.vendor.name = "Tenx Technology, Inc."
>      device.product.id = "1620"
>     device.product.name = "USB  AUDIO  "
>      device.serial = "1130_USB_AUDIO"
>     device.string = "iec958:0"
>      device.buffering.buffer_size = "384000"
>     device.buffering.fragment_size = "192000"
>      device.access_mode = "mmap+timer"
>     device.profile.name = "iec958-stereo"
>      device.profile.description = "Digital Stereo (IEC958)"
>      device.description = "USB  AUDIO   Digital Stereo (IEC958)"
>      alsa.mixer_name = "USB Mixer"
>      alsa.components = "USB1130:1620"
>      module-udev-detect.discovered = "1"
>      device.icon_name = "audio-card-usb"
>      Ports:
>      iec958-stereo-output: Digital Output (S/PDIF) (priority: 0)
>      Active Port: iec958-stereo-output
>      Formats:
>      pcm
> 
>     Sink #1
>      State: SUSPENDED
>      Name: alsa_output.pci-0000_00_1b.0.analog-stereo
>      Description: Built-in Audio Analog Stereo
>      Driver: module-alsa-card.c
>      Sample Specification: s16le 2ch 44100Hz
>      Channel Map: front-left,front-right
>      Owner Module: 8
>      Mute: no
>      Volume: front-left: 30129 /  46% / -20.25 dB,   front-right: 30129 /  46% / -20.25 dB
>             balance 0.00
>      Base Volume: 65536 / 100% / 0.00 dB
>      Monitor Source: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
>      Latency: 0 usec, configured 0 usec
>      Flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY 
>      Properties:
>     alsa.resolution_bits = "16"
>      device.api = "alsa"
>      device.class = "sound"
>      alsa.class = "generic"
>      alsa.subclass = "generic-mix"
>      alsa.name = "ALC283 Analog"
>     alsa.id = "ALC283 Analog"
>      alsa.subdevice = "0"
>     alsa.subdevice_name = "subdevice #0"
>      alsa.device = "0"
>     alsa.card = "2"
>      alsa.card_name = "HDA Intel PCH"
>     alsa.long_card_name = "HDA Intel PCH at 0xf7c30000 irq 47"
>      alsa.driver_name = "snd_hda_intel"
>     device.bus_path = "pci-0000:00:1b.0"
>      sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card2"
>     device.bus = "pci"
>      device.vendor.id = "8086"
>     device.vendor.name = "Intel Corporation"
>      device.product.id = "9c20"
>     device.product.name = "8 Series HD Audio Controller"
>      device.form_factor = "internal"
>     device.string = "front:2"
>      device.buffering.buffer_size = "352800"
>     device.buffering.fragment_size = "176400"
>      device.access_mode = "mmap+timer"
>     device.profile.name = "analog-stereo"
>      device.profile.description = "Analog Stereo"
>     device.description = "Built-in Audio Analog Stereo"
>      alsa.mixer_name = "Realtek ALC283"
>     alsa.components = "HDA:10ec0283,80862054,00100003"
>      module-udev-detect.discovered = "1"
>     device.icon_name = "audio-card-pci"
>      Ports:
>     analog-output-headphones: Headphones (priority: 9000, available)
>      Active Port: analog-output-headphones
>      Formats:
>     pcm
> 
> Am I on the right track? (I don't know PulseAudio)
> set-sink-port only sets the port. What else do I need to set?
> 
> Switching sound on Gnome Sound Settings works but it takes a lot of clicks (right-click on Desktop > Settings > Sound).
> I have tried "sound-output-device-chooser" but it takes 2 clicks to switch to headphone, and then two more clicks to go back in and adjust the volume.
> 
> I posted the question on the LinuxQuestions forum last week, and got 589 views and 0 replies:
> https://www.linuxquestions.org/questions/linux-software-2/pactl-command-to-switch-from-speakers-to-headphone-4175624840/

The internal sound card sink doesn't seem to have any other ports than
the headphone port, so setting the port doesn't do anything. I guess by
"speakers" you mean the usb sound card's digital output? If you have
applications playing to the usb sound card sink and you want them to
move to the internal sound card sink, you can do that with the "pactl
move-sink-input" command. That requires figuring out the sink input
indexes first, though. Assuming that you never need to use the usb
sound card sink and the internal sound card sink simultaneously, a
simpler solution would be to disable the sink that you don't need. That
will force streams to be moved away from it.

So, these commands should do the trick:

Switch to the internal sound card:
    pactl set-card-profile alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo
    pactl set-card-profile alsa_card.usb-1130_USB_AUDIO-00 off

Switch to the usb sound card:
    pactl set-card-profile alsa_card.usb-1130_USB_AUDIO-00 output:iec958-stereo
    pactl set-card-profile alsa_card.pci-0000_00_1b.0 input:analog-stereo

The commands assume that you have a stereo input device on the internal
sound card that you want to use during the calls, and that you don't
need any input devices on the usb sound card. If that's an incorrect
assumption, the selected profiles need to be different.

By the way, please subscribe to the list. As you noticed, your message
went to the moderation queue and I let it through, but it's hard to
have discussions with people who don't subscribe to the list. I
remembered to add you to Cc this time, but I might not always remember
to do that (and no, "reply to all" doesn't work, because the mailing
list software sets the "Reply-to" header).

-- 
Tanu

https://liberapay.com/tanuk
https://www.patreon.com/tanuk


More information about the pulseaudio-discuss mailing list