snowboy.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from trigger.snowboy import snowboydecoder
  2. class MissingParameterException(Exception):
  3. pass
  4. class Snowboy(object):
  5. def __init__(self, **kwargs):
  6. print "loaded"
  7. # pause listening boolean
  8. self.interrupted = False
  9. # callback function to call when hotword caught
  10. self.callback = kwargs.get('callback', None)
  11. if self.callback is None:
  12. raise MissingParameterException("callback function is required with snowboy")
  13. # get the pmdl file to load
  14. self.pmdl = kwargs.get('pmdl_file', None)
  15. if self.pmdl is None:
  16. raise MissingParameterException("Pmdl file is required with snowboy")
  17. def interrupt_callback(self):
  18. """
  19. This function will be passed to snowboy to stop the main thread
  20. :return:
  21. """
  22. return self.interrupted
  23. def start(self):
  24. """
  25. Start the snowboy thread and wait for a Jarvis trigger word
  26. :return:
  27. """
  28. print "started"
  29. detector = snowboydecoder.HotwordDetector(self.pmdl, sensitivity=0.5)
  30. # start snowboy loop
  31. detector.start(detected_callback=self.callback,
  32. interrupt_check=self.interrupt_callback,
  33. sleep_time=0.03)
  34. # we wait that a callback
  35. detector.terminate()
  36. def pause(self):
  37. """
  38. Stop the Snowboy main thread
  39. :return:
  40. """
  41. self.interrupted = True
  42. def unpause(self):
  43. self.interrupted = False