allocate filename when signal starts
allocate filename when signal starts

file:a/scannr.py -> file:b/scannr.py
--- a/scannr.py
+++ b/scannr.py
@@ -2,31 +2,43 @@
 import time
 from datetime import date
 import threading
+from pydispatch import dispatcher
+import wave
 
-filename = ""
+filename = "demo.wav"
+
 def worker(filename):
     """thread worker function
     http://www.doughellmann.com/PyMOTW/threading/
 
-    https://github.com/uskr/pynma"""
+    https://github.com/uskr/pynma
+
+http://stackoverflow.com/questions/1092531/event-system-in-python """
     print 'Worker for '+filename
+    
     return
 
 def filenameMaker():
     global filename
-    while True:
-        filename = date.today().isoformat()+'-'+str(time.time())+'-demo.wav'
-        time.sleep(5)
+    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)
 
-
-threads = []
-fm = threading.Thread(target=filenameMaker)
-fm.daemon = True
-fm.start()
+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"
-    snd.record_to_file(filename)
-    t = threading.Thread(target=worker,args=(filename,))
-    t.start()
+    record_to_async_file()

file:a/snd.py -> file:b/snd.py
--- a/snd.py
+++ b/snd.py
@@ -10,12 +10,14 @@
 from array import array
 from struct import unpack, pack
 import threading
+from pydispatch import dispatcher
 
 CHANNELS = 1
 THRESHOLD = 500
 CHUNK_SIZE = 1024
 FORMAT = pyaudio.paInt16
 RATE = 44100
+MAX_SILENT = 30
 
 def is_silent(L):
     "Returns `True` if below the 'silent' threshold"
@@ -97,9 +99,10 @@
             num_silent += 1
             print num_silent
         elif not silent and not snd_started:
+            dispatcher.send( signal='SND_STARTED')
             snd_started = True
             print snd_started
-        if snd_started and num_silent > 30:
+        if snd_started and num_silent > MAX_SILENT:
             break
 
     sample_width = p.get_sample_size(FORMAT)