From 136173f4de612b3b2691ef12c3facc9dd54698ca Mon Sep 17 00:00:00 2001 From: guilevi Date: Wed, 8 Mar 2023 13:14:48 +0000 Subject: [PATCH] Add Eleven Labs TTS --- tts/eleven/index.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tts/eleven/index.js diff --git a/tts/eleven/index.js b/tts/eleven/index.js new file mode 100644 index 0000000..3bb9fc3 --- /dev/null +++ b/tts/eleven/index.js @@ -0,0 +1,46 @@ +const BaseEngine = require('../BaseEngine'); +const fetch = require('node-fetch'); +const querystring = require('querystring'); + +module.exports = class extends BaseEngine { + constructor() { + super('eleven',"Eleven Labs TTS", "mp3"); + this.voices = {}; + this.populateVoiceList(); + } + async populateVoiceList() { + const url = "https://api.elevenlabs.io/v1/voices"; + const authorization = process.env.XI_API_KEY; + const opts = { + method: "get", + headers: { + 'xi-api-key': authorization + }, + } + const res = await fetch(url, opts); + const voices = await res.json(); + voices.voices.forEach((i) => { + let voiceName = i.name.toLowerCase(); + this.voices[voiceName] = i.voice_id; + }); + } + getDefaultVoice() { + return 'Guillem'; + } + async getSpeech(text, voice = this.getSpeechVoice(), params = {}) { + const url = "https://api.elevenlabs.io/v1/text-to-speech/" + this.getInternalVoiceName(voice); + const authorization = process.env.XI_API_KEY; + const opts = { + method: "post", + headers: { + 'Content-Type': 'application/json', + 'voice_id': this.getInternalVoiceName(voice), + 'xi-api-key': authorization + }, + body: JSON.stringify({ + text: text + }) + }; + return fetch(url, opts); + } +}; \ No newline at end of file