Browse Source

Merge pull request #274 from kalliope-project/kill_snowboy

Kill trigger process instead of pausing it
Monf 7 years ago
parent
commit
6e7839a192

+ 0 - 78
install/install.yml

@@ -1,78 +0,0 @@
----
-#- hosts: localhost
-#  gather_facts: no
-#  connection: local
-#  vars:
-#    - git_url: "git@github.com:kalliope-project/kalliope.git"
-#    - temp_local_repo: "/tmp/kalliope_repo"
-#
-#  tasks:
-#    - name: clone project in /tmp as standart user
-#      git:
-#        repo: "{{ git_url }}"
-#        dest: /tmp/kalliope_repo
-#        accept_hostkey: yes
-
-
-- hosts: localhost
-  gather_facts: no
-  connection: local
-  become: True
-  vars:
-    - git_url: "git@github.com:kalliope-project/kalliope.git"
-    - temp_local_repo: "/tmp/kalliope_repo"
-    - install_path: "/usr/lib/kalliope"
-  tasks:
-    - name: Install packages
-      apt: name={{ item }} update_cache=yes
-      with_items:
-        - python-pip
-        - python-dev
-        - libsmpeg0
-        - libttspico-utils
-        - libsmpeg0
-        - flac
-        - dialog
-        - libffi-dev
-        - libssl-dev
-        - portaudio19-dev
-        - build-essential
-        - sox
-        - libatlas3-base
-        - mplayer
-
-    - name: Copy requirement
-      copy:
-        src: python_requirements.txt
-        dest: /tmp/requirements.txt
-
-    - name: Upgrade pip
-      pip:
-        name: pip
-        state: latest
-
-    - name: Install python libs
-      pip:
-        requirements=/tmp/requirements.txt
-
-#    - name: Check that the repo has been cloned
-#      stat:
-#        path: "{{ temp_local_repo }}"
-#      register: git_repo_stat
-#
-#    - name: Remove old version if exist
-#      file:
-#        path: "{{ install_path }}"
-#        state: absent
-#
-#    - name: Move repo in /usr/lib
-#      command: mv "{{ temp_local_repo }}" "{{ install_path }}"
-#      when: git_repo_stat.stat.exists
-#
-#    - name: Add entry in /usr/bin
-#      file:
-#        src: "{{ install_path }}/kalliope.py"
-#        dest: /usr/bin/kalliope
-#        state: link
-#        force: True
-

+ 0 - 15
install/install_kalliope.sh

@@ -1,15 +0,0 @@
-#!/usr/bin/env bash
-
-# install packages
-sudo apt-get install -y python-pip libssl-dev
-
-# this is used to help the RPI
-sudo apt-get install -y libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
-sudo apt-get install -y libffi-dev python-yaml python-pycparser python-paramiko python-markupsafe apt-transport-https
-
-# install ansible
-sudo pip install ansible==2.1.1.0
-
-# Install the project
-cd kalliope/install
-ansible-playbook install.yml -K

+ 47 - 0
install/rpi_install_kalliope.sh

@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+
+# This script will install automatically everything needed for Kalliope
+# usage: ./rpi_install_kalliope.sh [<branch_name>]
+# E.g: ./rpi_install_kalliope.sh dev
+# If no branch are set, the master branch will be installed
+
+# name of the branch to install
+branch="master"
+
+# get the branch name to install from passed arguments if exist
+if [ $# -eq 0 ]
+  then
+    echo "No arguments supplied. Master branch will be installed"
+else
+    branch=$1
+    echo "Selected branch name to install: ${branch}"
+fi
+
+echo "Installing python pip..."
+wget https://bootstrap.pypa.io/get-pip.py
+sudo python get-pip.py
+echo "Installing python pip... [OK]"
+
+# install packages
+echo "Installing system packages..."
+sudo apt-get update
+sudo apt-get install -y git python-dev libsmpeg0 libttspico-utils libsmpeg0 \
+flac dialog libffi-dev libffi-dev libssl-dev portaudio19-dev build-essential \
+libssl-dev libffi-dev sox libatlas3-base mplayer libyaml-dev libpython2.7-dev pulseaudio pulseaudio-utils
+
+# this is used to help the RPI
+sudo apt-get install -y libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
+sudo apt-get install -y libffi-dev python-yaml python-pycparser python-paramiko python-markupsafe apt-transport-https
+echo "Installing system packages...[OK]"
+
+echo "Cloning the project"
+# clone the project
+git clone https://github.com/kalliope-project/kalliope.git
+echo "Cloning the project...[OK]"
+
+# Install the project
+echo "Installing Kalliope..."
+cd kalliope
+git checkout ${branch}
+sudo python setup.py install
+echo "Installing Kalliope...[OK]"

+ 18 - 25
kalliope/core/MainController.py

@@ -3,19 +3,14 @@ import random
 from time import sleep
 
 from flask import Flask
-
-from kalliope.core.NeuronLauncher import NeuronLauncher
-from kalliope.core.NeuronParameterLoader import NeuronParameterLoader
-from kalliope.core.OrderAnalyser import OrderAnalyser
-from kalliope.core.SynapseLauncher import SynapseLauncher
 from transitions import Machine
 
 from kalliope.core import Utils
 from kalliope.core.ConfigurationManager import SettingLoader
-from kalliope.core.OrderAnalyser import OrderAnalyser
 from kalliope.core.OrderListener import OrderListener
 from kalliope.core.Players import Mplayer
 from kalliope.core.RestAPI.FlaskAPI import FlaskAPI
+from kalliope.core.SynapseLauncher import SynapseLauncher
 from kalliope.core.TriggerLauncher import TriggerLauncher
 from kalliope.neurons.say.say import Say
 
@@ -29,9 +24,9 @@ class MainController:
     """
     states = ['init',
               'starting_trigger',
-              'unpausing_trigger',
               'playing_ready_sound',
               'waiting_for_trigger_callback',
+              'stopping_trigger',
               'start_order_listener',
               'playing_wake_up_answer',
               'waiting_for_order_listener_callback',
@@ -63,10 +58,10 @@ class MainController:
         self.machine = Machine(model=self, states=MainController.states, initial='init', queued=True)
 
         # define transitions
-        self.machine.add_transition('start_trigger', 'init', 'starting_trigger')
-        self.machine.add_transition('unpause_trigger', ['starting_trigger', 'analysing_order'], 'unpausing_trigger')
-        self.machine.add_transition('play_ready_sound', 'unpausing_trigger', 'playing_ready_sound')
+        self.machine.add_transition('start_trigger', ['init', 'analysing_order'], 'starting_trigger')
+        self.machine.add_transition('play_ready_sound', 'starting_trigger', 'playing_ready_sound')
         self.machine.add_transition('wait_trigger_callback', 'playing_ready_sound', 'waiting_for_trigger_callback')
+        self.machine.add_transition('stop_trigger', 'waiting_for_trigger_callback', 'stopping_trigger')
         self.machine.add_transition('play_wake_up_answer', 'waiting_for_trigger_callback', 'playing_wake_up_answer')
         self.machine.add_transition('wait_for_order', 'playing_wake_up_answer', 'waiting_for_order_listener_callback')
         self.machine.add_transition('analyse_order', 'waiting_for_order_listener_callback', 'analysing_order')
@@ -78,10 +73,10 @@ class MainController:
         self.machine.on_enter_playing_ready_sound('play_ready_sound_process')
         self.machine.on_enter_waiting_for_trigger_callback('waiting_for_trigger_callback_thread')
         self.machine.on_enter_playing_wake_up_answer('play_wake_up_answer_thread')
+        self.machine.on_enter_stopping_trigger('stop_trigger_process')
         self.machine.on_enter_start_order_listener('start_order_listener_thread')
         self.machine.on_enter_waiting_for_order_listener_callback('waiting_for_order_listener_callback_thread')
         self.machine.on_enter_analysing_order('analysing_order_thread')
-        self.machine.on_enter_unpausing_trigger('unpausing_trigger_process')
 
         self.start_trigger()
 
@@ -91,21 +86,12 @@ class MainController:
         """
         logger.debug("Entering state: %s" % self.state)
         self.trigger_instance = self._get_default_trigger()
+        self.trigger_callback_called = False
         self.trigger_instance.daemon = True
         # Wait that the kalliope trigger is pronounced by the user
         self.trigger_instance.start()
         self.next_state()
 
-    def unpausing_trigger_process(self):
-        """
-        If the trigger was in pause, this method will unpause it to listen again for the hotword
-        """
-        logger.debug("Entering state: %s" % self.state)
-        self.trigger_instance.unpause()
-        self.trigger_callback_called = False
-        Utils.print_info("Waiting for trigger detection")
-        self.next_state()
-
     def play_ready_sound_process(self):
         """
         Play a sound when Kalliope is ready to be awaken at the first start
@@ -128,6 +114,7 @@ class MainController:
         Method to print in debug that the main process is waiting for a trigger detection
         """
         logger.debug("Entering state: %s" % self.state)
+        Utils.print_info("Waiting for trigger detection")
         # this loop is used to keep the main thread alive
         while not self.trigger_callback_called:
             sleep(0.1)
@@ -149,16 +136,22 @@ class MainController:
         The user can speak out loud his order during this time.
         """
         logger.debug("Trigger callback called, switching to the next state")
-        # self.next_state()
         self.trigger_callback_called = True
 
+    def stop_trigger_process(self):
+        """
+        The trigger has been awaken, we don't needed it anymore
+        :return: 
+        """
+        logger.debug("Entering state: %s" % self.state)
+        self.trigger_instance.stop()
+        self.next_state()
+
     def start_order_listener_thread(self):
         """
         Start the STT engine thread
         """
         logger.debug("Entering state: %s" % self.state)
-        # pause the trigger process
-        self.trigger_instance.pause()
         # start listening for an order
         self.order_listener_callback_called = False
         self.order_listener = OrderListener(callback=self.order_listener_callback)
@@ -202,7 +195,7 @@ class MainController:
                                                         is_api_call=False)
 
         # return to the state "unpausing_trigger"
-        self.unpause_trigger()
+        self.start_trigger()
 
     def _get_default_trigger(self):
         """

+ 2 - 0
kalliope/core/OrderListener.py

@@ -48,12 +48,14 @@ class OrderListener(Thread):
         """
         Start thread
         """
+        logger.debug("[OrderListener] running ...")
         self.stt_instance = self.load_stt_plugin()
 
     def load_stt_plugin(self):
         if self.stt is None:
             self.stt_module_name = self.settings.default_stt_name
 
+        logger.debug("[OrderListener] stt module name : %s" % self.stt_module_name)
         for stt_object in self.settings.stts:
             if stt_object.name == self.stt_module_name:
                 stt_object.parameters["callback"] = self.callback

+ 9 - 0
kalliope/trigger/snowboy/snowboy.py

@@ -78,6 +78,15 @@ class Snowboy(Thread):
         logger.debug("Unpausing snowboy process")
         self.detector.paused = False
 
+    def stop(self):
+        """
+        Kill the snowboy process
+        :return: 
+        """
+        logger.debug("Killing snowboy process")
+        self.interrupted = True
+        self.detector.terminate()
+
     @staticmethod
     def _ignore_stderr():
         """

+ 2 - 1
kalliope/trigger/snowboy/snowboydecoder.py

@@ -152,7 +152,7 @@ class HotwordDetector(Thread):
                     if callback is not None:
                         callback()
 
-        logger.debug("finished.")
+        logger.debug("[Snowboy] process finished.")
 
     def terminate(self):
         """
@@ -162,3 +162,4 @@ class HotwordDetector(Thread):
         self.stream_in.stop_stream()
         self.stream_in.close()
         self.audio.terminate()
+        logger.debug("[Snowboy] Audio stream cleaned.")