Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python - How to select a specific input device with PyAudio

When recording audio via PyAudio, how do you specify the exact input device to use?

My computer has two microphones, one built-in and one via USB, and I want to record using the USB mic. The Stream class has an input_device_index for selecting the device, but it's unclear how this index correlates to the devices. For example, how do I know which device index 0 refers to? If I had to guess, I'd say 0 refers to the built-in device while 1 refers to the USB device, but I'd like to find some programmatic way of confirming this. On Linux, is there a way to get a list of these indexes and the devices they refer to?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use: get_device_info_by_host_api_device_index. For instance:

import pyaudio
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print "Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...