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