From 9717b7a107b07a6d0777f9101c57b73d157f62b7 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Fri, 28 Jan 2022 09:02:21 +0100 Subject: [PATCH] [googlestt] Adjust service label for consistency with TTS services. (#12134) Documentation enhanced to explain how to setup the default STT. Signed-off-by: Laurent Garnier --- bundles/org.openhab.voice.googlestt/README.md | 14 +++++++ .../internal/GoogleSTTConstants.java | 2 +- .../googlestt/internal/GoogleSTTService.java | 40 +++++++++++++++---- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.voice.googlestt/README.md b/bundles/org.openhab.voice.googlestt/README.md index 6868ffe4ce046..d2d2a41246dbf 100644 --- a/bundles/org.openhab.voice.googlestt/README.md +++ b/bundles/org.openhab.voice.googlestt/README.md @@ -60,3 +60,17 @@ org.openhab.voice.googlestt:refreshSupportedLocales=false org.openhab.voice.googlestt:noResultsMessage="Sorry, I didn't understand you" org.openhab.voice.googlestt:errorMessage="Sorry, something went wrong" ``` + +### Default Speech-to-Text Configuration + +You can setup your preferred default Speech-to-Text in the UI: + +* Go to **Settings**. +* Edit **System Services - Voice**. +* Set **Google Cloud** as **Speech-to-Text**. + +In case you would like to setup these settings via a text file, you can edit the file `runtime.cfg` in `$OPENHAB_ROOT/conf/services` and set the following entries: + +``` +org.openhab.voice:defaultSTT=googlestt +``` diff --git a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTConstants.java b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTConstants.java index aa9dd6e54f581..194b3fd5ea26a 100644 --- a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTConstants.java +++ b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTConstants.java @@ -25,7 +25,7 @@ public class GoogleSTTConstants { /** * Service name */ - public static final String SERVICE_NAME = "Google Cloud Speech-to-Text"; + public static final String SERVICE_NAME = "Google Cloud"; /** * Service id */ diff --git a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java index a2f7bbe74378b..8198f1850819b 100644 --- a/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java +++ b/bundles/org.openhab.voice.googlestt/src/main/java/org/openhab/voice/googlestt/internal/GoogleSTTService.java @@ -15,7 +15,12 @@ import static org.openhab.voice.googlestt.internal.GoogleSTTConstants.*; import java.io.IOException; -import java.util.*; +import java.util.Comparator; +import java.util.Dictionary; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicBoolean; @@ -25,11 +30,21 @@ import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioStream; -import org.openhab.core.auth.client.oauth2.*; +import org.openhab.core.auth.client.oauth2.AccessTokenResponse; +import org.openhab.core.auth.client.oauth2.OAuthClientService; +import org.openhab.core.auth.client.oauth2.OAuthException; +import org.openhab.core.auth.client.oauth2.OAuthFactory; +import org.openhab.core.auth.client.oauth2.OAuthResponseException; import org.openhab.core.common.ThreadPoolManager; import org.openhab.core.config.core.ConfigurableService; import org.openhab.core.config.core.Configuration; -import org.openhab.core.voice.*; +import org.openhab.core.voice.STTListener; +import org.openhab.core.voice.STTService; +import org.openhab.core.voice.STTServiceHandle; +import org.openhab.core.voice.SpeechRecognitionErrorEvent; +import org.openhab.core.voice.SpeechRecognitionEvent; +import org.openhab.core.voice.SpeechStartEvent; +import org.openhab.core.voice.SpeechStopEvent; import org.osgi.framework.Constants; import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.component.annotations.Activate; @@ -45,7 +60,14 @@ import com.google.auth.Credentials; import com.google.auth.oauth2.AccessToken; import com.google.auth.oauth2.OAuth2Credentials; -import com.google.cloud.speech.v1.*; +import com.google.cloud.speech.v1.RecognitionConfig; +import com.google.cloud.speech.v1.SpeechClient; +import com.google.cloud.speech.v1.SpeechRecognitionAlternative; +import com.google.cloud.speech.v1.SpeechSettings; +import com.google.cloud.speech.v1.StreamingRecognitionConfig; +import com.google.cloud.speech.v1.StreamingRecognitionResult; +import com.google.cloud.speech.v1.StreamingRecognizeRequest; +import com.google.cloud.speech.v1.StreamingRecognizeResponse; import com.google.protobuf.ByteString; import io.grpc.LoadBalancerRegistry; @@ -58,8 +80,8 @@ */ @NonNullByDefault @Component(configurationPid = SERVICE_PID, property = Constants.SERVICE_PID + "=" + SERVICE_PID) -@ConfigurableService(category = SERVICE_CATEGORY, label = SERVICE_NAME, description_uri = SERVICE_CATEGORY + ":" - + SERVICE_ID) +@ConfigurableService(category = SERVICE_CATEGORY, label = SERVICE_NAME + + " Speech-to-Text", description_uri = SERVICE_CATEGORY + ":" + SERVICE_ID) public class GoogleSTTService implements STTService { private static final String GCP_AUTH_URI = "https://accounts.google.com/o/oauth2/auth"; @@ -318,11 +340,13 @@ public TranscriptionListener(STTListener sttListener, GoogleSTTConfiguration con this.completeListener = completeListener; } + @Override public void onStart(@Nullable StreamController controller) { sttListener.sttEventReceived(new SpeechStartEvent()); lastInputTime = System.currentTimeMillis(); } + @Override public void onResponse(StreamingRecognizeResponse response) { lastInputTime = System.currentTimeMillis(); List results = response.getResultsList(); @@ -354,9 +378,10 @@ public void onResponse(StreamingRecognizeResponse response) { }); } + @Override public void onComplete() { sttListener.sttEventReceived(new SpeechStopEvent()); - float averageConfidence = confidenceSum / (float) responseCount; + float averageConfidence = confidenceSum / responseCount; String transcript = transcriptBuilder.toString(); if (!transcript.isBlank()) { sttListener.sttEventReceived(new SpeechRecognitionEvent(transcript, averageConfidence)); @@ -369,6 +394,7 @@ public void onComplete() { } } + @Override public void onError(@Nullable Throwable t) { logger.warn("Recognition error: ", t); completeListener.accept(t);