0

I have a raspberry pi model B, running speech_recognition 4.1 and pyaudio 0.2.8, and I am using a Logitech usb mincrophone. When I try and start speech recognition using with sr.Microphone() as source: audio = r.listen(source)(which I got from the github page), I get this error

    ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
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 "AI.py", line 62, in 
with sr.Microphone(0) as source:
File "/usr/local/lib/python2.7/dist-packages/SpeechRecognition-1.4.0-py2.7.egg/speech_recognition/init.py", line 61, in enter
input = True, # stream is an input stream
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

The part at the bottom IOError: [Errno Invalid number of channels] -9998 I had got before when I specified the wrong number of channels in a pyaudio script, but here I am not specifying any channels

0

I had the exact same problem when I first started working with the speech recog library on my Pi, and was unable to get microphone working at all.

I worked around this problem by using PyAudio to save my speech to a .WAV file and then using the speech recognition librarys "from wav" functionality that worked perfectly

I used audioop to see if there was any sound being picked up by the mic like this:

p = pyaudio.PyAudio()

stream = p.open(format = pyaudio.paInt16,
    channels = 1,
    rate = sRate,
    input = True,
    frames_per_buffer = fPerBuffer,
    input_device_index = 2)

recog = sr.Recognizer("en-GB")
r = array('h')

## You will need to loop this next bit until you are finished recording
data = array('h',stream.read(framesPerBuffer))
rms = audioop.rms(data,2)
r.extend(data)
r.append(data)
########

Where RMS is the sound level being picked up by the mic, then started recording, then if that value fell below a certain threshold (i.e the user stopped talking) and stayed that way for a second, then to stop recording and save the file.

When you are finished recording and want to save the Wav file, you can do so like this:

data = pack('<' + ('h'*len(r)), *r)
sample_width = p.get_sample_size(pyaudio.paInt16)

wf = wave.open("/home/pi/Desktop/foo.wav", 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(sRate)
wf.writeframes(data)
wf.close()

Then send the .WAV file to SR for transcription like so:

with sr.WavFile("foo.wav") as source:
    audio = recog.record(source)

I know it wont fix your microphone class issue, and is abit of a pain to code up, but this is currently working for me, I learnt alot about PyAudio in the process, and it has made things much easier for me doing it this way round, as you have a lot of flexibility to try out different sampling rates and sound levels etc.

  • I am quite new to pyaudio, could you tell me what sRate and fPerBuffer and meant to be, and what 'r = array('h') data = array('h',stream.read(framesPerBuffer))' does – Richard Beattie Jun 12 '15 at 11:34
  • Sure, sRate is the sample rate for the device you are using, and fPerBuffer is the amount of frames you pull into each "chunk". These are essentially integer numbers stored in a varaible, they will change depending on what device you are using. – Aphire Jun 12 '15 at 11:37
  • stackoverflow.com/questions/20760589/… this link will show you how to obtain that information – Aphire Jun 12 '15 at 11:37
  • And data = array('h',stream.read(framesPerBuffer)) will pull the sound information from the stream you open at the top, and store it in an array of values called "data". This is the information you will save to your wav file, and check the sound level etc – Aphire Jun 12 '15 at 11:38
  • I get this error 'NameError: name 'audioop' is not defined' what am I supposed to define audioop as – Richard Beattie Jun 12 '15 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.