bfinio
Posts: 38
Joined: Mon Oct 14, 2013 8:53 pm

Invalid sample rate -9997 with PyAudio and pygsr

Wed Dec 11, 2013 10:52 pm

Hi - I am a relative Raspberry Pi and Linux newbie, so I apologize in advance.

I am trying to do simple voice recognition with the Raspberry Pi, to execute commands like turning the GPIO pins on and off for home automation. I got it working in a terminal pretty quickly using this great tutorial by Steven Hickson, but I wanted to try it with Python instead. I googled it and found this: https://bitbucket.org/drneox/pygsr.

I followed the necessary directions to download and install pyaudio and pygsr, then tried running their example code:

Code: Select all

from pygsr import Pygsr
speech = Pygsr()
speech.record(3) # duration in seconds (3)
phrase, complete_response = speech.speech_to_text('es_ES') # select the language
print phrase
which gave me the following error:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Documents/speech_recognition.py", line 4, in <module>
    speech.record(3) # duration in seconds
  File "/usr/local/lib/python2.7/dist-packages/pygsr/__init__.py", line 29, in record
    frames_per_buffer=self.chunk)
  File "/usr/lib/pymodules/python2.7/pyaudio.py", line 714, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/pyaudio.py", line 396, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno Invalid sample rate] -9997
Looking at this error, I guessed it had something to do with the audio sample rate from my microphone (Logitech Quickcam Pro 9000 webcam). I googled it and found this thread, and followed the directions there to figure out my device's sample rate. That returned

'defaultSampleRate': 16000.0

So...now I know the sample rate should be 16,000, but I cannot figure out how to set it. I opened the files __init__.py, and pyaudio.py and looked at lines 29, 714, and 396 respectively (as listed in the error message above), but could not find anything that looked like it was hard-coding the sample rate. I googled "pyaudio set sample rate", expecting there to be some command to manually set the sample rate (again, newbie here) but didn't see anything obvious.

So, here I am. I'd appreciate any help in newbie-friendly terms. I'm guessing I need to do something, somewhere, to make Python accept my device's sample rate, but have no idea how.

Finally, I'm running the version of Raspbian from the official raspberrypi.org download page. This code is running in Python 2.7 because I couldn't get pygsr to work with 3.3.

korween
Posts: 2
Joined: Thu Dec 12, 2013 8:41 am

Re: Invalid sample rate -9997 with PyAudio and pygsr

Thu Dec 12, 2013 9:18 am

Hello, this is my 1st post on this website.

I've been testing the pygsr plugin on the raspi recently, and I needed to have this plugin working.

Let me explain:

- You are probably using a USB Sound Card. It has specifications, such as the sample rate. The problem is that the sample rate defined by pyaudio (and so by pygsr) differs from the expected sample rate. You would want to fix that. (But it's not that simple).

- First, follow this tutorial to determine your sample rate (go to USB section): http://www.voxforge.org/home/docs/faq/f ... pling-rate

- Then, find your plugin. It should be an egg file:

Code: Select all

cd /usr/lib/python2.7/site-packages/
ls
- As strange as it sounds, you don't want the egg file. Remove it using:

Code: Select all

rm <name of the egg>
I agree, that is NOT very clean, especially if you installed it using pip, but it works.

- Now you want to replace the missing file. First, get the git package from the repo:

Code: Select all

sudo apt-get install git
Then clone the following git: https://bitbucket.org/drneox/pygsr.git :

Code: Select all

git clone https://bitbucket.org/drneox/pygsr.git
Finally, just move the python file to the right folder:

Code: Select all

mv pygsr/pygsr.py pygsr.py
rm -rf pygsr
- You are now good to go. Let's tweak the plugin itself. Don't worry, it will not affect your code, in fact, you won't have to change anything. Edit the python file with your favorite editor (nano or vim):

Code: Select all

vim pygsr.py
Now, your error hides another one, so we'll correct them both:

Line 17: this is where you set your sample rate. Just use the one you found at the beginning.
Line 19: this is the tweaky part. Set your chunk to half of the sample rate (mine is 48000, so my chunks will be 24000). Otherwise, you'll have an input overflow.
Line 23 and 50 : replace the chunk size with the one you defined earlier.

That's it, save the file and exit.

You are done sir! Take it for a spin. Make sure however that you record for too long (10 sec recording won't work, it's going to overflow). You could try increasing the chunk size to record for a longer recording time.

Hope this helps

- KoR

bfinio
Posts: 38
Joined: Mon Oct 14, 2013 8:53 pm

Re: Invalid sample rate -9997 with PyAudio and pygsr

Thu Dec 12, 2013 4:08 pm

Hi - thank you for the very detailed response. I will try this procedure out in a little bit - but first, could you explain:
korween wrote: - You are probably using a USB Sound Card. It has specifications, such as the sample rate. The problem is that the sample rate defined by pyaudio (and so by pygsr) differs from the expected sample rate. You would want to fix that. (But it's not that simple).
I just have my Logitech Quickcam Pro 9000 plugged directly into one of the Raspberry Pi's USB ports. Does the Quickcam (or any USB webcam in general) have a built-in USB sound card that's specific to the device? My understanding of sound cards was that they're something fancy audiophiles put in their PCs if they want better audio output than a standard 3.5mm stereo jack.

korween
Posts: 2
Joined: Thu Dec 12, 2013 8:41 am

Re: Invalid sample rate -9997 with PyAudio and pygsr

Thu Dec 12, 2013 8:22 pm

Hello,

First, I would like to warn you about using webcams on a raspi. If you are simply trying to use it for sound, then your webcam might be a little bit overkill. I am using the Logilink UA0053 USB sound card, which has jack ports on it. It allows me to record good quality sound using my mic, and it is fully compatible with the Raspi.

To answer your question, your webcam does indeed have an integrated sound card, you can even check it by typing:

Code: Select all

arecord -l
You will see "card 1", meaning that it is a sound card.

However, I can't guarantee the quality of the sound nor the performance of the device on the Raspi (even though I have to admit, your webcam works really well on a PC). Concerning the sample rate, I think you webcam defaults to 48kHz aswell.

My advice to you would be to get yourself a USB sound card and a mic. However, if you want to use video capture, the Raspi camera module is by far the best choice out there. Finally, here are some useful links to check the compatibility of your webcam and sound card with the Raspi:

Webcams: http://elinux.org/RPi_USB_Webcams
Sound cards: http://elinux.org/RPi_VerifiedPeriphera ... ound_Cards

bfinio
Posts: 38
Joined: Mon Oct 14, 2013 8:53 pm

Re: Invalid sample rate -9997 with PyAudio and pygsr

Thu Dec 12, 2013 8:26 pm

Ah. The only reason I'm using this webcam is because I already had it sitting around and it has a built-in mic. I have no need for video at the moment. In the long run I will probably do as you suggested and look for a dedicated sound card and mic.

Thanks again for the help - still haven't had a chance to go through your previous suggestions, but will get there eventually...

Return to “Troubleshooting”