]> git.basschouten.com Git - openhab-addons.git/commitdiff
[picotts] Add LRU cache (#14565)
authorGwendal Roulleau <dalgwen@users.noreply.github.com>
Sat, 8 Jul 2023 16:09:06 +0000 (18:09 +0200)
committerGitHub <noreply@github.com>
Sat, 8 Jul 2023 16:09:06 +0000 (18:09 +0200)
* [picotts] Add LRU cache

Signed-off-by: Gwendal Roulleau <gwendal.roulleau@gmail.com>
---------

Signed-off-by: Gwendal Roulleau <gwendal.roulleau@gmail.com>
bundles/org.openhab.voice.picotts/README.md
bundles/org.openhab.voice.picotts/src/main/java/org/openhab/voice/picotts/internal/PicoTTSAudioStream.java
bundles/org.openhab.voice.picotts/src/main/java/org/openhab/voice/picotts/internal/PicoTTSService.java

index cee6eb0ae1ffdf0e2abf2da450a20570084f4532..ad7d6a151c1c53a37101c8051f4f5a4dc11ffc8b 100644 (file)
@@ -49,3 +49,7 @@ org.openhab.voice:defaultVoice=picotts:frFR
 ## Supported Audio Formats
 
 The Pico service produces audio streams using WAV containers and PCM (signed) codec with 16bit depth.
+
+## Caching
+
+The Pico TTS service uses the openHAB TTS cache to cache audio files produced from the most recent queries in order to reduce traffic, improve performance and reduce number of requests.
index af4e24c6ec647e2c850a4fa82adf4bc420ed5f94..da384f443c373e1d16466469f2de5034fd3b29bd 100644 (file)
@@ -24,6 +24,7 @@ import org.openhab.core.audio.AudioException;
 import org.openhab.core.audio.AudioFormat;
 import org.openhab.core.audio.AudioStream;
 import org.openhab.core.audio.FixedLengthAudioStream;
+import org.openhab.core.common.Disposable;
 import org.openhab.core.voice.Voice;
 
 /**
@@ -32,7 +33,8 @@ import org.openhab.core.voice.Voice;
  * @author Florian Schmidt - Initial Contribution
  */
 @NonNullByDefault
-class PicoTTSAudioStream extends FixedLengthAudioStream {
+class PicoTTSAudioStream extends FixedLengthAudioStream implements Disposable {
+
     private final Voice voice;
     private final String text;
     private final AudioFormat audioFormat;
@@ -127,4 +129,18 @@ class PicoTTSAudioStream extends FixedLengthAudioStream {
             throw new AudioException("No temporary audio file available.");
         }
     }
+
+    @Override
+    public void dispose() throws IOException {
+        File localFile = file;
+        if (localFile != null && localFile.exists()) {
+            try {
+                if (!localFile.delete()) {
+                    throw new IOException("Failed to delete the file " + localFile.getAbsolutePath());
+                }
+            } catch (SecurityException e) {
+                throw new IOException("Failed to delete the file " + localFile.getAbsolutePath(), e);
+            }
+        }
+    }
 }
index 442f4acdc5cf3c0e2609eb3a727262519f4f3a73..ecb6a2ebb559173c961395d6a230e7a85e7437ef 100644 (file)
@@ -23,17 +23,27 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.audio.AudioException;
 import org.openhab.core.audio.AudioFormat;
 import org.openhab.core.audio.AudioStream;
+import org.openhab.core.voice.AbstractCachedTTSService;
+import org.openhab.core.voice.TTSCache;
 import org.openhab.core.voice.TTSException;
 import org.openhab.core.voice.TTSService;
 import org.openhab.core.voice.Voice;
+import org.osgi.service.component.annotations.Activate;
 import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
 
 /**
  * @author Florian Schmidt - Initial Contribution
  */
-@Component
+@Component(service = TTSService.class)
 @NonNullByDefault
-public class PicoTTSService implements TTSService {
+public class PicoTTSService extends AbstractCachedTTSService {
+
+    @Activate
+    public PicoTTSService(@Reference TTSCache ttsCache) {
+        super(ttsCache);
+    }
+
     private final Set<Voice> voices = Stream
             .of(new PicoTTSVoice("de-DE"), new PicoTTSVoice("en-US"), new PicoTTSVoice("en-GB"),
                     new PicoTTSVoice("es-ES"), new PicoTTSVoice("fr-FR"), new PicoTTSVoice("it-IT"))
@@ -53,7 +63,7 @@ public class PicoTTSService implements TTSService {
     }
 
     @Override
-    public AudioStream synthesize(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
+    public AudioStream synthesizeForCache(String text, Voice voice, AudioFormat requestedFormat) throws TTSException {
         if (text.isEmpty()) {
             throw new TTSException("The passed text can not be empty");
         }