async def start( self, preload: Optional[bool] = None, block: bool = True, timeout: float = 60, observer: Optional[RhasspyActor] = None, ) -> None: """Start Rhasspy core.""" if self.actor_system is None: self.actor_system = ActorSystem() if preload is None: preload = self.profile.get("rhasspy.preload_profile", False) assert self.actor_system is not None self.dialogue_manager = self.actor_system.createActor(DialogueManager) with self.actor_system.private() as sys: await sys.async_ask( self.dialogue_manager, ConfigureEvent( self.profile, preload=preload, ready=block, transitions=False, load_timeout_sec=30, observer=observer, ), ) # Block until ready if block: await sys.async_listen(timeout)
async def start_rhasspy() -> None: """Create actor system and Rhasspy core.""" global core # Load core system = ActorSystem() core = RhasspyCore( args.profile, system_profiles_dir, user_profiles_dir, actor_system=system ) # Set environment variables os.environ["RHASSPY_BASE_DIR"] = os.getcwd() os.environ["RHASSPY_PROFILE"] = core.profile.name os.environ["RHASSPY_PROFILE_DIR"] = core.profile.write_dir() # Add profile settings from the command line extra_settings = {} for key, value in args.set: try: value = json.loads(value) except Exception: pass logger.debug("Profile: %s=%s", key, value) extra_settings[key] = value core.profile.set(key, value) # Load observer actor to catch intents observer = system.createActor(WebSocketObserver) system.ask(observer, ConfigureEvent(core.profile)) await core.start(observer=observer) logger.info("Started")
def in_training_intent(self, message: Any, sender: RhasspyActor) -> None: """Handle messages in training_intent state.""" if isinstance(message, IntentTrainingComplete): self._logger.info("Training complete") if self.reload_actors_after_training: self._logger.debug("Reloading actors") # Wake listener self.send(self.wake, ActorExitRequest()) self._wake = self.createActor(self.wake_class) self.actors["wake"] = self.wake # Speech decoder self.send(self.decoder, ActorExitRequest()) self._decoder = self.createActor(self.decoder_class) self.actors["decoder"] = self.decoder # Intent recognizer self.send(self.recognizer, ActorExitRequest()) self._recognizer = self.createActor(self.recognizer_class) self.actors["recognizer"] = self.recognizer # Configure actors self.wait_actors = { "wake": self.wake, "decoder": self.decoder, "recognizer": self.recognizer, } for actor in self.wait_actors.values(): if actor == self.mqtt: continue # skip self.send( actor, ConfigureEvent(self.profile, preload=self.preload, **self.actors), ) self.transition("training_loading") else: self.transition("ready") self.send(self.training_receiver, ProfileTrainingComplete()) elif isinstance(message, IntentTrainingFailed): self.transition("ready") self.send(self.training_receiver, ProfileTrainingFailed(message.reason))
def to_started(self, from_state: str) -> None: """Transition to started state.""" self.cache_dir = self.profile.write_dir( self.profile.get("text_to_speech.wavenet.cache_dir", "tts/googlewavenet")) # Create cache directory in profile if it doesn't exist os.makedirs(self.cache_dir, exist_ok=True) # Load settings self.url = self.profile.get( "text_to_speech.wavenet.url", "https://texttospeech.googleapis.com/v1/text:synthesize", ) self.voice = self.profile.get("text_to_speech.wavenet.voice", "Wavenet-C") self.gender = self.profile.get("text_to_speech.wavenet.gender", "FEMALE") self.sample_rate = int( self.profile.get("text_to_speech.wavenet.sample_rate", 22050)) self.language_code = self.profile.get( "text_to_speech.wavenet.language_code", "en-US") self.player = self.config["player"] self.wake = self.config.get("wake") self.wake_on_start = self.profile.get("rhasspy.listen_on_start", False) self.disable_wake = self.profile.get("text_to_speech.disable_wake", True) # Create a child actor as a fallback. # This will load the appropriate settings, etc. fallback_tts = self.profile.get("text_to_speech.wavenet.fallback_tts", "espeak") assert fallback_tts != "wavenet", "Cannot fall back from wavenet to wavenet" if fallback_tts: self._logger.debug("Using %s as a fallback text to speech system", fallback_tts) fallback_class = get_speech_class(fallback_tts) self.fallback_actor = self.createActor(fallback_class) self.send(self.fallback_actor, ConfigureEvent(self.profile, **self.config)) self.transition("ready")
def to_loading_mqtt(self, from_state: str) -> None: """Transition to loading_mqtt state.""" self._logger.debug("Loading MQTT first") # MQTT client *first* from rhasspy.mqtt import HermesMqtt self.mqtt_class = HermesMqtt self.mqtt = self.createActor(self.mqtt_class) assert self.mqtt is not None self.actors["mqtt"] = self.mqtt self.send( self.mqtt, ConfigureEvent(self.profile, preload=self.preload, **self.actors)) if self.timeout_sec is not None: self._logger.debug("Loading...will time out after %s second(s)", self.timeout_sec) self.wakeupAfter(timedelta(seconds=self.timeout_sec))
def load_actors(self) -> None: """Load all system actors.""" self._logger.debug("Loading actors") # Microphone mic_system = self.profile.get("microphone.system", "dummy") self.recorder_class = get_microphone_class(mic_system) self._recorder = self.createActor(self.recorder_class) self.actors["recorder"] = self.recorder # Audio player player_system = self.profile.get("sounds.system", "dummy") self.player_class = get_sound_class(player_system) self._player = self.createActor(self.player_class) self.actors["player"] = self.player # Text to Speech speech_system = self.profile.get("text_to_speech.system", "dummy") self.speech_class = get_speech_class(speech_system) self._speech = self.createActor(self.speech_class) self.actors["speech"] = self.speech # Wake listener wake_system = self.profile.get("wake.system", "dummy") self.wake_class = get_wake_class(wake_system) self._wake = self.createActor(self.wake_class) self.actors["wake"] = self.wake # Command listener command_system = self.profile.get("command.system", "dummy") self.command_class = get_command_class(command_system) self._command = self.createActor(self.command_class) self.actors["command"] = self.command # Speech decoder decoder_system = self.profile.get("speech_to_text.system", "dummy") self.decoder_class = get_decoder_class(decoder_system) self._decoder = self.createActor(self.decoder_class) self.actors["decoder"] = self.decoder # Intent recognizer recognizer_system = self.profile.get("intent.system", "dummy") self.recognizer_class = get_recognizer_class(recognizer_system) self._recognizer = self.createActor(self.recognizer_class) self.actors["recognizer"] = self.recognizer # Intent handler handler_system = self.profile.get("handle.system", "dummy") self.handler_class = get_intent_handler_class(handler_system) self._handler = self.createActor(self.handler_class) self.actors["handler"] = self.handler self.hass_handler = None forward_to_hass = self.profile.get("handle.forward_to_hass", False) if (handler_system != "hass") and forward_to_hass: # Create a separate actor just for home assistant from rhasspy.intent_handler import HomeAssistantIntentHandler self.hass_handler = self.createActor(HomeAssistantIntentHandler) self.actors["hass_handler"] = self.hass_handler # Speech trainer speech_trainer_system = self.profile.get( "training.speech_to_text.system", "auto") self.speech_trainer_class = get_speech_trainer_class( speech_trainer_system, decoder_system) self._speech_trainer = self.createActor(self.speech_trainer_class) self.actors["speech_trainer"] = self.speech_trainer # Intent trainer intent_trainer_system = self.profile.get("training.intent.system", "auto") self.intent_trainer_class = get_intent_trainer_class( intent_trainer_system, recognizer_system) self._intent_trainer = self.createActor(self.intent_trainer_class) self.actors["intent_trainer"] = self.intent_trainer # Word pronouncer from rhasspy.pronounce import PhonetisaurusPronounce self.word_pronouncer_class = PhonetisaurusPronounce self._word_pronouncer = self.createActor(self.word_pronouncer_class) self.actors["word_pronouncer"] = self.word_pronouncer # Configure actors self.wait_actors = {} for name, actor in self.actors.items(): if (actor is None) or (actor == self.mqtt): continue # skip self.send( actor, ConfigureEvent(self.profile, preload=self.preload, **self.actors)) self.wait_actors[name] = actor actor_names = list(self.wait_actors.keys()) self._logger.debug("Actors created. Waiting for %s to start.", actor_names)