| 16 | import {TextToSpeechClient} from '@google-cloud/text-to-speech'; |
| 17 | |
| 18 | async function main() { |
| 19 | // Create a Text-to-Speech client instance |
| 20 | const client = new TextToSpeechClient(); |
| 21 | |
| 22 | // The text that should be converted to speech |
| 23 | const text = 'hello, world!'; |
| 24 | |
| 25 | const outputFile = 'quickstart_output.mp3'; |
| 26 | |
| 27 | // Configure the text-to-speech request |
| 28 | const request = { |
| 29 | input: {text}, |
| 30 | // Select the language and SSML voice gender (optional) |
| 31 | voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'}, |
| 32 | // Configure the audio format of the output |
| 33 | audioConfig: {audioEncoding: 'MP3'}, |
| 34 | }; |
| 35 | |
| 36 | // Performs the text-to-speech request |
| 37 | const [response] = await client.synthesizeSpeech(request); |
| 38 | |
| 39 | // Save the generated binary audio content to a local file |
| 40 | await writeFile(outputFile, response.audioContent, 'binary'); |
| 41 | console.log(`Audio content written to file: ${outputFile}`); |
| 42 | } |
| 43 | |
| 44 | main().catch(err => { |
| 45 | console.error(err); |