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

Function main

speech/quickstart.v2.js:21–72  ·  view source on GitHub ↗
(
  projectId,
  recognizerId = 'my-recognizer',
  audioFilePath = 'resources/brooklyn.flac'
)

Source from the content-addressed store, hash-verified

19// description: Transcribe a local file using Speech-to-Text v2
20
21async function main(
22 projectId,
23 recognizerId = 'my-recognizer',
24 audioFilePath = 'resources/brooklyn.flac'
25) {
26 // [START speech_quickstart_v2]
27 /**
28 * TODO(developer): Uncomment these variables before running the sample.
29 */
30 // const projectId = "your-project-id";
31 // const recognizerId = "your-recognizer-id";
32 // const audioFilePath = "path/to/audio/file";
33
34 // Imports the Google Cloud Speech-to-Text library (v2)
35 const speech = require('@google-cloud/speech');
36 const fs = require('fs');
37 async function quickstartV2() {
38 // Instantiates a client
39 const client = new speech.v2.SpeechClient();
40
41 const recognizerRequest = {
42 parent: `projects/${projectId}/locations/global`,
43 recognizerId: recognizerId,
44 recognizer: {
45 languageCodes: ['en-US'],
46 model: 'latest_long',
47 },
48 };
49
50 const operation = await client.createRecognizer(recognizerRequest);
51 const recognizer = operation[0].result;
52
53 const content = fs.readFileSync(audioFilePath).toString('base64');
54
55 const transcriptionRequest = {
56 recognizer: recognizer.name,
57 config: {
58 // Automatically detects audio encoding
59 autoDecodingConfig: {},
60 },
61 content: content,
62 };
63
64 const response = await client.recognize(transcriptionRequest);
65 for (const result of response[0].results) {
66 console.log(`Transcript: ${result.alternatives[0].transcript}`);
67 }
68 }
69
70 await quickstartV2();
71 // [END speech_quickstart_v2]
72}
73
74exports.quickstartV2 = main;

Callers

nothing calls this directly

Calls 1

quickstartV2Function · 0.85

Tested by

no test coverage detected