| 67 | } |
| 68 | |
| 69 | async function synthesizeTextFile(textFile, outputFile) { |
| 70 | // [START tts_synthesize_text_file] |
| 71 | const textToSpeech = require('@google-cloud/text-to-speech'); |
| 72 | const fs = require('fs'); |
| 73 | const util = require('util'); |
| 74 | |
| 75 | const client = new textToSpeech.TextToSpeechClient(); |
| 76 | |
| 77 | /** |
| 78 | * TODO(developer): Uncomment the following lines before running the sample. |
| 79 | */ |
| 80 | // const textFile = 'Local path to text file, eg. input.txt'; |
| 81 | // const outputFile = 'Local path to save audio file to, e.g. output.mp3'; |
| 82 | |
| 83 | const request = { |
| 84 | input: {text: fs.readFileSync(textFile)}, |
| 85 | voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'}, |
| 86 | audioConfig: {audioEncoding: 'MP3'}, |
| 87 | }; |
| 88 | |
| 89 | const [response] = await client.synthesizeSpeech(request); |
| 90 | const writeFile = util.promisify(fs.writeFile); |
| 91 | await writeFile(outputFile, response.audioContent, 'binary'); |
| 92 | console.log(`Audio content written to file: ${outputFile}`); |
| 93 | // [END tts_synthesize_text_file] |
| 94 | } |
| 95 | |
| 96 | async function synthesizeSsmlFile(ssmlFile, outputFile) { |
| 97 | // [START tts_synthesize_ssml_file] |