From ee8139f442e50e241ea26cf0534ac380e1771a5a Mon Sep 17 00:00:00 2001 From: Darren <39422044+dargilco@users.noreply.github.com> Date: Thu, 3 Dec 2020 18:33:05 -0800 Subject: [PATCH] Java Sample: Fix logic for reading from a FileInputStream and other minor issues (#885) * Fix issue #874. Properly handle shorter read buffers at the end of the WAV file. * Suppress two warnings of unused variables (they are required for code readability) * Resolve one warning of unused import --- .../samples/console/SpeechRecognitionSamples.java | 13 +++++++++++-- .../speech/samples/console/TranslationSamples.java | 1 - .../speech/samples/console/WavStream.java | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/SpeechRecognitionSamples.java b/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/SpeechRecognitionSamples.java index 4291d5796..4457f9272 100644 --- a/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/SpeechRecognitionSamples.java +++ b/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/SpeechRecognitionSamples.java @@ -356,9 +356,18 @@ else if (e.getResult().getReason() == ResultReason.NoMatch) { // Push audio read from the file into the PushStream. // The audio can be pushed into the stream before, after, or during recognition // and recognition will continue as data becomes available. - while( inputStream.read(readBuffer) != -1) + int bytesRead; + while ((bytesRead = inputStream.read(readBuffer)) != -1) { - pushStream.write(readBuffer); + if (bytesRead == readBuffer.length) + { + pushStream.write(readBuffer); + } + else + { + // Last buffer read from the WAV file is likely to have less bytes + pushStream.write(Arrays.copyOfRange(readBuffer, 0, bytesRead)); + } } pushStream.close(); diff --git a/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/TranslationSamples.java b/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/TranslationSamples.java index 2ac9b3423..02a349e9b 100644 --- a/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/TranslationSamples.java +++ b/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/TranslationSamples.java @@ -5,7 +5,6 @@ // import java.io.IOException; -import java.util.ArrayList; import java.util.Map; import java.util.Scanner; import java.util.concurrent.ExecutionException; diff --git a/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/WavStream.java b/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/WavStream.java index 3739ecc61..9d1a2c56b 100644 --- a/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/WavStream.java +++ b/samples/java/jre/console/src/com/microsoft/cognitiveservices/speech/samples/console/WavStream.java @@ -91,7 +91,9 @@ public InputStream parseWavHeader(InputStream reader) throws IOException { int formatTag = ReadUInt16(reader); int channels = ReadUInt16(reader); int samplesPerSec = (int) ReadUInt32(reader); + @SuppressWarnings("unused") int avgBytesPerSec = (int) ReadUInt32(reader); + @SuppressWarnings("unused") int blockAlign = ReadUInt16(reader); int bitsPerSample = ReadUInt16(reader); ThrowIfFalse(formatTag == 1, "PCM"); // PCM