add some pyaudio test files
[scannr.git] / test2.py
blob:a/test2.py -> blob:b/test2.py
--- a/test2.py
+++ b/test2.py
@@ -14,24 +14,27 @@
     "Returns `True` if below the 'silent' 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)
 
@@ -48,12 +51,14 @@
     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():
     """
@@ -66,9 +71,9 @@
     it without getting chopped off.
     """
     p = pyaudio.PyAudio()
-    stream = p.open(format=FORMAT, channels=2, rate=RATE, 
-                    input=True, 
-                    frames_per_buffer=CHUNK_SIZE)
+    stream = p.open(format=FORMAT, channels=2, rate=RATE,
+        input=True,
+        frames_per_buffer=CHUNK_SIZE)
 
     num_silent = 0
     snd_started = False
@@ -77,7 +82,7 @@
 
     while 1:
         data = stream.read(CHUNK_SIZE)
-        L = unpack('<' + ('h'*(len(data)/2)), data) # little endian, signed short
+        L = unpack('<' + ('h' * (len(data) / 2)), data) # little endian, signed short
         L = array('h', L)
         LRtn.extend(L)
 
@@ -102,10 +107,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(2)