js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
137 lines (136 loc) • 5.89 kB
JavaScript
// Factory for TTS clients (browser/server compatible)
import { AzureTTSClient } from "./engines/azure.js";
import { CartesiaTTSClient } from "./engines/cartesia.js";
import { CereVoiceTTSClient } from "./engines/cerevoice.js";
import { DeepgramTTSClient } from "./engines/deepgram.js";
import { ElevenLabsTTSClient } from "./engines/elevenlabs.js";
import { EspeakTTSClient } from "./engines/espeak.js";
import { EspeakWasmTTSClient } from "./engines/espeak-wasm.js";
import { FishAudioTTSClient } from "./engines/fishaudio.js";
import { GeminiTTSClient } from "./engines/gemini.js";
import { GoogleTTSClient } from "./engines/google.js";
import { HumeTTSClient } from "./engines/hume.js";
import { MistralTTSClient } from "./engines/mistral.js";
import { ModelsLabTTSClient } from "./engines/modelslab.js";
import { MurfTTSClient } from "./engines/murf.js";
import { OpenAITTSClient } from "./engines/openai.js";
import { PlayHTTTSClient } from "./engines/playht.js";
import { PollyTTSClient } from "./engines/polly.js";
import { ResembleTTSClient } from "./engines/resemble.js";
import { SAPITTSClient } from "./engines/sapi.js";
import { SherpaOnnxTTSClient } from "./engines/sherpaonnx.js";
import { SherpaOnnxWasmTTSClient } from "./engines/sherpaonnx-wasm.js";
import { UnrealSpeechTTSClient } from "./engines/unrealspeech.js";
import { UpliftAITTSClient } from "./engines/upliftai.js";
import { WatsonTTSClient } from "./engines/watson.js";
import { WitAITTSClient } from "./engines/witai.js";
import { XaiTTSClient } from "./engines/xai.js";
// Import MockTTSClient for testing
let MockTTSClient;
try {
// Dynamic import to avoid circular dependencies
import("./__tests__/mock-tts-client.helper.js")
.then((module) => {
MockTTSClient = module.MockTTSClient;
})
.catch(() => {
// Ignore errors
});
}
catch (_e) {
// Ignore errors
}
export function createTTSClient(engine, credentials) {
const applyProperties = (client) => {
if (!credentials || typeof client?.setProperty !== "function") {
return client;
}
const rawProps = credentials.properties ??
credentials.propertiesJson ??
credentials.propertiesJSON;
let parsedProps = null;
if (typeof rawProps === "string") {
try {
parsedProps = JSON.parse(rawProps);
}
catch (error) {
console.warn("Failed to parse properties JSON passed to factory:", error);
}
}
else if (rawProps && typeof rawProps === "object") {
parsedProps = rawProps;
}
if (parsedProps) {
for (const [key, value] of Object.entries(parsedProps)) {
try {
client.setProperty(key, value);
}
catch (error) {
console.warn(`Failed to apply property '${key}' in factory:`, error);
}
}
}
return client;
};
switch (engine) {
case "azure":
return applyProperties(new AzureTTSClient(credentials));
case "cartesia":
return applyProperties(new CartesiaTTSClient(credentials));
case "cerevoice":
return applyProperties(new CereVoiceTTSClient(credentials));
case "deepgram":
return applyProperties(new DeepgramTTSClient(credentials));
case "fishaudio":
return applyProperties(new FishAudioTTSClient(credentials));
case "gemini":
return applyProperties(new GeminiTTSClient(credentials));
case "google":
return applyProperties(new GoogleTTSClient(credentials));
case "polly":
return applyProperties(new PollyTTSClient(credentials));
case "elevenlabs":
return applyProperties(new ElevenLabsTTSClient(credentials));
case "openai":
return applyProperties(new OpenAITTSClient(credentials));
case "playht":
return applyProperties(new PlayHTTTSClient(credentials));
case "watson":
return applyProperties(new WatsonTTSClient(credentials));
case "witai":
return applyProperties(new WitAITTSClient(credentials));
case "xai":
return applyProperties(new XaiTTSClient(credentials));
case "upliftai":
return applyProperties(new UpliftAITTSClient(credentials));
case "hume":
return applyProperties(new HumeTTSClient(credentials));
case "mistral":
return applyProperties(new MistralTTSClient(credentials));
case "murf":
return applyProperties(new MurfTTSClient(credentials));
case "modelslab":
return applyProperties(new ModelsLabTTSClient(credentials));
case "resemble":
return applyProperties(new ResembleTTSClient(credentials));
case "unrealspeech":
return applyProperties(new UnrealSpeechTTSClient(credentials));
case "sherpaonnx":
return applyProperties(new SherpaOnnxTTSClient(credentials));
case "sherpaonnx-wasm":
return applyProperties(new SherpaOnnxWasmTTSClient(credentials));
case "espeak":
return applyProperties(new EspeakTTSClient(credentials));
case "espeak-wasm":
return applyProperties(new EspeakWasmTTSClient(credentials));
case "sapi":
return applyProperties(new SAPITTSClient(credentials));
case "mock":
if (MockTTSClient) {
return applyProperties(new MockTTSClient());
}
throw new Error("MockTTSClient is not available. This is only available in development/testing environments.");
default:
throw new Error(`Engine '${engine}' is not supported in the factory.`);
}
}