( inFile = 'resources/example.txt', outFile = 'resources/example.mp3' )
| 15 | 'use strict'; |
| 16 | |
| 17 | async function main( |
| 18 | inFile = 'resources/example.txt', |
| 19 | outFile = 'resources/example.mp3' |
| 20 | ) { |
| 21 | // [START tts_ssml_address_test] |
| 22 | /** |
| 23 | * TODO (developer): Uncomment these variables before running the sample |
| 24 | * |
| 25 | */ |
| 26 | // inFile = 'resources/example.txt', |
| 27 | // outFile = 'resources/example.mp3' |
| 28 | // [START tts_ssml_address_imports] |
| 29 | // Imports the Google Cloud client library |
| 30 | const textToSpeech = require('@google-cloud/text-to-speech'); |
| 31 | |
| 32 | // Import other required libraries |
| 33 | const fs = require('fs'); |
| 34 | //const escape = require('escape-html'); |
| 35 | const util = require('util'); |
| 36 | // [END tts_ssml_address_imports] |
| 37 | |
| 38 | // [START tts_ssml_address_audio] |
| 39 | /** |
| 40 | * Generates synthetic audio from a String of SSML text. |
| 41 | * |
| 42 | * Given a string of SSML text and an output file name, this function |
| 43 | * calls the Text-to-Speech API. The API returns a synthetic audio |
| 44 | * version of the text, formatted according to the SSML commands. This |
| 45 | * function saves the synthetic audio to the designated output file. |
| 46 | * |
| 47 | * ARGS |
| 48 | * ssmlText: String of tagged SSML text |
| 49 | * outfile: String name of file under which to save audio output |
| 50 | * RETURNS |
| 51 | * nothing |
| 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 | } |
no test coverage detected