通过 bash 命令切换音频设备

通过 bash 命令切换音频设备

我正在尝试编写一个 bash 命令来在音频设备(而不是不同的卡)之间切换。我发现这篇 AskUbuntu 帖子关于这个话题,然后我开始按照下面的列表尝试每个输出设备,以找出哪个是我的扬声器,哪个是我的耳机。 有没有更好的方法?

我尝试过的:

pactl set-card-profile 0 output:hdmi-surround71

对于每个音频设备,从扬声器到耳机输出没有变化。

$ pacmd list-cards | grep output\:
        output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400, available: unknown)
        output:hdmi-surround: Digital Surround 5.1 (HDMI) Output (priority 300, available: unknown)
        output:hdmi-surround71: Digital Surround 7.1 (HDMI) Output (priority 300, available: unknown)
        output:hdmi-stereo-extra1: Digital Stereo (HDMI 2) Output (priority 5200, available: unknown)
        output:hdmi-surround71-extra1: Digital Surround 7.1 (HDMI 2) Output (priority 100, available: unknown)
        output:hdmi-stereo-extra2: Digital Stereo (HDMI 3) Output (priority 5200, available: unknown)
        output:hdmi-surround-extra2: Digital Surround 5.1 (HDMI 3) Output (priority 100, available: unknown)
        output:hdmi-surround71-extra2: Digital Surround 7.1 (HDMI 3) Output (priority 100, available: unknown)
        output:hdmi-stereo-extra3: Digital Stereo (HDMI 4) Output (priority 5200, available: unknown)
        output:hdmi-surround-extra3: Digital Surround 5.1 (HDMI 4) Output (priority 100, available: unknown)
        output:hdmi-surround71-extra3: Digital Surround 7.1 (HDMI 4) Output (priority 100, available: unknown)
    active profile: <output:hdmi-surround71>
        output:analog-stereo: Analog Stereo Output (priority 6000, available: unknown)
        output:analog-stereo+input:analog-stereo: Analog Stereo Duplex (priority 6060, available: unknown)
        output:analog-surround-21: Analog Surround 2.1 Output (priority 800, available: unknown)
        output:analog-surround-21+input:analog-stereo: Analog Surround 2.1 Output + Analog Stereo Input (priority 860, available: unknown)
        output:analog-surround-40: Analog Surround 4.0 Output (priority 700, available: unknown)
        output:analog-surround-40+input:analog-stereo: Analog Surround 4.0 Output + Analog Stereo Input (priority 760, available: unknown)
        output:analog-surround-41: Analog Surround 4.1 Output (priority 800, available: unknown)
        output:analog-surround-41+input:analog-stereo: Analog Surround 4.1 Output + Analog Stereo Input (priority 860, available: unknown)
        output:analog-surround-50: Analog Surround 5.0 Output (priority 700, available: unknown)
        output:analog-surround-50+input:analog-stereo: Analog Surround 5.0 Output + Analog Stereo Input (priority 760, available: unknown)
        output:analog-surround-51: Analog Surround 5.1 Output (priority 800, available: unknown)
        output:analog-surround-51+input:analog-stereo: Analog Surround 5.1 Output + Analog Stereo Input (priority 860, available: unknown)
        output:analog-surround-71: Analog Surround 7.1 Output (priority 700, available: unknown)
        output:analog-surround-71+input:analog-stereo: Analog Surround 7.1 Output + Analog Stereo Input (priority 760, available: unknown)
        output:iec958-stereo: Digital Stereo (IEC958) Output (priority 5500, available: unknown)
        output:iec958-stereo+input:analog-stereo: Digital Stereo (IEC958) Output + Analog Stereo Input (priority 5560, available: unknown)
    active profile: <output:analog-stereo+input:analog-stereo>
        iec958-stereo-output: Digital Output (S/PDIF) (priority 0, latency offset 0 usec, available: unknown)
$ pactl set-card-profile 0 output:hdmi-surround71

我的水槽:

$ pacmd list-sinks
2 sink(s) available.
  * index: 10
    name: <alsa_output.pci-0000_00_14.2.analog-stereo>
    driver: <module-alsa-card.c>
    flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
    state: RUNNING
    suspend cause: 
    priority: 9959
    volume: front-left: 50462 /  77% / -6.81 dB,   front-right: 50462 /  77% / -6.81 dB
            balance 0.00
    base volume: 65536 / 100% / 0.00 dB
    volume steps: 65537
    muted: no
    current latency: 23.16 ms
    max request: 4 KiB
    max rewind: 64 KiB
    monitor source: 11
    sample spec: s16le 2ch 44100Hz
    channel map: front-left,front-right
                 Stereo
    used by: 1
    linked by: 1
    configured latency: 23.22 ms; range is 1.00 .. 371.52 ms
    card: 1 <alsa_card.pci-0000_00_14.2>
    module: 7
    properties:
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = "ALC892 Analog"
        alsa.id = "ALC892 Analog"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "0"
        alsa.card = "0"
        alsa.card_name = "HDA ATI SB"
        alsa.long_card_name = "HDA ATI SB at 0xfe200000 irq 16"
        alsa.driver_name = "snd_hda_intel"
        device.bus_path = "pci-0000:00:14.2"
        sysfs.path = "/devices/pci0000:00/0000:00:14.2/sound/card0"
        device.bus = "pci"
        device.vendor.id = "1002"
        device.vendor.name = "Advanced Micro Devices, Inc. [AMD/ATI]"
        device.product.id = "4383"
        device.product.name = "SBx00 Azalia (Intel HDA)"
        device.form_factor = "internal"
        device.string = "front:0"
        device.buffering.buffer_size = "65536"
        device.buffering.fragment_size = "32768"
        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 ALC892"
        alsa.components = "HDA:10ec0892,1462f640,00100302"
        module-udev-detect.discovered = "1"
        device.icon_name = "audio-card-pci"
    ports:
        analog-output-lineout: Line Out (priority 9900, latency offset 0 usec, available: no)
            properties:

        analog-output-headphones: Headphones (priority 9000, latency offset 0 usec, available: yes)
            properties:
                device.icon_name = "audio-headphones"
    active port: <analog-output-headphones>
    index: 14
    name: <alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1>
    driver: <module-alsa-card.c>
    flags: HARDWARE DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
    state: SUSPENDED
    suspend cause: IDLE 
    priority: 9050
    volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB
            balance 0.00
    base volume: 65536 / 100% / 0.00 dB
    volume steps: 65537
    muted: no
    current latency: 0.00 ms
    max request: 0 KiB
    max rewind: 0 KiB
    monitor source: 15
    sample spec: s16le 2ch 44100Hz
    channel map: front-left,front-right
                 Stereo
    used by: 0
    linked by: 0
    configured latency: 0.00 ms; range is 0.50 .. 371.52 ms
    card: 0 <alsa_card.pci-0000_01_00.1>
    module: 6
    properties:
        alsa.resolution_bits = "16"
        device.api = "alsa"
        device.class = "sound"
        alsa.class = "generic"
        alsa.subclass = "generic-mix"
        alsa.name = "HDMI 1"
        alsa.id = "HDMI 1"
        alsa.subdevice = "0"
        alsa.subdevice_name = "subdevice #0"
        alsa.device = "7"
        alsa.card = "1"
        alsa.card_name = "HDA NVidia"
        alsa.long_card_name = "HDA NVidia at 0xfe080000 irq 26"
        alsa.driver_name = "snd_hda_intel"
        device.bus_path = "pci-0000:01:00.1"
        sysfs.path = "/devices/pci0000:00/0000:00:02.0/0000:01:00.1/sound/card1"
        device.bus = "pci"
        device.vendor.id = "10de"
        device.vendor.name = "NVIDIA Corporation"
        device.product.id = "0fbb"
        device.product.name = "GM204 High Definition Audio Controller"
        device.string = "hdmi:1,1"
        device.buffering.buffer_size = "65536"
        device.buffering.fragment_size = "32768"
        device.access_mode = "mmap+timer"
        device.profile.name = "hdmi-stereo-extra1"
        device.profile.description = "Digital Stereo (HDMI 2)"
        device.description = "GM204 High Definition Audio Controller Digital Stereo (HDMI 2)"
        alsa.mixer_name = "Nvidia GPU 71 HDMI/DP"
        alsa.components = "HDA:10de0071,196e1131,00100100"
        module-udev-detect.discovered = "1"
        device.icon_name = "audio-card-pci"
    ports:
        hdmi-output-1: HDMI / DisplayPort 2 (priority 5800, latency offset 0 usec, available: yes)
            properties:
                device.icon_name = "video-display"
                device.product.name = "ASUS VH238
  "
    active port: <hdmi-output-1>

答案1

来自 AskUbuntu 上的一条评论关于更换音频接收器来自@Julian:

#!/bin/bash

SINK=`pacmd list-sinks | grep '  index' | cut -f6 -d' '`
pacmd set-default-sink ${SINK}
pacmd list-sink-inputs | grep index | while read line
do
pacmd move-sink-input `echo $line | cut -f2 -d' '` ${SINK}
done

切换到最后一个未使用的输出。这足以在输入之间切换。

答案2

我知道这不是你想要的答案,但测试表明,只需将一副耳机插入耳机插孔即可消除扬声器插孔的任何输出。您可以通过声音应用程序亲自测试这一点,如下所示。运行它,拔下耳机,您将看到以下内容:

无耳机声音

使用测试声音按钮来确认扬声器的输出是否正常。

然后插入耳机。您将看到它们自动突出显示,如下所示:

声音耳机

如果您现在突出显示之前选择的输出(在我的情况下为模拟输出)并重复测试声音过程,您将不会从扬声器中得到任何声音,因为插入耳机的行为会在硬件级别改变路由。这通常是通过内部看起来像这样的开关触点连接器来实​​现的。

开关接触器连接器

一旦您插入插孔,插孔中的“弹簧式”连接器就会连接到公插头,并与扬声器内部断开。


为了有机会做到这一点,你可能需要更换或修改耳机插孔,以确保插入时信号不会中断到扬声器。我不是建议采取这一行动,并将不是承担任何责任损害您尝试对您的系统造成损害。

然后,您必须利用该find命令来确定在更改声音应用程序的输出所花的几秒钟内更改了哪些文件,并分析这些更改以获取有关如何通过 CLI 复制它们的线索。这部分未经测试,完全是我根据手头的信息做出的假设。

资料来源:

测试

http://www.instructables.com/answers/How-do-I-make-a-headphone-jack-cancel-the-output-o/

相关内容