Skip to content

Commit 0392815

Browse files
authored
refactored and added tags to infinite speech streaming sample (#1605)
1 parent 3cd29aa commit 0392815

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 Google LLC
2+
* Copyright 2019 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package com.example.speech;
1818

19-
// [START speech_transcribe_infinite_streaming]
19+
// [START speech_transcribe_infinite_streaming_imports]
2020

2121
import com.google.api.gax.rpc.ClientStream;
2222
import com.google.api.gax.rpc.ResponseObserver;
@@ -43,13 +43,13 @@
4343
import javax.sound.sampled.DataLine.Info;
4444
import javax.sound.sampled.TargetDataLine;
4545

46+
// [END speech_transcribe_infinite_streaming_imports]
47+
4648
public class InfiniteStreamRecognize {
4749

48-
private static final int STREAMING_LIMIT = 290000; // ~5 minutes
50+
// [START speech_transcribe_infinite_streaming_globals]
4951

50-
public static final String RED = "\033[0;31m";
51-
public static final String GREEN = "\033[0;32m";
52-
public static final String YELLOW = "\033[0;33m";
52+
private static final int STREAMING_LIMIT = 290000; // ~5 minutes
5353

5454
// Creating shared object
5555
private static volatile BlockingQueue<byte[]> sharedQueue = new LinkedBlockingQueue();
@@ -68,6 +68,19 @@ public class InfiniteStreamRecognize {
6868
private static StreamController referenceToStreamController;
6969
private static ByteString tempByteString;
7070

71+
// [END speech_transcribe_infinite_streaming_globals]
72+
73+
public static String convertMillisToDate(double milliSeconds) {
74+
long millis = (long) milliSeconds;
75+
DecimalFormat format = new DecimalFormat();
76+
format.setMinimumIntegerDigits(2);
77+
return String.format("%s:%s /",
78+
format.format(TimeUnit.MILLISECONDS.toMinutes(millis)),
79+
format.format(TimeUnit.MILLISECONDS.toSeconds(millis)
80+
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)))
81+
);
82+
}
83+
// [START speech_transcribe_infinite_streaming_main]
7184
public static void main(String... args) {
7285
InfiniteStreamRecognizeOptions options = InfiniteStreamRecognizeOptions.fromFlags(args);
7386
if (options == null) {
@@ -82,17 +95,7 @@ public static void main(String... args) {
8295
System.out.println("Exception caught: " + e);
8396
}
8497
}
85-
86-
public static String convertMillisToDate(double milliSeconds) {
87-
long millis = (long) milliSeconds;
88-
DecimalFormat format = new DecimalFormat();
89-
format.setMinimumIntegerDigits(2);
90-
return String.format("%s:%s /",
91-
format.format(TimeUnit.MILLISECONDS.toMinutes(millis)),
92-
format.format(TimeUnit.MILLISECONDS.toSeconds(millis)
93-
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)))
94-
);
95-
}
98+
// [END speech_transcribe_infinite_streaming_main]
9699

97100
/** Performs infinite streaming speech recognition */
98101
public static void infiniteStreamingRecognize(String languageCode) throws Exception {
@@ -102,7 +105,6 @@ class MicBuffer implements Runnable {
102105

103106
@Override
104107
public void run() {
105-
System.out.println(YELLOW);
106108
System.out.println("Start speaking...Press Ctrl-C to stop");
107109
targetDataLine.start();
108110
byte[] data = new byte[BYTES_PER_BUFFER];
@@ -134,7 +136,8 @@ public void run() {
134136
public void onStart(StreamController controller) {
135137
referenceToStreamController = controller;
136138
}
137-
139+
140+
// [START speech_transcribe_infinite_streaming_output]
138141
public void onResponse(StreamingRecognizeResponse response) {
139142
responses.add(response);
140143
StreamingRecognitionResult result = response.getResultsList().get(0);
@@ -146,8 +149,6 @@ public void onResponse(StreamingRecognizeResponse response) {
146149

147150
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
148151
if (result.getIsFinal()) {
149-
System.out.print(GREEN);
150-
System.out.print("\033[2K\r");
151152
System.out.printf("%s: %s [confidence: %.2f]\n",
152153
convertMillisToDate(correctedTime),
153154
alternative.getTranscript(),
@@ -156,15 +157,13 @@ public void onResponse(StreamingRecognizeResponse response) {
156157
isFinalEndTime = resultEndTimeInMS;
157158
lastTranscriptWasFinal = true;
158159
} else {
159-
System.out.print(RED);
160-
System.out.print("\033[2K\r");
161160
System.out.printf("%s: %s", convertMillisToDate(correctedTime),
162161
alternative.getTranscript()
163162
);
164163
lastTranscriptWasFinal = false;
165164
}
166165
}
167-
166+
// [END speech_transcribe_infinite_streaming_output]
168167
public void onComplete() {
169168
}
170169

@@ -213,6 +212,8 @@ public void onError(Throwable t) {
213212
micThread.start();
214213

215214
long startTime = System.currentTimeMillis();
215+
216+
// [START speech_transcribe_infinite_streaming_generator]
216217

217218
while (true) {
218219

@@ -247,7 +248,6 @@ public void onError(Throwable t) {
247248
.setStreamingConfig(streamingRecognitionConfig)
248249
.build();
249250

250-
System.out.println(YELLOW);
251251
System.out.printf("%d: RESTARTING REQUEST\n", restartCounter * STREAMING_LIMIT);
252252

253253
startTime = System.currentTimeMillis();
@@ -296,7 +296,7 @@ public void onError(Throwable t) {
296296
audioInput.add(tempByteString);
297297

298298
}
299-
299+
// [END speech_transcribe_infinite_streaming_generator]
300300
clientStream.send(request);
301301
}
302302
} catch (Exception e) {
@@ -306,4 +306,3 @@ public void onError(Throwable t) {
306306
}
307307

308308
}
309-
// [END speech_transcribe_infinite_streaming]

0 commit comments

Comments
 (0)