Skip to content

Commit

Permalink
fix/tts_reload (#57)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JarbasAl authored Apr 29, 2024
1 parent 4133309 commit 07bcedf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ovos_audio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -326,18 +327,24 @@ 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()
# Create new tts instance
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", ""):
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions test/unittests/test_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 07bcedf

Please sign in to comment.