1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import snd import time from datetime import date import threading from pydispatch import dispatcher import wave filename = "demo.wav" def worker(filename): """thread worker function http://www.doughellmann.com/PyMOTW/threading/ https://github.com/uskr/pynma http://stackoverflow.com/questions/1092531/event-system-in-python """ print 'Worker for '+filename return def filenameMaker(): global filename filename = date.today().isoformat()+'-'+str(time.time())+'-demo.wav' def record_to_async_file(): "Records from the microphone and outputs the resulting data to `path`" sample_width, data = snd.record() data = snd.pack('<' + ('h'*len(data)), *data) path = filename dispatcher.send( signal='FILE_CREATED', sender=filename, filename=filename) wf = wave.open(path, 'wb') wf.setnchannels(1) wf.setsampwidth(sample_width) wf.setframerate(snd.RATE) wf.writeframes(data) wf.close() print("done - result written to "+path) dispatcher.connect( filenameMaker, signal='SND_STARTED', sender=dispatcher.Any ) dispatcher.connect( worker, signal='FILE_CREATED', sender=dispatcher.Any ) print "Scannr started..." while True: print "ready to record" record_to_async_file() |