![](/img/trans.png)
[英]Python SpeechRecognition - OSError: [Errno -9998] Invalid number of channels
[英]pyaudio OSError: [Errno -9998] Invalid number of channels (On mac with External sound card)
我正在從聲卡編寫一個 python 程序到 stream 數據。 當我 select 內部麥克風或外部麥克風時,它工作得很好,但不能與外部立體聲聲卡 (TI PCM2900C) 一起使用。
我已經嘗試了 1 通道和 2 通道以及不同的采樣率,但錯誤仍然存在。
我的輸入設備是:
0: USB 音頻編解碼器
1:外置麥克風
2:iMac 麥克風
設備 1 和 2 工作正常,但設備 0 出現錯誤。
p = pyaudio.PyAudio()
# Open the selected audio input device
stream = p.open(
format=pyaudio.paInt16,
channels=2,
rate=48000,
input=True,
input_device_index= 0,
frames_per_buffer=1024)
將通道號更改為 1 或 2 沒有任何區別,也不會更改采樣率。
我試過了...
# Get the number of channels that the selected device supports
device_channels = p.get_device_info_by_host_api_device_index(0, device_index).get('maxInputChannels')
print('Input device channels is ', device_channels)
它為設備“0”返回“0”,即使它是立體聲 CODEC。
我解決了這個問題,原來有些音頻設備有單獨的輸入索引和 output 通道。 在我的實例中,USB AUDIO CODEC 顯示索引 0 和 1。
{'index': 0, 'structVersion': 2, 'name': 'USB AUDIO CODEC', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.01, 'defaultLowOutputLatency': 0.004354166666666667, 'defaultHighInputLatency': 0.1, 'defaultHighOutputLatency': 0.0136875, 'defaultSampleRate': 48000.0}
{'index': 1, 'structVersion': 2, 'name': 'USB AUDIO CODEC', 'hostApi': 0, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.0057083333333333335, 'defaultLowOutputLatency': 0.01, 'defaultHighInputLatency': 0.015041666666666667, 'defaultHighOutputLatency': 0.1, 'defaultSampleRate': 48000.0}
當我選擇設備索引 0(輸出)時,我收到了一個錯誤,因為輸入通道的數量為零。
只要確保在選擇輸入之前列出所有設備即可。
希望這是有道理的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.