获取Python中的音频输入设备列表 [英] Getting list of audio Input devices in Python

查看:412
本文介绍了获取Python中的音频输入设备列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get the list of audio input devices in linux using python in this format as hw:0,1 ?

我已经尝试使用pyaudio进行以下操作:

I've tried the following using 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

I'm also able to retrieve using "arecord -l" but I need to just get it like hw:0,1 hw:0,2

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

I need it in this format. Do you have any suggestions?

谢谢.

推荐答案

如果PaDeviceInfo结构存储的名称足够,那么您可以直接从get_device_info_by_index()返回的字典访问名称",然后也许切片最后的信息:

If the name stored by the PaDeviceInfo structure is sufficient, then you could just access the 'name' from the dict returned by get_device_info_by_index(), and then perhaps slice the information off the end:

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"做同样的事情.

But this doesn't give you what you want with the default devices, the name seems to be stored as "default". In that case, executing "arecord -l" in Python can work, if that's what you're looking for. Of course, you can do the same thing for "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

这篇关于获取Python中的音频输入设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆