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

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

Bad news, everyone!

I try to use a microphone on my Raspberry Pi through PyAudio but without success. The microphone is connected to a USB sound card.

The microphone works when I go through '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 

However, when I do the test record.py in PyAudio, I got an error. The error is the same for all programs in Python that uses 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 

Thanks for your help

回答1:

Do you have a sound card attached to the Raspberry PI ? By default you get audio output, but not audio input. You can check by running

alsamixer 

If you press F6 you should see your sound cards listed. If you press F4 you should see capture devices(if any)

I used a cheap USB soundcard from eBay for testing.

After you have a sound card with an audio input, make sure you configure pyaudio to use the correct sound card index and the number of channels(mine had mono audio input, not stereo).

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) 

You can use pyaudio.PyAudio's instance methods get_device_count and get_device_info_by_index to get the indices.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!