12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- # This is part of Kaylee
- # -- this code is licensed GPLv3
- # Copyright 2015-2017 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_BOOLEAN,
- (GObject.TYPE_STRING,))
- }
-
- def __init__(self, config):
- GObject.GObject.__init__(self)
-
- src = config.options.microphone
- if src:
- audio_src = f'alsasrc device="hw:{src},0"'
- else:
- audio_src = 'autoaudiosrc'
-
- # Build the pipeline
- cmd = (
- f'{audio_src}'
- f' ! audioconvert'
- f' ! audioresample'
- f' ! pocketsphinx lm={config.lang_file} dict={config.dic_file}'
- f' ! 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.lower())
|