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

Function speechCallback

speech/infiniteStreaming.js:117–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115 }
116
117 const speechCallback = stream => {
118 // Convert API result end time from seconds + nanoseconds to milliseconds
119 resultEndTime =
120 stream.results[0].resultEndTime.seconds * 1000 +
121 Math.round(stream.results[0].resultEndTime.nanos / 1000000);
122
123 // Calculate correct time based on offset from audio sent twice
124 const correctedTime =
125 resultEndTime - bridgingOffset + streamingLimit * restartCounter;
126
127 process.stdout.clearLine();
128 process.stdout.cursorTo(0);
129 let stdoutText = '';
130 if (stream.results[0] && stream.results[0].alternatives[0]) {
131 stdoutText =
132 correctedTime + ': ' + stream.results[0].alternatives[0].transcript;
133 }
134
135 if (stream.results[0].isFinal) {
136 process.stdout.write(chalk.green(`${stdoutText}\n`));
137
138 isFinalEndTime = resultEndTime;
139 lastTranscriptWasFinal = true;
140 } else {
141 // Make sure transcript does not exceed console character length
142 if (stdoutText.length > process.stdout.columns) {
143 stdoutText =
144 stdoutText.substring(0, process.stdout.columns - 4) + '...';
145 }
146 process.stdout.write(chalk.red(`${stdoutText}`));
147
148 lastTranscriptWasFinal = false;
149 }
150 };
151
152 const audioInputStreamTransform = new Writable({
153 write(chunk, encoding, next) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected