@lobehub/tts
Version:
A high-quality & reliable TTS React Hooks library
48 lines (46 loc) • 1.4 kB
JavaScript
import { arrayBufferConvert } from "../utils/arrayBufferConvert.mjs";
import { OPENAI_BASE_URL } from "../const/api.mjs";
import voiceList, { getOpenaiVoiceOptions } from "./voiceList.mjs";
import urlJoin from "url-join";
//#region src/core/OpenAITTS/index.ts
var OpenAITTS = class {
constructor(api = {}) {
this.fetch = async (payload) => {
const url = urlJoin(this.OPENAI_BASE_URL, "audio/speech");
return this.serviceUrl ? fetch(this.serviceUrl, {
body: JSON.stringify(payload),
headers: this.headers,
method: "POST"
}) : fetch(url, {
body: JSON.stringify({
input: payload.input,
model: payload.options?.model || "tts-1",
voice: payload.options.voice
}),
headers: new Headers({
"Authorization": `Bearer ${this.OPENAI_API_KEY}`,
"Content-Type": "application/json"
}),
method: "POST"
});
};
this.create = async (payload) => {
return await this.fetch(payload);
};
this.createAudio = async (payload) => {
return await arrayBufferConvert(await (await this.create(payload)).arrayBuffer());
};
this.OPENAI_BASE_URL = api.OPENAI_PROXY_URL || OPENAI_BASE_URL;
this.OPENAI_API_KEY = api.OPENAI_API_KEY;
this.serviceUrl = api.serviceUrl;
this.headers = api.headers;
}
get voiceOptions() {
return getOpenaiVoiceOptions();
}
static {
this.voiceList = voiceList;
}
};
//#endregion
export { OpenAITTS };