2016-04-27 179 views
5

當通過PyAudio錄製音頻時,如何指定要使用的確切輸入設備?如何用PyAudio選擇特定輸入設備

我的電腦有兩個麥克風,一個是內置麥克風,另一個是通過USB,我想用USB麥克風錄音。 Stream class有一個input_device_index用於選擇設備,但不清楚這個索引與設備的關係。例如,我怎麼知道哪個設備索引0指的是?如果我不得不猜測,我會說0指的是內置設備,而1指的是USB設備,但我想找到一些確認此問題的編程方式。在Linux上,有沒有辦法獲得這些索引及其引用設備的列表?

回答

1

我還沒有看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) 
7

你可以使用: 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') 
+0

此代碼失敗。需要添加:info = p.get_host_api_info_by_index(0)然後numdevices = info.get('deviceCount')。此外,我需要使用p而不是self.p,然後它工作。請修復您的答案,然後我會立即投票。謝謝。 –

+0

@WaynePiekarski完成。謝謝! – slegroux