Sfoglia il codice sorgente

add jarvis trigger

Nicolas Marcq 8 anni fa
parent
commit
a964e87b65
3 ha cambiato i file con 56 aggiunte e 35 eliminazioni
  1. 43 0
      core/JarvisTrigger.py
  2. 13 0
      jarvis.py
  3. 0 35
      test.py

+ 43 - 0
core/JarvisTrigger.py

@@ -0,0 +1,43 @@
+from stt.snowboy import snowboydecoder
+
+
+class JarvisTrigger:
+    """
+    Class used to catch the trigger word before listening for an order to process
+    """
+    def __init__(self):
+        # TODO update this to load the file from settings
+        self.model = "stt/snowboy/resources/snowboy.umdl"
+        # boolean used to stop the snowbow listening
+        self.interrupted = False
+
+    def interrupt_callback(self):
+        """
+        This function will be passed to snowboy to stop the main thread
+        :return:
+        """
+        return self.interrupted
+
+    def start(self):
+        """
+        Start the snowboy thread and wait for a Jarvis trigger word
+        :return:
+        """
+
+        detector = snowboydecoder.HotwordDetector(self.model, sensitivity=0.5)
+
+        # start snowboy loop
+        # TODO change to callback, call a function that wait for an audio order
+        detector.start(detected_callback=snowboydecoder.play_audio_file,
+                       interrupt_check=self.interrupt_callback,
+                       sleep_time=0.03)
+
+        # we wait that a callback
+        detector.terminate()
+
+    def stop(self):
+        """
+        Stop the Snowboy main thread
+        :return:
+        """
+        self.interrupted = True

+ 13 - 0
jarvis.py

@@ -0,0 +1,13 @@
+from core.JarvisTrigger import JarvisTrigger
+
+
+def main():
+    """
+    Entry point of jarvis program
+    """
+    # Wait that the jarvis trigger is pronounced by the user
+    jarvis_triger = JarvisTrigger()
+    jarvis_triger.start()
+
+if __name__ == '__main__':
+    main()

+ 0 - 35
test.py

@@ -1,35 +0,0 @@
-from stt.snowboy import snowboydecoder
-import sys
-import signal
-
-interrupted = False
-
-
-def signal_handler(signal, frame):
-    global interrupted
-    interrupted = True
-
-
-def interrupt_callback():
-    global interrupted
-    return interrupted
-
-# if len(sys.argv) == 1:
-#     print("Error: need to specify model name")
-#     print("Usage: python demo.py your.model")
-#     sys.exit(-1)
-
-model = "stt/snowboy/resources/snowboy.umdl"
-
-# capture SIGINT signal, e.g., Ctrl+C
-signal.signal(signal.SIGINT, signal_handler)
-
-detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
-print('Listening... Press Ctrl+C to exit')
-
-# main loop
-detector.start(detected_callback=snowboydecoder.play_audio_file,
-               interrupt_check=interrupt_callback,
-               sleep_time=0.03)
-
-detector.terminate()