UNPKG

js-tts-wrapper

Version:

A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services

101 lines (100 loc) 4.06 kB
// Factory for TTS clients (browser/server compatible) import { AzureTTSClient } from "./engines/azure.js"; import { ElevenLabsTTSClient } from "./engines/elevenlabs.js"; import { EspeakWasmTTSClient } from "./engines/espeak-wasm.js"; import { EspeakTTSClient } from "./engines/espeak.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 { UpliftAITTSClient } from "./engines/upliftai.js"; import { SherpaOnnxWasmTTSClient } from "./engines/sherpaonnx-wasm.js"; import { SherpaOnnxTTSClient } from "./engines/sherpaonnx.js"; import { WatsonTTSClient } from "./engines/watson.js"; import { WitAITTSClient } from "./engines/witai.js"; import { SAPITTSClient } from "./engines/sapi.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 "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 "upliftai": return applyProperties(new UpliftAITTSClient(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.`); } }