MainController.py 3.4 KB

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