Somewhat fancy voice command recognition software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # This is part of Kaylee
  2. # -- this code is licensed GPLv3
  3. # Copyright 2015-2016 Clayton G. Hobbs
  4. # Portions Copyright 2013 Jezra
  5. import os.path
  6. import sys
  7. import gi
  8. gi.require_version('Gst', '1.0')
  9. from gi.repository import GObject, Gst
  10. GObject.threads_init()
  11. Gst.init(None)
  12. class Recognizer(GObject.GObject):
  13. __gsignals__ = {
  14. 'finished' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
  15. (GObject.TYPE_STRING,))
  16. }
  17. def __init__(self, config):
  18. GObject.GObject.__init__(self)
  19. self.commands = {}
  20. src = config.options.microphone
  21. if src:
  22. audio_src = 'alsasrc device="hw:{0},0"'.format(src)
  23. else:
  24. audio_src = 'autoaudiosrc'
  25. # Build the pipeline
  26. cmd = (
  27. audio_src +
  28. ' ! audioconvert' +
  29. ' ! audioresample' +
  30. ' ! pocketsphinx lm=' + config.lang_file + ' dict=' +
  31. config.dic_file +
  32. ' ! appsink sync=false'
  33. )
  34. try:
  35. self.pipeline = Gst.parse_launch(cmd)
  36. except Exception as e:
  37. print(e.message)
  38. print("You may need to install gstreamer1.0-pocketsphinx")
  39. raise e
  40. # Process results from the pipeline with self.result()
  41. bus = self.pipeline.get_bus()
  42. bus.add_signal_watch()
  43. bus.connect('message::element', self.result)
  44. def listen(self):
  45. self.pipeline.set_state(Gst.State.PLAYING)
  46. def pause(self):
  47. self.pipeline.set_state(Gst.State.PAUSED)
  48. def result(self, bus, msg):
  49. msg_struct = msg.get_structure()
  50. # Ignore messages that aren't from pocketsphinx
  51. msgtype = msg_struct.get_name()
  52. if msgtype != 'pocketsphinx':
  53. return
  54. # If we have a final command, send it for processing
  55. command = msg_struct.get_string('hypothesis')
  56. if command != '' and msg_struct.get_boolean('final')[1]:
  57. self.emit("finished", command)