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); + } } }