* Translate text from an audio file. * @param {string} filename local path to * @param {string} encoding the encoding of the audio rate, e.g. Linear16 * @param {string} sourceLanguage language translating from, as BCP-47 code * @param {string} targetLanguage languate translating to, as BCP-47 co
(filename, encoding, sourceLanguage, targetLanguage)
| 21 | * @param {string} targetLanguage languate translating to, as BCP-47 code |
| 22 | */ |
| 23 | function main(filename, encoding, sourceLanguage, targetLanguage) { |
| 24 | // [START mediatranslation_quickstart] |
| 25 | const fs = require('fs'); |
| 26 | |
| 27 | // Imports the CLoud Media Translation client library |
| 28 | const { |
| 29 | SpeechTranslationServiceClient, |
| 30 | } = require('@google-cloud/media-translation'); |
| 31 | |
| 32 | // Creates a client |
| 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 |
no test coverage detected