title: Speech or no speech detection in Python
body: <p>I am writing a program that recognizes speech. What it does is it records audio from the microphone and converts it to text using Sphinx. My problem is I want to start recording audio only when something is spoken by the user.</p>
  
  <p>I experimented by reading the audio levels from the microphone and recording only when the level is above a particular value. But it ain't that effective. The program starts recording whenever it detects anything loud. This is the code I used </p>
  
  <pre><code>import audioop
  import pyaudio as pa
  import wav
  
  class speech():
      def __init__(self):
          # soundtrack properties
          self.format = pa.paInt16
          self.rate = 16000
          self.channel = 1
          self.chunk = 1024
          self.threshold = 150
          self.file = 'audio.wav'
  
          # intialise microphone stream
          self.audio = pa.PyAudio()
          self.stream = self.audio.open(format=self.format,
                                    channels=self.channel,
                                    rate=self.rate,
                                    input=True,
                                    frames_per_buffer=self.chunk)
  
  
      def record(self)
          while True:
              data = self.stream.read(self.chunk)
              rms = audioop.rms(data,2) #get input volume
              if rms&gt;self.threshold: #if input volume greater than threshold
                  break
  
          # array to store frames
          frames = []
          # record upto silence only
          while rms&gt;threshold:
              data = self.stream.read(self.chunk)
              rms = audioop.rms(data,2)
              frames.append(data)
  
          print 'finished recording.... writing file....'
          write_frames = wav.open(self.file, 'wb')
          write_frames.setnchannels(self.channel)
          write_frames.setsampwidth(self.audio.get_sample_size(self.format))
          write_frames.setframerate(self.rate)
          write_frames.writeframes(''.join(frames))
          write_frames.close()
  </code></pre>
  
  <p>Is there a way I can differentiate between human voice and other noise in Python ? Hope somebody can find me a solution.</p>