切换音频设备会破坏光纤音频输出

切换音频设备会破坏光纤音频输出

我想快速切换耳机和扬声器的音频输出。两者都连接到主板,主板上有Line OutOptical Audio Out线。我理想情况下希望通过两个命令来执行此操作,以便我可以将它们添加为宏/热键。

  • 扬声器 -> 光纤音频
  • 耳机 -> 线路输出

我可以使用菜单完成从扬声器到耳机的切换,Settings > Sound > Output菜单成功列出了我插入的 4 个音频选项,另外两个是通过显卡的 DP/HDMI 连接。如果我现在尝试从耳机切换回扬声器,则不会输出任何音频。我有一个“解决方法”,我暂停所有音频,切换到其中一个显卡输出,等待 5 秒钟,切换到扬声器,等待 5 秒钟,然后播放音乐,到目前为止,这已经可靠地解决了这个问题。

我一直试图使用pactl来改变输出,我认为是这样sinks,但我只有两个可用的接收器:

➜  ~ pactl list sinks short
55  alsa_output.pci-0000_2d_00.4.iec958-stereo  PipeWire    s32le 2ch 48000Hz   RUNNING
52  alsa_output.pci-0000_2b_00.1.hdmi-stereo-extra1 PipeWire    s16le 2ch 48000Hz   SUSPENDED

这是在扬声器上收听音频时显示的。下面是在耳机上收听音频时显示的。

➜  ~ pactl list sinks short
52  alsa_output.pci-0000_2b_00.1.hdmi-stereo-extra1 PipeWire    s16le 2ch 48000Hz   SUSPENDED
55  alsa_output.pci-0000_2d_00.4.analog-stereo  PipeWire    s32le 2ch 48000Hz   RUNNING

我假设这些sinks对应于主板声卡和 GPU 输出。在这种情况下,我认为更改是device-port我正在寻找的命令。

➜  ~ pactl set-sink-port alsa_output.pci-0000_2d_00.4.iec958-stereo analog-stereo        
Failure: No such entity

我对如何实现这个功能感到很困惑。如能得到任何帮助我将不胜感激。

以下是我认为重要的一些信息。操作系统:Ubuntu 21.10

pactl info以下是删除我的用户名后的输出:

Server String: /run/user/1000/pulse/native
Library Protocol Version: 35
Server Protocol Version: 35
Is Local: yes
Client Index: 68
Tile Size: 65472
User Name: {USER}
Host Name: ubuntu
Server Name: PulseAudio (on PipeWire 0.3.38)
Server Version: 15.0.0
Default Sample Specification: float32le 2ch 48000Hz
Default Channel Map: front-left,front-right
Default Sink: alsa_output.pci-0000_2d_00.4.iec958-stereo
Default Source: alsa_input.usb-Sonix_Technology_Co.__Ltd._USB_2.0_Camera_SN0001-02.analog-stereo
Cookie: bf70:5e64

编辑

鉴于@kanehekili 的回答和评论,我似乎希望设置sink-port。执行此操作的方法是使用pactl set-sink-port {SINK_NAME} {PORT_NAME},但是根据以下情况,我的端口不存在pactl list sinks

➜  ~ pactl list sinks
Sink #47
    State: RUNNING
    Name: alsa_output.pci-0000_2d_00.4.iec958-stereo
    Description: Starship/Matisse HD Audio Controller Digital Stereo (IEC958)
    Driver: PipeWire
    Sample Specification: s32le 2ch 48000Hz
    Channel Map: front-left,front-right
    Owner Module: 4294967295
    Mute: no
    Volume: front-left: 28524 /  44% / -21.68 dB,   front-right: 28524 /  44% / -21.68 dB
            balance 0.00
    Base Volume: 65536 / 100% / 0.00 dB
    Monitor Source: alsa_output.pci-0000_2d_00.4.iec958-stereo.monitor
    Latency: 0 usec, configured 0 usec
    Flags: HARDWARE HW_MUTE_CTRL DECIBEL_VOLUME LATENCY SET_FORMATS 
    Properties:
        object.path = "alsa:pcm:1:iec958:1:playback"
        api.alsa.path = "iec958:1"
        api.alsa.pcm.card = "1"
        api.alsa.pcm.stream = "playback"
        audio.channels = "2"
        audio.position = "FL,FR"
        device.routes = "1"
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = "ALCS1200A Digital"
        alsa.id = "ALCS1200A Digital"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "1"
        alsa.card = "1"
        alsa.card_name = "HD-Audio Generic"
        alsa.long_card_name = "HD-Audio Generic at 0xfc400000 irq 80"
        alsa.driver_name = "snd_hda_intel"
        device.profile.name = "iec958-stereo"
        device.profile.description = "Digital Stereo (IEC958)"
        card.profile.device = "11"
        device.id = "41"
        factory.name = "api.alsa.pcm.sink"
        priority.driver = "672"
        priority.session = "672"
        media.class = "Audio/Sink"
        node.nick = "HD-Audio Generic"
        node.name = "alsa_output.pci-0000_2d_00.4.iec958-stereo"
        device.description = "Starship/Matisse HD Audio Controller Digital Stereo (IEC958)"
        device.icon_name = "audio-card-iec958"
        device.bus = "pci"
        device.bus_path = "pci-0000:2d:00.4"
        node.pause-on-idle = "false"
        factory.id = "18"
        client.id = "32"
        node.driver = "true"
        factory.mode = "merge"
        audio.adapt.follower = ""
        library.name = "audioconvert/libspa-audioconvert"
        object.id = "47"
        node.max-latency = "8192/48000"
    Ports:
        iec958-stereo-output: Digital Output (S/PDIF) (type: SPDIF, priority: 0, availability unknown)
    Active Port: iec958-stereo-output
    Formats:
        pcm

当我尝试切换接收器端口时,出现错误

➜  ~ pactl set-sink-port alsa_output.pci-0000_2d_00.4.iec958-stereo analog-stereo        
Failure: No such entity

即使使用接收器的数字标识符,也会发生上述情况。看来我正在寻找的端口不存在,但是我可以使用上面提到的“设置”菜单更改端口,运行相同的操作pactl list sinks会更改端口,这样我的iec958-stereo端口就不再存在了:

➜  ~ pactl list sinks
Sink #47
    State: RUNNING
    Name: alsa_output.pci-0000_2d_00.4.analog-stereo
    Description: Starship/Matisse HD Audio Controller Analog Stereo
    Driver: PipeWire
    Sample Specification: s32le 2ch 48000Hz
    Channel Map: front-left,front-right
    Owner Module: 4294967295
    Mute: no
    Volume: front-left: 31249 /  48% / -19.30 dB,   front-right: 31249 /  48% / -19.30 dB
            balance 0.00
    Base Volume: 65536 / 100% / 0.00 dB
    Monitor Source: alsa_output.pci-0000_2d_00.4.analog-stereo.monitor
    Latency: 0 usec, configured 0 usec
    Flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY 
    Properties:
        object.path = "alsa:pcm:1:front:1:playback"
        api.alsa.path = "front:1"
        api.alsa.pcm.card = "1"
        api.alsa.pcm.stream = "playback"
        audio.channels = "2"
        audio.position = "FL,FR"
        device.routes = "2"
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = "ALCS1200A Analog"
        alsa.id = "ALCS1200A Analog"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "0"
        alsa.card = "1"
        alsa.card_name = "HD-Audio Generic"
        alsa.long_card_name = "HD-Audio Generic at 0xfc400000 irq 80"
        alsa.driver_name = "snd_hda_intel"
        device.profile.name = "analog-stereo"
        device.profile.description = "Analog Stereo"
        card.profile.device = "5"
        device.id = "41"
        factory.name = "api.alsa.pcm.sink"
        priority.driver = "689"
        priority.session = "689"
        media.class = "Audio/Sink"
        node.nick = "HD-Audio Generic"
        node.name = "alsa_output.pci-0000_2d_00.4.analog-stereo"
        device.description = "Starship/Matisse HD Audio Controller Analog Stereo"
        device.icon_name = "audio-card-analog"
        device.bus = "pci"
        device.bus_path = "pci-0000:2d:00.4"
        node.pause-on-idle = "false"
        factory.id = "18"
        client.id = "32"
        node.driver = "true"
        factory.mode = "merge"
        audio.adapt.follower = ""
        library.name = "audioconvert/libspa-audioconvert"
        object.id = "47"
        node.max-latency = "8192/48000"
    Ports:
        analog-output-lineout: Line Out (type: Line, priority: 9000, availability group: Legacy 4, available)
        analog-output-headphones: Headphones (type: Headphones, priority: 9900, availability group: Legacy 5, not available)
    Active Port: analog-output-lineout
    Formats:
        pcm

答案1

诀窍在于,您需要切换接收器输入 - 仅在其处于活动/运行时才可用:

pactl list short sink-inputs
28  8   31  protocol-native.c   float32le 2ch 48000Hz

您的流“28”已映射到输出“8”。可以使用以下命令检查该输出:

pactl list short sinks
1   alsa_output.pci-0000_1e_00.3.analog-stereo  module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED
8   alsa_output.pci-0000_1c_00.1.hdmi-stereo-extra3 module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED

因此,目前输入流 28(无论源流是什么)都指向 HDMI 卡。如果您想将输出更改为内部的模拟卡(例如耳机)只需发出以下命令即可移动它:

pactl move-sink-input 28 1

因此,您的一个接收器代表耳机,另一个代表扬声器。因此,您无需使用 (”),而是需要将输入切换为 52 或 55。

编辑

由于除了模拟耳机之外我不熟悉任何光学设备,因此我会尝试拖曳开关端口:

pactl list sinks
Sink #16
    State: RUNNING
    Name: alsa_output.pci-0000_1e_00.3.analog-stereo
    ....
    Ports: 
        analog-output-lineout: Line Out (type: Line, priority: 9000, available)
        analog-output-headphones: Headphones (type: Headphones, priority: 9900, available)
    Active Port: analog-output-lineout

在这种情况下,你可以使用以下命令切换接收器 #16 中的端口:

pactl set-sink-port 16 analog-output-lineout

或者

pactl set-sink-port 16 analog-output-headphones

您需要找到正确的接收器索引和端口名称 - 仅适用于数字输出是端口的情况。

这个例子默认情况下并不工作,因为 pulseaudio 和 alsa 通常会在插入扬声器时自动静音(见这里欲了解更多信息),但通过适当的设置,它确实可以工作。

答案2

我的问题显然是由于使用 PipeWire 造成的。我无法解决在使用 PipeWire 时遇到的这些问题,因此我尝试卸载,结果遇到了 GUI 崩溃,不得不sudo apt install --reinstall ubuntu-desktop重新启动。在此之后,仅运行 PulseAudio 守护程序,我能够使用:

pactl set-card-profile {card_number} {profile_name}

成功切换输出,延迟时间很短(延迟期间没有声音输出)。这似乎是能做到的最好了,我就不多说了。

相关内容