I am trying to use Pocketsphinx for voice recognition. When I was trying to include it in python, using this file:


from os import environ, path

from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *

MODELDIR = "pocketsphinx-5prealpha/model"
DATADIR = "pocketsphinx-5prealpha/test/data"

config = Decoder.default_config()
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin'))
config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
config.set_string('-logfn', '/dev/null')
decoder = Decoder(config)

import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)

in_speech_bf = False
while True:
    buf = stream.read(1024)
    if buf:
        decoder.process_raw(buf, False, False)
        if decoder.get_in_speech() != in_speech_bf:
            in_speech_bf = decoder.get_in_speech()
            if not in_speech_bf:
                print 'Result:', decoder.hyp().hypstr

But when I run the file, I get an error from pyAudio saying Invalid Sample Rate. This is the output that I get: Error Screenshot

  • 1
    Apparently, your sound device (whatever it is) does not support 16 kHz.
    – CL.
    Mar 7, 2017 at 8:51
  • So, how can I reduce it? If i reduce it will it be ok? Mar 7, 2017 at 11:09
  • 1
    Use a supported rate. I don't know your hardware, but try 48 or 44.1 kHz.
    – CL.
    Mar 7, 2017 at 11:51
  • 1
    Yes... It worked when I put 44100.. Thanks! But now i have got the error input overflowed... i have checked with almost all of the questions. One said trying to change the chunk value but it didn't work for me when i put a few values... I will try... Can you give a solution?? Mar 11, 2017 at 16:16
  • You need a larger buffer size (as large as possible). But if your CPU is too slow, no amount of buffering will help.
    – CL.
    Mar 11, 2017 at 19:04


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.