diff options
author | Irene Knapp <ireneista@irenes.space> | 2025-09-06 15:42:27 -0700 |
---|---|---|
committer | Irene Knapp <ireneista@irenes.space> | 2025-09-06 15:42:27 -0700 |
commit | cf6e693773101b5ac6aea7a4186e7e15ce4508d5 (patch) | |
tree | 139914c41bc17e7e5df132f5b3f2672b68394afd /src/kayleevc/recognizer.py | |
parent | baeb0e1fa08fb6b21e4a22458de16b4948d1f2d0 (diff) |
move all the Kaylee code into a subdirectory
this is preparation for merging it into the Pollyana repo Force-Push: yes Change-Id: I155624ba39a0d212c999f38aaf2de2cd62b7aa49
Diffstat (limited to 'src/kayleevc/recognizer.py')
-rw-r--r-- | src/kayleevc/recognizer.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/kayleevc/recognizer.py b/src/kayleevc/recognizer.py new file mode 100644 index 0000000..09e14e4 --- /dev/null +++ b/src/kayleevc/recognizer.py @@ -0,0 +1,69 @@ +# This is part of Kaylee +# -- this code is licensed GPLv3 +# Copyright 2015-2016 Clayton G. Hobbs +# Portions Copyright 2013 Jezra + +import os.path +import sys + +import gi +gi.require_version('Gst', '1.0') +from gi.repository import GObject, Gst +GObject.threads_init() +Gst.init(None) + + +class Recognizer(GObject.GObject): + __gsignals__ = { + 'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, + (GObject.TYPE_STRING,)) + } + + def __init__(self, config): + GObject.GObject.__init__(self) + self.commands = {} + + src = config.options.microphone + if src: + audio_src = 'alsasrc device="hw:{0},0"'.format(src) + else: + audio_src = 'autoaudiosrc' + + # Build the pipeline + cmd = ( + audio_src + + ' ! audioconvert' + + ' ! audioresample' + + ' ! pocketsphinx lm=' + config.lang_file + ' dict=' + + config.dic_file + + ' ! appsink sync=false' + ) + try: + self.pipeline = Gst.parse_launch(cmd) + except Exception as e: + print(e.message) + print("You may need to install gstreamer1.0-pocketsphinx") + raise e + + # Process results from the pipeline with self.result() + bus = self.pipeline.get_bus() + bus.add_signal_watch() + bus.connect('message::element', self.result) + + 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) |