Skip to content

Commit

Permalink
hotfix/TTS audio ext (#60) + tts_cache (#61)
Browse files Browse the repository at this point in the history
hotfix/dynamic TTS audio extension engines (#60)

self.audio_ext code has been cleaned up, it is now detected instead of hard coded

tts engine has a default audio extension, but we can not be sure that the file actually uses the same extension, for instance in chatterbox a mp3 engine might include some cache in wav format, cached utterances might also be saved by the user in the resources directory

hotfix/tts_cache (#61)

fiX - MycroftAI#2934 was not a functional fix, get_tts returns a string not a cache object
companion PR - MycroftAI#2938

cache was deleted on boot, the min_percent param is set to 100%, "Remove cache data if disk space is running low." actually means "always remove cache data". Value was made configurable (per tts engine) and defaults to 75% now
  • Loading branch information
JarbasAl committed Jul 5, 2021
1 parent 25ce902 commit 221b505
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion mycroft/tts/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def __init__(self, tts_config, tts_name, audio_file_type):
self.audio_file_type = audio_file_type
self.resource_dir = Path(__file__).parent.parent.joinpath("res")
self.cached_sentences = dict()
# curate cache if disk usage is above min %
self.min_free_percent = self.config.get("min_free_percent", 75)

def __contains__(self, sha):
"""The cache contains a SHA if it knows of it and it exists on disk."""
Expand Down Expand Up @@ -312,7 +314,7 @@ def clear(self):
def curate(self):
"""Remove cache data if disk space is running low."""
files_removed = curate_cache(self.temporary_cache_dir,
min_free_percent=100)
min_free_percent=self.min_free_percent)

hashes = set([hash_from_path(Path(path)) for path in files_removed])
for sentence_hash in hashes:
Expand Down
17 changes: 13 additions & 4 deletions mycroft/tts/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import os.path
from os.path import dirname, exists, isdir, join

from pathlib import Path
import mycroft.util
from mycroft.enclosure.api import EnclosureAPI
from mycroft.configuration import Configuration
Expand Down Expand Up @@ -376,15 +376,16 @@ def _execute(self, sentence, ident, listen):
# TODO 21.08: remove mutation of audio_file.path.
returned_file, phonemes = self.get_tts(
sentence, str(audio_file.path))
if returned_file.path != audio_file.path:
returned_file = Path(returned_file)
if returned_file != audio_file.path:
warn(
DeprecationWarning(
f"{self.tts_name} is saving files "
"to a different path than requested. If you are "
"the maintainer of this plugin, please adhere to "
"the file path argument provided. Modified paths "
"will be ignored in a future release."))
audio_file = returned_file
audio_file.path = returned_file
if phonemes:
phoneme_file = self.cache.define_phoneme_file(
sentence_hash
Expand All @@ -396,8 +397,16 @@ def _execute(self, sentence, ident, listen):
audio_file, phoneme_file
)
viseme = self.viseme(phonemes) if phonemes else None

# tts engine has a default audio extension, but we can not be
# sure that the file actually uses the same extension,
# for instance a mp3 engine might include some cache in wav format
# cached utterances might also be saved by the user in the
# resources directory
_, audio_ext = os.path.splitext(str(audio_file.path))
audio_ext = audio_ext[1:]
self.queue.put(
(self.audio_ext, str(audio_file.path), viseme, ident, l)
(audio_ext, str(audio_file.path), viseme, ident, l)
)

def _get_sentence_from_cache(self, sentence_hash):
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/tts/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_curate_cache(self, curate_mock):
files['gobo'].path]
tts_cache.curate()
curate_mock.assert_called_with(tts_cache.temporary_cache_dir,
min_free_percent=100)
min_free_percent=75)

# Verify that the "hashes" kermit and gobo was removed from the
# dict of hashes.
Expand Down

0 comments on commit 221b505

Please sign in to comment.