JarvisTrigger.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from core.OrderListener import OrderListener
  2. from stt.snowboy import snowboydecoder
  3. class JarvisTrigger:
  4. """
  5. Class used to catch the trigger word before listening for an order to process
  6. """
  7. def __init__(self):
  8. # TODO update this to load the file from settings
  9. self.model = "stt/snowboy/resources/jarviss.pmdl"
  10. # boolean used to stop the snowbow listening
  11. self.interrupted = False
  12. self.order_listener = OrderListener()
  13. def interrupt_callback(self):
  14. """
  15. This function will be passed to snowboy to stop the main thread
  16. :return:
  17. """
  18. return self.interrupted
  19. def start(self):
  20. """
  21. Start the snowboy thread and wait for a Jarvis trigger word
  22. :return:
  23. """
  24. detector = snowboydecoder.HotwordDetector(self.model, sensitivity=0.4)
  25. # start snowboy loop
  26. # TODO change to callback, call a function that wait for an audio order
  27. detector.start(detected_callback=self.order_listener.hotword_detected,
  28. interrupt_check=self.interrupt_callback,
  29. sleep_time=0.03)
  30. # we wait that a callback
  31. detector.terminate()
  32. def stop(self):
  33. """
  34. Stop the Snowboy main thread
  35. :return:
  36. """
  37. self.interrupted = True