MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / write

Function write

speech/infiniteStreaming.js:153–185  ·  view source on GitHub ↗
(chunk, encoding, next)

Source from the content-addressed store, hash-verified

151
152 const audioInputStreamTransform = new Writable({
153 write(chunk, encoding, next) {
154 if (newStream && lastAudioInput.length !== 0) {
155 // Approximate math to calculate time of chunks
156 const chunkTime = streamingLimit / lastAudioInput.length;
157 if (chunkTime !== 0) {
158 if (bridgingOffset < 0) {
159 bridgingOffset = 0;
160 }
161 if (bridgingOffset > finalRequestEndTime) {
162 bridgingOffset = finalRequestEndTime;
163 }
164 const chunksFromMS = Math.floor(
165 (finalRequestEndTime - bridgingOffset) / chunkTime
166 );
167 bridgingOffset = Math.floor(
168 (lastAudioInput.length - chunksFromMS) * chunkTime
169 );
170
171 for (let i = chunksFromMS; i < lastAudioInput.length; i++) {
172 recognizeStream.write(lastAudioInput[i]);
173 }
174 }
175 newStream = false;
176 }
177
178 audioInput.push(chunk);
179
180 if (recognizeStream) {
181 recognizeStream.write(chunk);
182 }
183
184 next();
185 },
186
187 final() {
188 if (recognizeStream) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected