js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
60 lines (59 loc) • 2.33 kB
JavaScript
// Browser-compatible factory for TTS clients
import { AzureTTSClient } from "./engines/azure.js";
import { ElevenLabsTTSClient } from "./engines/elevenlabs.js";
import { EspeakBrowserTTSClient } from "./engines/espeak-wasm.js";
import { GoogleTTSClient } from "./engines/google.js";
import { OpenAITTSClient } from "./engines/openai.js";
import { PlayHTTTSClient } from "./engines/playht.js";
import { PollyTTSClient } from "./engines/polly.js";
import { SherpaOnnxWasmTTSClient } from "./engines/sherpaonnx-wasm.js";
import { WatsonTTSClient } from "./engines/watson.js";
import { WitAITTSClient } from "./engines/witai.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 createBrowserTTSClient(engine, credentials) {
switch (engine) {
case "azure":
return new AzureTTSClient(credentials);
case "google":
return new GoogleTTSClient(credentials);
case "polly":
return new PollyTTSClient(credentials);
case "elevenlabs":
return new ElevenLabsTTSClient(credentials);
case "openai":
return new OpenAITTSClient(credentials);
case "playht":
return new PlayHTTTSClient(credentials);
case "watson":
return new WatsonTTSClient(credentials);
case "witai":
return new WitAITTSClient(credentials);
case "sherpaonnx-wasm":
return new SherpaOnnxWasmTTSClient(credentials);
case "espeak-wasm":
return new EspeakBrowserTTSClient(credentials);
case "mock":
if (MockTTSClient) {
return 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 browser factory.`);
}
}
// Re-export the main factory function with a different name for compatibility
export { createBrowserTTSClient as createTTSClient };