From 0e32b399462ae406870448baf13c4f75bb60319d Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Mon, 8 Jul 2019 13:15:11 -0700 Subject: [PATCH 1/7] added different language option for other than english --- speech/cloud-client/README.md | 6 +- speech/cloud-client/pom.xml | 6 +- .../speech/InfiniteStreamRecognize.java | 13 ++++- .../InfiniteStreamRecognizeOptions.java | 56 +++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognizeOptions.java diff --git a/speech/cloud-client/README.md b/speech/cloud-client/README.md index dfb1728aff0..7c8df8d8eaa 100644 --- a/speech/cloud-client/README.md +++ b/speech/cloud-client/README.md @@ -155,7 +155,11 @@ mvn exec:java -DRecognize -Dexec.args="word-level-conf gs://cloud-samples-tests/ ``` ## Infinite Streaming -Continuously stream audio to the speech API over multiple requests +Continuously stream audio to the speech API over multiple requests (by default en-US). ``` mvn exec:java -DInfiniteStreamRecognize ``` +If stream audio is in different language, you could also pass language code as a command line argument (for example, korea). +``` +mvn exec:java -Dexec.args="-lang_code=ko-KR" -DInfiniteStreamRecognize +``` diff --git a/speech/cloud-client/pom.xml b/speech/cloud-client/pom.xml index 81ebabe8835..c1df5dd1734 100644 --- a/speech/cloud-client/pom.xml +++ b/speech/cloud-client/pom.xml @@ -43,7 +43,11 @@ 1.9.0 - + + commons-cli + commons-cli + 1.3 + junit diff --git a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java index f926fa7ea6c..55daad7d6d1 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java @@ -29,6 +29,7 @@ import com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse; import com.google.protobuf.ByteString; import com.google.protobuf.Duration; + import java.lang.Math; import java.text.DecimalFormat; import java.util.ArrayList; @@ -66,15 +67,21 @@ public class InfiniteStreamRecognize { private static ByteString tempByteString; public static void main(String... args) { + InfiniteStreamRecognizeOptions options = InfiniteStreamRecognizeOptions.fromFlags(args); + if (options == null) { + // Could not parse. + System.exit(1); + } + try { - infiniteStreamingRecognize(); + infiniteStreamingRecognize(options.langCode); } catch (Exception e) { System.out.println("Exception caught: " + e); } } /** Performs infinite streaming speech recognition */ - public static void infiniteStreamingRecognize() throws Exception { + public static void infiniteStreamingRecognize(String languageCode) throws Exception { // Microphone Input buffering class MicBuffer implements Runnable { @@ -159,7 +166,7 @@ public void onError(Throwable t) {} RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder() .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) - .setLanguageCode("en-US") + .setLanguageCode(languageCode) .setSampleRateHertz(16000) .build(); diff --git a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognizeOptions.java b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognizeOptions.java new file mode 100644 index 00000000000..5966c151b9e --- /dev/null +++ b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognizeOptions.java @@ -0,0 +1,56 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.speech; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +public class InfiniteStreamRecognizeOptions { + String langCode = "en-US"; //by default english US + + /** Construct an InfiniteStreamRecognizeOptions class from command line flags. */ + public static InfiniteStreamRecognizeOptions fromFlags(String[] args) { + Options options = new Options(); + options.addOption( + Option.builder() + .type(String.class) + .longOpt("lang_code") + .hasArg() + .desc("Language code") + .build()); + + CommandLineParser parser = new DefaultParser(); + CommandLine commandLine; + try { + commandLine = parser.parse(options, args); + InfiniteStreamRecognizeOptions res = new InfiniteStreamRecognizeOptions(); + + if (commandLine.hasOption("lang_code")) { + res.langCode = commandLine.getOptionValue("lang_code"); + } + return res; + } catch (ParseException e) { + System.err.println(e.getMessage()); + return null; + } + } + +} From 38032c030dcdec3854ad8b8a3b47768f6c63a62f Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 9 Jul 2019 11:01:00 -0700 Subject: [PATCH 2/7] changed it to english --- speech/cloud-client/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/speech/cloud-client/README.md b/speech/cloud-client/README.md index 7c8df8d8eaa..664709d0cfe 100644 --- a/speech/cloud-client/README.md +++ b/speech/cloud-client/README.md @@ -159,7 +159,7 @@ Continuously stream audio to the speech API over multiple requests (by default e ``` mvn exec:java -DInfiniteStreamRecognize ``` -If stream audio is in different language, you could also pass language code as a command line argument (for example, korea). +If stream audio is in different language, you could also pass language code as a command line argument (for example, en-GB for english (Great Britian), en-US for english U.S., and more available in [this link](https://cloud.google.com/speech-to-text/docs/languages)). ``` -mvn exec:java -Dexec.args="-lang_code=ko-KR" -DInfiniteStreamRecognize +mvn exec:java -Dexec.args="-lang_code=en-US" -DInfiniteStreamRecognize ``` From df21c309003906d1769efbb9b2037da1fca37911 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 9 Jul 2019 12:57:06 -0700 Subject: [PATCH 3/7] timestamp change --- .../speech/InfiniteStreamRecognize.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java index 55daad7d6d1..5a400a77a1f 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; @@ -80,6 +81,15 @@ public static void main(String... args) { } } + public static String convertMillisToDate(double milliSeconds, DecimalFormat format) { + long millis = (long) milliSeconds; + return String.format("%s min : %s sec", + format.format( TimeUnit.MILLISECONDS.toMinutes(millis)), + format.format( TimeUnit.MILLISECONDS.toSeconds(millis) - + TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))) + ); + + } /** Performs infinite streaming speech recognition */ public static void infiniteStreamingRecognize(String languageCode) throws Exception { @@ -134,21 +144,22 @@ public void onResponse(StreamingRecognizeResponse response) { double correctedTime = resultEndTimeInMS - bridgingOffset + (STREAMING_LIMIT * restartCounter); - DecimalFormat format = new DecimalFormat("0.#"); + DecimalFormat format = new DecimalFormat(); + format.setMinimumIntegerDigits(2); SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); if (result.getIsFinal()) { System.out.print(GREEN); System.out.print("\033[2K\r"); - System.out.printf("%s: %s\n", format.format(correctedTime), - alternative.getTranscript()); + System.out.printf("%s: %s\n", convertMillisToDate(correctedTime, format), + alternative.getTranscript() + " - " + alternative.getConfidence()); isFinalEndTime = resultEndTimeInMS; lastTranscriptWasFinal = true; } else { System.out.print(RED); System.out.print("\033[2K\r"); - System.out.printf("%s: %s", format.format(correctedTime), + System.out.printf("%s: %s", convertMillisToDate(correctedTime, format), alternative.getTranscript()); lastTranscriptWasFinal = false; From c559145f24db3f7e671e32468608b663a55e1f32 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 11 Jul 2019 12:52:12 -0700 Subject: [PATCH 4/7] changed millisecond format to min:sec --- .../speech/InfiniteStreamRecognize.java | 108 +++++++++--------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java index 5a400a77a1f..8c0723c4437 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java @@ -17,6 +17,7 @@ package com.example.speech; // [START speech_transcribe_infinite_streaming] + import com.google.api.gax.rpc.ClientStream; import com.google.api.gax.rpc.ResponseObserver; import com.google.api.gax.rpc.StreamController; @@ -44,19 +45,17 @@ public class InfiniteStreamRecognize { - private static final int STREAMING_LIMIT = 290000; // ~5 minutes - public static final String RED = "\033[0;31m"; public static final String GREEN = "\033[0;32m"; public static final String YELLOW = "\033[0;33m"; - + private static final int STREAMING_LIMIT = 290000; // ~5 minutes // Creating shared object private static volatile BlockingQueue sharedQueue = new LinkedBlockingQueue(); private static TargetDataLine targetDataLine; private static int BYTES_PER_BUFFER = 6400; // buffer size in bytes private static int restartCounter = 0; - private static ArrayList audioInput = new ArrayList(); + private static ArrayList audioInput = new ArrayList(); private static ArrayList lastAudioInput = new ArrayList(); private static int resultEndTimeInMS = 0; private static int isFinalEndTime = 0; @@ -83,14 +82,17 @@ public static void main(String... args) { public static String convertMillisToDate(double milliSeconds, DecimalFormat format) { long millis = (long) milliSeconds; - return String.format("%s min : %s sec", - format.format( TimeUnit.MILLISECONDS.toMinutes(millis)), - format.format( TimeUnit.MILLISECONDS.toSeconds(millis) - - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))) + return String.format("%s:%s /", + format.format(TimeUnit.MILLISECONDS.toMinutes(millis)), + format.format(TimeUnit.MILLISECONDS.toSeconds(millis) + - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))) ); } - /** Performs infinite streaming speech recognition */ + + /** + * Performs infinite streaming speech recognition + */ public static void infiniteStreamingRecognize(String languageCode) throws Exception { // Microphone Input buffering @@ -121,10 +123,10 @@ public void run() { Thread micThread = new Thread(micrunnable); ResponseObserver responseObserver = null; try (SpeechClient client = SpeechClient.create()) { + ClientStream clientStream; responseObserver = new ResponseObserver() { - ArrayList responses = new ArrayList<>(); public void onStart(StreamController controller) { @@ -132,65 +134,61 @@ public void onStart(StreamController 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)); - + + (resultEndTime.getNanos() / 1000000)); double correctedTime = resultEndTimeInMS - bridgingOffset - + (STREAMING_LIMIT * restartCounter); + + (STREAMING_LIMIT * restartCounter); DecimalFormat format = new DecimalFormat(); format.setMinimumIntegerDigits(2); - SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); if (result.getIsFinal()) { System.out.print(GREEN); System.out.print("\033[2K\r"); - System.out.printf("%s: %s\n", convertMillisToDate(correctedTime, format), - alternative.getTranscript() + " - " + alternative.getConfidence()); - + System.out.printf("%s: %s [confidence: %.2f]\n", convertMillisToDate(correctedTime, format), + 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, format), - alternative.getTranscript()); - + System.out.printf("%s: %s", format.format(correctedTime), + alternative.getTranscript() + ); lastTranscriptWasFinal = false; } } - public void onComplete() {} - - public void onError(Throwable t) {} + 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(); + RecognitionConfig.newBuilder() + .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) + .setLanguageCode(languageCode) + .setSampleRateHertz(16000) + .build(); StreamingRecognitionConfig streamingRecognitionConfig = - StreamingRecognitionConfig.newBuilder() - .setConfig(recognitionConfig) - .setInterimResults(true) - .build(); + StreamingRecognitionConfig.newBuilder() + .setConfig(recognitionConfig) + .setInterimResults(true) + .build(); StreamingRecognizeRequest request = - StreamingRecognizeRequest.newBuilder() - .setStreamingConfig(streamingRecognitionConfig) - .build(); // The first request in a streaming call has to be a config + StreamingRecognizeRequest.newBuilder() + .setStreamingConfig(streamingRecognitionConfig) + .build(); // The first request in a streaming call has to be a config clientStream.send(request); @@ -199,9 +197,9 @@ public void onError(Throwable t) {} // bigEndian: false AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false); DataLine.Info targetInfo = - new Info( - TargetDataLine.class, - audioFormat); // Set the system information to read from the microphone audio + new Info( + TargetDataLine.class, + audioFormat); // Set the system information to read from the microphone audio // stream if (!AudioSystem.isLineSupported(targetInfo)) { @@ -244,9 +242,9 @@ public void onError(Throwable t) {} clientStream = client.streamingRecognizeCallable().splitCall(responseObserver); request = - StreamingRecognizeRequest.newBuilder() - .setStreamingConfig(streamingRecognitionConfig) - .build(); + StreamingRecognizeRequest.newBuilder() + .setStreamingConfig(streamingRecognitionConfig) + .build(); System.out.println(YELLOW); System.out.printf("%d: RESTARTING REQUEST\n", restartCounter * STREAMING_LIMIT); @@ -271,17 +269,16 @@ public void onError(Throwable t) {} bridgingOffset = finalRequestEndTime; } int chunksFromMS = (int) Math.floor((finalRequestEndTime - - bridgingOffset) / chunkTime); + - bridgingOffset) / chunkTime); // chunks from MS is number of chunks to resend bridgingOffset = (int) Math.floor((lastAudioInput.size() - - chunksFromMS) * chunkTime); + - chunksFromMS) * chunkTime); // set bridging offset for next request for (int i = chunksFromMS; i < lastAudioInput.size(); i++) { - request = - StreamingRecognizeRequest.newBuilder() - .setAudioContent(lastAudioInput.get(i)) - .build(); + StreamingRecognizeRequest.newBuilder() + .setAudioContent(lastAudioInput.get(i)) + .build(); clientStream.send(request); } } @@ -291,9 +288,9 @@ public void onError(Throwable t) {} tempByteString = ByteString.copyFrom(sharedQueue.take()); request = - StreamingRecognizeRequest.newBuilder() - .setAudioContent(tempByteString) - .build(); + StreamingRecognizeRequest.newBuilder() + .setAudioContent(tempByteString) + .build(); audioInput.add(tempByteString); @@ -306,5 +303,6 @@ public void onError(Throwable t) {} } } } + } // [END speech_transcribe_infinite_streaming] From 968560c1e4fd7f6d0e0d956f3caf5f9b01e3e733 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 11 Jul 2019 16:23:42 -0700 Subject: [PATCH 5/7] Revert "timestamp change" This reverts commit df21c309003906d1769efbb9b2037da1fca37911. --- .../java/com/example/speech/InfiniteStreamRecognize.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java index 8c0723c4437..022e2123b98 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java @@ -90,9 +90,7 @@ public static String convertMillisToDate(double milliSeconds, DecimalFormat form } - /** - * Performs infinite streaming speech recognition - */ + /** Performs infinite streaming speech recognition */ public static void infiniteStreamingRecognize(String languageCode) throws Exception { // Microphone Input buffering @@ -143,6 +141,7 @@ public void onResponse(StreamingRecognizeResponse response) { + (STREAMING_LIMIT * restartCounter); DecimalFormat format = new DecimalFormat(); format.setMinimumIntegerDigits(2); + SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0); if (result.getIsFinal()) { System.out.print(GREEN); From efbc2c8d321ad212141e4db8a0c3e8f20fca770c Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 11 Jul 2019 16:53:07 -0700 Subject: [PATCH 6/7] removed indent issues --- .../speech/InfiniteStreamRecognize.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java index 022e2123b98..c429b525010 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java @@ -45,10 +45,12 @@ public class InfiniteStreamRecognize { + private static final int STREAMING_LIMIT = 290000; // ~5 minutes + public static final String RED = "\033[0;31m"; public static final String GREEN = "\033[0;32m"; public static final String YELLOW = "\033[0;33m"; - private static final int STREAMING_LIMIT = 290000; // ~5 minutes + // Creating shared object private static volatile BlockingQueue sharedQueue = new LinkedBlockingQueue(); private static TargetDataLine targetDataLine; @@ -121,10 +123,10 @@ public void run() { Thread micThread = new Thread(micrunnable); ResponseObserver responseObserver = null; try (SpeechClient client = SpeechClient.create()) { - ClientStream clientStream; responseObserver = new ResponseObserver() { + ArrayList responses = new ArrayList<>(); public void onStart(StreamController controller) { @@ -146,7 +148,8 @@ public void onResponse(StreamingRecognizeResponse response) { if (result.getIsFinal()) { System.out.print(GREEN); System.out.print("\033[2K\r"); - System.out.printf("%s: %s [confidence: %.2f]\n", convertMillisToDate(correctedTime, format), + System.out.printf("%s: %s [confidence: %.2f]\n", + convertMillisToDate(correctedTime, format), alternative.getTranscript(), alternative.getConfidence() ); @@ -170,24 +173,23 @@ public void onError(Throwable t) { }; clientStream = client.streamingRecognizeCallable().splitCall(responseObserver); - RecognitionConfig recognitionConfig = - RecognitionConfig.newBuilder() - .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) - .setLanguageCode(languageCode) - .setSampleRateHertz(16000) - .build(); + RecognitionConfig.newBuilder() + .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16) + .setLanguageCode("en-US") + .setSampleRateHertz(16000) + .build(); StreamingRecognitionConfig streamingRecognitionConfig = - StreamingRecognitionConfig.newBuilder() - .setConfig(recognitionConfig) - .setInterimResults(true) - .build(); + StreamingRecognitionConfig.newBuilder() + .setConfig(recognitionConfig) + .setInterimResults(true) + .build(); StreamingRecognizeRequest request = - StreamingRecognizeRequest.newBuilder() - .setStreamingConfig(streamingRecognitionConfig) - .build(); // The first request in a streaming call has to be a config + StreamingRecognizeRequest.newBuilder() + .setStreamingConfig(streamingRecognitionConfig) + .build(); // The first request in a streaming call has to be a config clientStream.send(request); @@ -196,9 +198,9 @@ public void onError(Throwable t) { // bigEndian: false AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false); DataLine.Info targetInfo = - new Info( - TargetDataLine.class, - audioFormat); // Set the system information to read from the microphone audio + new Info( + TargetDataLine.class, + audioFormat); // Set the system information to read from the microphone audio // stream if (!AudioSystem.isLineSupported(targetInfo)) { @@ -241,9 +243,9 @@ public void onError(Throwable t) { clientStream = client.streamingRecognizeCallable().splitCall(responseObserver); request = - StreamingRecognizeRequest.newBuilder() - .setStreamingConfig(streamingRecognitionConfig) - .build(); + StreamingRecognizeRequest.newBuilder() + .setStreamingConfig(streamingRecognitionConfig) + .build(); System.out.println(YELLOW); System.out.printf("%d: RESTARTING REQUEST\n", restartCounter * STREAMING_LIMIT); @@ -275,9 +277,9 @@ public void onError(Throwable t) { // set bridging offset for next request for (int i = chunksFromMS; i < lastAudioInput.size(); i++) { request = - StreamingRecognizeRequest.newBuilder() - .setAudioContent(lastAudioInput.get(i)) - .build(); + StreamingRecognizeRequest.newBuilder() + .setAudioContent(lastAudioInput.get(i)) + .build(); clientStream.send(request); } } @@ -287,9 +289,9 @@ public void onError(Throwable t) { tempByteString = ByteString.copyFrom(sharedQueue.take()); request = - StreamingRecognizeRequest.newBuilder() - .setAudioContent(tempByteString) - .build(); + StreamingRecognizeRequest.newBuilder() + .setAudioContent(tempByteString) + .build(); audioInput.add(tempByteString); From 2e4724a9279fa4b5c71622bcfc02baa870854c2e Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Fri, 19 Jul 2019 11:07:31 -0700 Subject: [PATCH 7/7] made requested changes --- .../com/example/speech/InfiniteStreamRecognize.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java index c429b525010..1ab6059a594 100644 --- a/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java +++ b/speech/cloud-client/src/main/java/com/example/speech/InfiniteStreamRecognize.java @@ -72,6 +72,7 @@ public static void main(String... args) { InfiniteStreamRecognizeOptions options = InfiniteStreamRecognizeOptions.fromFlags(args); if (options == null) { // Could not parse. + System.out.println("Failed to parse options."); System.exit(1); } @@ -82,14 +83,15 @@ public static void main(String... args) { } } - public static String convertMillisToDate(double milliSeconds, DecimalFormat format) { + public static String convertMillisToDate(double milliSeconds) { long millis = (long) milliSeconds; + DecimalFormat format = new DecimalFormat(); + format.setMinimumIntegerDigits(2); return String.format("%s:%s /", format.format(TimeUnit.MILLISECONDS.toMinutes(millis)), format.format(TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))) ); - } /** Performs infinite streaming speech recognition */ @@ -141,15 +143,13 @@ public void onResponse(StreamingRecognizeResponse response) { + (resultEndTime.getNanos() / 1000000)); double correctedTime = resultEndTimeInMS - bridgingOffset + (STREAMING_LIMIT * restartCounter); - DecimalFormat format = new DecimalFormat(); - format.setMinimumIntegerDigits(2); 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, format), + convertMillisToDate(correctedTime), alternative.getTranscript(), alternative.getConfidence() ); @@ -158,7 +158,7 @@ public void onResponse(StreamingRecognizeResponse response) { } else { System.out.print(RED); System.out.print("\033[2K\r"); - System.out.printf("%s: %s", format.format(correctedTime), + System.out.printf("%s: %s", convertMillisToDate(correctedTime), alternative.getTranscript() ); lastTranscriptWasFinal = false;