title: Play and stream transcribe audio with python speech_recognition
body: <p>I am new to Python and trying to figure out on how I can <strong>transcribe an audio speech from file in realtime</strong> with the sound playing along in the background.</p>
  
  <p>Update:</p>
  
  <blockquote>
    <p>@petezurich Sorry for the bad question. Currently, I can hear the
    audio playing in the background. However, I am having trouble getting
    Sphinx to transcribe the audio. Is there something wrong with the way
    I am passing the audio to Sphinx?
    It's constantly outputting "Sphinx error" message.</p>
  </blockquote>
  
  <p>I am using <em>PocketSpinx</em> with the <em>Uberi/speech_recognition</em> library.</p>
  
  <p>This is what I have put together so far:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>#!/usr/bin/env python
  # recognitions.py : Transcribe Test from an Audio File
  
  import os
  import sys
  import time
  import wave
  import pyaudio
  import speech_recognition as sr
  import threading
  
  try:
      import pocketsphinx
  except:
      print("PocketSphinx is not installed.")
  
  # import audio file within script folder
  from os import path
  audio_file = path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "samples/OSR_us_000_0061_8k.wav")
  print("Transcribing... " + audio_file)
  wf = wave.open(audio_file, 'rb')
  
  # set PyAudio instance
  pa = pyaudio.PyAudio()
  # set recognizer instance (unmodified)
  r = sr.Recognizer()
  
  stream_buffer = bytes()
  stream_counter = 0
  audio_sampling_rate = 48000
  
  def main_recognize(stream):
      global audio_sampling_rate
      # Create a new AudioData instance, which represents "mono" audio data
      audio_data = sr.AudioData(stream, audio_sampling_rate, 2)
  
      # recognize using CMU Sphinx (en-US only)
      try:
          print("Sphinx: " + r.recognize_sphinx(audio_data, language="en-US"))
      except sr.UnknownValueError:
          print("Sphinx error")
      except sr.RequestError as e:
          print("Sphinx error; {0}".format(e))
  
  def stream_audio(data):
      global stream_buffer
      global stream_counter
  
      buffer_set_size = 200
  
      if stream_counter &lt; buffer_set_size:
          # force 'data' to BYTES to allow concat
          data = bytes()
          stream_buffer += data
          stream_counter += 1
      else:
          threading.Thread(target=main_recognize, args=(stream_buffer,)).start()
          # reset
          stream_buffer = bytes()
          stream_counter = 0
  
  
  # define callback
  def callback(in_data, frame_count, time_info, status):
      data = wf.readframes(frame_count)
      stream_audio(in_data)
      return (data, pyaudio.paContinue)
  # open audio stream
  stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()),
                  channels=wf.getnchannels(),
                  rate=wf.getframerate(),
                  output=True,
                  stream_callback=callback)
  # start the stream
  stream.start_stream()
  # wait for stream to finish
  while stream.is_active():
      time.sleep(0.1)
  # stop stream
  stream.stop_stream()
  stream.close()
  wf.close()
  # close PyAudio
  pa.terminate()</code></pre>
  </div>
  </div>
  </p>
  
  <p>Any advice or recommendation on what might I been doing wrong?</p>
  
  <p>Is my approach heading to the right direction?</p>
  
  <p>Thank you in advance!</p>
  
  <p><a href="https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst" rel="nofollow noreferrer">https://github.com/Uberi/speech_recognition/blob/master/reference/library-reference.rst</a></p>