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

Function main

speech/recognize.v1p1beta1.js:30–90  ·  view source on GitHub ↗
(
  filename,
  encoding = 'LINEAR16',
  sampleRateHertz = 16000,
  languageCode = 'en-US'
)

Source from the content-addressed store, hash-verified

28// usage: node recognize.v1p1beta1.js ./resources/commercial_mono.wav <encoding> <sampleRateHertz> <languageCode>
29
30function main(
31 filename,
32 encoding = 'LINEAR16',
33 sampleRateHertz = 16000,
34 languageCode = 'en-US'
35) {
36 // [START speech_transcribe_recognition_metadata_beta]
37 // Imports the Google Cloud client library for Beta API
38 /**
39 * TODO(developer): Update client library import to use new
40 * version of API when desired features become available
41 */
42 const speech = require('@google-cloud/speech').v1p1beta1;
43 const fs = require('fs');
44
45 // Creates a client
46 const client = new speech.SpeechClient();
47
48 async function syncRecognizeWithMetaData() {
49 /**
50 * TODO(developer): Uncomment the following lines before running the sample.
51 */
52 // const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
53 // const encoding = 'Encoding of the audio file, e.g. LINEAR16';
54 // const sampleRateHertz = 16000;
55 // const languageCode = 'BCP-47 language code, e.g. en-US';
56
57 const recognitionMetadata = {
58 interactionType: 'DISCUSSION',
59 microphoneDistance: 'NEARFIELD',
60 recordingDeviceType: 'SMARTPHONE',
61 recordingDeviceName: 'Pixel 2 XL',
62 industryNaicsCodeOfAudio: 519190,
63 };
64
65 const config = {
66 encoding: encoding,
67 sampleRateHertz: sampleRateHertz,
68 languageCode: languageCode,
69 metadata: recognitionMetadata,
70 };
71
72 const audio = {
73 content: fs.readFileSync(filename).toString('base64'),
74 };
75
76 const request = {
77 config: config,
78 audio: audio,
79 };
80
81 // Detects speech in the audio file
82 const [response] = await client.recognize(request);
83 response.results.forEach(result => {
84 const alternative = result.alternatives[0];
85 console.log(alternative.transcript);
86 });
87 // [END speech_transcribe_recognition_metadata_beta]

Callers 1

Calls 1

Tested by

no test coverage detected