Skip to content

Commit

Permalink
Update streamingRecognizeFile with try-catch based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nnegrey committed Feb 21, 2018
1 parent 933fe44 commit 0532936
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions speech/cloud-client/src/main/java/com/example/speech/Recognize.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,78 +342,78 @@ public static void streamingRecognizeFile(String fileName) throws Exception, IOE
byte[] data = Files.readAllBytes(path);

// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
SpeechClient speech = SpeechClient.create();

// Configure request with local raw PCM audio
RecognitionConfig recConfig = RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.build();
StreamingRecognitionConfig config = StreamingRecognitionConfig.newBuilder()
.setConfig(recConfig)
.build();

class ResponseApiStreamingObserver<T> implements ApiStreamObserver<T> {
private final SettableFuture<List<T>> future = SettableFuture.create();
private final List<T> messages = new java.util.ArrayList<T>();

@Override
public void onNext(T message) {
messages.add(message);
}
try (SpeechClient speech = SpeechClient.create()) {

@Override
public void onError(Throwable t) {
future.setException(t);
}
// Configure request with local raw PCM audio
RecognitionConfig recConfig = RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.build();
StreamingRecognitionConfig config = StreamingRecognitionConfig.newBuilder()
.setConfig(recConfig)
.build();

@Override
public void onCompleted() {
future.set(messages);
}
class ResponseApiStreamingObserver<T> implements ApiStreamObserver<T> {
private final SettableFuture<List<T>> future = SettableFuture.create();
private final List<T> messages = new java.util.ArrayList<T>();

@Override
public void onNext(T message) {
messages.add(message);
}

@Override
public void onError(Throwable t) {
future.setException(t);
}

@Override
public void onCompleted() {
future.set(messages);
}

// Returns the SettableFuture object to get received messages / exceptions.
public SettableFuture<List<T>> future() {
return future;
// Returns the SettableFuture object to get received messages / exceptions.
public SettableFuture<List<T>> future() {
return future;
}
}
}

ResponseApiStreamingObserver<StreamingRecognizeResponse> responseObserver =
new ResponseApiStreamingObserver<>();
ResponseApiStreamingObserver<StreamingRecognizeResponse> responseObserver =
new ResponseApiStreamingObserver<>();

BidiStreamingCallable<StreamingRecognizeRequest,StreamingRecognizeResponse> callable =
speech.streamingRecognizeCallable();
BidiStreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable =
speech.streamingRecognizeCallable();

ApiStreamObserver<StreamingRecognizeRequest> requestObserver =
callable.bidiStreamingCall(responseObserver);
ApiStreamObserver<StreamingRecognizeRequest> requestObserver =
callable.bidiStreamingCall(responseObserver);

// The first request must **only** contain the audio configuration:
requestObserver.onNext(StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(config)
.build());
// The first request must **only** contain the audio configuration:
requestObserver.onNext(StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(config)
.build());

// Subsequent requests must **only** contain the audio data.
requestObserver.onNext(StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(data))
.build());
// Subsequent requests must **only** contain the audio data.
requestObserver.onNext(StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(data))
.build());

// Mark transmission as completed after sending the data.
requestObserver.onCompleted();
// Mark transmission as completed after sending the data.
requestObserver.onCompleted();

List<StreamingRecognizeResponse> responses = responseObserver.future().get();
List<StreamingRecognizeResponse> responses = responseObserver.future().get();

for (StreamingRecognizeResponse response: responses) {
// For streaming recognize, the results list has one is_final result (if available) followed
// by a number of in-progress results (if iterim_results is true) for subsequent utterances.
// Just print the first result here.
StreamingRecognitionResult result = response.getResultsList().get(0);
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript : %s\n", alternative.getTranscript());
for (StreamingRecognizeResponse response : responses) {
// For streaming recognize, the results list has one is_final result (if available) followed
// by a number of in-progress results (if iterim_results is true) for subsequent utterances.
// Just print the first result here.
StreamingRecognitionResult result = response.getResultsList().get(0);
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript : %s\n", alternative.getTranscript());
}
}
speech.close();
}

// [START speech_transcribe_model_selection]
Expand Down

0 comments on commit 0532936

Please sign in to comment.