let { playAudioFile } = await kit("audio")
let { format } = await npm("date-fns")
let textToSpeech = await npm("@google-cloud/text-to-speech")
let client = new textToSpeech.TextToSpeechClient()
let text = await arg("What should I say?")
let voicesDB = db("voices", { voices: [] })
let voices = voicesDB.get("voices").value()
if (voices.length === 0) {
let [{ voices: englishVoices }] = await client.listVoices(
{
languageCode: "en",
}
)
let voiceChoices = englishVoices.map(voice => {
return {
name: `${voice.ssmlGender} - ${voice.name}`,
value: {
...voice,
languageCode: voice.name.slice(0, 4),
},
}
})
voicesDB.set("voices", voiceChoices).write()
voices = voicesDB.get("voices").value()
}
let voice =
typeof arg?.voice === "number"
? voices[arg?.voice].value
: await arg("Select voice", voices)
let effectsProfileId = ["headphone-class-device"]
let createRequest = (voice, text) => {
let speakingRate = 1
return {
input: { text },
voice,
audioConfig: {
audioEncoding: "MP3",
effectsProfileId,
speakingRate,
},
}
}
let request = createRequest(voice, text)
let safeFileName = text
.slice(0, 10)
.replace(/[^a-z0-9]/gi, "-")
.toLowerCase()
let date = format(new Date(), "yyyy-MM-dd-hh-mm-ss")
let fileName = `${date}-${safeFileName}.mp3`
let [response] = await client.synthesizeSpeech(request)
let textAudioPath = tmp(fileName)
await writeFile(
textAudioPath,
response.audioContent,
"binary"
)
playAudioFile(textAudioPath)