use of org.openhab.core.voice.STTException in project openhab-addons by openhab.
the class VoskSTTService method recognize.
@Override
public STTServiceHandle recognize(STTListener sttListener, AudioStream audioStream, Locale locale, Set<String> set) throws STTException {
AtomicBoolean aborted = new AtomicBoolean(false);
try {
var frequency = audioStream.getFormat().getFrequency();
if (frequency == null) {
throw new IOException("missing audio stream frequency");
}
backgroundRecognize(sttListener, audioStream, frequency, aborted);
} catch (IOException e) {
throw new STTException(e);
}
return () -> {
aborted.set(true);
};
}
use of org.openhab.core.voice.STTException in project openhab-addons by openhab.
the class WatsonSTTService method recognize.
@Override
public STTServiceHandle recognize(STTListener sttListener, AudioStream audioStream, Locale locale, Set<String> set) throws STTException {
if (config.apiKey.isBlank() || config.instanceUrl.isBlank()) {
throw new STTException("service is not correctly configured");
}
String contentType = getContentType(audioStream);
if (contentType == null) {
throw new STTException("Unsupported format, unable to resolve audio content type");
}
logger.debug("Content-Type: {}", contentType);
var speechToText = new SpeechToText(new IamAuthenticator.Builder().apikey(config.apiKey).build());
speechToText.setServiceUrl(config.instanceUrl);
if (config.optOutLogging) {
speechToText.setDefaultHeaders(Map.of("X-Watson-Learning-Opt-Out", "1"));
}
RecognizeWithWebsocketsOptions wsOptions = new RecognizeWithWebsocketsOptions.Builder().audio(audioStream).contentType(contentType).redaction(config.redaction).smartFormatting(config.smartFormatting).model(locale.toLanguageTag() + "_BroadbandModel").interimResults(true).backgroundAudioSuppression(config.backgroundAudioSuppression).speechDetectorSensitivity(config.speechDetectorSensitivity).inactivityTimeout(config.inactivityTimeout).build();
final AtomicReference<@Nullable WebSocket> socketRef = new AtomicReference<>();
final AtomicBoolean aborted = new AtomicBoolean(false);
executor.submit(() -> {
int retries = 2;
while (retries > 0) {
try {
socketRef.set(speechToText.recognizeUsingWebSocket(wsOptions, new TranscriptionListener(sttListener, config, aborted)));
break;
} catch (RuntimeException e) {
var cause = e.getCause();
if (cause instanceof SSLPeerUnverifiedException) {
logger.debug("Retrying on error: {}", cause.getMessage());
retries--;
} else {
var errorMessage = e.getMessage();
logger.warn("Aborting on error: {}", errorMessage);
sttListener.sttEventReceived(new SpeechRecognitionErrorEvent(errorMessage != null ? errorMessage : "Unknown error"));
break;
}
}
}
});
return new STTServiceHandle() {
@Override
public void abort() {
if (!aborted.getAndSet(true)) {
var socket = socketRef.get();
if (socket != null) {
socket.close(1000, null);
socket.cancel();
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
}
}
}
};
}
use of org.openhab.core.voice.STTException in project openhab-core by openhab.
the class DialogProcessor method ksEventReceived.
@Override
public void ksEventReceived(KSEvent ksEvent) {
if (!processing) {
isSTTServerAborting = false;
if (ksEvent instanceof KSpottedEvent) {
abortSTT();
closeStreamSTT();
isSTTServerAborting = false;
AudioFormat fmt = sttFormat;
if (fmt != null) {
try {
AudioStream stream = source.getInputStream(fmt);
streamSTT = stream;
sttServiceHandle = stt.recognize(this, stream, locale, new HashSet<>());
} catch (AudioException e) {
logger.warn("Error creating the audio stream: {}", e.getMessage());
} catch (STTException e) {
closeStreamSTT();
String msg = e.getMessage();
String text = i18nProvider.getText(bundle, "error.stt-exception", null, locale);
if (msg != null) {
say(text == null ? msg : text.replace("{0}", msg));
} else if (text != null) {
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;
String text = i18nProvider.getText(bundle, "error.ks-error", null, locale);
say(text == null ? kse.getMessage() : text.replace("{0}", kse.getMessage()));
}
}
}
use of org.openhab.core.voice.STTException in project openhab-core by openhab.
the class DialogProcessor method executeSimpleDialog.
private void executeSimpleDialog() {
abortSTT();
closeStreamSTT();
isSTTServerAborting = false;
AudioFormat fmt = sttFormat;
if (fmt == null) {
logger.warn("No compatible audio format found for stt '{}' and source '{}'", stt.getId(), source.getId());
return;
}
try {
AudioStream stream = source.getInputStream(fmt);
streamSTT = stream;
sttServiceHandle = stt.recognize(this, stream, locale, new HashSet<>());
} catch (AudioException e) {
logger.warn("Error creating the audio stream: {}", e.getMessage());
} catch (STTException e) {
closeStreamSTT();
String msg = e.getMessage();
String text = i18nProvider.getText(bundle, "error.stt-exception", null, locale);
if (msg != null) {
say(text == null ? msg : text.replace("{0}", msg));
} else if (text != null) {
say(text.replace("{0}", ""));
}
}
}
use of org.openhab.core.voice.STTException in project openhab-addons by openhab.
the class WatsonSTTService method getContentType.
@Nullable
private String getContentType(AudioStream audioStream) throws STTException {
AudioFormat format = audioStream.getFormat();
String container = format.getContainer();
String codec = format.getCodec();
if (container == null || codec == null) {
throw new STTException("Missing audio stream info");
}
Long frequency = format.getFrequency();
Integer bitDepth = format.getBitDepth();
switch(container) {
case AudioFormat.CONTAINER_WAVE:
if (AudioFormat.CODEC_PCM_SIGNED.equals(codec)) {
if (bitDepth == null || bitDepth != 16) {
return "audio/wav";
}
// rate is a required parameter for this type
if (frequency == null) {
return null;
}
StringBuilder contentTypeL16 = new StringBuilder(HttpMediaType.AUDIO_PCM).append(";rate=").append(frequency);
// // those are optional
Integer channels = format.getChannels();
if (channels != null) {
contentTypeL16.append(";channels=").append(channels);
}
Boolean bigEndian = format.isBigEndian();
if (bigEndian != null) {
contentTypeL16.append(";").append(bigEndian ? "endianness=big-endian" : "endianness=little-endian");
}
return contentTypeL16.toString();
}
case AudioFormat.CONTAINER_OGG:
switch(codec) {
case AudioFormat.CODEC_VORBIS:
return "audio/ogg;codecs=vorbis";
case "OPUS":
return "audio/ogg;codecs=opus";
}
break;
case AudioFormat.CONTAINER_NONE:
if (AudioFormat.CODEC_MP3.equals(codec)) {
return "audio/mp3";
}
break;
}
return null;
}
Aggregations