use of com.google.api.gax.rpc.StreamController in project java-speech by googleapis.
the class InfiniteStreamRecognize method infiniteStreamingRecognize.
/**
* Performs infinite streaming speech recognition
*/
public static void infiniteStreamingRecognize(String languageCode) throws Exception {
// Microphone Input buffering
class MicBuffer implements Runnable {
@Override
public void run() {
System.out.println(YELLOW);
System.out.println("Start speaking...Press Ctrl-C to stop");
targetDataLine.start();
byte[] data = new byte[BYTES_PER_BUFFER];
while (targetDataLine.isOpen()) {
try {
int numBytesRead = targetDataLine.read(data, 0, data.length);
if ((numBytesRead <= 0) && (targetDataLine.isOpen())) {
continue;
}
sharedQueue.put(data.clone());
} catch (InterruptedException e) {
System.out.println("Microphone input buffering interrupted : " + e.getMessage());
}
}
}
}
// Creating microphone input buffer thread
MicBuffer micrunnable = new MicBuffer();
Thread micThread = new Thread(micrunnable);
ResponseObserver<StreamingRecognizeResponse> responseObserver = null;
try (SpeechClient client = SpeechClient.create()) {
ClientStream<StreamingRecognizeRequest> clientStream;
responseObserver = new ResponseObserver<StreamingRecognizeResponse>() {
ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();
public void onStart(StreamController controller) {
referenceToStreamController = controller;
}
public void onResponse(StreamingRecognizeResponse response) {
responses.add(response);
StreamingRecognitionResult result = response.getResultsList().get(0);
Duration resultEndTime = result.getResultEndTime();
resultEndTimeInMS = (int) ((resultEndTime.getSeconds() * 1000) + (resultEndTime.getNanos() / 1000000));
double correctedTime = resultEndTimeInMS - bridgingOffset + (STREAMING_LIMIT * restartCounter);
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
if (result.getIsFinal()) {
System.out.print(GREEN);
System.out.print("\033[2K\r");
System.out.printf("%s: %s [confidence: %.2f]\n", convertMillisToDate(correctedTime), alternative.getTranscript(), alternative.getConfidence());
isFinalEndTime = resultEndTimeInMS;
lastTranscriptWasFinal = true;
} else {
System.out.print(RED);
System.out.print("\033[2K\r");
System.out.printf("%s: %s", convertMillisToDate(correctedTime), alternative.getTranscript());
lastTranscriptWasFinal = false;
}
}
public void onComplete() {
}
public void onError(Throwable t) {
}
};
clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.LINEAR16).setLanguageCode(languageCode).setSampleRateHertz(16000).build();
StreamingRecognitionConfig streamingRecognitionConfig = StreamingRecognitionConfig.newBuilder().setConfig(recognitionConfig).setInterimResults(true).build();
StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().setStreamingConfig(streamingRecognitionConfig).build();
clientStream.send(request);
try {
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
// bigEndian: false
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info targetInfo = new Info(TargetDataLine.class, // Set the system information to read from the microphone audio
audioFormat);
if (!AudioSystem.isLineSupported(targetInfo)) {
System.out.println("Microphone not supported");
System.exit(0);
}
// Target data line captures the audio stream the microphone produces.
targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
targetDataLine.open(audioFormat);
micThread.start();
long startTime = System.currentTimeMillis();
while (true) {
long estimatedTime = System.currentTimeMillis() - startTime;
if (estimatedTime >= STREAMING_LIMIT) {
clientStream.closeSend();
// remove Observer
referenceToStreamController.cancel();
if (resultEndTimeInMS > 0) {
finalRequestEndTime = isFinalEndTime;
}
resultEndTimeInMS = 0;
lastAudioInput = null;
lastAudioInput = audioInput;
audioInput = new ArrayList<ByteString>();
restartCounter++;
if (!lastTranscriptWasFinal) {
System.out.print('\n');
}
newStream = true;
clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
request = StreamingRecognizeRequest.newBuilder().setStreamingConfig(streamingRecognitionConfig).build();
System.out.println(YELLOW);
System.out.printf("%d: RESTARTING REQUEST\n", restartCounter * STREAMING_LIMIT);
startTime = System.currentTimeMillis();
} else {
if ((newStream) && (lastAudioInput.size() > 0)) {
// if this is the first audio from a new request
// calculate amount of unfinalized audio from last request
// resend the audio to the speech client before incoming audio
double chunkTime = STREAMING_LIMIT / lastAudioInput.size();
// ms length of each chunk in previous request audio arrayList
if (chunkTime != 0) {
if (bridgingOffset < 0) {
// bridging Offset accounts for time of resent audio
// calculated from last request
bridgingOffset = 0;
}
if (bridgingOffset > finalRequestEndTime) {
bridgingOffset = finalRequestEndTime;
}
int chunksFromMs = (int) Math.floor((finalRequestEndTime - bridgingOffset) / chunkTime);
// chunks from MS is number of chunks to resend
bridgingOffset = (int) Math.floor((lastAudioInput.size() - chunksFromMs) * chunkTime);
// set bridging offset for next request
for (int i = chunksFromMs; i < lastAudioInput.size(); i++) {
request = StreamingRecognizeRequest.newBuilder().setAudioContent(lastAudioInput.get(i)).build();
clientStream.send(request);
}
}
newStream = false;
}
tempByteString = ByteString.copyFrom(sharedQueue.take());
request = StreamingRecognizeRequest.newBuilder().setAudioContent(tempByteString).build();
audioInput.add(tempByteString);
}
clientStream.send(request);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
use of com.google.api.gax.rpc.StreamController in project java-speech by googleapis.
the class Recognize method streamingMicRecognize.
// [END speech_stream_recognize_punctuation]
// [START speech_transcribe_streaming_mic]
/**
* Performs microphone streaming speech recognition with a duration of 1 minute.
*/
public static void streamingMicRecognize() throws Exception {
ResponseObserver<StreamingRecognizeResponse> responseObserver = null;
try (SpeechClient client = SpeechClient.create()) {
responseObserver = new ResponseObserver<StreamingRecognizeResponse>() {
ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();
public void onStart(StreamController controller) {
}
public void onResponse(StreamingRecognizeResponse response) {
responses.add(response);
}
public void onComplete() {
for (StreamingRecognizeResponse response : responses) {
StreamingRecognitionResult result = response.getResultsList().get(0);
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
System.out.printf("Transcript : %s\n", alternative.getTranscript());
}
}
public void onError(Throwable t) {
System.out.println(t);
}
};
ClientStream<StreamingRecognizeRequest> clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder().setEncoding(RecognitionConfig.AudioEncoding.LINEAR16).setLanguageCode("en-US").setSampleRateHertz(16000).build();
StreamingRecognitionConfig streamingRecognitionConfig = StreamingRecognitionConfig.newBuilder().setConfig(recognitionConfig).build();
StreamingRecognizeRequest request = StreamingRecognizeRequest.newBuilder().setStreamingConfig(streamingRecognitionConfig).build();
clientStream.send(request);
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
// bigEndian: false
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info targetInfo = new Info(TargetDataLine.class, // Set the system information to read from the microphone audio stream
audioFormat);
if (!AudioSystem.isLineSupported(targetInfo)) {
System.out.println("Microphone not supported");
System.exit(0);
}
// Target data line captures the audio stream the microphone produces.
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
System.out.println("Start speaking");
long startTime = System.currentTimeMillis();
// Audio Input Stream
AudioInputStream audio = new AudioInputStream(targetDataLine);
while (true) {
long estimatedTime = System.currentTimeMillis() - startTime;
byte[] data = new byte[6400];
audio.read(data);
if (estimatedTime > 60000) {
// 60 seconds
System.out.println("Stop speaking.");
targetDataLine.stop();
targetDataLine.close();
break;
}
request = StreamingRecognizeRequest.newBuilder().setAudioContent(ByteString.copyFrom(data)).build();
clientStream.send(request);
}
} catch (Exception e) {
System.out.println(e);
}
responseObserver.onComplete();
}
use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.
the class ReadRowsAttemptCallable method onCancel.
/**
* Called when the outer {@link ResponseObserver} wants to prematurely cancel the stream.
*
* @see StreamController#cancel()
*/
private void onCancel() {
StreamController localInnerController;
synchronized (lock) {
if (cancellationCause != null) {
return;
}
// NOTE: BasicRetryingFuture will replace j.u.c.CancellationExceptions with it's own,
// which will not have the current stacktrace, so a special wrapper has be used here.
cancellationCause = new ServerStreamingAttemptException(new CancellationException("User cancelled stream"), resumptionStrategy.canResume(), seenSuccessSinceLastError);
localInnerController = innerController;
}
if (localInnerController != null) {
localInnerController.cancel();
}
}
use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.
the class ReadRowsAttemptCallable method call.
/**
* Sends the actual RPC. The request being sent will first be transformed by the {@link
* StreamResumptionStrategy}.
*
* <p>This method expects to be called by one thread at a time. Furthermore, it expects that the
* current RPC finished before the next time it's called.
*/
@Override
public Void call() {
Preconditions.checkState(isStarted, "Must be started first");
ReadRowsRequest request = (++numAttempts == 1) ? initialRequest : resumptionStrategy.getResumeRequest(initialRequest);
// Should never happen. onAttemptError will check if ResumptionStrategy can create a resume
// request,
// which the RetryingFuture/StreamResumptionStrategy should respect.
Preconditions.checkState(request != null, "ResumptionStrategy returned a null request.");
innerAttemptFuture = SettableApiFuture.create();
seenSuccessSinceLastError = false;
ApiCallContext attemptContext = context;
if (!outerRetryingFuture.getAttemptSettings().getRpcTimeout().isZero()) {
attemptContext = attemptContext.withStreamWaitTimeout(outerRetryingFuture.getAttemptSettings().getRpcTimeout());
}
attemptContext.getTracer().attemptStarted(outerRetryingFuture.getAttemptSettings().getOverallAttemptCount());
innerCallable.call(request, new StateCheckingResponseObserver<ReadRowsResponse>() {
@Override
public void onStartImpl(StreamController controller) {
onAttemptStart(controller);
}
@Override
public void onResponseImpl(ReadRowsResponse response) {
onAttemptResponse(response);
}
@Override
public void onErrorImpl(Throwable t) {
onAttemptError(t);
}
@Override
public void onCompleteImpl() {
onAttemptComplete();
}
}, attemptContext);
outerRetryingFuture.setAttemptFuture(innerAttemptFuture);
return null;
}
use of com.google.api.gax.rpc.StreamController in project java-bigquerystorage by googleapis.
the class ReadRowsAttemptCallable method call.
/**
* Sends the actual RPC. The request being sent will first be transformed by the {@link
* StreamResumptionStrategy}.
*
* <p>This method expects to be called by one thread at a time. Furthermore, it expects that the
* current RPC finished before the next time it's called.
*/
@Override
public Void call() {
Preconditions.checkState(isStarted, "Must be started first");
ReadRowsRequest request = (++numAttempts == 1) ? initialRequest : resumptionStrategy.getResumeRequest(initialRequest);
// Should never happen. onAttemptError will check if ResumptionStrategy can create a resume
// request,
// which the RetryingFuture/StreamResumptionStrategy should respect.
Preconditions.checkState(request != null, "ResumptionStrategy returned a null request.");
innerAttemptFuture = SettableApiFuture.create();
seenSuccessSinceLastError = false;
ApiCallContext attemptContext = context;
if (!outerRetryingFuture.getAttemptSettings().getRpcTimeout().isZero()) {
attemptContext = attemptContext.withStreamWaitTimeout(outerRetryingFuture.getAttemptSettings().getRpcTimeout());
}
attemptContext.getTracer().attemptStarted(outerRetryingFuture.getAttemptSettings().getOverallAttemptCount());
innerCallable.call(request, new StateCheckingResponseObserver<ReadRowsResponse>() {
@Override
public void onStartImpl(StreamController controller) {
onAttemptStart(controller);
}
@Override
public void onResponseImpl(ReadRowsResponse response) {
onAttemptResponse(response);
}
@Override
public void onErrorImpl(Throwable t) {
onAttemptError(t);
}
@Override
public void onCompleteImpl() {
onAttemptComplete();
}
}, attemptContext);
outerRetryingFuture.setAttemptFuture(innerAttemptFuture);
return null;
}
Aggregations