allocate filename when signal starts
[scannr.git] / snd.py
blob:a/snd.py -> blob:b/snd.py
--- a/snd.py
+++ b/snd.py
@@ -12,11 +12,15 @@
 import threading
 from pydispatch import dispatcher
 
-CHANNELS = 1
 THRESHOLD = 500
 CHUNK_SIZE = 1024
 FORMAT = pyaudio.paInt16
 RATE = 44100
+if sys.platform.startswith('darwin'):
+	CHANNELS = 2
+elif sys.platform.startswith('win32'):
+	CHANNELS = 1
+
 MAX_SILENT = 30
 
 def is_silent(L):
@@ -77,8 +81,8 @@
     it without getting chopped off.
     """
     p = pyaudio.PyAudio()
-    stream = p.open(format=FORMAT, channels=1, rate=RATE, 
-                    input=True, output=True,
+    stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, 
+                    input=True,
                     frames_per_buffer=CHUNK_SIZE)
 
     num_silent = 0
@@ -90,7 +94,6 @@
         data = stream.read(CHUNK_SIZE)
         L = unpack('<' + ('h'*(len(data)/2)), data) # little endian, signed short
         L = array('h', L)
-        LRtn.extend(L)
 
         silent = is_silent(L)
         #print silent, num_silent, L[:10]
@@ -102,6 +105,8 @@
             dispatcher.send( signal='SND_STARTED')
             snd_started = True
             print snd_started
+        if snd_started:
+            LRtn.extend(L)
         if snd_started and num_silent > MAX_SILENT:
             break
 
@@ -127,6 +132,7 @@
     wf.writeframes(data)
     wf.close()
     print("done - result written to "+path)
+    del data