فهرست منبع

refactor on ready notification

nico 8 سال پیش
والد
کامیت
10f5a042aa

+ 21 - 25
Docs/settings.md

@@ -177,41 +177,37 @@ random_wake_up_sounds:
 E.g: `# random_wake_up_answers:`
 
 
-## On ready answers configuration
+## On ready notification
+This section is used to notify the user when Kalliope is waiting for a trigger detection by playing a sound or speak a sentence out loud
 
-#### random_on_ready_answers (optional)
-When Kalliope is ready, it lets you know that it's operational and now waiting for order. It's done by answering randomly
-one of the sentences provided in the variable random_on_ready_answers.
+#### play_on_ready_notification
+This parameter define if you play the on ready notification:
+ - `always`: every time Kalliope is ready to be awaken
+ - `never`: never play a sound or sentences when kalliope is ready
+ - `once`: at the first start of Kalliope
 
-This variable must contain a list of strings as shown bellow
+E.g:
 ```yml
-random_on_ready_answers:
-  - "Your sentence"
-  - "Another sentence"
+play_on_ready_notification: always
 ```
+#### on_ready_answers
+The on ready notification can be a sentence. Place here a sentence or a list of sentence. If you set a list, one sentence will be picked up randomly
 
-E.g
+E.g:
 ```yml
-random_on_ready_answers:
-  - "I am ready"
-  - "Let's roll"
+on_ready_answers:
+  - "I'm ready"
+  - "Waiting for order"
 ```
 
-#### random_on_ready_sounds (optional)
-You can play a sound when Kalliope is ready instead of saying something from the `random_on_ready_answers`.
-Place here a list of full paths of the sound files you want to use. Otherwise, you can use some default sounds provided by Kalliope which you can find in `/usr/lib/kalliope/sounds`.
-By default two file are provided: ding.wav and dong.wav. In all cases, the file must be in `.wav` or `.mp3` format. If more than on file is present in the list,
-Kalliope will select one randomly at each wake up.
+#### on_ready_sounds
+You can play a sound instead of a sentence.
+Remove the `on_ready_answers` parameters by commenting it out and use this one instead.
+Place here the path of the sound file. Files must be .wav or .mp3 format.
 
+E.g:
 ```yml
-random_on_ready_sounds:
-  - "local_file_in_sounds_folder.wav"
-  - "/my/personal/full/path/my_file.mp3"
-```
-
-E.g
-```yml
-random_on_ready_sounds:
+on_ready_sounds:
   - "ding.wav"
   - "dong.wav"
   - "/my/personal/full/path/my_file.mp3"

+ 15 - 8
Tests/settings/settings_test.yml

@@ -63,17 +63,24 @@ random_wake_up_sounds:
   - "sounds/dong.wav"
 
 # ---------------------------
-# On ready answers
+# On ready notification
 # ---------------------------
-# You can play a sound when Kalliope is started and is waiting for the trigger.
-random_on_ready_answers:
+# This section is used to notify the user when Kalliope is waiting for a trigger detection by playing a sound or speak a sentence out loud
+
+# This parameter define if you play the on ready answer:
+# - always: every time Kalliope is ready to be awaken
+# - never: never play a sound or sentences when kalliope is ready
+# - once: at the first start of Kalliope
+play_on_ready_notification: never
+
+# The on ready notification can be a sentence. Place here a sentence or a list of sentence. If you set a list, one sentence will be picked up randomly
+on_ready_answers:
   - "Kalliope is ready"
 
-# If you prefer, you can play a sound instead of a sentence.
-# Remove the `random_on_ready_answers` parameters to use this one.
-# Place here the full path of the sound file or just the name of the file in /usr/lib/kalliope/sounds
-# The file must be .wav or .mp3 format. By default two file are provided: ding.wav and dong.wav
-random_on_ready_sounds:
+# You can play a sound instead of a sentence.
+# Remove the `on_ready_answers` parameters by commenting it out and use this one instead.
+# Place here the path of the sound file. Files must be .wav or .mp3 format.
+on_ready_sounds:
   - "sounds/ding.wav"
   - "sounds/dong.wav"
 

+ 26 - 20
Tests/test_settings_loader.py

@@ -25,36 +25,36 @@ class TestSettingLoader(unittest.TestCase):
         self.settings_file_to_test = root_dir + os.sep + "Tests/settings/settings_test.yml"
 
         self.settings_dict = {
+            'default_synapse': 'Default-synapse',
             'rest_api':
-                {'active': True,
+                {'allowed_cors_origin': False,
+                 'active': True,
                  'login': 'admin',
-                 'password': 'secret',
                  'password_protected': True,
-                 'port': 5000,
-                 'allowed_cors_origin': False},
+                 'password': 'secret', 'port': 5000},
             'default_trigger': 'snowboy',
+            'play_on_ready_notification': 'never',
             'triggers': [{'snowboy': {'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'}}],
             'speech_to_text': [{'google': {'language': 'fr-FR'}}],
+            'on_ready_answers': ['Kalliope is ready'],
             'cache_path': '/tmp/kalliope_tts_cache',
             'random_wake_up_answers': ['Oui monsieur?'],
+            'on_ready_sounds': ['sounds/ding.wav', 'sounds/dong.wav'],
+            'resource_directory': {
+                'stt': '/tmp/kalliope/tests/kalliope_resources_dir/stt',
+                'tts': '/tmp/kalliope/tests/kalliope_resources_dir/tts',
+                'neuron': '/tmp/kalliope/tests/kalliope_resources_dir/neurons',
+                'trigger': '/tmp/kalliope/tests/kalliope_resources_dir/trigger'},
             'default_text_to_speech': 'pico2wave',
             'default_speech_to_text': 'google',
             'random_wake_up_sounds': ['sounds/ding.wav', 'sounds/dong.wav'],
-            'random_on_ready_answers': ['Kalliope is ready'],
-            'random_on_ready_sounds': ['sounds/ding.wav', 'sounds/dong.wav'],
             'text_to_speech': [
                 {'pico2wave': {'cache': True, 'language': 'fr-FR'}},
                 {'voxygen': {'voice': 'Agnes', 'cache': True}}
-            ],
-            'default_synapse': 'Default-synapse',
-            'resource_directory':{
-                'neuron': "/tmp/kalliope/tests/kalliope_resources_dir/neurons",
-                'stt': "/tmp/kalliope/tests/kalliope_resources_dir/stt",
-                'tts': "/tmp/kalliope/tests/kalliope_resources_dir/tts",
-                'trigger': "/tmp/kalliope/tests/kalliope_resources_dir/trigger"
-            }
+            ]
         }
 
+
         # Init the folders, otherwise it raises an exceptions
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/neurons")
         os.makedirs("/tmp/kalliope/tests/kalliope_resources_dir/stt")
@@ -90,8 +90,9 @@ class TestSettingLoader(unittest.TestCase):
         settings_object.stts = [stt]
         settings_object.random_wake_up_answers = ['Oui monsieur?']
         settings_object.random_wake_up_sounds = ['sounds/ding.wav', 'sounds/dong.wav']
-        settings_object.random_on_ready_answers = ['Kalliope is ready']
-        settings_object.random_on_ready_sounds = ['sounds/ding.wav', 'sounds/dong.wav']
+        settings_object.play_on_ready_notification = "never"
+        settings_object.on_ready_answers = ['Kalliope is ready']
+        settings_object.on_ready_sounds = ['sounds/ding.wav', 'sounds/dong.wav']
         trigger1 = Trigger(name="snowboy",
                            parameters={'pmdl_file': 'trigger/snowboy/resources/kalliope-FR-6samples.pmdl'})
         settings_object.triggers = [trigger1]
@@ -105,7 +106,7 @@ class TestSettingLoader(unittest.TestCase):
                               tts_folder="/tmp/kalliope/tests/kalliope_resources_dir/tts",
                               trigger_folder="/tmp/kalliope/tests/kalliope_resources_dir/trigger")
 
-        settings_object.resources=resources
+        settings_object.resources = resources
         settings_object.machine = platform.machine()
 
         sl = SettingLoader(file_path=self.settings_file_to_test)
@@ -150,10 +151,15 @@ class TestSettingLoader(unittest.TestCase):
         sl = SettingLoader(file_path=self.settings_file_to_test)
         self.assertEqual(expected_random_wake_up_answers, sl._get_random_wake_up_answers(self.settings_dict))
 
-    def test_get_random_on_ready_answers(self):
-        expected_random_on_ready_answers = ['Kalliope is ready']
+    def test_get_on_ready_answers(self):
+        expected_on_ready_answers = ['Kalliope is ready']
+        sl = SettingLoader(file_path=self.settings_file_to_test)
+        self.assertEqual(expected_on_ready_answers, sl._get_on_ready_answers(self.settings_dict))
+
+    def test_get_on_ready_sounds(self):
+        expected_on_ready_sounds = ['sounds/ding.wav', 'sounds/dong.wav']
         sl = SettingLoader(file_path=self.settings_file_to_test)
-        self.assertEqual(expected_random_on_ready_answers, sl._get_random_on_ready_answers(self.settings_dict))
+        self.assertEqual(expected_on_ready_sounds, sl._get_on_ready_sounds(self.settings_dict))
 
     def test_get_rest_api(self):
         expected_rest_api = RestAPI(password_protected=True, active=True,

+ 61 - 62
kalliope/core/ConfigurationManager/SettingLoader.py

@@ -104,9 +104,10 @@ class SettingLoader(object):
         ttss = self._get_ttss(settings)
         triggers = self._get_triggers(settings)
         random_wake_up_answers = self._get_random_wake_up_answers(settings)
-        random_wake_up_sounds = self._get_random_wake_up_sounds(settings)
-        random_on_ready_answers = self._get_random_on_ready_answers(settings)
-        random_on_ready_sounds = self._get_random_on_ready_sounds(settings)
+        random_wake_up_sound = self._get_random_wake_up_sounds(settings)
+        play_on_ready_notification = self._get_play_on_ready_notification(settings)
+        on_ready_answers = self._get_on_ready_answers(settings)
+        on_ready_sounds = self._get_on_ready_sounds(settings)
         rest_api = self._get_rest_api(settings)
         cache_path = self._get_cache_path(settings)
         default_synapse = self._get_default_synapse(settings)
@@ -120,9 +121,10 @@ class SettingLoader(object):
         setting_object.ttss = ttss
         setting_object.triggers = triggers
         setting_object.random_wake_up_answers = random_wake_up_answers
-        setting_object.random_wake_up_sounds = random_wake_up_sounds
-        setting_object.random_on_ready_answers = random_on_ready_answers
-        setting_object.random_on_ready_sounds = random_on_ready_sounds
+        setting_object.random_wake_up_sounds = random_wake_up_sound
+        setting_object.play_on_ready_notification = play_on_ready_notification
+        setting_object.on_ready_answers = on_ready_answers
+        setting_object.on_ready_sounds = on_ready_sounds
         setting_object.rest_api = rest_api
         setting_object.cache_path = cache_path
         setting_object.default_synapse = default_synapse
@@ -397,62 +399,6 @@ class SettingLoader(object):
 
         return random_wake_up_sounds_list
 
-    @staticmethod
-    def _get_random_on_ready_answers(settings):
-        """
-        Return a list of the wake up answers set up on the settings.yml file
-
-        :param settings: The YAML settings file
-        :type settings: dict
-        :return: List of on ready answers
-        :rtype: list of str
-
-        :Example:
-
-            wakeup = cls._get_random_on_ready_answers(settings)
-
-        .. seealso::
-        .. warnings:: Class Method and Private
-        """
-
-        try:
-            random_on_ready_answers_list = settings["random_on_ready_answers"]
-        except KeyError:
-            # User does not provide this settings
-            return None
-
-        return random_on_ready_answers_list
-
-    @staticmethod
-    def _get_random_on_ready_sounds(settings):
-        """
-        Return a list of the on ready sounds set up on the settings.yml file
-
-        :param settings: The YAML settings file
-        :type settings: dict
-        :return: list of wake up sounds
-        :rtype: list of str
-
-        :Example:
-
-            wakeup_sounds = cls._get_random_on_ready_sounds(settings)
-
-        .. seealso::
-        .. warnings:: Class Method and Private
-        """
-
-        try:
-            random_on_ready_sounds_list = settings["random_on_ready_sounds"]
-            # In case files are declared in settings.yml, make sure kalliope can access them.
-            for sound in random_on_ready_sounds_list:
-                if Utils.get_real_file_path(sound) is None:
-                    raise SettingInvalidException("sound file %s not found" % sound)
-        except KeyError:
-            # User does not provide this settings
-            return None
-
-        return random_on_ready_sounds_list
-
     @staticmethod
     def _get_rest_api(settings):
         """
@@ -643,4 +589,57 @@ class SettingLoader(object):
 
         return resource_object
 
+    @staticmethod
+    def _get_play_on_ready_notification(settings):
+        """
+        Return the on_ready_notification setting. If the user didn't provided it the default is never
+        :param settings: The YAML settings file
+        :type settings: dict
+        :return:
+        """
+        try:
+            play_on_ready_notification = settings["play_on_ready_notification"]
+        except KeyError:
+            # User does not provide this settings, by default we set it to never
+            play_on_ready_notification = "never"
+            return play_on_ready_notification
+        return play_on_ready_notification
+
+    @staticmethod
+    def _get_on_ready_answers( settings):
+        """
+        Return the list of on_ready_answers string from the settings.
+        :param settings: The YAML settings file
+        :type settings: dict
+        :return: String parameter on_ready_answers
+        """
+        try:
+            on_ready_answers = settings["on_ready_answers"]
+        except KeyError:
+            # User does not provide this settings
+            return None
+
+        return on_ready_answers
+
+    @staticmethod
+    def _get_on_ready_sounds(settings):
+        """
+        Return the list of on_ready_sounds string from the settings.
+        :param settings: The YAML settings file
+        :type settings: dict
+        :return: String parameter on_ready_sounds
+        """
+        try:
+            on_ready_sounds = settings["on_ready_sounds"]
+            # In case files are declared in settings.yml, make sure kalliope can access them.
+            for sound in on_ready_sounds:
+                if Utils.get_real_file_path(sound) is None:
+                    raise SettingInvalidException("sound file %s not found" % sound)
+        except KeyError:
+            # User does not provide this settings
+            return None
+
+        return on_ready_sounds
+
+
 

+ 13 - 7
kalliope/core/MainController.py

@@ -49,6 +49,9 @@ class MainController:
         self.order_listener = None
         self.order_listener_callback_called = False
 
+        # boolean used to know id we played the on ready notification at least one time
+        self.on_ready_notification_played_once = False
+
         # Initialize the state machine
         self.machine = Machine(model=self, states=MainController.states, initial='init')
 
@@ -100,14 +103,17 @@ class MainController:
         """
         Play a sound when Kalliope is ready to be awaken at the first start
         """
-        # TODO place a settings to play the sound every time kalliope is waiting for a wake up
         logger.debug("Entering state: %s" % self.state)
-        # here we tell the user that we are listening
-        if self.settings.random_on_ready_answers is not None:
-            Say(message=self.settings.random_on_ready_answers)
-        elif self.settings.random_on_ready_sounds is not None:
-            random_sound_to_play = self._get_random_sound(self.settings.random_on_ready_sounds)
-            Mplayer.play(random_sound_to_play)
+        if (not self.on_ready_notification_played_once and self.settings.play_on_ready_notification == "once") or \
+                self.settings.play_on_ready_notification == "always":
+            # we remember that we played the notification one time
+            self.on_ready_notification_played_once = True
+            # here we tell the user that we are listening
+            if self.settings.on_ready_answers is not None:
+                Say(message=self.settings.on_ready_answers)
+            elif self.settings.on_ready_sounds is not None:
+                random_sound_to_play = self._get_random_sound(self.settings.on_ready_sounds)
+                Mplayer.play(random_sound_to_play)
         self.next_state()
 
     def waiting_for_trigger_callback_thread(self):

+ 6 - 4
kalliope/core/Models/Settings.py

@@ -16,8 +16,9 @@ class Settings(object):
                  stts=None,
                  random_wake_up_answers=None,
                  random_wake_up_sounds=None,
-                 random_on_ready_answers=None,
-                 random_on_ready_sounds=None,
+                 play_on_ready_notification=None,
+                 on_ready_answers=None,
+                 on_ready_sounds=None,
                  triggers=None,
                  rest_api=None,
                  cache_path=None,
@@ -33,8 +34,9 @@ class Settings(object):
         self.stts = stts
         self.random_wake_up_answers = random_wake_up_answers
         self.random_wake_up_sounds = random_wake_up_sounds
-        self.random_on_ready_answers = random_on_ready_answers
-        self.random_on_ready_sounds = random_on_ready_sounds
+        self.play_on_ready_notification = play_on_ready_notification
+        self.on_ready_answers = on_ready_answers
+        self.on_ready_sounds = on_ready_sounds
         self.triggers = triggers
         self.rest_api = rest_api
         self.cache_path = cache_path

+ 21 - 13
kalliope/settings.yml

@@ -99,19 +99,27 @@ random_wake_up_answers:
 
 
 # ---------------------------
-# On ready answers
-# ---------------------------
-# You can play a sound when Kalliope is started and is waiting for the trigger.
-#random_on_ready_answers:
-#  - "Kalliope is ready"
-
-# If you prefer, you can play a sound instead of a sentence.
-# Remove the `random_on_ready_answers` parameters to use this one.
-# Place here the full path of the sound file or just the name of the file in /usr/lib/kalliope/sounds
-# The file must be .wav or .mp3 format. By default two file are provided: ding.wav and dong.wav
-#random_on_ready_sounds:
-#  - "sounds/ding.wav"
-#  - "sounds/dong.wav"
+# On ready notification
+# ---------------------------
+# This section is used to notify the user when Kalliope is waiting for a trigger detection by playing a sound or speak a sentence out loud
+
+# This parameter define if you play the on ready answer:
+# - always: every time Kalliope is ready to be awaken
+# - never: never play a sound or sentences when kalliope is ready
+# - once: at the first start of Kalliope
+play_on_ready_notification: never
+
+# The on ready notification can be a sentence. Place here a sentence or a list of sentence. If you set a list, one sentence will be picked up randomly
+on_ready_answers:
+  - "Kalliope is ready"
+  - "Waiting for order"
+
+# You can play a sound instead of a sentence.
+# Remove the `on_ready_answers` parameters by commenting it out and use this one instead.
+# Place here the path of the sound file. Files must be .wav or .mp3 format.
+on_ready_sounds:
+  - "sounds/ding.wav"
+  - "sounds/dong.wav"
 
 
 # ---------------------------