forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfiniteStreamRecognizeSoftHandover.java
414 lines (359 loc) · 16.4 KB
/
InfiniteStreamRecognizeSoftHandover.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.speech;
// [START speech_transcribe_infinite_streaming_soft_handover]
import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
import com.google.cloud.speech.v1p1beta1.SpeechClient;
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig;
import com.google.cloud.speech.v1p1beta1.StreamingRecognitionResult;
import com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest;
import com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse;
import com.google.protobuf.ByteString;
import com.google.protobuf.Duration;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.TargetDataLine;
/*
* This class demonstrates how to perform infinite streaming speech recognition using the
* StreamingRecognize functionality of the Speech API.
* This class is almost identical to the InfiniteStreamRecognize.java class, except that it
* demonstrates how to perform "soft handover" between two streams.
* A "soft handover" is making a new stream before breaking the old stream. As against a "hard
* handover" where you break the old stream before making the new stream.
* This is useful in situations where you want to perform speech recognition on a continuous audio
* input, but need to periodically restart the stream to avoid exceeding the maximum allowed
* continuous streaming duration of 30 seconds.
* This class uses two streams, STREAM1 and STREAM2, and alternates between them.
* When one stream is active, the other stream is used to buffer audio input.
* When the active stream is stopped, the buffered audio input is used to create a new stream.
* This class also demonstrates how to align the transcript of the previous stream with the
* transcript of the current stream. This is useful in situations where you want to perform
* continuous speech recognition on a continuous audio input, but need to restart the stream
*/
public class InfiniteStreamRecognizeSoftHandOver {
private static final int STREAMING_LIMIT = 30000; // resetting 30 seconds to test
enum Stream {
STREAM1,
STREAM2;
}
public static final String RED = "\033[0;31m";
public static final String GREEN = "\033[0;32m";
public static final String YELLOW = "\033[0;33m";
// Creating shared object
private static volatile BlockingQueue<byte[]> sharedQueue = new LinkedBlockingQueue<byte[]>();
private static TargetDataLine targetDataLine;
private static int BYTES_PER_BUFFER = 6400; // buffer size in bytes
private static int restartCounter = 0;
private static ArrayList<ByteString> audioInput = new ArrayList<ByteString>();
private static int resultEndTimeInMS = 0;
private static boolean newStream = false;
private static double bridgingOffset = 0;
private static volatile Stream activeStream = Stream.STREAM1;
private static ByteString tempByteString;
private static Map<String, StreamingRecognitionResult> streamWiseLatestResults =
new HashMap<String, StreamingRecognitionResult>();
public static void main(String... args) {
InfiniteStreamRecognizeOptions options = InfiniteStreamRecognizeOptions.fromFlags(args);
if (options == null) {
// Could not parse.
System.out.println("Failed to parse options.");
System.exit(1);
}
try {
infiniteStreamingRecognize(options.langCode);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
}
public static String convertMillisToDate(double milliSeconds) {
long millis = (long) milliSeconds;
DecimalFormat format = new DecimalFormat();
format.setMinimumIntegerDigits(2);
return String.format(
"%s:%s /",
format.format(TimeUnit.MILLISECONDS.toMinutes(millis)),
format.format(
TimeUnit.MILLISECONDS.toSeconds(millis)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
}
private static boolean computeIntersectionRatio(
String previousTranscript, String currentTranscript) { // Compare the
// transcripts
// for
// alignment
int commonWordCount = 0;
String[] previousWords = previousTranscript.split("\\s+");
String[] currentWords = currentTranscript.split("\\s+");
// Compute word intersection ratio between previous and current by reverse traversing the arrays
for (int i = previousWords.length - 1; i >= 0; i--) {
for (int j = currentWords.length - 1; j >= 0; j--) {
if (previousWords[i].equalsIgnoreCase(currentWords[j])) {
commonWordCount++;
// This is a simplistic alignment algorithm which works ok.
// However, real world use cases may require more sophisticated algorithms
if (commonWordCount >= 3) {
return true;
}
}
}
}
return false;
}
/** Performs infinite streaming speech recognition */
public static void infiniteStreamingRecognize(String languageCode) throws Exception {
// Microphone Input buffering
class MicBuffer implements Runnable {
@Override
public void run() {
System.out.println(YELLOW);
System.out.println("Start speaking...Press Ctrl-C to stop");
targetDataLine.start();
byte[] data = new byte[BYTES_PER_BUFFER];
while (targetDataLine.isOpen()) {
try {
int numBytesRead = targetDataLine.read(data, 0, data.length);
if ((numBytesRead <= 0) && (targetDataLine.isOpen())) {
continue;
}
sharedQueue.put(data.clone());
} catch (InterruptedException e) {
System.out.println("Microphone input buffering interrupted : " + e.getMessage());
}
}
}
}
// Creating microphone input buffer thread
MicBuffer micrunnable = new MicBuffer();
Thread micThread = new Thread(micrunnable);
// one for each stream, just for clarity. Can be optimized.
ResponseObserver<StreamingRecognizeResponse> responseObserver1 = null;
ResponseObserver<StreamingRecognizeResponse> responseObserver2 = null;
try (SpeechClient client = SpeechClient.create()) {
// Active stream is used to send data.
ClientStream<StreamingRecognizeRequest> stream1 = null;
ClientStream<StreamingRecognizeRequest> stream2 = null;
responseObserver1 =
new ResponseObserver<StreamingRecognizeResponse>() {
ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();
public void onStart(StreamController controller) {}
public void onResponse(StreamingRecognizeResponse response) {
responses.add(response);
if (response.getResults(0).getIsFinal()) { // we only compare final and finals has
// only one results
streamWiseLatestResults.put("stream1", response.getResults(0));
}
StreamingRecognitionResult result = response.getResultsList().get(0);
Duration resultEndTime = result.getResultEndTime();
resultEndTimeInMS =
(int)
((resultEndTime.getSeconds() * 1000) + (resultEndTime.getNanos() / 1000000));
double correctedTime =
resultEndTimeInMS - bridgingOffset + (STREAMING_LIMIT * restartCounter);
if (activeStream == Stream.STREAM1) {
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
if (result.getIsFinal()) {
System.out.print(GREEN);
System.out.print("\033[2K\r");
System.out.printf(
"%s: %s [confidence: %.2f] [stream: %s]\n",
convertMillisToDate(correctedTime),
alternative.getTranscript(),
alternative.getConfidence(),
"stream1");
} else {
System.out.print(RED);
System.out.print("\033[2K\r");
System.out.printf(
"%s: %s", convertMillisToDate(correctedTime), alternative.getTranscript());
}
}
}
public void onComplete() {}
@Override
public void onError(Throwable t) {}
};
stream1 = client.streamingRecognizeCallable().splitCall(responseObserver1);
responseObserver2 =
new ResponseObserver<StreamingRecognizeResponse>() {
ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();
public void onStart(StreamController controller) {}
public void onResponse(StreamingRecognizeResponse response) {
responses.add(response);
if (response.getResults(0).getIsFinal()) { // we only compare final and finals has
// only one results
streamWiseLatestResults.put("stream2", response.getResults(0));
}
StreamingRecognitionResult result = response.getResultsList().get(0);
Duration resultEndTime = result.getResultEndTime();
resultEndTimeInMS =
(int)
((resultEndTime.getSeconds() * 1000) + (resultEndTime.getNanos() / 1000000));
double correctedTime =
resultEndTimeInMS - bridgingOffset + (STREAMING_LIMIT * restartCounter);
if (activeStream == Stream.STREAM2) {
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
if (result.getIsFinal()) {
System.out.print(GREEN);
System.out.print("\033[2K\r");
System.out.printf(
"%s: %s [confidence: %.2f] [stream: %s]\n",
convertMillisToDate(correctedTime),
alternative.getTranscript(),
alternative.getConfidence(),
"stream2");
} else {
System.out.print(RED);
System.out.print("\033[2K\r");
System.out.printf(
"%s: %s", convertMillisToDate(correctedTime), alternative.getTranscript());
}
}
}
public void onComplete() {}
public void onError(Throwable t) {}
};
RecognitionConfig recognitionConfig =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setLanguageCode(languageCode)
.setSampleRateHertz(16000)
.build();
StreamingRecognitionConfig streamingRecognitionConfig =
StreamingRecognitionConfig.newBuilder()
.setConfig(recognitionConfig)
.setInterimResults(true)
.build();
StreamingRecognizeRequest request =
StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(streamingRecognitionConfig)
.build(); // The first request in a streaming call has to be a config
stream1.send(request);
try {
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed:
// true,
// bigEndian: false
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info targetInfo =
new Info(
TargetDataLine.class,
audioFormat); // Set the system information to read from the microphone audio
// stream
if (!AudioSystem.isLineSupported(targetInfo)) {
System.out.println("Microphone not supported");
System.exit(0);
}
// Target data line captures the audio stream the microphone produces.
targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
targetDataLine.open(audioFormat);
micThread.start();
long startTime = System.currentTimeMillis();
AtomicBoolean isAligned = new AtomicBoolean(false);
while (true) {
long estimatedTime = System.currentTimeMillis() - startTime;
if (estimatedTime >= STREAMING_LIMIT) {
// reset started, bring passive stream to life!
restartCounter++;
newStream = true;
request =
StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(streamingRecognitionConfig)
.build();
if (activeStream
== Stream.STREAM2) { // meaning stream2 is active, so prepare stream 1 for reset
stream1 = client.streamingRecognizeCallable().splitCall(responseObserver1);
while (!stream1.isSendReady()) {
Thread.sleep(5);
}
stream1.send(request);
} else { // meaning stream1 is active, so prepare stream 2 for reset
stream2 = client.streamingRecognizeCallable().splitCall(responseObserver2);
while (!stream2.isSendReady()) {
Thread.sleep(5);
}
stream2.send(request);
}
System.out.println(YELLOW);
System.out.printf("%d: SOFT RESTARTING REQUEST\n", restartCounter * STREAMING_LIMIT);
// reset startTime
startTime = System.currentTimeMillis();
} else {
if (newStream) {
// we're here, that means the second stream has been opened now
if (!isAligned.get()) {
// now we're comparing for aligment
// Send audio to two streams to check for alignment:
request =
StreamingRecognizeRequest.newBuilder().setAudioContent(tempByteString).build();
stream1.send(request);
stream2.send(request);
if (streamWiseLatestResults.size() > 0) {
// we have results to compare!
StreamingRecognitionResult result1 = streamWiseLatestResults.get("stream1");
StreamingRecognitionResult result2 = streamWiseLatestResults.get("stream2");
if (result1 != null && result2 != null) {
isAligned.set(
computeIntersectionRatio(
result1.getAlternatives(0).getTranscript(),
result2.getAlternatives(0).getTranscript()));
}
}
} else {
newStream = false;
isAligned.set(false);
// toggle streams
if (activeStream == Stream.STREAM1) {
activeStream = Stream.STREAM2;
stream1.closeSend();
stream1 = null;
} else {
activeStream = Stream.STREAM1;
stream2.closeSend();
stream2 = null;
}
}
}
tempByteString = ByteString.copyFrom(sharedQueue.take());
request =
StreamingRecognizeRequest.newBuilder().setAudioContent(tempByteString).build();
audioInput.add(tempByteString);
if (activeStream == Stream.STREAM1) {
stream1.send(request);
} else {
stream2.send(request);
}
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
}
// [END speech_transcribe_infinite_streaming_soft_handover]