tardis-bot/tts/unreal/index.js

47 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-11-12 22:59:03 +00:00
const BaseEngine = require('../BaseEngine');
const fetch = require('node-fetch');
const querystring = require('querystring');
const fs = require("fs");
module.exports = class extends BaseEngine {
constructor() {
super('unreal', "Unreal Speech TTS", "mp3");
this.voices = {
scarlett: 'Scarlett',
liv: 'Liv',
dan: 'Dan',
will: 'Will',
amy: 'Amy'
};
}
getDefaultVoice() {
return 'Liv';
}
async getSpeechFile(text, filepath, voice = this.getDefaultVoice(), params = {}) {
const url = "https://api.v6.unrealspeech.com/speech";
const authorization = process.env.UNREAL_API_KEY;
const opts = {
method: "post",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authorization}`
},
body: JSON.stringify({
Bitrate: "320k",
Temperature: 0.1,
VoiceId: this.getInternalVoiceName(voice),
Text: text,
AudioFormat: "mp3"
})
};
const res = await fetch(url, opts);
const json = await res.json();
const data = await fetch(json.OutputUri);
const contents = await data.arrayBuffer();
const buf = Buffer.from(contents);
fs.writeFileSync(filepath, buf);
return filepath;
}
};