Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion speech/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
6 changes: 5 additions & 1 deletion speech/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
<version>1.9.0</version>
</dependency>
<!-- [END speech_quickstart_dependencies] -->

<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

}