如何使用PyAudio选择特定的输入设备

29

在使用PyAudio录制音频时,如何指定要使用的确切输入设备?

我的电脑有两个麦克风,一个是内置的,一个是通过USB连接的,我想使用USB麦克风进行录音。 Stream类 有一个 input_device_index 用于选择设备,但不清楚该索引如何与设备相关联。例如,如何确定设备索引0代表哪个设备?如果要猜测,我会说0代表内置设备,1代表USB设备,但我想找到一些编程方式来确认这一点。在Linux上,有没有一种获取这些索引及其所指设备列表的方法?

8个回答

42

您可以使用get_device_info_by_host_api_device_index。

例如:

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

1
这段代码对我来说失败了。我还需要添加:info = p.get_host_api_info_by_index(0),然后numdevices = info.get('deviceCount')。并且我需要使用p而不是self.p,然后它就可以工作了。请修正您的答案,我会点赞的。谢谢。 - Wayne Piekarski
3
那么,您如何将列出的设备之一分配为pyaudio设备? - TestinginProd
2
如何选择输入设备?get_device_info_by_host_api_device_index 只能获取信息。 - fsp
11
input_device_index=x 是解决方案。 - fsp
这也适用于输出设备吗? - Chris P
显示剩余2条评论

3

我还没有看过pyaudio,但我在几个场合使用过sounddevice

这里有一个列出可用的inputoutputaudio devices的示例代码。

import sounddevice as sd
print sd.query_devices() 

如下所示,当我将耳机插入麦克风插孔时,索引1可用作输入。1 Jack Mic (IDT High Definition A, MME (2 in, 0 out) 而默认的笔记本电脑麦克风则出现在索引2输出
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
   0 Microsoft Sound Mapper - Input, MME (2 in, 0 out)
>  1 Jack Mic (IDT High Definition A, MME (2 in, 0 out)
   2 Microphone Array (IDT High Defi, MME (2 in, 0 out)
   3 Microsoft Sound Mapper - Output, MME (0 in, 2 out)
<  4 Speakers / Headphones (IDT High, MME (0 in, 2 out)
   5 Communication Headphones (IDT H, MME (0 in, 2 out)
   6 Primary Sound Capture Driver, Windows DirectSound (2 in, 0 out)
   7 Jack Mic (IDT High Definition Audio CODEC), Windows DirectSound (2 in, 0 out)
   8 Microphone Array (IDT High Definition Audio CODEC), Windows DirectSound (2 in, 0 out)
   9 Primary Sound Driver, Windows DirectSound (0 in, 2 out)
  10 Speakers / Headphones (IDT High Definition Audio CODEC), Windows DirectSound (0 in, 2 out)
  11 Communication Headphones (IDT High Definition Audio CODEC), Windows DirectSound (0 in, 2 out)
  12 Communication Headphones (IDT High Definition Audio CODEC), Windows WASAPI (0 in, 2 out)
  13 Speakers / Headphones (IDT High Definition Audio CODEC), Windows WASAPI (0 in, 2 out)
  14 Jack Mic (IDT High Definition Audio CODEC), Windows WASAPI (2 in, 0 out)
  15 Microphone Array (IDT High Definition Audio CODEC), Windows WASAPI (2 in, 0 out)
  16 Headset Microphone (Bluetooth Hands-free Audio), Windows WDM-KS (1 in, 0 out)
  17 Headphones (Bluetooth Hands-free Audio), Windows WDM-KS (0 in, 2 out)
  18 Headphones (HpOut), Windows WDM-KS (0 in, 2 out)
  19 Microphone Array (MicIn2), Windows WDM-KS (2 in, 0 out)
  20 Jack Mic (MuxedIn), Windows WDM-KS (2 in, 0 out)
  21 Dock Mic (MuxedIn), Windows WDM-KS (2 in, 0 out)
  22 Rec. Playback (MuxedIn), Windows WDM-KS (2 in, 0 out)
  23 Speakers (Speaker/HP), Windows WDM-KS (0 in, 2 out)

3
使用@slegroux的精彩代码来查找音频索引:
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'))

一旦确定了要使用的麦克风的索引,就在p.open()后面添加input_device_index选项,然后跟上麦克风的索引(在我的情况下,麦克风的索引为1),如下所示:
stream = p.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    frames_per_buffer=FRAMES_PER_BUFFER,
    input_device_index=1
)

希望这有所帮助!

1
你当前使用的麦克风索引始终为1。你可以通过@Anil_M的代码进行测试。
import sounddevice as sd
print(sd.query_devices())

运行这段代码,然后查看索引一。现在拔掉你的麦克风,再次运行代码。麦克风仍然保持在1。
只有当你想使用另一台设备时,比如我需要操作系统的音频时,你可以使用以下代码:
p = pyaudio.PyAudio()

# if there is no speaker device this all makes no sense anyways
try:
    wasapi_info = p.get_host_api_info_by_type(pyaudio.paWASAPI)
except OSError:
    exit()

#choosing the speaker device 
default_speakers=p.get_device_info_by_index(wasapi_info["defaultOutputDevice"]) 

# or "defaultInputDevice"

我必须提到的是,这段代码只适用于Windows操作系统。

0
PyAudio文档中指出,您可以定义一个input_device_index
要找出该设备索引是什么,您可以按照此Github Gist提供的代码或按照Raspberry Pi论坛上找到的代码进行操作,该论坛提供了输出代码的示例。

0

-3

我不了解PyAudio,但使用sounddevice模块是这样的:

python3 -m sounddevice

-3

只需使用arecord -l列出所有可用的输入设备。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接