【问题标题】:Getting list of audio Input devices in Python在 Python 中获取音频输入设备列表
【发布时间】:2015-12-26 14:20:31
【问题描述】:

如何使用 python 获取 linux 中的音频输入设备列表,格式为 hw:0,1 ?

我使用 pyaudio 尝试了以下操作:

def getaudiodevices():
        p = pyaudio.PyAudio()
        print p.get_default_input_device_info()
        for i in range(p.get_device_count()):
                print ''#p.get_device_info_by_index(i)

我也可以使用“arecord -l”进行检索,但我需要像这样 硬件:0,1 硬件:0,2

我需要这种格式的。你有什么建议吗?

谢谢。

【问题讨论】:

    标签: python-2.7 audio device


    【解决方案1】:

    如果 PaDeviceInfo 结构存储的名称足够,那么您可以从 get_device_info_by_index() 返回的 dict 中访问“名称”,然后可能将信息从末尾切掉:

    import pyaudio
    
    def getaudiodevices():
        p = pyaudio.PyAudio()
        for i in range(p.get_device_count()):
            print p.get_device_info_by_index(i).get('name')
    

    给我

    HDA Intel HDMI: 0 (hw:0,3)
    HDA Intel HDMI: 1 (hw:0,7)
    HDA Intel HDMI: 2 (hw:0,8)
    HDA Intel PCH: CS4208 Analog (hw:1,0)
    HDA Intel PCH: CS4208 Digital (hw:1,1)
    hdmi
    default
    

    但这并没有为您提供您想要的默认设备,名称似乎存储为“默认”。在这种情况下,在 Python 中执行“arecord -l”可以工作,如果那是你正在寻找的。当然,你也可以对“aplay -l”做同样的事情。

    import os
    
    def getaudiodevices():
        devices = os.popen("arecord -l")
        device_string = devices.read()
        device_string = device_string.split("\n")
        for line in device_string:
                if(line.find("card") != -1):
                    print "hw:" + line[line.find("card")+5] + "," +\
     line[line.find("device")+7]
    

    输出

    hw:1,0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 2021-09-03
      • 2011-06-02
      相关资源
      最近更新 更多