]> git.basschouten.com Git - openhab-addons.git/commitdiff
[googletts] Use returned sound to get play informations (#11877)
authordalgwen <dalgwen@users.noreply.github.com>
Fri, 7 Jan 2022 23:44:28 +0000 (00:44 +0100)
committerGitHub <noreply@github.com>
Fri, 7 Jan 2022 23:44:28 +0000 (00:44 +0100)
* [googletts] Use real sound returned to get play informations (#10015)

When google tts returns a wav file, it is now parsed to get correct informations about it.(mandatory for playing it with at least the System speaker audio sink)
Close #10015

* Fix a regression, as the WAV fix was incorrectly applied to the MP3 file

Signed-off-by: Gwendal Roulleau <gwendal.roulleau@gmail.com>
bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java

index fb249fa6370cb8a1673bf411d4456d2beb0c8316..edf7d548389654f03da215d575a4f9af930d7fce 100644 (file)
@@ -14,7 +14,10 @@ package org.openhab.voice.googletts.internal;
 
 import static org.openhab.voice.googletts.internal.GoogleTTSService.*;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Locale;
@@ -27,6 +30,7 @@ import org.openhab.core.OpenHAB;
 import org.openhab.core.audio.AudioFormat;
 import org.openhab.core.audio.AudioStream;
 import org.openhab.core.audio.ByteArrayAudioStream;
+import org.openhab.core.audio.utils.AudioWaveUtils;
 import org.openhab.core.auth.client.oauth2.OAuthFactory;
 import org.openhab.core.config.core.ConfigurableService;
 import org.openhab.core.voice.TTSException;
@@ -332,6 +336,21 @@ public class GoogleTTSService implements TTSService {
         if (audio == null) {
             throw new TTSException("Could not synthesize text via Google Cloud TTS Service");
         }
-        return new ByteArrayAudioStream(audio, requestedFormat);
+
+        // compute the real format returned by google if wave file
+        AudioFormat finalFormat = requestedFormat;
+        if (AudioFormat.CONTAINER_WAVE.equals(requestedFormat.getContainer())) {
+            finalFormat = parseAudioFormat(audio);
+        }
+
+        return new ByteArrayAudioStream(audio, finalFormat);
+    }
+
+    private AudioFormat parseAudioFormat(byte[] audio) throws TTSException {
+        try (InputStream inputStream = new ByteArrayInputStream(audio)) {
+            return AudioWaveUtils.parseWavFormat(inputStream);
+        } catch (IOException e) {
+            throw new TTSException("Cannot parse WAV format", e);
+        }
     }
 }