UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

42 lines 1.38 kB
export class TextToSpeech { static elevenlabs(options) { return new ElevenLabsTextToSpeech(options); } } export class ElevenLabsTextToSpeech { constructor(options) { this._config = { apiKey: options.apiKey, model: options.model || "eleven_monolingual_v1", voiceId: options.voiceId || "pNInz6obpgDQGcFmaJgB", voice_settings: options.voice_settings || { stability: 0.5, similarity_boost: 0.5 }, }; } async ask(text) { const response = await fetch(`https://api.elevenlabs.io/v1/text-to-speech/${this._config.voiceId}`, { method: 'POST', headers: { 'accept': 'audio/mpeg', 'xi-api-key': this._config.apiKey, 'Content-Type': 'application/json' }, body: JSON.stringify({ "text": text, "model_id": this._config.model, "voice_settings": this._config.voice_settings }), }); if (response.ok) { const data = await response.blob(); const audioUrl = URL.createObjectURL(data); return audioUrl; } else { throw new Error("Failed to convert text to speech."); } } } //# sourceMappingURL=text2speech.js.map