Search in sources :

Example 1 with STTException

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);
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) STTException(org.openhab.core.voice.STTException) IOException(java.io.IOException)

Example 2 with STTException

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) {
                    }
                }
            }
        }
    };
}
Also used : STTException(org.openhab.core.voice.STTException) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) AtomicReference(java.util.concurrent.atomic.AtomicReference) WebSocket(okhttp3.WebSocket) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SpeechRecognitionErrorEvent(org.openhab.core.voice.SpeechRecognitionErrorEvent) RecognizeWithWebsocketsOptions(com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions) SpeechToText(com.ibm.watson.speech_to_text.v1.SpeechToText) STTServiceHandle(org.openhab.core.voice.STTServiceHandle)

Example 3 with STTException

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()));
        }
    }
}
Also used : AudioStream(org.openhab.core.audio.AudioStream) KSErrorEvent(org.openhab.core.voice.KSErrorEvent) STTException(org.openhab.core.voice.STTException) AudioException(org.openhab.core.audio.AudioException) AudioFormat(org.openhab.core.audio.AudioFormat) KSpottedEvent(org.openhab.core.voice.KSpottedEvent) HashSet(java.util.HashSet)

Example 4 with STTException

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}", ""));
        }
    }
}
Also used : AudioStream(org.openhab.core.audio.AudioStream) STTException(org.openhab.core.voice.STTException) AudioException(org.openhab.core.audio.AudioException) AudioFormat(org.openhab.core.audio.AudioFormat) HashSet(java.util.HashSet)

Example 5 with STTException

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;
}
Also used : STTException(org.openhab.core.voice.STTException) AudioFormat(org.openhab.core.audio.AudioFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

STTException (org.openhab.core.voice.STTException)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AudioFormat (org.openhab.core.audio.AudioFormat)3 HashSet (java.util.HashSet)2 AudioException (org.openhab.core.audio.AudioException)2 AudioStream (org.openhab.core.audio.AudioStream)2 STTServiceHandle (org.openhab.core.voice.STTServiceHandle)2 SpeechRecognitionErrorEvent (org.openhab.core.voice.SpeechRecognitionErrorEvent)2 IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)1 SpeechToText (com.ibm.watson.speech_to_text.v1.SpeechToText)1 RecognizeWithWebsocketsOptions (com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions)1 IOException (java.io.IOException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 WebSocket (okhttp3.WebSocket)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 KSErrorEvent (org.openhab.core.voice.KSErrorEvent)1 KSpottedEvent (org.openhab.core.voice.KSpottedEvent)1 SpeechRecognitionEvent (org.openhab.core.voice.SpeechRecognitionEvent)1