snowboy.py 1.5 KB

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