Skip to content

Instantly share code, notes, and snippets.

@bossjones
Created March 11, 2014 14:35
Show Gist options
  • Save bossjones/9487017 to your computer and use it in GitHub Desktop.
Save bossjones/9487017 to your computer and use it in GitHub Desktop.
snippet
class Listener:
def __init__(self, gobject, gst):
self.failed = 0
self.keyword_identified = 0
self.recording = tempfile.mktemp(suffix='pi.wav')
self.lat = scarlett_config.gimmie('FORECAST_LAT')
self.lng = scarlett_config.gimmie('FORECAST_LNG')
self.api_key = scarlett_config.gimmie('FORECAST_API_KEY')
self.pipeline = gst.parse_launch(' ! '.join(['alsasrc device=' + scarlett_config.gimmie('audio_input_device'),
'queue silent=false leaky=2 max-size-buffers=0 max-size-time=0 max-size-bytes=0',
'audioconvert',
'audioresample',
'audio/x-raw-int, rate=16000, width=16, depth=16, channels=1',
'audioresample',
'audio/x-raw-int, rate=8000',
'vader name=vader auto-threshold=true',
'pocketsphinx lm=' + scarlett_config.gimmie('LM') + ' dict=' + scarlett_config.gimmie('DICT') + ' hmm=' + scarlett_config.gimmie('HMM') + ' name=listener',
'fakesink dump=1 t.']))
vader = self.pipeline.get_by_name('vader')
valve = self.pipeline.get_by_name('valve')
listener = self.pipeline.get_by_name('listener')
listener.connect('result', self.__result__)
listener.set_property('configured', True)
print "KEYWORDS WE'RE LOOKING FOR: " + scarlett_config.gimmie('ourkeywords')
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::application', self.__application_message__)
self.pipeline.set_state(gst.STATE_PLAYING)
# Scarlett's greetings
self.greetings_play()
def partial_result(self, asr, text, uttid):
"""Forward partial result signals on the bus to the main thread."""
def result(self, hyp, uttid):
"""Forward result signals on the bus to the main thread."""
if hyp in scarlett_config.gimmie('ourkeywords'):
print "HYP-IS-SOMETHING: " + hyp + "\n\n\n"
print "UTTID-IS-SOMETHING:" + uttid + "\n"
self.failed = 0
self.keyword_identified = 1
pi.play('pi-listening')
else:
self.failed += 1
if self.failed > 4:
pi.speak("" + scarlett_config.gimmie('scarlett_owner') + ", if you need me, just say my name.")
self.failed = 0
def run_cmd(self, hyp, uttid):
print "KEYWORD IDENTIFIED BABY"
self.check_cmd(hyp)
def listen(self,valve,vader):
self.pipeline.set_state(gst.STATE_PAUSED)
pi.play('pi-listening')
valve.set_property('drop',False)
Recorder(self,vader)
valve.set_property('drop',True)
def cancel_listening(self,valve):
pi.play('pi-cancel')
valve.set_property('drop',False)
self.pipeline.set_state(gst.STATE_PLAYING)
# question - sound recording
def answer(self, question):
pi.play('pi-cancel')
self.time_commands = {
"WHAT TIME IS IT": "echo \"Its `date +\%l\%M\%P` oclock\" | festival --tts",
"TIME IS IT": "echo \"Its `date +\%l\%M\%P` oclock\" | festival --tts",
"TIME IT IS": "echo \"Its `date +\%l\%M\%P` oclock\" | festival --tts",
}
self.general_commands = {
"CANCEL": "cancel",
}
if self.command in self.time_commands.keys():
print "** received %s, sending 'time %s'" % (self.command, self.time_commands[self.command])
try:
self.time_play(self.time_commands[command])
except AttributeError:
print "time expetion b"
elif self.command in self.general_commands.keys():
print "** received %s, sending 'general command: %s'" % (self.command, self.general_commands[self.command])
self.general_play(self.general_commands[command])
def time_play(self,cmd):
subprocess.call([cmd],shell=True)
self.keyword_identified = 0
pi.play('pi-response')
def general_play(self,cmd):
self.keyword_identified = 0
pi.play('pi-cancel')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment