aiwrapper
Version:
A Universal AI Wrapper for JavaScript & TypeScript
50 lines (49 loc) • 1.57 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
class TextToSpeech {
static elevenlabs(options) {
return new ElevenLabsTextToSpeech(options);
}
}
class ElevenLabsTextToSpeech {
constructor(options) {
__publicField(this, "_config");
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.");
}
}
}
export {
ElevenLabsTextToSpeech,
TextToSpeech
};
//# sourceMappingURL=text2speech.js.map