Created
June 20, 2020 17:03
-
-
Save fredriccliver/4fb13a8c8b6a6a5c42d044478ce91626 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const _ = require('lodash'); | |
const fs = require('fs'); | |
const textToSpeech = require('@google-cloud/text-to-speech'); | |
const client = new textToSpeech.TextToSpeechClient(); | |
const request = { | |
// The text to synthesize | |
input: { | |
text: 'This is an example' | |
}, | |
// The language code and SSML Voice Gender | |
voice: { | |
languageCode: 'en-US', | |
ssmlGender: 'NEUTRAL' | |
}, | |
// The audio encoding type | |
audioConfig: { | |
audioEncoding: 'MP3' | |
}, | |
}; | |
const outputFileName = 'output.mp3'; | |
client.synthesizeSpeech(request) | |
.then(async (response) => { | |
console.log(response); | |
const audioContent = _.get(response[0], 'audioContent'); | |
if (audioContent) { | |
fs.writeFileSync(outputFileName, audioContent, 'binary'); | |
console.log(`Audio content successfully written to file: ${outputFileName}`); | |
} else { | |
console.log('Failed to get audio content'); | |
} | |
}) | |
.catch((err) => { | |
console.error('ERROR:', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment