add pynma and postgres
[scannr.git] / scannr.py
blob:a/scannr.py -> blob:b/scannr.py
import snd import snd
import time import time
from datetime import date from datetime import date
import threading import threading
from pydispatch import dispatcher from pydispatch import dispatcher
import wave import wave
  import psycopg2
  import sys,os
   
   
  sys.path.insert(0, os.path.join(os.path.dirname(__file__) or '.', 'pynma'))
  import pynma
   
filename = "demo.wav" filename = "demo.wav"
  nma = pynma.PyNMA( "apikey(s)", "developerkey")
  conn = psycopg2.connect("dbname=test user=postgres")
   
def worker(filename): def worker(filename):
   
"""thread worker function """thread worker function
http://www.doughellmann.com/PyMOTW/threading/ 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 """ http://stackoverflow.com/questions/1092531/event-system-in-python """
print 'Worker for '+filename print 'Worker for '+filename
  """nma.push(application, event, description, (opt) url,)"""
  """http://initd.org/psycopg/docs/usage.html"""
  #cur = conn.cursor()
  #cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",(100, "abc'def"))
  #conn.commit()
  #cur.close()
   
return return
   
def filenameMaker(): def filenameMaker():
global filename global filename
filename = date.today().isoformat()+'-'+str(time.time())+'-demo.wav' filename = date.today().isoformat()+'-'+str(time.time())+'-demo.wav'
   
def record_to_async_file(): def record_to_async_file():
"Records from the microphone and outputs the resulting data to `path`" "Records from the microphone and outputs the resulting data to `path`"
sample_width, data = snd.record() sample_width, data = snd.record()
data = snd.pack('<' + ('h'*len(data)), *data) data = snd.pack('<' + ('h'*len(data)), *data)
path = filename path = filename
dispatcher.send( signal='FILE_CREATED', sender=filename, filename=filename) dispatcher.send( signal='FILE_CREATED', sender=filename, filename=filename)
wf = wave.open(path, 'wb') wf = wave.open(path, 'wb')
wf.setnchannels(1) wf.setnchannels(1)
wf.setsampwidth(sample_width) wf.setsampwidth(sample_width)
wf.setframerate(snd.RATE) wf.setframerate(snd.RATE)
wf.writeframes(data) wf.writeframes(data)
wf.close() wf.close()
print("done - result written to "+path) print("done - result written to "+path)
   
dispatcher.connect( filenameMaker, signal='SND_STARTED', sender=dispatcher.Any ) dispatcher.connect( filenameMaker, signal='SND_STARTED', sender=dispatcher.Any )
dispatcher.connect( worker, signal='FILE_CREATED', sender=dispatcher.Any ) dispatcher.connect( worker, signal='FILE_CREATED', sender=dispatcher.Any )
print "Scannr started..." print "Scannr started..."
while True: while True:
print "ready to record" print "ready to record"
record_to_async_file() record_to_async_file()