Skip to content

Instantly share code, notes, and snippets.

@fredriccliver
Created June 20, 2020 17:03
Show Gist options
  • Save fredriccliver/4fb13a8c8b6a6a5c42d044478ce91626 to your computer and use it in GitHub Desktop.
Save fredriccliver/4fb13a8c8b6a6a5c42d044478ce91626 to your computer and use it in GitHub Desktop.
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