natural-language-api-google/utils/textToSpeech.js

47 lines
1.2 KiB
JavaScript

// Imports the Google Cloud client library
const textToSpeech = require('@google-cloud/text-to-speech')
const fs = require('fs')
const util = require('util')
// Creates a client
const client = new textToSpeech.TextToSpeechClient()
const convertTextToSpeech = async (
text,
voice_name = 'pt-BR-Standard-B',
voice_gender = 'MALE',
languageCode = 'pt-BR'
) => {
try {
const request = {
input: { text: text },
voice: {
languageCode: languageCode,
name: voice_name,
ssmlGender: voice_gender,
},
audioConfig: { audioEncoding: 'MP3' },
}
// console.log('REQUEST: ', request)
// Performs the text-to-speech request
const [response] = await client.synthesizeSpeech(request)
return response.audioContent
// Write the binary audio content to a local file
// const writeFile = util.promisify(fs.writeFile)
// await writeFile('output.mp3', response.audioContent, 'binary')
// console.log('Audio content written to file: output.mp3')
} catch (error) {
console.log(
'There was an error on textToSpeech.ts file. Error in convertTextToSpeech function: ',
error
)
}
}
module.exports = convertTextToSpeech