6565 */
6666public class AsyncRecognizeClient {
6767
68- private static final Logger logger =
69- Logger .getLogger (AsyncRecognizeClient .class .getName ());
68+ private static final Logger logger = Logger .getLogger (AsyncRecognizeClient .class .getName ());
7069
7170 private static final List <String > OAUTH2_SCOPES =
7271 Arrays .asList ("https://www.googleapis.com/auth/cloud-platform" );
@@ -77,8 +76,8 @@ public class AsyncRecognizeClient {
7776 private final int samplingRate ;
7877
7978 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 ;
8281
8382 /**
8483 * 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)
9291
9392 GoogleCredentials creds = GoogleCredentials .getApplicationDefault ();
9493 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 );
103103 }
104104
105105 public void shutdown () throws InterruptedException {
@@ -116,23 +116,23 @@ public void recognize() {
116116 return ;
117117 }
118118 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 ();
127126
128127 Operation operation ;
129128 Operation status ;
130129 try {
131- operation = stub .asyncRecognize (request );
130+ operation = speechClient .asyncRecognize (request );
132131
133132 //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 ()));
136136 } catch (StatusRuntimeException e ) {
137137 logger .log (Level .WARNING , "RPC failed: {0}" , e .getStatus ());
138138 return ;
@@ -142,14 +142,11 @@ public void recognize() {
142142 try {
143143 logger .log (Level .INFO , "Waiting 2s for operation, {0} processing..." , operation .getName ());
144144 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 ());
153150
154151 if (status .getDone ()) {
155152 break ;
@@ -164,7 +161,7 @@ public void recognize() {
164161
165162 logger .info ("Received response: " + asyncRes );
166163 } 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 ());
168165 }
169166 }
170167
@@ -178,26 +175,30 @@ public static void main(String[] args) throws Exception {
178175 CommandLineParser parser = new DefaultParser ();
179176
180177 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 ());
201202
202203 try {
203204 CommandLine line = parser .parse (options , args );
0 commit comments