Skip to content

Commit

Permalink
[voice] Add length limit to TTS handled by cache
Browse files Browse the repository at this point in the history
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 committed Jul 12, 2023
1 parent a656073 commit 0b58c9f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class TTSLRUCacheImpl implements TTSCache {

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 +67,11 @@ 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;
/**
* Length text limit. If exceeded, the cache will passthrough the request without storing it.
* (One can safely assume that long TTS are generated for report, probably not meant to be repeated)
*/
private long maxTextLengthCacheTTS = 150;
protected boolean enableCacheTTS = true;

private StorageService storageService;
Expand All @@ -89,6 +95,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, 150);

if (enableCacheTTS) {
this.lruMediaCache = new LRUMediaCache<>(storageService, cacheSizeTTS, VOICE_TTS_CACHE_PID,
Expand All @@ -100,7 +108,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 limit length of a text handled by the TTS cache. If exceeded, will passthrough 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 0b58c9f

Please sign in to comment.