Skip to content

Commit

Permalink
[googlestt] Adjust service label for consistency with TTS services. (#…
Browse files Browse the repository at this point in the history
…12134)

Documentation enhanced to explain how to setup the default STT.

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored Jan 28, 2022
1 parent 7b3a1c9 commit 9717b7a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
14 changes: 14 additions & 0 deletions bundles/org.openhab.voice.googlestt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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<StreamingRecognitionResult> results = response.getResultsList();
Expand Down Expand Up @@ -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));
Expand All @@ -369,6 +394,7 @@ public void onComplete() {
}
}

@Override
public void onError(@Nullable Throwable t) {
logger.warn("Recognition error: ", t);
completeListener.accept(t);
Expand Down

0 comments on commit 9717b7a

Please sign in to comment.