From 07bcedfd5e983eee0287a7d47e84eb228632a7b1 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Mon, 29 Apr 2024 23:48:25 +0100 Subject: [PATCH] fix/tts_reload (#57) * fix/tts_reload TTS reload was only working for plugin changes the check for config changes only accounted for module, not for the whole config, so changing voice etc would require a reload * fix tests --- ovos_audio/service.py | 17 ++++++++++++----- test/unittests/test_speech.py | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index 46f4c9d..110e928 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -4,6 +4,7 @@ import os import os.path import time +import json from hashlib import md5 from os.path import exists from queue import Queue @@ -326,10 +327,16 @@ def _maybe_reload_tts(self): Load TTS modules if not yet loaded or if configuration has changed. Optionally pre-loads fallback TTS if configured """ - config = self.config.get("tts", {}) + config = Configuration().get("tts", {}) + tts_m = config.get("module", "") + ftts_m = config.get("fallback_module", "") + _tts_hash = hash(json.dumps(config.get(tts_m, {}), + sort_keys=True)) + _ftts_hash = hash(json.dumps(config.get(ftts_m, {}), + sort_keys=True)) # update TTS object if configuration has changed - if not self._tts_hash or self._tts_hash != config.get("module", ""): + if not self._tts_hash or self._tts_hash != _tts_hash: with self.lock: if self.tts: self.tts.shutdown() @@ -337,7 +344,7 @@ def _maybe_reload_tts(self): LOG.info("(re)loading TTS engine") self.tts = TTSFactory.create(config) self.tts.init(self.bus, self.playback_thread) - self._tts_hash = config.get("module", "") + self._tts_hash = _tts_hash # if fallback TTS is the same as main TTS dont load it if config.get("module", "") == config.get("fallback_module", "") or not config.get("fallback_module", ""): @@ -349,14 +356,14 @@ def _maybe_reload_tts(self): return if not self._fallback_tts_hash or \ - self._fallback_tts_hash != config.get("fallback_module", ""): + self._fallback_tts_hash != ftts_m: with self.lock: if self.fallback_tts: self.fallback_tts.shutdown() # Create new tts instance LOG.info("(re)loading fallback TTS engine") self._get_tts_fallback() - self._fallback_tts_hash = config.get("fallback_module", "") + self._fallback_tts_hash = _ftts_hash def execute_tts(self, utterance, ident, listen=False, message: Message = None): """Mute mic and start speaking the utterance using selected tts backend. diff --git a/test/unittests/test_speech.py b/test/unittests/test_speech.py index 0e84e58..3257752 100644 --- a/test/unittests/test_speech.py +++ b/test/unittests/test_speech.py @@ -149,6 +149,7 @@ def execute(*args, **kwargs): speech = PlaybackService(bus=bus) speech.tts = FailingTTS() + speech._tts_hash = "123failing" speech.execute_tts("hello", "123") self.assertTrue(speech.fallback_tts.execute.called)