[Voice] get audio format refactor (#2795)
Signed-off-by: Miguel Álvarez Díez <miguelwork92@gmail.com>pull/2799/head
parent
c0b5f09c2b
commit
534be1d3ad
|
@ -12,12 +12,9 @@
|
|||
*/
|
||||
package org.openhab.core.voice.internal;
|
||||
|
||||
import static org.openhab.core.voice.internal.VoiceManagerImpl.getBestMatch;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
@ -87,7 +84,9 @@ public class DialogProcessor implements KSListener, STTListener {
|
|||
private final TranslationProvider i18nProvider;
|
||||
private final Bundle bundle;
|
||||
|
||||
private final @Nullable AudioFormat format;
|
||||
private final @Nullable AudioFormat ksFormat;
|
||||
private final @Nullable AudioFormat sttFormat;
|
||||
private final @Nullable AudioFormat ttsFormat;
|
||||
|
||||
/**
|
||||
* If the processor is currently processing a keyword event and thus should not spot further ones.
|
||||
|
@ -120,14 +119,15 @@ public class DialogProcessor implements KSListener, STTListener {
|
|||
this.eventPublisher = eventPublisher;
|
||||
this.i18nProvider = i18nProvider;
|
||||
this.bundle = bundle;
|
||||
this.format = AudioFormat.getBestMatch(source.getSupportedFormats(), sink.getSupportedFormats());
|
||||
this.ksFormat = AudioFormat.getBestMatch(source.getSupportedFormats(), ks.getSupportedFormats());
|
||||
this.sttFormat = AudioFormat.getBestMatch(source.getSupportedFormats(), stt.getSupportedFormats());
|
||||
this.ttsFormat = AudioFormat.getBestMatch(sink.getSupportedFormats(), tts.getSupportedFormats());
|
||||
}
|
||||
|
||||
public void start() {
|
||||
AudioFormat fmt = format;
|
||||
AudioFormat fmt = ksFormat;
|
||||
if (fmt == null) {
|
||||
logger.warn("No compatible audio format found between source '{}' and sink '{}'", source.getId(),
|
||||
sink.getId());
|
||||
logger.warn("No compatible audio format found for ks '{}' and source '{}'", ks.getId(), source.getId());
|
||||
return;
|
||||
}
|
||||
abortKS();
|
||||
|
@ -213,7 +213,7 @@ public class DialogProcessor implements KSListener, STTListener {
|
|||
abortSTT();
|
||||
closeStreamSTT();
|
||||
isSTTServerAborting = false;
|
||||
AudioFormat fmt = format;
|
||||
AudioFormat fmt = sttFormat;
|
||||
if (fmt != null) {
|
||||
try {
|
||||
AudioStream stream = source.getInputStream(fmt);
|
||||
|
@ -231,6 +231,9 @@ public class DialogProcessor implements KSListener, STTListener {
|
|||
say(text.replace("{0}", ""));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.warn("No compatible audio format found for stt '{}' and source '{}'", stt.getId(),
|
||||
source.getId());
|
||||
}
|
||||
} else if (ksEvent instanceof KSErrorEvent) {
|
||||
KSErrorEvent kse = (KSErrorEvent) ksEvent;
|
||||
|
@ -294,8 +297,7 @@ public class DialogProcessor implements KSListener, STTListener {
|
|||
throw new TTSException("Unable to find a suitable voice");
|
||||
}
|
||||
|
||||
Set<AudioFormat> audioFormats = tts.getSupportedFormats();
|
||||
AudioFormat audioFormat = getBestMatch(audioFormats, sink.getSupportedFormats());
|
||||
AudioFormat audioFormat = ttsFormat;
|
||||
if (audioFormat == null) {
|
||||
throw new TTSException("No compatible audio format found for TTS '" + tts.getId() + "' and sink '"
|
||||
+ sink.getId() + "'");
|
||||
|
|
|
@ -225,19 +225,19 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider {
|
|||
throw new TTSException(
|
||||
"Unable to find a voice for language " + localeProvider.getLocale().getLanguage());
|
||||
}
|
||||
Set<AudioFormat> audioFormats = tts.getSupportedFormats();
|
||||
Set<AudioFormat> sttAudioFormats = tts.getSupportedFormats();
|
||||
AudioSink sink = audioManager.getSink(sinkId);
|
||||
if (sink == null) {
|
||||
throw new TTSException("Unable to find the audio sink " + sinkId);
|
||||
}
|
||||
|
||||
AudioFormat audioFormat = getBestMatch(audioFormats, sink.getSupportedFormats());
|
||||
if (audioFormat == null) {
|
||||
AudioFormat sttAudioFormat = AudioFormat.getBestMatch(sink.getSupportedFormats(), sttAudioFormats);
|
||||
if (sttAudioFormat == null) {
|
||||
throw new TTSException("No compatible audio format found for TTS '" + tts.getId() + "' and sink '"
|
||||
+ sink.getId() + "'");
|
||||
}
|
||||
|
||||
AudioStream audioStream = tts.synthesize(text, voice, audioFormat);
|
||||
AudioStream audioStream = tts.synthesize(text, voice, sttAudioFormat);
|
||||
if (!sink.getSupportedStreams().stream().anyMatch(clazz -> clazz.isInstance(audioStream))) {
|
||||
throw new TTSException(
|
||||
"Failed playing audio stream '" + audioStream + "' as audio sink doesn't support it");
|
||||
|
@ -426,22 +426,6 @@ public class VoiceManagerImpl implements VoiceManager, ConfigOptionProvider {
|
|||
return null;
|
||||
}
|
||||
|
||||
public static @Nullable AudioFormat getBestMatch(Set<AudioFormat> inputs, Set<AudioFormat> outputs) {
|
||||
AudioFormat preferredFormat = getPreferredFormat(inputs);
|
||||
for (AudioFormat output : outputs) {
|
||||
if (output.isCompatible(preferredFormat)) {
|
||||
return preferredFormat;
|
||||
} else {
|
||||
for (AudioFormat input : inputs) {
|
||||
if (output.isCompatible(input)) {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Voice getPreferredVoice(Set<Voice> voices) {
|
||||
// Express preferences with a Language Priority List
|
||||
|
|
Loading…
Reference in New Issue