Skip to content

Commit

Permalink
move async call to proper pull of promise
Browse files Browse the repository at this point in the history
  • Loading branch information
victorabarros authored and danicuki committed Aug 16, 2023
1 parent 731b33f commit f3942cd
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ async function textToAudio(text, languageCode, gender, voiceName) {

async function convertConversation(fullConversation) {
let audioData = Buffer.alloc(0)
const tasks = []

for (const [role, message] of fullConversation) {
let audio
if (role === "user") {
audio = await textToAudio(message, "pt-BR", "MALE", "pt-BR-Wavenet-B")
tasks.push(textToAudio(message, "pt-BR", "MALE", "pt-BR-Wavenet-B"))
} else {
audio = await textToAudio(message, "pt-BR", "FEMALE", "pt-BR-Wavenet-A")
tasks.push(textToAudio(message, "pt-BR", "FEMALE", "pt-BR-Wavenet-A"))
}
audioData = Buffer.concat([audioData, audio])
}

const audios = await Promise.all(tasks)

audios.forEach(audio => {
audioData = Buffer.concat([audioData, audio])
})

// Salve o áudio em um arquivo
const writeFile = util.promisify(fs.writeFile)
await writeFile("tts_conversation.mp3", audioData)
Expand Down

0 comments on commit f3942cd

Please sign in to comment.