47 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 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({
 | |
| 				model_id: 'eleven_multilingual_v2',
 | |
| 				text: text
 | |
| 			})
 | |
| 		};
 | |
| 		return fetch(url, opts);
 | |
| 	}
 | |
| }; |