1
1
/*
2
- * Copyright 2018 Google LLC
2
+ * Copyright 2019 Google LLC
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package com .example .speech ;
18
18
19
- // [START speech_transcribe_infinite_streaming ]
19
+ // [START speech_transcribe_infinite_streaming_imports ]
20
20
21
21
import com .google .api .gax .rpc .ClientStream ;
22
22
import com .google .api .gax .rpc .ResponseObserver ;
43
43
import javax .sound .sampled .DataLine .Info ;
44
44
import javax .sound .sampled .TargetDataLine ;
45
45
46
+ // [END speech_transcribe_infinite_streaming_imports]
47
+
46
48
public class InfiniteStreamRecognize {
47
49
48
- private static final int STREAMING_LIMIT = 290000 ; // ~5 minutes
50
+ // [START speech_transcribe_infinite_streaming_globals]
49
51
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
53
53
54
54
// Creating shared object
55
55
private static volatile BlockingQueue <byte []> sharedQueue = new LinkedBlockingQueue ();
@@ -68,6 +68,19 @@ public class InfiniteStreamRecognize {
68
68
private static StreamController referenceToStreamController ;
69
69
private static ByteString tempByteString ;
70
70
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]
71
84
public static void main (String ... args ) {
72
85
InfiniteStreamRecognizeOptions options = InfiniteStreamRecognizeOptions .fromFlags (args );
73
86
if (options == null ) {
@@ -82,17 +95,7 @@ public static void main(String... args) {
82
95
System .out .println ("Exception caught: " + e );
83
96
}
84
97
}
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]
96
99
97
100
/** Performs infinite streaming speech recognition */
98
101
public static void infiniteStreamingRecognize (String languageCode ) throws Exception {
@@ -102,7 +105,6 @@ class MicBuffer implements Runnable {
102
105
103
106
@ Override
104
107
public void run () {
105
- System .out .println (YELLOW );
106
108
System .out .println ("Start speaking...Press Ctrl-C to stop" );
107
109
targetDataLine .start ();
108
110
byte [] data = new byte [BYTES_PER_BUFFER ];
@@ -134,7 +136,8 @@ public void run() {
134
136
public void onStart (StreamController controller ) {
135
137
referenceToStreamController = controller ;
136
138
}
137
-
139
+
140
+ // [START speech_transcribe_infinite_streaming_output]
138
141
public void onResponse (StreamingRecognizeResponse response ) {
139
142
responses .add (response );
140
143
StreamingRecognitionResult result = response .getResultsList ().get (0 );
@@ -146,8 +149,6 @@ public void onResponse(StreamingRecognizeResponse response) {
146
149
147
150
SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
148
151
if (result .getIsFinal ()) {
149
- System .out .print (GREEN );
150
- System .out .print ("\033 [2K\r " );
151
152
System .out .printf ("%s: %s [confidence: %.2f]\n " ,
152
153
convertMillisToDate (correctedTime ),
153
154
alternative .getTranscript (),
@@ -156,15 +157,13 @@ public void onResponse(StreamingRecognizeResponse response) {
156
157
isFinalEndTime = resultEndTimeInMS ;
157
158
lastTranscriptWasFinal = true ;
158
159
} else {
159
- System .out .print (RED );
160
- System .out .print ("\033 [2K\r " );
161
160
System .out .printf ("%s: %s" , convertMillisToDate (correctedTime ),
162
161
alternative .getTranscript ()
163
162
);
164
163
lastTranscriptWasFinal = false ;
165
164
}
166
165
}
167
-
166
+ // [END speech_transcribe_infinite_streaming_output]
168
167
public void onComplete () {
169
168
}
170
169
@@ -213,6 +212,8 @@ public void onError(Throwable t) {
213
212
micThread .start ();
214
213
215
214
long startTime = System .currentTimeMillis ();
215
+
216
+ // [START speech_transcribe_infinite_streaming_generator]
216
217
217
218
while (true ) {
218
219
@@ -247,7 +248,6 @@ public void onError(Throwable t) {
247
248
.setStreamingConfig (streamingRecognitionConfig )
248
249
.build ();
249
250
250
- System .out .println (YELLOW );
251
251
System .out .printf ("%d: RESTARTING REQUEST\n " , restartCounter * STREAMING_LIMIT );
252
252
253
253
startTime = System .currentTimeMillis ();
@@ -296,7 +296,7 @@ public void onError(Throwable t) {
296
296
audioInput .add (tempByteString );
297
297
298
298
}
299
-
299
+ // [END speech_transcribe_infinite_streaming_generator]
300
300
clientStream .send (request );
301
301
}
302
302
} catch (Exception e ) {
@@ -306,4 +306,3 @@ public void onError(Throwable t) {
306
306
}
307
307
308
308
}
309
- // [END speech_transcribe_infinite_streaming]
0 commit comments