Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: migrate code from googleapis/java-speech #7437

Merged
merged 165 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
165 commits
Select commit Hold shift + click to select a range
a181f3a
samples: Speech GA - library update (#1212)
nirupa-kumar Sep 24, 2018
cbb55b6
samples: Due to API backend changes, update the samples to match (#1595)
nnegrey Oct 1, 2019
70d1b1b
fix: update retry configs, adds generated samples (#26)
yoshi-automation Oct 18, 2019
f88cae0
build: move clirr to separate check (#30)
yoshi-automation Oct 21, 2019
8cd1669
feat: add speaker_tag to WordInfo (#40)
yoshi-automation Nov 12, 2019
ad7b7a0
chore: update common templates, regenerate tests
yoshi-automation Nov 19, 2019
58363e5
samples: Fix flaky speech test for speaker diarization (#1829)
nnegrey Jan 7, 2020
05c55fc
chore(regen): update license year for generated files (#82)
yoshi-automation Jan 15, 2020
b7d2aab
chore(regen): regenerate with updated year
yoshi-automation Feb 21, 2020
f026bfc
samples: move generated samples to generated directory (#105)
chingor13 Feb 24, 2020
3dd75da
chore: update common templates
yoshi-automation Feb 25, 2020
f28413b
samples: fix: flaky tests in speech (#2286)
nnegrey Mar 2, 2020
b26a8cd
samples: speech: move samples out of branch (#2324)
gcf-merge-on-green[bot] Mar 5, 2020
c0630ec
samples: scaffold pom.xml files (#118)
yoshi-automation Mar 17, 2020
8f9ba43
chore(deps): update dependency com.google.cloud:libraries-bom to v4.3…
renovate-bot Mar 23, 2020
89420ec
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Mar 25, 2020
f1d9952
samples: update shared config (#2443)
averikitsch Mar 27, 2020
77550e0
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Mar 30, 2020
45f14ec
chore(deps): update dependency com.google.cloud:libraries-bom to v4.4…
renovate-bot Apr 1, 2020
cdacf9a
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 6, 2020
aaab49b
chore(deps): update dependency com.google.cloud:libraries-bom to v4.4…
renovate-bot Apr 6, 2020
b37ae65
chore(deps): update dependency com.google.cloud:libraries-bom to v5 (…
renovate-bot Apr 14, 2020
deede1e
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 16, 2020
9869594
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 17, 2020
2c805ad
chore: fix samples snippets and update name in repo-metadata (#155)
chingor13 Apr 20, 2020
b155343
chore(deps): update dependency com.google.cloud:libraries-bom to v5.2…
renovate-bot Apr 23, 2020
8370d07
chore(deps): update dependency com.google.cloud:libraries-bom to v5.3…
renovate-bot Apr 28, 2020
50b70f6
chore(deps): update dependency com.google.cloud:libraries-bom to v5.5…
renovate-bot Jun 5, 2020
8cfbec0
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Jun 10, 2020
b1c8686
chore(deps): update dependency com.google.cloud:libraries-bom to v5.7…
renovate-bot Jun 10, 2020
1b82f18
chore(deps): update dependency com.google.cloud:libraries-bom to v6 (…
renovate-bot Jun 16, 2020
3834fd1
chore(deps): update dependency com.google.cloud:libraries-bom to v7 (…
renovate-bot Jun 17, 2020
37fb7f0
chore(deps): update dependency com.google.cloud:libraries-bom to v7.0…
renovate-bot Jun 22, 2020
17aad4f
chore(deps): update dependency com.google.cloud:libraries-bom to v8 (…
renovate-bot Jun 26, 2020
6380fa3
chore(deps): update dependency com.google.cloud:libraries-bom to v8.1…
renovate-bot Jul 16, 2020
e227e74
Merge remote-tracking branch 'dpe/master' into samples-speech-beta
chingor13 Aug 3, 2020
5a06849
samples: Add Speech API quickstart sample. (#497)
jmdobry Feb 2, 2017
6e66e4c
samples: Adds sync / async examples for local and remote files
gguuss Feb 7, 2017
3d7d7f0
samples: Fixes whitespace around while blocks
gguuss Feb 7, 2017
87656fd
samples: Adds some basic javadocs and comments
gguuss Feb 7, 2017
70b61d8
samples: Infer project from env
Mar 21, 2017
48522cd
samples: Updates to use v1 release.
gguuss Apr 6, 2017
d3607ff
samples: Fixes checkstyle issues.
gguuss Apr 6, 2017
df9b66a
samples: Adds streaming example and tests.
gguuss Apr 7, 2017
e59914f
samples: Nits found in self-review.
gguuss Apr 7, 2017
667fb0f
samples: Removes commented out code snippet and adds note on async lo…
gguuss Apr 11, 2017
5f2f0f0
samples: Speech async examples (#612)
gguuss Apr 13, 2017
53deb98
samples: Vision speech upgrade (#641)
gguuss May 2, 2017
4354cff
samples: updating to latest google-cloud-* dependencies (#723)
jabubake Jun 19, 2017
375ea2b
samples: Upgrades client and addresses changes to long running operat…
gguuss Jul 12, 2017
a245ce6
samples: Adds support for word time offset
gguuss Jul 31, 2017
027d0e2
samples: Minimizes cloud maven dependencies and fixes lint warnings
gguuss Jul 31, 2017
23b3862
samples: Fixes seconds reported in word time offsets and enables mave…
gguuss Aug 1, 2017
30bb89c
samples: Updates to highlight word time offsets (#787)
gguuss Aug 3, 2017
1b6f864
samples: Use only first alternative. Comments for clarity (#837)
jerjou Sep 1, 2017
5a77721
samples: Auto-update dependencies. (#853)
dpebot Sep 20, 2017
aa531df
samples: Auto-update dependencies. (#912)
dpebot Nov 15, 2017
8431d8a
samples: Updated mlengine, monitoring, pubsub, spanner, and speech. (…
kurtisvg Jan 18, 2018
16cd359
samples: Speech samples (#1036)
nnegrey Feb 21, 2018
c9faebc
samples: Add model selection to streaming sample (#1073)
nnegrey Mar 28, 2018
3001154
samples: Model selection (#1074)
nnegrey Mar 28, 2018
6835abd
samples: Add Auto-Punctuation samples to speech (#1079)
nnegrey Apr 6, 2018
feecc26
samples: Add samples for enhanced models and metadata (#1093)
nnegrey Apr 30, 2018
7c5b71f
samples: Add speech beta samples (#1151)
nirupa-kumar Jul 20, 2018
fc65abd
samples: [DO_NOT_MERGE] Microphone streaming with a 1 minute duration…
nirupa-kumar Aug 17, 2018
1918b1e
samples: Speech region tag update (#1188)
alixhami Aug 20, 2018
bd43af1
samples: updates word time offsets region tag (#1191)
alixhami Aug 21, 2018
6c704ee
samples: Speech GA - library update (#1212)
nirupa-kumar Sep 24, 2018
19f7dde
samples: Bump QuickStartSample to v1 (#1285)
nnegrey Dec 5, 2018
6dd4c26
samples: Infinite Stream recognition (#1297)
nirupa-kumar Dec 14, 2018
a8ac347
samples: Speech multi-channel GA (#1341)
nirupa-kumar Feb 19, 2019
7fc2cdb
samples: Data logging opt-in is no longer required for enhanced model…
Mar 22, 2019
dd52b3a
samples: Updated Infinite streaming sample (#1422)
May 16, 2019
ab222e0
samples: Revert Tests, product team rolled back changes, Auto Punctua…
nnegrey May 21, 2019
a3c36cd
samples: Increase timeout to 5 mins (#1453)
nnegrey Jun 10, 2019
a02abf4
samples: Update Recognize.java (#1460)
gogasca Jun 12, 2019
d250d85
samples: Add back missing break statement (#1512)
nnegrey Jul 15, 2019
c3e846c
samples: Added command line option class + option to pass different l…
munkhuushmgl Jul 22, 2019
3c274eb
samples: Update a default value to parameter (#1522)
munkhuushmgl Jul 23, 2019
4c18563
samples: Add samples for speech diarization ga (auto-punctuation samp…
nnegrey Nov 19, 2019
f6681b9
samples: speech: add ga samples and fix some flaky tests (#2049)
nnegrey Jan 31, 2020
bf14f71
samples: update shared config (#2443)
averikitsch Mar 27, 2020
cdfcf59
samples: speech: make flaky tests generic (#2825)
nnegrey May 1, 2020
5bb9ed5
samples: fix test dependencies
chingor13 Aug 14, 2020
89d8047
Merge branch 'master' into samples-speech-beta
chingor13 Aug 15, 2020
a952ad3
chore(deps): update dependency com.google.cloud:libraries-bom to v9 (…
renovate-bot Sep 8, 2020
5e92783
chore(deps): update dependency com.google.cloud:libraries-bom to v10 …
renovate-bot Sep 10, 2020
84b7bf4
chore(deps): update dependency com.google.cloud:libraries-bom to v11
renovate-bot Sep 24, 2020
555ebc7
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Oct 2, 2020
189cd4c
chore(deps): update dependency com.google.cloud:libraries-bom to v12 …
renovate-bot Oct 6, 2020
30e66f6
test(deps): update dependency junit:junit to v4.13.1
renovate-bot Oct 12, 2020
4a4b53f
chore(deps): update dependency com.google.cloud:libraries-bom to v12.…
renovate-bot Oct 15, 2020
6139b59
chore(deps): update dependency com.google.cloud:libraries-bom to v13 …
renovate-bot Oct 20, 2020
06cc424
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 21, 2020
52a440e
test(deps): update dependency com.google.truth:truth to v1.1 (#322)
renovate-bot Oct 22, 2020
af7ba57
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 23, 2020
b5a5574
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 27, 2020
9aa473f
chore(deps): update dependency com.google.cloud:libraries-bom to v13.…
renovate-bot Oct 30, 2020
346f478
chore(deps): update dependency com.google.cloud:libraries-bom to v14 …
renovate-bot Nov 4, 2020
a61949b
chore(deps): update dependency com.google.cloud:libraries-bom to v15 …
renovate-bot Nov 5, 2020
fba5c4a
chore(deps): update dependency com.google.cloud:libraries-bom to v15.…
renovate-bot Nov 17, 2020
db1e963
chore(deps): update dependency com.google.cloud:libraries-bom to v16 …
renovate-bot Nov 19, 2020
42c0f42
samples: add recognize sample with profanity filter (#376)
b-loved-dreamer Dec 12, 2020
5e3e78e
samples: refactor quickstart to use a gcs file (#378)
b-loved-dreamer Dec 14, 2020
ce83132
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Dec 15, 2020
919dda5
samples: add multi region transcribe sample (#394)
b-loved-dreamer Dec 17, 2020
946adaa
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Jan 7, 2021
46ba2f8
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Jan 20, 2021
36f3207
test(deps): update dependency com.google.truth:truth to v1.1.2 (#407)
renovate-bot Feb 2, 2021
b97ee31
chore(deps): update dependency com.google.cloud:libraries-bom to v16.…
renovate-bot Feb 10, 2021
b7ed82c
test(deps): update dependency junit:junit to v4.13.2 (#428)
renovate-bot Feb 16, 2021
3b8a888
chore(deps): update dependency com.google.cloud:libraries-bom to v17 …
renovate-bot Feb 24, 2021
6382340
chore(deps): update dependency com.google.cloud:libraries-bom to v18 …
renovate-bot Feb 25, 2021
8301c82
chore(deps): update dependency com.google.cloud:libraries-bom to v18.…
renovate-bot Mar 3, 2021
e5fb373
chore(deps): update dependency com.google.cloud:libraries-bom to v19 …
renovate-bot Mar 4, 2021
f9ef990
chore(samples): adds model adaptation sample (#468)
b-loved-dreamer Mar 18, 2021
3bacd64
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Apr 13, 2021
c5336dd
chore(deps): update dependency com.google.cloud:libraries-bom to v20 …
renovate-bot Apr 13, 2021
3048e22
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Apr 19, 2021
19b9256
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Apr 29, 2021
bafcfd0
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot May 11, 2021
1e7cff1
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot May 17, 2021
0bf4f09
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot May 25, 2021
7dd5898
test(deps): update dependency com.google.truth:truth to v1.1.3 (#537)
renovate-bot May 26, 2021
3d47150
chore: change region (#538)
munkhuushmgl Jun 1, 2021
e06f4a3
samples: adds export to GCS sample (#544)
munkhuushmgl Jun 4, 2021
cda3a18
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Jun 7, 2021
3f73a7f
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Jun 7, 2021
da0b9ee
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Jun 23, 2021
78fdd0a
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Jul 9, 2021
f5c870e
chore(deps): update dependency com.google.cloud:libraries-bom to v20.…
renovate-bot Jul 26, 2021
30891b3
chore(deps): update dependency com.google.cloud:libraries-bom to v21 …
renovate-bot Aug 17, 2021
054f549
chore(deps): update dependency com.google.cloud:libraries-bom to v22 …
renovate-bot Aug 27, 2021
bac1aae
chore(deps): update dependency com.google.cloud:libraries-bom to v23 …
renovate-bot Sep 8, 2021
a104598
chore: migrate to owlbot (#660)
Neenu1995 Sep 9, 2021
6ca35f2
chore(deps): update dependency com.google.cloud:libraries-bom to v23.…
renovate-bot Oct 1, 2021
8a5caca
chore(deps): update dependency com.google.cloud:libraries-bom to v24 …
renovate-bot Oct 27, 2021
9a659c7
deps: update dependency commons-cli:commons-cli to v1.5.0 (#720)
renovate-bot Nov 3, 2021
2cd39ba
sample: Configure polling algorithm in long recognition sample (#464)
baeminbo Nov 6, 2021
8c6d16a
chore: cleanup cloud RAD generation (#1269) (#725)
gcf-owl-bot[bot] Nov 8, 2021
0873c66
docs(samples): refactors the export-to-gcs sample (#737)
b-loved-dreamer Dec 2, 2021
0134bb0
deps: update dependency org.json:json to v20211205 (#745)
renovate-bot Dec 6, 2021
1fb0b2a
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Dec 7, 2021
23896e9
chore(deps): update dependency com.google.cloud.samples:shared-config…
renovate-bot Dec 8, 2021
2db7570
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Dec 8, 2021
1e33b1c
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Dec 28, 2021
f8f6b39
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Jan 7, 2022
eebff18
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Jan 12, 2022
9ab3654
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Feb 8, 2022
b51d1bd
chore(deps): update dependency com.google.cloud:libraries-bom to v24.…
renovate-bot Mar 2, 2022
b5ae200
deps: update dependency org.json:json to v20220320 (#835)
renovate-bot Mar 21, 2022
52bca21
chore(deps): update dependency com.google.cloud:libraries-bom to v25 …
renovate-bot Mar 30, 2022
d7ee484
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Apr 1, 2022
1a90bea
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Apr 27, 2022
b498631
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot May 16, 2022
6482f21
chore(deps): update dependency com.google.cloud:libraries-bom to v25.…
renovate-bot Jun 6, 2022
50d1c61
chore(deps): update dependency com.google.cloud:libraries-bom to v26 …
renovate-bot Jul 11, 2022
a49f63c
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Aug 16, 2022
7097d7f
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Aug 31, 2022
8dbd7fb
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Sep 20, 2022
5ecafb8
deps: update dependency org.json:json to v20220924 (#961)
renovate-bot Sep 26, 2022
c1937b1
chore(deps): update dependency com.google.cloud:libraries-bom to v26.…
renovate-bot Oct 7, 2022
24db28a
Merge remote-tracking branch 'migration/main' into java-speech-migration
Shabirmean Nov 17, 2022
254d129
chore: post migration updates - groupId, artifact url, repo references
Shabirmean Nov 17, 2022
e7c1c73
Merge branch 'main' into java-speech-migration
Shabirmean Nov 17, 2022
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
3 changes: 0 additions & 3 deletions speech/README.md

This file was deleted.

75 changes: 75 additions & 0 deletions speech/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.speech</groupId>
<artifactId>google-cloud-speech-snippets</artifactId>
<packaging>jar</packaging>
<name>Google Cloud Speech Snippets</name>
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/speech</url>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<!-- [START speech_install_with_bom] -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.1.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-speech</artifactId>
</dependency>
<!-- [START_EXCLUDE] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<!-- [END_EXCLUDE] -->
</dependencies>
<!-- [END speech_install_with_bom] -->
</project>
Binary file added speech/resources/Google_Gnome.wav
Binary file not shown.
Binary file added speech/resources/audio.raw
Binary file not shown.
Binary file added speech/resources/commercial_mono.wav
Binary file not shown.
Binary file added speech/resources/commercial_stereo.wav
Binary file not shown.
302 changes: 302 additions & 0 deletions speech/src/main/java/com/example/speech/InfiniteStreamRecognize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
/*
* Copyright 2018 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;

// [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;
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
import com.google.cloud.speech.v1p1beta1.SpeechClient;
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig;
import com.google.cloud.speech.v1p1beta1.StreamingRecognitionResult;
import com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest;
import com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse;
import com.google.protobuf.ByteString;
import com.google.protobuf.Duration;
import java.text.DecimalFormat;
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;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.TargetDataLine;

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";

// Creating shared object
private static volatile BlockingQueue<byte[]> 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<ByteString> audioInput = new ArrayList<ByteString>();
private static ArrayList<ByteString> lastAudioInput = new ArrayList<ByteString>();
private static int resultEndTimeInMS = 0;
private static int isFinalEndTime = 0;
private static int finalRequestEndTime = 0;
private static boolean newStream = true;
private static double bridgingOffset = 0;
private static boolean lastTranscriptWasFinal = false;
private static StreamController referenceToStreamController;
private static ByteString tempByteString;

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);
}

try {
infiniteStreamingRecognize(options.langCode);
} catch (Exception e) {
System.out.println("Exception caught: " + e);
}
}

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 */
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(); // The first request in a streaming call has to be a config

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,
audioFormat); // Set the system information to read from the microphone audio
// stream

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();
referenceToStreamController.cancel(); // remove Observer

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);
}
}
}
}
// [END speech_transcribe_infinite_streaming]
Loading