65
65
*/
66
66
public class AsyncRecognizeClient {
67
67
68
- private static final Logger logger =
69
- Logger .getLogger (AsyncRecognizeClient .class .getName ());
68
+ private static final Logger logger = Logger .getLogger (AsyncRecognizeClient .class .getName ());
70
69
71
70
private static final List <String > OAUTH2_SCOPES =
72
71
Arrays .asList ("https://www.googleapis.com/auth/cloud-platform" );
@@ -77,8 +76,8 @@ public class AsyncRecognizeClient {
77
76
private final int samplingRate ;
78
77
79
78
private final ManagedChannel channel ;
80
- private final SpeechGrpc .SpeechBlockingStub stub ;
81
- private final OperationsGrpc .OperationsBlockingStub statusStub ;
79
+ private final SpeechGrpc .SpeechBlockingStub speechClient ;
80
+ private final OperationsGrpc .OperationsBlockingStub statusClient ;
82
81
83
82
/**
84
83
* Construct client connecting to Cloud Speech server at {@code host:port}.
@@ -92,14 +91,15 @@ public AsyncRecognizeClient(String host, int port, URI input, int samplingRate)
92
91
93
92
GoogleCredentials creds = GoogleCredentials .getApplicationDefault ();
94
93
creds = creds .createScoped (OAUTH2_SCOPES );
95
- channel = NettyChannelBuilder .forAddress (host , port )
96
- .negotiationType (NegotiationType .TLS )
97
- .intercept (new ClientAuthInterceptor (creds , Executors .newSingleThreadExecutor ()))
98
- .build ();
99
- stub = SpeechGrpc .newBlockingStub (channel );
100
- statusStub = OperationsGrpc .newBlockingStub (channel );
101
-
102
- logger .info ("Created stub for " + host + ":" + port );
94
+ channel =
95
+ NettyChannelBuilder .forAddress (host , port )
96
+ .negotiationType (NegotiationType .TLS )
97
+ .intercept (new ClientAuthInterceptor (creds , Executors .newSingleThreadExecutor ()))
98
+ .build ();
99
+ speechClient = SpeechGrpc .newBlockingStub (channel );
100
+ statusClient = OperationsGrpc .newBlockingStub (channel );
101
+
102
+ logger .info ("Created speech clientfor " + host + ":" + port );
103
103
}
104
104
105
105
public void shutdown () throws InterruptedException {
@@ -116,23 +116,23 @@ public void recognize() {
116
116
return ;
117
117
}
118
118
logger .info ("Sending " + audio .getContent ().size () + " bytes from audio uri input: " + input );
119
- RecognitionConfig config = RecognitionConfig .newBuilder ()
120
- .setEncoding (AudioEncoding .LINEAR16 )
121
- .setSampleRate (samplingRate )
122
- .build ();
123
- AsyncRecognizeRequest request = AsyncRecognizeRequest .newBuilder ()
124
- .setConfig (config )
125
- .setAudio (audio )
126
- .build ();
119
+ RecognitionConfig config =
120
+ RecognitionConfig .newBuilder ()
121
+ .setEncoding (AudioEncoding .LINEAR16 )
122
+ .setSampleRate (samplingRate )
123
+ .build ();
124
+ AsyncRecognizeRequest request =
125
+ AsyncRecognizeRequest .newBuilder ().setConfig (config ).setAudio (audio ).build ();
127
126
128
127
Operation operation ;
129
128
Operation status ;
130
129
try {
131
- operation = stub .asyncRecognize (request );
130
+ operation = speechClient .asyncRecognize (request );
132
131
133
132
//Print the long running operation handle
134
- logger .log (Level .INFO , String .format ("Operation handle: %s, URI: %s" , operation .getName (),
135
- input .toString ()));
133
+ logger .log (
134
+ Level .INFO ,
135
+ String .format ("Operation handle: %s, URI: %s" , operation .getName (), input .toString ()));
136
136
} catch (StatusRuntimeException e ) {
137
137
logger .log (Level .WARNING , "RPC failed: {0}" , e .getStatus ());
138
138
return ;
@@ -142,14 +142,11 @@ public void recognize() {
142
142
try {
143
143
logger .log (Level .INFO , "Waiting 2s for operation, {0} processing..." , operation .getName ());
144
144
Thread .sleep (2000 );
145
- GetOperationRequest operationReq = GetOperationRequest .newBuilder ()
146
- .setName (operation .getName ())
147
- .build ();
148
- status = statusStub .getOperation (
149
- GetOperationRequest .newBuilder ()
150
- .setName (operation .getName ())
151
- .build ()
152
- );
145
+ GetOperationRequest operationReq =
146
+ GetOperationRequest .newBuilder ().setName (operation .getName ()).build ();
147
+ status =
148
+ statusClient .getOperation (
149
+ GetOperationRequest .newBuilder ().setName (operation .getName ()).build ());
153
150
154
151
if (status .getDone ()) {
155
152
break ;
@@ -164,7 +161,7 @@ public void recognize() {
164
161
165
162
logger .info ("Received response: " + asyncRes );
166
163
} catch (com .google .protobuf .InvalidProtocolBufferException ex ) {
167
- logger .log (Level .WARNING , "Unpack error, {0}" ,ex .getMessage ());
164
+ logger .log (Level .WARNING , "Unpack error, {0}" , ex .getMessage ());
168
165
}
169
166
}
170
167
@@ -178,26 +175,30 @@ public static void main(String[] args) throws Exception {
178
175
CommandLineParser parser = new DefaultParser ();
179
176
180
177
Options options = new Options ();
181
- options .addOption (OptionBuilder .withLongOpt ("uri" )
182
- .withDescription ("path to audio uri" )
183
- .hasArg ()
184
- .withArgName ("FILE_PATH" )
185
- .create ());
186
- options .addOption (OptionBuilder .withLongOpt ("host" )
187
- .withDescription ("endpoint for api, e.g. speech.googleapis.com" )
188
- .hasArg ()
189
- .withArgName ("ENDPOINT" )
190
- .create ());
191
- options .addOption (OptionBuilder .withLongOpt ("port" )
192
- .withDescription ("SSL port, usually 443" )
193
- .hasArg ()
194
- .withArgName ("PORT" )
195
- .create ());
196
- options .addOption (OptionBuilder .withLongOpt ("sampling" )
197
- .withDescription ("Sampling Rate, i.e. 16000" )
198
- .hasArg ()
199
- .withArgName ("RATE" )
200
- .create ());
178
+ options .addOption (
179
+ OptionBuilder .withLongOpt ("uri" )
180
+ .withDescription ("path to audio uri" )
181
+ .hasArg ()
182
+ .withArgName ("FILE_PATH" )
183
+ .create ());
184
+ options .addOption (
185
+ OptionBuilder .withLongOpt ("host" )
186
+ .withDescription ("endpoint for api, e.g. speech.googleapis.com" )
187
+ .hasArg ()
188
+ .withArgName ("ENDPOINT" )
189
+ .create ());
190
+ options .addOption (
191
+ OptionBuilder .withLongOpt ("port" )
192
+ .withDescription ("SSL port, usually 443" )
193
+ .hasArg ()
194
+ .withArgName ("PORT" )
195
+ .create ());
196
+ options .addOption (
197
+ OptionBuilder .withLongOpt ("sampling" )
198
+ .withDescription ("Sampling Rate, i.e. 16000" )
199
+ .hasArg ()
200
+ .withArgName ("RATE" )
201
+ .create ());
201
202
202
203
try {
203
204
CommandLine line = parser .parse (options , args );
0 commit comments