123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import logging
- import os
- import random
- from core import AudioPlayer
- from core import Utils
- from core.ConfigurationManager import SettingLoader
- from core.ConfigurationManager.BrainLoader import BrainLoader
- from core.OrderAnalyser import OrderAnalyser
- from core.OrderListener import OrderListener
- from core.TriggerLauncher import TriggerLauncher
- from flask import Flask
- from core.RestAPI.FlaskAPI import FlaskAPI
- from neurons import Say
- logging.basicConfig()
- logger = logging.getLogger("kalliope")
- class MainController:
- def __init__(self, brain_file=None):
- self.brain_file = brain_file
-
- self.settings = SettingLoader.get_settings()
-
- if brain_file is None:
- self.brain = BrainLoader.get_brain()
- else:
- self.brain = BrainLoader.get_brain(file_path=brain_file)
-
- if self.settings.rest_api.active:
- Utils.print_info("Starting REST API Listening port: %s" % self.settings.rest_api.port)
- app = Flask(__name__)
- flask_api = FlaskAPI(app, port=self.settings.rest_api.port, brain_file=brain_file)
- flask_api.start()
-
- self.order_listener = OrderListener(self.analyse_order)
-
- self.trigger_instance = self._get_default_trigger()
- self.trigger_instance.start()
- Utils.print_info("Waiting for trigger detection")
- def callback(self):
- """
- # we have detected the hotword, we can now pause the kalliope Trigger for a while
- # The user can speak out loud his order during this time.
- :return:
- """
-
- self.trigger_instance.pause()
-
- self.order_listener.start()
-
- if self.settings.random_wake_up_answers is not None:
- Say(message=self.settings.random_wake_up_answers)
- else:
- ap = AudioPlayer()
- ap.init_play()
- random_sound_to_play = self._get_random_sound(self.settings.random_wake_up_sounds)
- ap.play_audio(random_sound_to_play)
- def analyse_order(self, order):
- """
- Receive an order, try to retreive it in the brain.yml to launch to attached plugins
- :return:
- """
- order_analyser = OrderAnalyser(order, main_controller=self, brain=self.brain)
- order_analyser.start()
-
- Utils.print_info("Waiting for trigger detection")
- self.trigger_instance.unpause()
-
- self.order_listener = OrderListener(self.analyse_order)
-
- self.trigger_instance.start()
- def _get_default_trigger(self):
- """
- Return an instance of the default trigger
- :return:
- """
- for trigger in self.settings.triggers:
- if trigger.name == self.settings.default_trigger_name:
- return TriggerLauncher.get_trigger(trigger, callback=self.callback)
- @staticmethod
- def _get_random_sound(random_wake_up_sounds):
- """
- Return a path of a sound to play
- If the path is absolute, test if file exist
- If the path is relative, we check if the file exist in the sound folder
- :param random_wake_up_sounds:
- :return:
- """
-
- random_path = random.choice(random_wake_up_sounds)
- logger.debug("Selected sound: %s" % random_path)
- if os.path.isabs(random_path):
- logger.debug("Path of file %s is absolute" % random_path)
- return random_path
- else:
- logger.debug("Path of file %s is relative" % random_path)
- return "sounds" + os.sep + random_path
|