I have been trying to use a sound card through raspberry pi with the help of pyaudio, however I am getting an error saying invalid No. of channels. I am unable to figure out how to resolve this error.

    import speech_recognition as sr

    # Record Audio
    r = sr.Recognizer()
    mic = sr.Microphone(device_index=2)
    with mic as source:
        print("Say something!")
        audio = r.listen(source)

    print("processing")
    # Speech recognition using Google Speech Recognition
    try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
        print("You said: " + r.recognize_google(audio))
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

The error I have been getting is -

Traceback (most recent call last):
  File "test1.py", line 7, in <module>
    with mic as source:
  File "/home/pi/.local/lib/python3.5/site-packages/speech_recognition/__init__.py", line 141, in __enter__
    input=True,  # stream is an input stream
  File "/usr/local/lib/python3.5/dist-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)

I have tried printing the device list to select the right device. Here is the detected device list -

  0 bcm2835 ALSA: - (hw:0,0), ALSA (0 in, 2 out)
  1 bcm2835 ALSA: IEC958/HDMI (hw:0,1), ALSA (0 in, 2 out)
  2 USB PnP Sound Device: Audio (hw:1,0), ALSA (0 in, 2 out)
  3 sysdefault, ALSA (0 in, 128 out)
  4 dmix, ALSA (0 in, 2 out)
< 5 default, ALSA (0 in, 2 out)

0 Answers