[voice] Add length limit to TTS handled by cache (#3699)

* [voice] Add length limit to TTS handled by cache

We can safely assume that long TTS are generated for report (meteo, chatGPT ?), probably not meant to be repeated, and so not cached.

Signed-off-by: Gwendal Roulleau <gwendal.roulleau@gmail.com>
pull/3703/head
Gwendal Roulleau 2023-07-15 09:47:59 +02:00 committed by GitHub
parent 141b7b637a
commit 07e88234c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View File

@ -53,9 +53,11 @@ public class TTSLRUCacheImpl implements TTSCache {
// a small default cache size for all the TTS services (in kB)
private static final long DEFAULT_CACHE_SIZE_TTS = 10240;
private static final int DEFAULT_MAX_TEXT_LENGTH_CACHE_TTS = 150;
static final String CONFIG_CACHE_SIZE_TTS = "cacheSizeTTS";
static final String CONFIG_ENABLE_CACHE_TTS = "enableCacheTTS";
static final String CONFIG_MAX_TEXTLENGTH_CACHE_TTS = "maxTextLengthCacheTTS";
static final String VOICE_TTS_CACHE_PID = "org.openhab.voice.tts";
@ -66,6 +68,12 @@ public class TTSLRUCacheImpl implements TTSCache {
* current request is not known and may or may not exceed the limit.
*/
protected long cacheSizeTTS = DEFAULT_CACHE_SIZE_TTS * 1024;
/**
* The maximum length of texts handled by the TTS cache (in character). If exceeded, will pass the text to
* the TTS without storing it. (One can safely assume that long TTS are generated for report, probably not meant to
* be repeated)
*/
private long maxTextLengthCacheTTS = DEFAULT_MAX_TEXT_LENGTH_CACHE_TTS;
protected boolean enableCacheTTS = true;
private StorageService storageService;
@ -89,6 +97,8 @@ public class TTSLRUCacheImpl implements TTSCache {
this.enableCacheTTS = ConfigParser.valueAsOrElse(config.get(CONFIG_ENABLE_CACHE_TTS), Boolean.class, true);
this.cacheSizeTTS = ConfigParser.valueAsOrElse(config.get(CONFIG_CACHE_SIZE_TTS), Long.class,
DEFAULT_CACHE_SIZE_TTS) * 1024;
this.maxTextLengthCacheTTS = ConfigParser.valueAsOrElse(config.get(CONFIG_MAX_TEXTLENGTH_CACHE_TTS),
Integer.class, DEFAULT_MAX_TEXT_LENGTH_CACHE_TTS);
if (enableCacheTTS) {
this.lruMediaCache = new LRUMediaCache<>(storageService, cacheSizeTTS, VOICE_TTS_CACHE_PID,
@ -100,7 +110,8 @@ public class TTSLRUCacheImpl implements TTSCache {
public AudioStream get(CachedTTSService tts, String text, Voice voice, AudioFormat requestedFormat)
throws TTSException {
LRUMediaCache<AudioFormatInfo> lruMediaCacheLocal = lruMediaCache;
if (!enableCacheTTS || lruMediaCacheLocal == null) {
if (!enableCacheTTS || lruMediaCacheLocal == null
|| (maxTextLengthCacheTTS > 0 && text.length() > maxTextLengthCacheTTS)) {
return tts.synthesizeForCache(text, voice, requestedFormat);
}

View File

@ -59,6 +59,12 @@
<description>The limit size of the TTS cache (in kB).</description>
<default>10240</default>
</parameter>
<parameter name="maxTextLengthCacheTTS" type="integer">
<label>TTS Cache Maximum Text Length</label>
<description>The maximum length of texts handled by the TTS cache (in character). If exceeded, will pass the text to
the TTS without storing it. 0 for no limit.</description>
<default>150</default>
</parameter>
</config-description>
</config-description:config-descriptions>

View File

@ -17,6 +17,12 @@ system.config.voice.listeningMelody.description = A melody to be played to adver
system.config.voice.listeningMelody.option.Bb = Bb
system.config.voice.listeningMelody.option.F# = F#
system.config.voice.listeningMelody.option.E = E
system.config.voice.enableCacheTTS.label = Enable TTS caching
system.config.voice.enableCacheTTS.description = true to allow TTS services to cache audio files on disk.
system.config.voice.cacheSizeTTS.label = TTS Cache Size
system.config.voice.cacheSizeTTS.description = The limit size of the TTS cache (in kB).
system.config.voice.maxTextLengthCacheTTS.label = TTS Cache Maximum Text Length
system.config.voice.maxTextLengthCacheTTS.description = The limit length of a text handled by the TTS cache. If exceeded, will passthrough to the TTS without storing it. 0 for no limit.
error.ks-error = Encountered error while spotting keywords, {0}
error.stt-error = Encountered error while recognizing text, {0}