* Generates synthetic audio from a String of SSML text. * * Given a string of SSML text and an output file name, this function * calls the Text-to-Speech API. The API returns a synthetic audio * version of the text, formatted according to the SSML commands. This * function saves the s
(ssmlText, outFile)
| 52 | * |
| 53 | */ |
| 54 | async function ssmlToAudio(ssmlText, outFile) { |
| 55 | // Creates a client |
| 56 | const client = new textToSpeech.TextToSpeechClient(); |
| 57 | |
| 58 | // Constructs the request |
| 59 | const request = { |
| 60 | // Select the text to synthesize |
| 61 | input: {ssml: ssmlText}, |
| 62 | // Select the language and SSML Voice Gender (optional) |
| 63 | voice: {languageCode: 'en-US', ssmlGender: 'MALE'}, |
| 64 | // Select the type of audio encoding |
| 65 | audioConfig: {audioEncoding: 'MP3'}, |
| 66 | }; |
| 67 | |
| 68 | // Performs the Text-to-Speech request |
| 69 | const [response] = await client.synthesizeSpeech(request); |
| 70 | // Write the binary audio content to a local file |
| 71 | const writeFile = util.promisify(fs.writeFile); |
| 72 | await writeFile(outFile, response.audioContent, 'binary'); |
| 73 | console.log('Audio content written to file ' + outFile); |
| 74 | } |
| 75 | // [END tts_ssml_address_audio] |
| 76 | |
| 77 | // [START tts_ssml_address_ssml] |