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

Function main

speech/transcribeStreaming.v2.js:21–94  ·  view source on GitHub ↗
(recognizerName)

Source from the content-addressed store, hash-verified

19// description: Streaming transcription of a local file using Speech-to-Text v2
20
21async function main(recognizerName) {
22 //[START speech_transcribe_streaming_v2]
23 /**
24 * TODO(developer): Uncomment the following lines before running the sample.
25 */
26 // const recognizerName = 'Resource name, e.g. projects/[projectId]/locations/global/recognizers/[recognizerId]'
27
28 const filename = 'resources/brooklyn.flac';
29 const fs = require('fs');
30
31 // Imports the Google Cloud client library
32 const speech = require('@google-cloud/speech').v2;
33
34 async function streamingRecognize() {
35 // Creates a client
36 const client = new speech.SpeechClient();
37
38 const recognitionConfig = {
39 // autoDecodingConfig removes the need to specify audio encoding.
40 // This field only needs to be present in the recognitionConfig
41 autoDecodingConfig: {},
42 };
43 const streamingConfig = {
44 config: recognitionConfig,
45 };
46
47 const configRequest = {
48 recognizer: recognizerName,
49 streamingConfig: streamingConfig,
50 };
51
52 // Read a file as bytes, base64 encoded
53 const readStream = fs.createReadStream(filename, {
54 highWaterMark: 4096,
55 encoding: 'base64',
56 });
57
58 const chunks = [];
59 let writeStream;
60 readStream
61 .on('data', chunk => {
62 const request = {
63 audio: chunk.toString(),
64 };
65 chunks.push(request);
66 })
67 .on('close', () => {
68 // Config-only request should be first in stream of requests
69 writeStream.write(configRequest);
70 for (const chunk of chunks) {
71 writeStream.write(chunk);
72 }
73 writeStream.end();
74 });
75
76 await new Promise((resolve, reject) => {
77 writeStream = client
78 ._streamingRecognize()

Callers

nothing calls this directly

Calls 1

streamingRecognizeFunction · 0.70

Tested by

no test coverage detected