From 82974b4c2f28ba005b399ac2d8fb4b986749423d Mon Sep 17 00:00:00 2001 From: dalgwen Date: Sat, 8 Jan 2022 00:44:28 +0100 Subject: [PATCH] [googletts] Use returned sound to get play informations (#11877) * [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 --- .../googletts/internal/GoogleTTSService.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java b/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java index fb249fa63..edf7d5483 100644 --- a/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java +++ b/bundles/org.openhab.voice.googletts/src/main/java/org/openhab/voice/googletts/internal/GoogleTTSService.java @@ -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); + } } }