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

s8vozzvw  于 2022-11-22  发布在  Linux
关注(0)|答案(7)|浏览(291)

当通过PyAudio录制音频时,如何指定要使用的确切输入设备?
我的计算机有两个麦克风,一个内置,一个通过USB,我想使用USB麦克风录音。Stream类有一个input_device_index用于选择设备,但不清楚该索引与设备的关系。例如,我如何知道索引0指的是哪个设备?如果要我猜,我会说0指的是内置设备,而1指的是USB设备。但是我想找到一些编程的方法来确认这一点。在Linux上,有没有办法得到这些索引和它们所引用的设备的列表?

guicsvcw

guicsvcw1#

您可以使用按主机api设备索引获取设备信息。
例如:

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

ttcibm8c2#

我还没有看过pyaudio,但我也在一些场合使用过sounddevice
下面是一个示例代码,列出了可用的inputoutputaudio devices

import sounddevice as sd
print sd.query_devices()

正如您可以从下面的输出看到的,当我把我的耳机麦克风插孔,Index 1是可用的输入。1 Jack Mic (IDT High Definition A, MME (2 in, 0 out)
而默认笔记本电脑音频麦克风显示为index 2
2 Microphone Array (IDT High Defi, MME (2 in, 0 out)

输出

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

lqfhib0f3#

在PyAudio文档中,它声明您可以定义input_device_index
要找出该设备索引是什么,您可以按照此Github Gist中提供的代码或Raspberry Pi Forum上的代码(提供了输出代码的示例)进行操作。

qqrboqgw

qqrboqgw4#

您可以使用PulseAudio选择输入设备。

fhg3lkii

fhg3lkii5#

使用@slegroux awesome代码查找音频索引:

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

找到要使用的麦克风的索引后,将input_device_index选项添加到www.example.com()中,后跟麦克风的索引(在我的例子中,麦克风的索引为1)p.open,如下所示:

stream = p.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    frames_per_buffer=FRAMES_PER_BUFFER,
    input_device_index=1
)

希望这对你有帮助!

tnkciper

tnkciper6#

我不了解PyAudio,但是对于sounddevice模块,它是这样的:

python3 -m sounddevice
svujldwt

svujldwt7#

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

相关问题