Getting IOError: [Errno Invalid number of channels] -9998 when using mic with PyAudio on Raspberry Pi

Getting IOError: [Errno Invalid number of channels] -9998 when using mic with PyAudio on Raspberry Pi

各位坏消息!

我尝试通过 PyAudio 在 Raspberry Pi 上使用麦克风,但没有成功。麦克风已连接到 USB 声卡。

当我通过 'arecord' 时麦克风工作:

pi@raspberrypi ~ $ arecord -D plughw:0,0 -f cd test2.wav
Recording WAVE 'test2.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

但是,当我在 PyAudio 中进行测试 record.py 时,出现错误。 Python 中所有使用 PyAudio 的程序的错误都是一样的:

pi@raspberrypi /usr/src/pyaudio/test $ python record.py 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1438
Expression 'ValidateParameters( inputParameters, hostApi, StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2742
Traceback (most recent call last):
  File "record.py", line 26, in <module>
frames_per_buffer=CHUNK)
  File "/usr/local/lib/python2.7/dist-packages/pyaudio.py", line 747, in open
stream = Stream(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyaudio.py", line 442, in __init__
self._stream = pa.open(**arguments)
IOError: [Errno Invalid number of channels] -9998

感谢您的帮助

Raspberry PI 是否安装了声卡? 默认情况下,您会获得音频输出,但不会获得音频输入。 您可以通过运行

查看
alsamixer

如果您按 F6,您应该会看到列出了您的声卡。 如果你按 F4 你应该会看到捕获设备(如果有的话)

我使用了 eBay 的廉价 USB 声卡进行测试。

拥有带音频输入的声卡后,确保配置 pyaudio 使用正确的声卡索引和通道数(我的是单声道音频输入,不是立体声)。

pyaudio.PyAudio().open(format=pyaudio.paInt16,
                        rate=44100,
                        channels=1, #change this to what your sound card supports
                        input_device_index=1, #change this your input sound card index
                        input=True,
                        output=False,
                        frames_per_buffer=1024)

您可以使用 pyaudio.PyAudio 的实例方法 get_device_countget_device_info_by_index 来获取索引。