14
14
* limitations under the License.
15
15
*/
16
16
17
- // Client that sends audio to Speech.NonStreamingRecognize via gRPC and returns transcription.
17
+ // Client that sends audio to Speech.SyncRecognize via gRPC and returns transcription.
18
18
//
19
19
// Uses a service account for OAuth2 authentication, which you may obtain at
20
20
// https://console.developers.google.com
26
26
package com .google .cloud .speech .grpc .demos ;
27
27
28
28
import com .google .auth .oauth2 .GoogleCredentials ;
29
- import com .google .cloud .speech .v1 . AudioRequest ;
30
- import com .google .cloud .speech .v1 . InitialRecognizeRequest ;
31
- import com .google .cloud .speech .v1 . InitialRecognizeRequest .AudioEncoding ;
32
- import com .google .cloud .speech .v1 . NonStreamingRecognizeResponse ;
33
- import com .google .cloud .speech .v1 . RecognizeRequest ;
34
- import com .google .cloud .speech .v1 .SpeechGrpc ;
29
+ import com .google .cloud .speech .v1beta1 . RecognitionAudio ;
30
+ import com .google .cloud .speech .v1beta1 . RecognitionConfig ;
31
+ import com .google .cloud .speech .v1beta1 . RecognitionConfig .AudioEncoding ;
32
+ import com .google .cloud .speech .v1beta1 . SyncRecognizeRequest ;
33
+ import com .google .cloud .speech .v1beta1 . SyncRecognizeResponse ;
34
+ import com .google .cloud .speech .v1beta1 .SpeechGrpc ;
35
35
import com .google .protobuf .TextFormat ;
36
36
37
37
import io .grpc .ManagedChannel ;
57
57
import java .util .logging .Logger ;
58
58
59
59
/**
60
- * Client that sends audio to Speech.NonStreamingRecognize and returns transcript.
60
+ * Client that sends audio to Speech.SyncRecognize and returns transcript.
61
61
*/
62
- public class NonStreamingRecognizeClient {
62
+ public class SyncRecognizeClient {
63
63
64
64
private static final Logger logger =
65
- Logger .getLogger (NonStreamingRecognizeClient .class .getName ());
65
+ Logger .getLogger (SyncRecognizeClient .class .getName ());
66
66
67
67
private static final List <String > OAUTH2_SCOPES =
68
68
Arrays .asList ("https://www.googleapis.com/auth/cloud-platform" );
@@ -78,7 +78,7 @@ public class NonStreamingRecognizeClient {
78
78
/**
79
79
* Construct client connecting to Cloud Speech server at {@code host:port}.
80
80
*/
81
- public NonStreamingRecognizeClient (String host , int port , URI input , int samplingRate )
81
+ public SyncRecognizeClient (String host , int port , URI input , int samplingRate )
82
82
throws IOException {
83
83
this .host = host ;
84
84
this .port = port ;
@@ -95,8 +95,8 @@ public NonStreamingRecognizeClient(String host, int port, URI input, int samplin
95
95
logger .info ("Created blockingStub for " + host + ":" + port );
96
96
}
97
97
98
- private AudioRequest createAudioRequest () throws IOException {
99
- return AudioRequestFactory . createRequest (this .input );
98
+ private RecognitionAudio createRecognitionAudio () throws IOException {
99
+ return RecognitionAudioFactory . createRecognitionAudio (this .input );
100
100
}
101
101
102
102
public void shutdown () throws InterruptedException {
@@ -105,25 +105,26 @@ public void shutdown() throws InterruptedException {
105
105
106
106
/** Send a non-streaming-recognize request to server. */
107
107
public void recognize () {
108
- AudioRequest audio ;
108
+ RecognitionAudio audio ;
109
109
try {
110
- audio = createAudioRequest ();
110
+ audio = createRecognitionAudio ();
111
111
} catch (IOException e ) {
112
112
logger .log (Level .WARNING , "Failed to read audio uri input: " + input );
113
113
return ;
114
114
}
115
115
logger .info ("Sending " + audio .getContent ().size () + " bytes from audio uri input: " + input );
116
- InitialRecognizeRequest initial = InitialRecognizeRequest .newBuilder ()
117
- .setEncoding (AudioEncoding .LINEAR16 )
118
- .setSampleRate (samplingRate )
116
+ RecognitionConfig config = RecognitionConfig .newBuilder ()
117
+ .setEncoding (AudioEncoding .LINEAR16 )
118
+ .setSampleRate (samplingRate )
119
+ .build ();
120
+ SyncRecognizeRequest request = SyncRecognizeRequest .newBuilder ()
121
+ .setConfig (config )
122
+ .setAudio (audio )
119
123
.build ();
120
- RecognizeRequest request = RecognizeRequest .newBuilder ()
121
- .setInitialRequest (initial )
122
- .setAudioRequest (audio )
123
- .build ();
124
- NonStreamingRecognizeResponse response ;
124
+
125
+ SyncRecognizeResponse response ;
125
126
try {
126
- response = blockingStub .nonStreamingRecognize (request );
127
+ response = blockingStub .syncRecognize (request );
127
128
} catch (StatusRuntimeException e ) {
128
129
logger .log (Level .WARNING , "RPC failed: {0}" , e .getStatus ());
129
130
return ;
@@ -196,8 +197,8 @@ public static void main(String[] args) throws Exception {
196
197
System .exit (1 );
197
198
}
198
199
199
- NonStreamingRecognizeClient client =
200
- new NonStreamingRecognizeClient (host , port , URI .create (audioFile ), sampling );
200
+ SyncRecognizeClient client =
201
+ new SyncRecognizeClient (host , port , URI .create (audioFile ), sampling );
201
202
try {
202
203
client .recognize ();
203
204
} finally {
0 commit comments