Search in sources :

Example 6 with StreamingRecognitionConfig

use of com.google.cloud.speech.v1.StreamingRecognitionConfig in project java-speech by googleapis.

the class Recognize method streamingTranscribeWithAutomaticPunctuation.

// [END speech_transcribe_auto_punctuation]
// [START speech_stream_recognize_punctuation]
/**
 * Performs streaming speech recognition on raw PCM audio data.
 *
 * @param fileName the path to a PCM audio file to transcribe.
 */
public static void streamingTranscribeWithAutomaticPunctuation(String fileName) throws Exception {
    Path path = Paths.get(fileName);
    byte[] data = Files.readAllBytes(path);
    // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
    try (SpeechClient speech = SpeechClient.create()) {
        // Configure request with local raw PCM audio
        RecognitionConfig recConfig = RecognitionConfig.newBuilder().setEncoding(AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000).setEnableAutomaticPunctuation(true).build();
        // Build the streaming config with the audio config
        StreamingRecognitionConfig config = StreamingRecognitionConfig.newBuilder().setConfig(recConfig).build();
        class ResponseApiStreamingObserver<T> implements ApiStreamObserver<T> {

            private final SettableFuture<List<T>> future = SettableFuture.create();

            private final List<T> messages = new java.util.ArrayList<T>();

            @Override
            public void onNext(T message) {
                messages.add(message);
            }

            @Override
            public void onError(Throwable t) {
                future.setException(t);
            }

            @Override
            public void onCompleted() {
                future.set(messages);
            }

            // Returns the SettableFuture object to get received messages / exceptions.
            public SettableFuture<List<T>> future() {
                return future;
            }
        }
        ResponseApiStreamingObserver<StreamingRecognizeResponse> responseObserver = new ResponseApiStreamingObserver<>();
        BidiStreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = speech.streamingRecognizeCallable();
        ApiStreamObserver<StreamingRecognizeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
        // The first request must **only** contain the audio configuration:
        requestObserver.onNext(StreamingRecognizeRequest.newBuilder().setStreamingConfig(config).build());
        // Subsequent requests must **only** contain the audio data.
        requestObserver.onNext(StreamingRecognizeRequest.newBuilder().setAudioContent(ByteString.copyFrom(data)).build());
        // Mark transmission as completed after sending the data.
        requestObserver.onCompleted();
        List<StreamingRecognizeResponse> responses = responseObserver.future().get();
        for (StreamingRecognizeResponse response : responses) {
            // For streaming recognize, the results list has one is_final result (if available) followed
            // by a number of in-progress results (if iterim_results is true) for subsequent utterances.
            // Just print the first result here.
            StreamingRecognitionResult result = response.getResultsList().get(0);
            // There can be several alternative transcripts for a given chunk of speech. Just use the
            // first (most likely) one here.
            SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
            System.out.printf("Transcript : %s\n", alternative.getTranscript());
        }
    }
}
Also used : Path(java.nio.file.Path) SettableFuture(com.google.common.util.concurrent.SettableFuture) StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig) SpeechRecognitionAlternative(com.google.cloud.speech.v1.SpeechRecognitionAlternative) StreamingRecognitionResult(com.google.cloud.speech.v1.StreamingRecognitionResult) StreamingRecognizeRequest(com.google.cloud.speech.v1.StreamingRecognizeRequest) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig) SpeechClient(com.google.cloud.speech.v1.SpeechClient) ArrayList(java.util.ArrayList) List(java.util.List) StreamingRecognizeResponse(com.google.cloud.speech.v1.StreamingRecognizeResponse)

Example 7 with StreamingRecognitionConfig

use of com.google.cloud.speech.v1.StreamingRecognitionConfig in project openhab-addons by openhab.

the class GoogleSTTService method sendStreamConfig.

private void sendStreamConfig(ClientStream<StreamingRecognizeRequest> clientStream, RecognitionConfig.AudioEncoding encoding, int sampleRate, int channels, Locale locale) {
    RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder().setEncoding(encoding).setAudioChannelCount(channels).setLanguageCode(locale.toLanguageTag()).setSampleRateHertz(sampleRate).build();
    StreamingRecognitionConfig streamingRecognitionConfig = StreamingRecognitionConfig.newBuilder().setConfig(recognitionConfig).setInterimResults(false).setSingleUtterance(config.singleUtteranceMode).build();
    clientStream.send(StreamingRecognizeRequest.newBuilder().setStreamingConfig(streamingRecognitionConfig).build());
}
Also used : StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig) RecognitionConfig(com.google.cloud.speech.v1.RecognitionConfig) StreamingRecognitionConfig(com.google.cloud.speech.v1.StreamingRecognitionConfig)

Aggregations

StreamingRecognitionConfig (com.google.cloud.speech.v1.StreamingRecognitionConfig)5 RecognitionConfig (com.google.cloud.speech.v1.RecognitionConfig)4 StreamingRecognizeRequest (com.google.cloud.speech.v1.StreamingRecognizeRequest)4 StreamingRecognizeResponse (com.google.cloud.speech.v1.StreamingRecognizeResponse)4 ArrayList (java.util.ArrayList)4 ApiStreamObserver (com.google.api.gax.rpc.ApiStreamObserver)3 SpeechClient (com.google.cloud.speech.v1.SpeechClient)3 SpeechRecognitionAlternative (com.google.cloud.speech.v1.SpeechRecognitionAlternative)3 StreamingRecognitionResult (com.google.cloud.speech.v1.StreamingRecognitionResult)3 SettableFuture (com.google.common.util.concurrent.SettableFuture)3 Path (java.nio.file.Path)3 List (java.util.List)3 StreamController (com.google.api.gax.rpc.StreamController)2 RecognitionConfig (com.google.cloud.speech.v1p1beta1.RecognitionConfig)2 SpeechClient (com.google.cloud.speech.v1p1beta1.SpeechClient)2 SpeechRecognitionAlternative (com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative)2 StreamingRecognitionConfig (com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig)2 StreamingRecognitionResult (com.google.cloud.speech.v1p1beta1.StreamingRecognitionResult)2 StreamingRecognizeRequest (com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest)2 StreamingRecognizeResponse (com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse)2