MainController.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import logging
  2. import os
  3. import random
  4. from core import AudioPlayer
  5. from core import Utils
  6. from core.ConfigurationManager import SettingLoader
  7. from core.OrderAnalyser import OrderAnalyser
  8. from core.OrderListener import OrderListener
  9. from core.TriggerLauncher import TriggerLauncher
  10. from flask import Flask
  11. from core.RestAPI.FlaskAPI import FlaskAPI
  12. from neurons import Say
  13. logging.basicConfig()
  14. logger = logging.getLogger("kalliope")
  15. class MainController:
  16. def __init__(self, brain_file=None):
  17. self.brain_file = brain_file
  18. # get global configuration
  19. self.settings = SettingLoader.get_settings()
  20. # run the api if the user want it
  21. if self.settings.rest_api.active:
  22. Utils.print_info("Starting REST API Listening port: %s" % self.settings.rest_api.port)
  23. app = Flask(__name__)
  24. flask_api = FlaskAPI(app, port=self.settings.rest_api.port, brain_file=brain_file)
  25. flask_api.start()
  26. # create an order listener object. This last will the trigger callback before starting
  27. self.order_listener = OrderListener(self.analyse_order)
  28. # Wait that the kalliope trigger is pronounced by the user
  29. self.trigger_instance = self._get_default_trigger()
  30. self.trigger_instance.start()
  31. Utils.print_info("Waiting for trigger detection")
  32. def callback(self):
  33. """
  34. # we have detected the hotword, we can now pause the kalliope Trigger for a while
  35. # The user can speak out loud his order during this time.
  36. :return:
  37. """
  38. # pause the snowboy process
  39. self.trigger_instance.pause()
  40. # start listening for an order
  41. self.order_listener.start()
  42. # if random wake answer sentence are present, we play this
  43. if self.settings.random_wake_up_answers is not None:
  44. Say(message=self.settings.random_wake_up_answers)
  45. else:
  46. ap = AudioPlayer()
  47. ap.init_play()
  48. random_sound_to_play = self._get_random_sound(self.settings.random_wake_up_sounds)
  49. ap.play_audio(random_sound_to_play)
  50. def analyse_order(self, order):
  51. """
  52. Receive an order, try to retreive it in the brain.yml to launch to attached plugins
  53. :return:
  54. """
  55. order_analyser = OrderAnalyser(order, main_controller=self, brain_file=self.brain_file)
  56. order_analyser.start()
  57. # restart the trigger when the order analyser has finish his job
  58. Utils.print_info("Waiting for trigger detection")
  59. self.trigger_instance.unpause()
  60. # create a new order listener that will wait for start
  61. self.order_listener = OrderListener(self.analyse_order)
  62. # restart the trigger to catch the hotword
  63. self.trigger_instance.start()
  64. def _get_default_trigger(self):
  65. """
  66. Return an instance of the default trigger
  67. :return:
  68. """
  69. for trigger in self.settings.triggers:
  70. if trigger.name == self.settings.default_trigger_name:
  71. return TriggerLauncher.get_trigger(trigger, callback=self.callback)
  72. @staticmethod
  73. def _get_random_sound(random_wake_up_sounds):
  74. """
  75. Return a path of a sound to play
  76. If the path is absolute, test if file exist
  77. If the path is relative, we check if the file exist in the sound folder
  78. :param random_wake_up_sounds:
  79. :return:
  80. """
  81. # take first randomly a path
  82. random_path = random.choice(random_wake_up_sounds)
  83. logger.debug("Selected sound: %s" % random_path)
  84. if os.path.isabs(random_path):
  85. logger.debug("Path of file %s is absolute" % random_path)
  86. return random_path
  87. else:
  88. logger.debug("Path of file %s is relative" % random_path)
  89. return "sounds" + os.sep + random_path