Skip to content

Commit

Permalink
[voice] Add length limit to TTS handled by cache (#3699)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
  • Loading branch information
dalgwen authored Jul 15, 2023
1 parent 141b7b6 commit 07e8823
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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;
Expand All @@ -89,6 +97,8 @@ protected void modified(Map<String, Object> config) {
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,
Expand All @@ -100,7 +110,8 @@ protected void modified(Map<String, Object> config) {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 07e8823

Please sign in to comment.