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

Function synthesizeText

texttospeech/synthesize.js:16–40  ·  view source on GitHub ↗
(text, outputFile)

Source from the content-addressed store, hash-verified

14
15'use strict';
16async function synthesizeText(text, outputFile) {
17 // [START tts_synthesize_text]
18 const textToSpeech = require('@google-cloud/text-to-speech');
19 const fs = require('fs');
20 const util = require('util');
21
22 const client = new textToSpeech.TextToSpeechClient();
23
24 /**
25 * TODO(developer): Uncomment the following lines before running the sample.
26 */
27 // const text = 'Text to synthesize, eg. hello';
28 // const outputFile = 'Local path to save audio file to, e.g. output.mp3';
29
30 const request = {
31 input: {text: text},
32 voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'},
33 audioConfig: {audioEncoding: 'MP3'},
34 };
35 const [response] = await client.synthesizeSpeech(request);
36 const writeFile = util.promisify(fs.writeFile);
37 await writeFile(outputFile, response.audioContent, 'binary');
38 console.log(`Audio content written to file: ${outputFile}`);
39 // [END tts_synthesize_text]
40}
41
42async function synthesizeSsml(ssml, outputFile) {
43 // [START tts_synthesize_ssml]

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected