Skip to content

Commit

Permalink
[googlestt] Fix drop bytes (openhab#14649)
Browse files Browse the repository at this point in the history
* [googlestt] Fix drop bytes
* fix unhandled cancelation error when using single utterance mode

Signed-off-by: Miguel Álvarez <[email protected]>
  • Loading branch information
GiviMAD authored and renescherer committed Mar 23, 2023
1 parent c38e42d commit 4d0e924
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,20 @@ private void streamAudio(ClientStream<StreamingRecognizeRequest> clientStream, A
long startTime = System.currentTimeMillis();
long maxTranscriptionMillis = (config.maxTranscriptionSeconds * 1000L);
long maxSilenceMillis = (config.maxSilenceSeconds * 1000L);
int readBytes = 6400;
while (!aborted.get()) {
byte[] data = new byte[readBytes];
int dataN = audioStream.read(data);
final int bufferSize = 6400;
int numBytesRead;
int remaining = bufferSize;
byte[] audioBuffer = new byte[bufferSize];
while (!aborted.get() && !responseObserver.isDone()) {
numBytesRead = audioStream.read(audioBuffer, bufferSize - remaining, remaining);
if (aborted.get()) {
logger.debug("Stops listening, aborted");
break;
}
if (numBytesRead == -1) {
logger.debug("End of stream");
break;
}
if (isExpiredInterval(maxTranscriptionMillis, startTime)) {
logger.debug("Stops listening, max transcription time reached");
break;
Expand All @@ -272,18 +278,17 @@ && isExpiredInterval(maxSilenceMillis, responseObserver.getLastInputTime())) {
logger.debug("Stops listening, max silence time reached");
break;
}
if (dataN != readBytes) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
if (numBytesRead != remaining) {
remaining = remaining - numBytesRead;
continue;
}
remaining = bufferSize;
StreamingRecognizeRequest dataRequest = StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(data)).build();
logger.debug("Sending audio data {}", dataN);
.setAudioContent(ByteString.copyFrom(audioBuffer)).build();
logger.debug("Sending audio data {}", bufferSize);
clientStream.send(dataRequest);
}
audioStream.close();
}

private void sendStreamConfig(ClientStream<StreamingRecognizeRequest> clientStream,
Expand Down Expand Up @@ -335,6 +340,7 @@ private static class TranscriptionListener implements ResponseObserver<Streaming
private float confidenceSum = 0;
private int responseCount = 0;
private long lastInputTime = 0;
private boolean done = false;

public TranscriptionListener(STTListener sttListener, GoogleSTTConfiguration config, AtomicBoolean aborted) {
this.sttListener = sttListener;
Expand Down Expand Up @@ -374,7 +380,7 @@ public void onResponse(StreamingRecognizeResponse response) {
responseCount++;
// when in single utterance mode we can just get one final result so complete
if (config.singleUtteranceMode) {
onComplete();
done = true;
}
}
});
Expand Down Expand Up @@ -411,6 +417,10 @@ public void onError(@Nullable Throwable t) {
}
}

public boolean isDone() {
return done;
}

public long getLastInputTime() {
return lastInputTime;
}
Expand Down

0 comments on commit 4d0e924

Please sign in to comment.