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

Function quickstart

mediatranslation/quickstart.js:35–97  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33 const client = new SpeechTranslationServiceClient();
34
35 async function quickstart() {
36 /**
37 * TODO(developer): Uncomment the following lines before running the sample.
38 */
39 // const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
40 // const encoding = 'Encoding of the audio file, e.g. LINEAR16';
41 // const sourceLanguage = 'BCP-47 source language code, e.g. en-US';
42 // const targetLanguage = 'BCP-47 target language code, e.g. es-ES';
43
44 const config = {
45 audioConfig: {
46 audioEncoding: encoding,
47 sourceLanguageCode: sourceLanguage,
48 targetLanguageCode: targetLanguage,
49 },
50 };
51
52 // First request needs to have only a streaming config, no data.
53 const initialRequest = {
54 streamingConfig: config,
55 audioContent: null,
56 };
57
58 const readStream = fs.createReadStream(filename, {
59 highWaterMark: 4096,
60 encoding: 'base64',
61 });
62
63 const chunks = [];
64 readStream
65 .on('data', chunk => {
66 const request = {
67 streamingConfig: config,
68 audioContent: chunk.toString(),
69 };
70 chunks.push(request);
71 })
72 .on('close', () => {
73 // Config-only request should be first in stream of requests
74 stream.write(initialRequest);
75 for (let i = 0; i < chunks.length; i++) {
76 stream.write(chunks[i]);
77 }
78 stream.end();
79 });
80
81 const stream = client.streamingTranslateSpeech().on('data', response => {
82 const {result} = response;
83 if (result.textTranslationResult.isFinal) {
84 console.log(
85 `\nFinal translation: ${result.textTranslationResult.translation}`
86 );
87 console.log(`Final recognition result: ${result.recognitionResult}`);
88 } else {
89 console.log(
90 `\nPartial translation: ${result.textTranslationResult.translation}`
91 );
92 console.log(`Partial recognition result: ${result.recognitionResult}`);

Callers 1

mainFunction · 0.70

Calls 1

toStringMethod · 0.80

Tested by

no test coverage detected