Skip to content

Commit 6161d0f

Browse files
authored
samples: migrate code from googleapis/java-speech (#7437)
### Migrating samples from [googleapis/java-speech](https://togithub.com/googleapis/java-speech/tree/main/samples) into [java-docs-samples/speech](https://togithub.com/GoogleCloudPlatform/java-docs-samples) --- - samples: Speech GA - library update (#1212) - samples: Due to API backend changes, update the samples to match (#1595) - fix: update retry configs, adds generated samples (#26) - build: move clirr to separate check (#30) - feat: add speaker_tag to WordInfo (#40) - chore: update common templates, regenerate tests - samples: Fix flaky speech test for speaker diarization (#1829) - chore(regen): update license year for generated files (#82) - chore(regen): regenerate with updated year - samples: move generated samples to generated directory (#105) - chore: update common templates - samples: fix: flaky tests in speech (#2286) - samples: speech: move samples out of branch (#2324) - samples: scaffold pom.xml files (#118) - chore(deps): update dependency com.google.cloud:libraries-bom to v4.3.0 (#122) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.13 (#126) - samples: update shared config (#2443) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.14 (#130) - chore(deps): update dependency com.google.cloud:libraries-bom to v4.4.0 (#131) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.15 (#133) - chore(deps): update dependency com.google.cloud:libraries-bom to v4.4.1 (#134) - chore(deps): update dependency com.google.cloud:libraries-bom to v5 (#144) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.16 (#149) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.17 (#153) - chore: fix samples snippets and update name in repo-metadata (#155) - chore(deps): update dependency com.google.cloud:libraries-bom to v5.2.0 (#160) - chore(deps): update dependency com.google.cloud:libraries-bom to v5.3.0 (#167) - chore(deps): update dependency com.google.cloud:libraries-bom to v5.5.0 (#177) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.18 (#200) - chore(deps): update dependency com.google.cloud:libraries-bom to v5.7.0 (#199) - chore(deps): update dependency com.google.cloud:libraries-bom to v6 (#210) - chore(deps): update dependency com.google.cloud:libraries-bom to v7 (#214) - chore(deps): update dependency com.google.cloud:libraries-bom to v7.0.1 (#222) - chore(deps): update dependency com.google.cloud:libraries-bom to v8 (#227) - chore(deps): update dependency com.google.cloud:libraries-bom to v8.1.0 (#237) - samples: Add Speech API quickstart sample. (#497) - samples: Adds sync / async examples for local and remote files - samples: Fixes whitespace around while blocks - samples: Adds some basic javadocs and comments - samples: Infer project from env - samples: Updates to use v1 release. - samples: Fixes checkstyle issues. - samples: Adds streaming example and tests. - samples: Nits found in self-review. - samples: Removes commented out code snippet and adds note on async local file limit. - samples: Speech async examples (#612) - samples: Vision speech upgrade (#641) - samples: updating to latest google-cloud-* dependencies (#723) - samples: Upgrades client and addresses changes to long running operations - samples: Adds support for word time offset - samples: Minimizes cloud maven dependencies and fixes lint warnings - samples: Fixes seconds reported in word time offsets and enables maven checks - samples: Updates to highlight word time offsets (#787) - samples: Use only first alternative. Comments for clarity (#837) - samples: Auto-update dependencies. (#853) - samples: Auto-update dependencies. (#912) - samples: Updated mlengine, monitoring, pubsub, spanner, and speech. (#993) - samples: Speech samples (#1036) - samples: Add model selection to streaming sample (#1073) - samples: Model selection (#1074) - samples: Add Auto-Punctuation samples to speech (#1079) - samples: Add samples for enhanced models and metadata (#1093) - samples: Add speech beta samples (#1151) - samples: [DO_NOT_MERGE] Microphone streaming with a 1 minute duration. (#1185) - samples: Speech region tag update (#1188) - samples: updates word time offsets region tag (#1191) - samples: Speech GA - library update (#1212) - samples: Bump QuickStartSample to v1 (#1285) - samples: Infinite Stream recognition (#1297) - samples: Speech multi-channel GA (#1341) - samples: Data logging opt-in is no longer required for enhanced models (#1360) - samples: Updated Infinite streaming sample (#1422) - samples: Revert Tests, product team rolled back changes, Auto Punctuation behavior is back to the expected output (#1428) - samples: Increase timeout to 5 mins (#1453) - samples: Update Recognize.java (#1460) - samples: Add back missing break statement (#1512) - samples: Added command line option class + option to pass different lang code as argument (#1504) - samples: Update a default value to parameter (#1522) - samples: Add samples for speech diarization ga (auto-punctuation samples alrea… (#1744) - samples: speech: add ga samples and fix some flaky tests (#2049) - samples: update shared config (#2443) - samples: speech: make flaky tests generic (#2825) - samples: fix test dependencies - chore(deps): update dependency com.google.cloud:libraries-bom to v9 (#263) - chore(deps): update dependency com.google.cloud:libraries-bom to v10 (#271) - chore(deps): update dependency com.google.cloud:libraries-bom to v11 - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.21 (#294) - chore(deps): update dependency com.google.cloud:libraries-bom to v12 (#298) - test(deps): update dependency junit:junit to v4.13.1 - chore(deps): update dependency com.google.cloud:libraries-bom to v12.1.0 (#310) - chore(deps): update dependency com.google.cloud:libraries-bom to v13 (#321) - chore(deps): update dependency com.google.cloud:libraries-bom to v13.1.0 (#326) - test(deps): update dependency com.google.truth:truth to v1.1 (#322) - chore(deps): update dependency com.google.cloud:libraries-bom to v13.2.0 (#332) - chore(deps): update dependency com.google.cloud:libraries-bom to v13.3.0 (#334) - chore(deps): update dependency com.google.cloud:libraries-bom to v13.4.0 (#338) - chore(deps): update dependency com.google.cloud:libraries-bom to v14 (#347) - chore(deps): update dependency com.google.cloud:libraries-bom to v15 (#350) - chore(deps): update dependency com.google.cloud:libraries-bom to v15.1.0 (#357) - chore(deps): update dependency com.google.cloud:libraries-bom to v16 (#364) - samples: add recognize sample with profanity filter (#376) - samples: refactor quickstart to use a gcs file (#378) - chore(deps): update dependency com.google.cloud:libraries-bom to v16.2.0 (#389) - samples: add multi region transcribe sample (#394) - chore(deps): update dependency com.google.cloud:libraries-bom to v16.2.1 (#398) - chore(deps): update dependency com.google.cloud:libraries-bom to v16.3.0 (#405) - test(deps): update dependency com.google.truth:truth to v1.1.2 (#407) - chore(deps): update dependency com.google.cloud:libraries-bom to v16.4.0 (#423) - test(deps): update dependency junit:junit to v4.13.2 (#428) - chore(deps): update dependency com.google.cloud:libraries-bom to v17 (#441) - chore(deps): update dependency com.google.cloud:libraries-bom to v18 (#445) - chore(deps): update dependency com.google.cloud:libraries-bom to v18.1.0 (#456) - chore(deps): update dependency com.google.cloud:libraries-bom to v19 (#459) - chore(samples): adds model adaptation sample (#468) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.22 (#482) - chore(deps): update dependency com.google.cloud:libraries-bom to v20 (#486) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.1.0 (#493) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.2.0 (#505) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.3.0 (#514) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.4.0 (#523) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.5.0 (#535) - test(deps): update dependency com.google.truth:truth to v1.1.3 (#537) - chore: change region (#538) - samples: adds export to GCS sample (#544) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.6.0 (#552) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.23 (#551) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.7.0 (#568) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.8.0 (#578) - chore(deps): update dependency com.google.cloud:libraries-bom to v20.9.0 (#589) - chore(deps): update dependency com.google.cloud:libraries-bom to v21 (#625) - chore(deps): update dependency com.google.cloud:libraries-bom to v22 (#650) - chore(deps): update dependency com.google.cloud:libraries-bom to v23 (#663) - chore: migrate to owlbot (#660) - chore(deps): update dependency com.google.cloud:libraries-bom to v23.1.0 (#702) - chore(deps): update dependency com.google.cloud:libraries-bom to v24 (#719) - deps: update dependency commons-cli:commons-cli to v1.5.0 (#720) - sample: Configure polling algorithm in long recognition sample (#464) - chore: cleanup cloud RAD generation (#1269) (#725) - docs(samples): refactors the export-to-gcs sample (#737) - deps: update dependency org.json:json to v20211205 (#745) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.0.24 (#742) - chore(deps): update dependency com.google.cloud.samples:shared-configuration to v1.2.0 (#753) - chore(deps): update dependency com.google.cloud:libraries-bom to v24.1.0 (#758) - chore(deps): update dependency com.google.cloud:libraries-bom to v24.1.1 (#759) - chore(deps): update dependency com.google.cloud:libraries-bom to v24.1.2 (#764) - chore(deps): update dependency com.google.cloud:libraries-bom to v24.2.0 (#775) - chore(deps): update dependency com.google.cloud:libraries-bom to v24.3.0 (#794) - chore(deps): update dependency com.google.cloud:libraries-bom to v24.4.0 (#823) - deps: update dependency org.json:json to v20220320 (#835) - chore(deps): update dependency com.google.cloud:libraries-bom to v25 (#834) - chore(deps): update dependency com.google.cloud:libraries-bom to v25.1.0 (#849) - chore(deps): update dependency com.google.cloud:libraries-bom to v25.2.0 (#876) - chore(deps): update dependency com.google.cloud:libraries-bom to v25.3.0 (#883) - chore(deps): update dependency com.google.cloud:libraries-bom to v25.4.0 (#892) - chore(deps): update dependency com.google.cloud:libraries-bom to v26 (#918) - chore(deps): update dependency com.google.cloud:libraries-bom to v26.1.0 (#938) - chore(deps): update dependency com.google.cloud:libraries-bom to v26.1.1 (#941) - chore(deps): update dependency com.google.cloud:libraries-bom to v26.1.2 (#957) - deps: update dependency org.json:json to v20220924 (#961) - chore(deps): update dependency com.google.cloud:libraries-bom to v26.1.3 (#975) Fixes #issue > It's a good idea to open an issue first for discussion. - [ ] I have followed [Sample Format Guide](https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md) - [ ] `pom.xml` parent set to latest `shared-configuration` - [ ] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [ ] **Tests** pass: `mvn clean verify` **required** - [ ] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [ ] Please **merge** this PR for me once it is approved.
1 parent eba49f5 commit 6161d0f

21 files changed

+2853
-3
lines changed

speech/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

speech/pom.xml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example.speech</groupId>
5+
<artifactId>google-cloud-speech-snippets</artifactId>
6+
<packaging>jar</packaging>
7+
<name>Google Cloud Speech Snippets</name>
8+
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/speech</url>
9+
10+
<!--
11+
The parent pom defines common style checks and testing strategies for our samples.
12+
Removing or replacing it should not affect the execution of the samples in anyway.
13+
-->
14+
<parent>
15+
<groupId>com.google.cloud.samples</groupId>
16+
<artifactId>shared-configuration</artifactId>
17+
<version>1.2.0</version>
18+
</parent>
19+
20+
<properties>
21+
<maven.compiler.target>1.8</maven.compiler.target>
22+
<maven.compiler.source>1.8</maven.compiler.source>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
</properties>
25+
26+
27+
<!-- [START speech_install_with_bom] -->
28+
<dependencyManagement>
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.google.cloud</groupId>
32+
<artifactId>libraries-bom</artifactId>
33+
<version>26.1.3</version>
34+
<type>pom</type>
35+
<scope>import</scope>
36+
</dependency>
37+
</dependencies>
38+
</dependencyManagement>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.json</groupId>
43+
<artifactId>json</artifactId>
44+
<version>20220924</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>google-cloud-speech</artifactId>
49+
</dependency>
50+
<!-- [START_EXCLUDE] -->
51+
<dependency>
52+
<groupId>com.google.cloud</groupId>
53+
<artifactId>google-cloud-storage</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>commons-cli</groupId>
57+
<artifactId>commons-cli</artifactId>
58+
<version>1.5.0</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>junit</groupId>
62+
<artifactId>junit</artifactId>
63+
<version>4.13.2</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.truth</groupId>
68+
<artifactId>truth</artifactId>
69+
<version>1.1.3</version>
70+
<scope>test</scope>
71+
</dependency>
72+
<!-- [END_EXCLUDE] -->
73+
</dependencies>
74+
<!-- [END speech_install_with_bom] -->
75+
</project>

speech/resources/Google_Gnome.wav

1.7 MB
Binary file not shown.

speech/resources/audio.raw

56.6 KB
Binary file not shown.

speech/resources/commercial_mono.wav

708 KB
Binary file not shown.
6.01 MB
Binary file not shown.
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.speech;
18+
19+
// [START speech_transcribe_infinite_streaming]
20+
21+
import com.google.api.gax.rpc.ClientStream;
22+
import com.google.api.gax.rpc.ResponseObserver;
23+
import com.google.api.gax.rpc.StreamController;
24+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
25+
import com.google.cloud.speech.v1p1beta1.SpeechClient;
26+
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
27+
import com.google.cloud.speech.v1p1beta1.StreamingRecognitionConfig;
28+
import com.google.cloud.speech.v1p1beta1.StreamingRecognitionResult;
29+
import com.google.cloud.speech.v1p1beta1.StreamingRecognizeRequest;
30+
import com.google.cloud.speech.v1p1beta1.StreamingRecognizeResponse;
31+
import com.google.protobuf.ByteString;
32+
import com.google.protobuf.Duration;
33+
import java.text.DecimalFormat;
34+
import java.util.ArrayList;
35+
import java.util.concurrent.BlockingQueue;
36+
import java.util.concurrent.LinkedBlockingQueue;
37+
import java.util.concurrent.TimeUnit;
38+
import javax.sound.sampled.AudioFormat;
39+
import javax.sound.sampled.AudioSystem;
40+
import javax.sound.sampled.DataLine;
41+
import javax.sound.sampled.DataLine.Info;
42+
import javax.sound.sampled.TargetDataLine;
43+
44+
public class InfiniteStreamRecognize {
45+
46+
private static final int STREAMING_LIMIT = 290000; // ~5 minutes
47+
48+
public static final String RED = "\033[0;31m";
49+
public static final String GREEN = "\033[0;32m";
50+
public static final String YELLOW = "\033[0;33m";
51+
52+
// Creating shared object
53+
private static volatile BlockingQueue<byte[]> sharedQueue = new LinkedBlockingQueue();
54+
private static TargetDataLine targetDataLine;
55+
private static int BYTES_PER_BUFFER = 6400; // buffer size in bytes
56+
57+
private static int restartCounter = 0;
58+
private static ArrayList<ByteString> audioInput = new ArrayList<ByteString>();
59+
private static ArrayList<ByteString> lastAudioInput = new ArrayList<ByteString>();
60+
private static int resultEndTimeInMS = 0;
61+
private static int isFinalEndTime = 0;
62+
private static int finalRequestEndTime = 0;
63+
private static boolean newStream = true;
64+
private static double bridgingOffset = 0;
65+
private static boolean lastTranscriptWasFinal = false;
66+
private static StreamController referenceToStreamController;
67+
private static ByteString tempByteString;
68+
69+
public static void main(String... args) {
70+
InfiniteStreamRecognizeOptions options = InfiniteStreamRecognizeOptions.fromFlags(args);
71+
if (options == null) {
72+
// Could not parse.
73+
System.out.println("Failed to parse options.");
74+
System.exit(1);
75+
}
76+
77+
try {
78+
infiniteStreamingRecognize(options.langCode);
79+
} catch (Exception e) {
80+
System.out.println("Exception caught: " + e);
81+
}
82+
}
83+
84+
public static String convertMillisToDate(double milliSeconds) {
85+
long millis = (long) milliSeconds;
86+
DecimalFormat format = new DecimalFormat();
87+
format.setMinimumIntegerDigits(2);
88+
return String.format(
89+
"%s:%s /",
90+
format.format(TimeUnit.MILLISECONDS.toMinutes(millis)),
91+
format.format(
92+
TimeUnit.MILLISECONDS.toSeconds(millis)
93+
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
94+
}
95+
96+
/** Performs infinite streaming speech recognition */
97+
public static void infiniteStreamingRecognize(String languageCode) throws Exception {
98+
99+
// Microphone Input buffering
100+
class MicBuffer implements Runnable {
101+
102+
@Override
103+
public void run() {
104+
System.out.println(YELLOW);
105+
System.out.println("Start speaking...Press Ctrl-C to stop");
106+
targetDataLine.start();
107+
byte[] data = new byte[BYTES_PER_BUFFER];
108+
while (targetDataLine.isOpen()) {
109+
try {
110+
int numBytesRead = targetDataLine.read(data, 0, data.length);
111+
if ((numBytesRead <= 0) && (targetDataLine.isOpen())) {
112+
continue;
113+
}
114+
sharedQueue.put(data.clone());
115+
} catch (InterruptedException e) {
116+
System.out.println("Microphone input buffering interrupted : " + e.getMessage());
117+
}
118+
}
119+
}
120+
}
121+
122+
// Creating microphone input buffer thread
123+
MicBuffer micrunnable = new MicBuffer();
124+
Thread micThread = new Thread(micrunnable);
125+
ResponseObserver<StreamingRecognizeResponse> responseObserver = null;
126+
try (SpeechClient client = SpeechClient.create()) {
127+
ClientStream<StreamingRecognizeRequest> clientStream;
128+
responseObserver =
129+
new ResponseObserver<StreamingRecognizeResponse>() {
130+
131+
ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();
132+
133+
public void onStart(StreamController controller) {
134+
referenceToStreamController = controller;
135+
}
136+
137+
public void onResponse(StreamingRecognizeResponse response) {
138+
responses.add(response);
139+
StreamingRecognitionResult result = response.getResultsList().get(0);
140+
Duration resultEndTime = result.getResultEndTime();
141+
resultEndTimeInMS =
142+
(int)
143+
((resultEndTime.getSeconds() * 1000) + (resultEndTime.getNanos() / 1000000));
144+
double correctedTime =
145+
resultEndTimeInMS - bridgingOffset + (STREAMING_LIMIT * restartCounter);
146+
147+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
148+
if (result.getIsFinal()) {
149+
System.out.print(GREEN);
150+
System.out.print("\033[2K\r");
151+
System.out.printf(
152+
"%s: %s [confidence: %.2f]\n",
153+
convertMillisToDate(correctedTime),
154+
alternative.getTranscript(),
155+
alternative.getConfidence());
156+
isFinalEndTime = resultEndTimeInMS;
157+
lastTranscriptWasFinal = true;
158+
} else {
159+
System.out.print(RED);
160+
System.out.print("\033[2K\r");
161+
System.out.printf(
162+
"%s: %s", convertMillisToDate(correctedTime), alternative.getTranscript());
163+
lastTranscriptWasFinal = false;
164+
}
165+
}
166+
167+
public void onComplete() {}
168+
169+
public void onError(Throwable t) {}
170+
};
171+
clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
172+
173+
RecognitionConfig recognitionConfig =
174+
RecognitionConfig.newBuilder()
175+
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
176+
.setLanguageCode(languageCode)
177+
.setSampleRateHertz(16000)
178+
.build();
179+
180+
StreamingRecognitionConfig streamingRecognitionConfig =
181+
StreamingRecognitionConfig.newBuilder()
182+
.setConfig(recognitionConfig)
183+
.setInterimResults(true)
184+
.build();
185+
186+
StreamingRecognizeRequest request =
187+
StreamingRecognizeRequest.newBuilder()
188+
.setStreamingConfig(streamingRecognitionConfig)
189+
.build(); // The first request in a streaming call has to be a config
190+
191+
clientStream.send(request);
192+
193+
try {
194+
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
195+
// bigEndian: false
196+
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
197+
DataLine.Info targetInfo =
198+
new Info(
199+
TargetDataLine.class,
200+
audioFormat); // Set the system information to read from the microphone audio
201+
// stream
202+
203+
if (!AudioSystem.isLineSupported(targetInfo)) {
204+
System.out.println("Microphone not supported");
205+
System.exit(0);
206+
}
207+
// Target data line captures the audio stream the microphone produces.
208+
targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
209+
targetDataLine.open(audioFormat);
210+
micThread.start();
211+
212+
long startTime = System.currentTimeMillis();
213+
214+
while (true) {
215+
216+
long estimatedTime = System.currentTimeMillis() - startTime;
217+
218+
if (estimatedTime >= STREAMING_LIMIT) {
219+
220+
clientStream.closeSend();
221+
referenceToStreamController.cancel(); // remove Observer
222+
223+
if (resultEndTimeInMS > 0) {
224+
finalRequestEndTime = isFinalEndTime;
225+
}
226+
resultEndTimeInMS = 0;
227+
228+
lastAudioInput = null;
229+
lastAudioInput = audioInput;
230+
audioInput = new ArrayList<ByteString>();
231+
232+
restartCounter++;
233+
234+
if (!lastTranscriptWasFinal) {
235+
System.out.print('\n');
236+
}
237+
238+
newStream = true;
239+
240+
clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
241+
242+
request =
243+
StreamingRecognizeRequest.newBuilder()
244+
.setStreamingConfig(streamingRecognitionConfig)
245+
.build();
246+
247+
System.out.println(YELLOW);
248+
System.out.printf("%d: RESTARTING REQUEST\n", restartCounter * STREAMING_LIMIT);
249+
250+
startTime = System.currentTimeMillis();
251+
252+
} else {
253+
254+
if ((newStream) && (lastAudioInput.size() > 0)) {
255+
// if this is the first audio from a new request
256+
// calculate amount of unfinalized audio from last request
257+
// resend the audio to the speech client before incoming audio
258+
double chunkTime = STREAMING_LIMIT / lastAudioInput.size();
259+
// ms length of each chunk in previous request audio arrayList
260+
if (chunkTime != 0) {
261+
if (bridgingOffset < 0) {
262+
// bridging Offset accounts for time of resent audio
263+
// calculated from last request
264+
bridgingOffset = 0;
265+
}
266+
if (bridgingOffset > finalRequestEndTime) {
267+
bridgingOffset = finalRequestEndTime;
268+
}
269+
int chunksFromMs =
270+
(int) Math.floor((finalRequestEndTime - bridgingOffset) / chunkTime);
271+
// chunks from MS is number of chunks to resend
272+
bridgingOffset =
273+
(int) Math.floor((lastAudioInput.size() - chunksFromMs) * chunkTime);
274+
// set bridging offset for next request
275+
for (int i = chunksFromMs; i < lastAudioInput.size(); i++) {
276+
request =
277+
StreamingRecognizeRequest.newBuilder()
278+
.setAudioContent(lastAudioInput.get(i))
279+
.build();
280+
clientStream.send(request);
281+
}
282+
}
283+
newStream = false;
284+
}
285+
286+
tempByteString = ByteString.copyFrom(sharedQueue.take());
287+
288+
request =
289+
StreamingRecognizeRequest.newBuilder().setAudioContent(tempByteString).build();
290+
291+
audioInput.add(tempByteString);
292+
}
293+
294+
clientStream.send(request);
295+
}
296+
} catch (Exception e) {
297+
System.out.println(e);
298+
}
299+
}
300+
}
301+
}
302+
// [END speech_transcribe_infinite_streaming]

0 commit comments

Comments
 (0)