diff options
Diffstat (limited to 'Recognizer.py')
-rwxr-xr-x | Recognizer.py | 102 |
1 files changed, 55 insertions, 47 deletions
diff --git a/Recognizer.py b/Recognizer.py index e9cb648..e962ea3 100755 --- a/Recognizer.py +++ b/Recognizer.py @@ -1,57 +1,65 @@ -#This is part of Blather +# This is part of Blather # -- this code is licensed GPLv3 # Copyright 2013 Jezra -import pygst -pygst.require('0.10') -import gst +import gi +gi.require_version('Gst', '1.0') +from gi.repository import GObject, Gst +GObject.threads_init() +Gst.init(None) import os.path -import gobject import sys -#define some global variables +# Define some global variables this_dir = os.path.dirname( os.path.abspath(__file__) ) -class Recognizer(gobject.GObject): - __gsignals__ = { - 'finished' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)) - } - def __init__(self, language_file, dictionary_file, src = None): - gobject.GObject.__init__(self) - self.commands = {} - if src: - audio_src = 'alsasrc device="hw:%d,0"' % (src) - else: - audio_src = 'autoaudiosrc' - - #build the pipeline - cmd = audio_src+' ! audioconvert ! audioresample ! vader name=vad ! pocketsphinx name=asr ! appsink sync=false' - try: - self.pipeline=gst.parse_launch( cmd ) - except Exception, e: - print e.message - print "You may need to install gstreamer0.10-pocketsphinx" - raise e - - #get the Auto Speech Recognition piece - asr=self.pipeline.get_by_name('asr') - asr.connect('result', self.result) - asr.set_property('lm', language_file) - asr.set_property('dict', dictionary_file) - asr.set_property('configured', True) - #get the Voice Activity DEtectoR - self.vad = self.pipeline.get_by_name('vad') - self.vad.set_property('auto-threshold',True) - - def listen(self): - self.pipeline.set_state(gst.STATE_PLAYING) - - def pause(self): - self.vad.set_property('silent', True) - self.pipeline.set_state(gst.STATE_PAUSED) - - def result(self, asr, text, uttid): - #emit finished - self.emit("finished", text) +class Recognizer(GObject.GObject): + __gsignals__ = { + 'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)) + } + def __init__(self, language_file, dictionary_file, src = None): + GObject.GObject.__init__(self) + self.commands = {} + if src: + audio_src = 'alsasrc device="hw:%d,0"' % (src) + else: + audio_src = 'autoaudiosrc' + + # Build the pipeline + cmd = audio_src+' ! audioconvert ! audioresample ! pocketsphinx name=asr ! appsink sync=false' + try: + self.pipeline=Gst.parse_launch( cmd ) + except Exception, e: + print e.message + print "You may need to install gstreamer1.0-pocketsphinx" + raise e + + bus = self.pipeline.get_bus() + bus.add_signal_watch() + + # Get the Auto Speech Recognition piece + asr=self.pipeline.get_by_name('asr') + bus.connect('message::element', self.result) + asr.set_property('lm', language_file) + asr.set_property('dict', dictionary_file) + asr.set_property('configured', True) + + def listen(self): + self.pipeline.set_state(Gst.State.PLAYING) + + def pause(self): + self.pipeline.set_state(Gst.State.PAUSED) + + def result(self, bus, msg): + msg_struct = msg.get_structure() + # Ignore messages that aren't from pocketsphinx + msgtype = msg_struct.get_name() + if msgtype != 'pocketsphinx': + return + + # If we have a final command, send it for processing + command = msg_struct.get_string('hypothesis') + if command != '' and msg_struct.get_boolean('final')[1]: + self.emit("finished", command) |