MainController.py 3.9 KB

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