16
16
17
17
package com .examples .cloud .speech ;
18
18
19
+ import static org .apache .log4j .ConsoleAppender .SYSTEM_OUT ;
20
+
19
21
import com .google .cloud .speech .v1beta1 .RecognitionConfig ;
20
22
import com .google .cloud .speech .v1beta1 .RecognitionConfig .AudioEncoding ;
21
23
import com .google .cloud .speech .v1beta1 .SpeechGrpc ;
26
28
import com .google .protobuf .TextFormat ;
27
29
28
30
import io .grpc .ManagedChannel ;
29
- import io .grpc .Status ;
30
31
import io .grpc .stub .StreamObserver ;
31
32
32
33
import org .apache .commons .cli .CommandLine ;
35
36
import org .apache .commons .cli .OptionBuilder ;
36
37
import org .apache .commons .cli .Options ;
37
38
import org .apache .commons .cli .ParseException ;
39
+ import org .apache .log4j .ConsoleAppender ;
40
+ import org .apache .log4j .Level ;
41
+ import org .apache .log4j .Logger ;
42
+ import org .apache .log4j .SimpleLayout ;
38
43
39
44
import java .io .File ;
40
45
import java .io .FileInputStream ;
43
48
import java .util .List ;
44
49
import java .util .concurrent .CountDownLatch ;
45
50
import java .util .concurrent .TimeUnit ;
46
- import java .util .logging .Level ;
47
- import java .util .logging .Logger ;
51
+
48
52
49
53
/**
50
54
* Client that sends streaming audio to Speech.Recognize and returns streaming transcript.
@@ -60,6 +64,9 @@ public class StreamingRecognizeClient {
60
64
61
65
private final SpeechGrpc .SpeechStub speechClient ;
62
66
67
+ private static final int BYTES_PER_BUFFER = 3200 ; //buffer size in bytes
68
+ private static final int BYTES_PER_SAMPLE = 2 ; //bytes per sample for LINEAR16
69
+
63
70
private static final List <String > OAUTH2_SCOPES =
64
71
Arrays .asList ("https://www.googleapis.com/auth/cloud-platform" );
65
72
@@ -73,6 +80,13 @@ public StreamingRecognizeClient(ManagedChannel channel, String file, int samplin
73
80
this .channel = channel ;
74
81
75
82
speechClient = SpeechGrpc .newStub (channel );
83
+
84
+ //Send log4j logs to Console
85
+ //If you are going to run this on GCE, you might wish to integrate with gcloud-java logging.
86
+ //See https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/README.md#stackdriver-logging-alpha
87
+
88
+ ConsoleAppender appender = new ConsoleAppender (new SimpleLayout (), SYSTEM_OUT );
89
+ logger .addAppender (appender );
76
90
}
77
91
78
92
public void shutdown () throws InterruptedException {
@@ -91,8 +105,7 @@ public void onNext(StreamingRecognizeResponse response) {
91
105
92
106
@ Override
93
107
public void onError (Throwable error ) {
94
- Status status = Status .fromThrowable (error );
95
- logger .log (Level .WARNING , "recognize failed: {0}" , status );
108
+ logger .log (Level .WARN , "recognize failed: {0}" , error );
96
109
finishLatch .countDown ();
97
110
}
98
111
@@ -127,9 +140,12 @@ public void onCompleted() {
127
140
// Open audio file. Read and send sequential buffers of audio as additional RecognizeRequests.
128
141
FileInputStream in = new FileInputStream (new File (file ));
129
142
// For LINEAR16 at 16000 Hz sample rate, 3200 bytes corresponds to 100 milliseconds of audio.
130
- byte [] buffer = new byte [3200 ];
143
+ byte [] buffer = new byte [BYTES_PER_BUFFER ];
131
144
int bytesRead ;
132
145
int totalBytes = 0 ;
146
+ int samplesPerBuffer = BYTES_PER_BUFFER / BYTES_PER_SAMPLE ;
147
+ int samplesPerMillis = samplingRate / 1000 ;
148
+
133
149
while ((bytesRead = in .read (buffer )) != -1 ) {
134
150
totalBytes += bytesRead ;
135
151
StreamingRecognizeRequest request =
@@ -138,8 +154,7 @@ public void onCompleted() {
138
154
.build ();
139
155
requestObserver .onNext (request );
140
156
// To simulate real-time audio, sleep after sending each audio buffer.
141
- // For 16000 Hz sample rate, sleep 100 milliseconds.
142
- Thread .sleep (samplingRate / 160 );
157
+ Thread .sleep (samplesPerBuffer / samplesPerMillis );
143
158
}
144
159
logger .info ("Sent " + totalBytes + " bytes from audio file: " + file );
145
160
} catch (RuntimeException e ) {
0 commit comments