--- a/snd.py +++ b/snd.py @@ -1,15 +1,12 @@ -""" Record a few seconds of audio and save to a WAVE file. +""" Record a few seconds of audio and save to a WAVE file. Based on http://stackoverflow.com/questions/892199/detect-record-audio-in-python/6743593#6743593 """ import pyaudio import wave import sys -import audioop # http://docs.python.org/library/audioop -from os.path import exists from array import array from struct import unpack, pack -import threading from pydispatch import dispatcher THRESHOLD = 500 @@ -17,11 +14,11 @@ FORMAT = pyaudio.paInt16 RATE = 44100 if sys.platform.startswith('darwin'): - CHANNELS = 2 + CHANNELS = 2 elif sys.platform.startswith('win32'): - CHANNELS = 1 + CHANNELS = 1 -MAX_SILENT = 30 +MAX_SILENT = 80 def is_silent(L): "Returns `True` if below the 'silent' threshold" @@ -29,24 +26,27 @@ "print max(L) < THRESHOLD" return max(L) < THRESHOLD + def normalize(L): "Average the volume out" MAXIMUM = 16384 - times = float(MAXIMUM)/max(abs(i) for i in L) + times = float(MAXIMUM) / max(abs(i) for i in L) LRtn = array('h') for i in L: - LRtn.append(int(i*times)) + LRtn.append(int(i * times)) return LRtn + def trim(L): "Trim the blank spots at the start and end" + def _trim(L): snd_started = False LRtn = array('h') for i in L: - if not snd_started and abs(i)>THRESHOLD: + if not snd_started and abs(i) > THRESHOLD: snd_started = True LRtn.append(i) @@ -63,27 +63,29 @@ L.reverse() return L + def add_silence(L, seconds): "Add silence to the start and end of `L` of length `seconds` (float)" - LRtn = array('h', [0 for i in xrange(int(seconds*RATE))]) + LRtn = array('h', [0 for i in xrange(int(seconds * RATE))]) LRtn.extend(L) - LRtn.extend([0 for i in xrange(int(seconds*RATE))]) + LRtn.extend([0 for i in xrange(int(seconds * RATE))]) return LRtn + def record(): """ - Record a word or words from the microphone and + Record a word or words from the microphone and return the data as an array of signed shorts. - Normalizes the audio, trims silence from the - start and end, and pads with 0.5 seconds of - blank sound to make sure VLC et al can play + Normalizes the audio, trims silence from the + start and end, and pads with 0.5 seconds of + blank sound to make sure VLC et al can play it without getting chopped off. """ p = pyaudio.PyAudio() - stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, - input=True, - frames_per_buffer=CHUNK_SIZE) + stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, + input=True, + frames_per_buffer=CHUNK_SIZE) num_silent = 0 snd_started = False @@ -91,8 +93,13 @@ LRtn = array('h') while 1: - data = stream.read(CHUNK_SIZE) - L = unpack('<' + ('h'*(len(data)/2)), data) # little endian, signed short + try: + data = stream.read(CHUNK_SIZE) + except IOError as ex: + if ex[1] != pyaudio.paInputOverflowed: + raise + data = '\x00' * CHUNK_SIZE + L = unpack('<' + ('h' * (len(data) / 2)), data) # little endian, signed short L = array('h', L) silent = is_silent(L) @@ -100,11 +107,13 @@ if silent and snd_started: num_silent += 1 - print num_silent + #print num_silent elif not silent and not snd_started: - dispatcher.send( signal='SND_STARTED') + dispatcher.send(signal='SND_STARTED') snd_started = True print snd_started + if snd_started and not silent: + num_silent = 0 if snd_started: LRtn.extend(L) if snd_started and num_silent > MAX_SILENT: @@ -120,10 +129,11 @@ LRtn = add_silence(LRtn, 0.5) return sample_width, LRtn + def record_to_file(path): "Records from the microphone and outputs the resulting data to `path`" sample_width, data = record() - data = pack('<' + ('h'*len(data)), *data) + data = pack('<' + ('h' * len(data)), *data) wf = wave.open(path, 'wb') wf.setnchannels(CHANNELS) @@ -131,14 +141,12 @@ wf.setframerate(RATE) wf.writeframes(data) wf.close() - print("done - result written to "+path) + print("done - result written to " + path) del data +if __name__ == '__main__': + filename = 'demo.wav' + record_to_file(filename) + print("done - result written to " + filename) - -if __name__ == '__main__': - filename = 'demo.wav' - record_to_file(filename) - print("done - result written to "+filename) -