[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 <gwendal.roulleau@gmail.com>
This commit is contained in:
dalgwen 2022-01-08 00:44:28 +01:00 committed by GitHub
parent 65002647df
commit 82974b4c2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 1 deletions

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