Getting list of audio Input devices in Python

烈酒焚心 提交于 2019-12-02 09:04:40

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')

gives me

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

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]

outputs

hw:1,0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!