|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.speech; |
| 18 | + |
| 19 | +// [START speech_transcribe_infinite_streaming] |
| 20 | +import com.google.api.gax.rpc.ClientStream; |
| 21 | +import com.google.api.gax.rpc.ResponseObserver; |
| 22 | +import com.google.api.gax.rpc.StreamController; |
| 23 | +import com.google.cloud.speech.v1.RecognitionConfig; |
| 24 | +import com.google.cloud.speech.v1.SpeechClient; |
| 25 | +import com.google.cloud.speech.v1.SpeechRecognitionAlternative; |
| 26 | +import com.google.cloud.speech.v1.StreamingRecognitionConfig; |
| 27 | +import com.google.cloud.speech.v1.StreamingRecognitionResult; |
| 28 | +import com.google.cloud.speech.v1.StreamingRecognizeRequest; |
| 29 | +import com.google.cloud.speech.v1.StreamingRecognizeResponse; |
| 30 | +import com.google.protobuf.ByteString; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.concurrent.BlockingQueue; |
| 33 | +import java.util.concurrent.LinkedBlockingQueue; |
| 34 | +import javax.sound.sampled.AudioFormat; |
| 35 | +import javax.sound.sampled.AudioSystem; |
| 36 | +import javax.sound.sampled.DataLine; |
| 37 | +import javax.sound.sampled.DataLine.Info; |
| 38 | +import javax.sound.sampled.TargetDataLine; |
| 39 | + |
| 40 | +public class InfiniteStreamRecognize { |
| 41 | + |
| 42 | + // Creating shared object |
| 43 | + private static volatile BlockingQueue<byte[]> sharedQueue = new LinkedBlockingQueue(); |
| 44 | + private static TargetDataLine targetDataLine; |
| 45 | + private static int BYTES_PER_BUFFER = 6400; // buffer size in bytes |
| 46 | + |
| 47 | + public static void main(String... args) { |
| 48 | + try { |
| 49 | + infiniteStreamingRecognize(); |
| 50 | + } catch (Exception e) { |
| 51 | + System.out.println("Exception caught: " + e); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** Performs infinite streaming speech recognition */ |
| 56 | + public static void infiniteStreamingRecognize() throws Exception { |
| 57 | + |
| 58 | + // Microphone Input buffering |
| 59 | + class MicBuffer implements Runnable { |
| 60 | + |
| 61 | + @Override |
| 62 | + public void run() { |
| 63 | + System.out.println("Start speaking...Press Ctrl-C to stop"); |
| 64 | + targetDataLine.start(); |
| 65 | + byte[] data = new byte[BYTES_PER_BUFFER]; |
| 66 | + while (targetDataLine.isOpen()) { |
| 67 | + try { |
| 68 | + int numBytesRead = targetDataLine.read(data, 0, data.length); |
| 69 | + if ((numBytesRead <= 0) && (targetDataLine.isOpen())) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + sharedQueue.put(data.clone()); |
| 73 | + } catch (InterruptedException e) { |
| 74 | + System.out.println("Microphone input buffering interrupted : " + e.getMessage()); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Creating microphone input buffer thread |
| 81 | + MicBuffer micrunnable = new MicBuffer(); |
| 82 | + Thread micThread = new Thread(micrunnable); |
| 83 | + ResponseObserver<StreamingRecognizeResponse> responseObserver = null; |
| 84 | + try (SpeechClient client = SpeechClient.create()) { |
| 85 | + ClientStream<StreamingRecognizeRequest> clientStream; |
| 86 | + responseObserver = |
| 87 | + new ResponseObserver<StreamingRecognizeResponse>() { |
| 88 | + |
| 89 | + ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>(); |
| 90 | + |
| 91 | + public void onStart(StreamController controller) {} |
| 92 | + |
| 93 | + public void onResponse(StreamingRecognizeResponse response) { |
| 94 | + responses.add(response); |
| 95 | + StreamingRecognitionResult result = response.getResultsList().get(0); |
| 96 | + // There can be several alternative transcripts for a given chunk of speech. Just |
| 97 | + // use the first (most likely) one here. |
| 98 | + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); |
| 99 | + System.out.printf("Transcript : %s\n", alternative.getTranscript()); |
| 100 | + } |
| 101 | + |
| 102 | + public void onComplete() { |
| 103 | + System.out.println("Done"); |
| 104 | + } |
| 105 | + |
| 106 | + public void onError(Throwable t) { |
| 107 | + System.out.println(t); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + clientStream = client.streamingRecognizeCallable().splitCall(responseObserver); |
| 112 | + |
| 113 | + RecognitionConfig recognitionConfig = |
| 114 | + RecognitionConfig.newBuilder() |
| 115 | + .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) |
| 116 | + .setLanguageCode("en-US") |
| 117 | + .setSampleRateHertz(16000) |
| 118 | + .build(); |
| 119 | + StreamingRecognitionConfig streamingRecognitionConfig = |
| 120 | + StreamingRecognitionConfig.newBuilder().setConfig(recognitionConfig).build(); |
| 121 | + |
| 122 | + StreamingRecognizeRequest request = |
| 123 | + StreamingRecognizeRequest.newBuilder() |
| 124 | + .setStreamingConfig(streamingRecognitionConfig) |
| 125 | + .build(); // The first request in a streaming call has to be a config |
| 126 | + |
| 127 | + clientStream.send(request); |
| 128 | + |
| 129 | + try { |
| 130 | + // SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true, |
| 131 | + // bigEndian: false |
| 132 | + AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false); |
| 133 | + DataLine.Info targetInfo = |
| 134 | + new Info( |
| 135 | + TargetDataLine.class, |
| 136 | + audioFormat); // Set the system information to read from the microphone audio |
| 137 | + // stream |
| 138 | + |
| 139 | + if (!AudioSystem.isLineSupported(targetInfo)) { |
| 140 | + System.out.println("Microphone not supported"); |
| 141 | + System.exit(0); |
| 142 | + } |
| 143 | + // Target data line captures the audio stream the microphone produces. |
| 144 | + targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo); |
| 145 | + targetDataLine.open(audioFormat); |
| 146 | + micThread.start(); |
| 147 | + |
| 148 | + long startTime = System.currentTimeMillis(); |
| 149 | + |
| 150 | + while (true) { |
| 151 | + |
| 152 | + long estimatedTime = System.currentTimeMillis() - startTime; |
| 153 | + |
| 154 | + if (estimatedTime >= 55000) { |
| 155 | + |
| 156 | + clientStream.closeSend(); |
| 157 | + clientStream = client.streamingRecognizeCallable().splitCall(responseObserver); |
| 158 | + |
| 159 | + request = |
| 160 | + StreamingRecognizeRequest.newBuilder() |
| 161 | + .setStreamingConfig(streamingRecognitionConfig) |
| 162 | + .build(); |
| 163 | + |
| 164 | + startTime = System.currentTimeMillis(); |
| 165 | + |
| 166 | + } else { |
| 167 | + request = |
| 168 | + StreamingRecognizeRequest.newBuilder() |
| 169 | + .setAudioContent(ByteString.copyFrom(sharedQueue.take())) |
| 170 | + .build(); |
| 171 | + } |
| 172 | + |
| 173 | + clientStream.send(request); |
| 174 | + } |
| 175 | + } catch (Exception e) { |
| 176 | + System.out.println(e); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +// [END speech_transcribe_infinite_streaming] |
0 commit comments