js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
136 lines (135 loc) • 5.71 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTTSClient = createTTSClient;
// Factory for TTS clients (browser/server compatible)
const azure_js_1 = require("./engines/azure.js");
const elevenlabs_js_1 = require("./engines/elevenlabs.js");
const espeak_wasm_js_1 = require("./engines/espeak-wasm.js");
const espeak_js_1 = require("./engines/espeak.js");
const google_js_1 = require("./engines/google.js");
const openai_js_1 = require("./engines/openai.js");
const playht_js_1 = require("./engines/playht.js");
const polly_js_1 = require("./engines/polly.js");
const upliftai_js_1 = require("./engines/upliftai.js");
const sherpaonnx_wasm_js_1 = require("./engines/sherpaonnx-wasm.js");
const sherpaonnx_js_1 = require("./engines/sherpaonnx.js");
const watson_js_1 = require("./engines/watson.js");
const witai_js_1 = require("./engines/witai.js");
const sapi_js_1 = require("./engines/sapi.js");
// Import MockTTSClient for testing
let MockTTSClient;
try {
// Dynamic import to avoid circular dependencies
Promise.resolve().then(() => __importStar(require("./__tests__/mock-tts-client.helper.js"))).then((module) => {
MockTTSClient = module.MockTTSClient;
})
.catch(() => {
// Ignore errors
});
}
catch (_e) {
// Ignore errors
}
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 azure_js_1.AzureTTSClient(credentials));
case "google":
return applyProperties(new google_js_1.GoogleTTSClient(credentials));
case "polly":
return applyProperties(new polly_js_1.PollyTTSClient(credentials));
case "elevenlabs":
return applyProperties(new elevenlabs_js_1.ElevenLabsTTSClient(credentials));
case "openai":
return applyProperties(new openai_js_1.OpenAITTSClient(credentials));
case "playht":
return applyProperties(new playht_js_1.PlayHTTTSClient(credentials));
case "watson":
return applyProperties(new watson_js_1.WatsonTTSClient(credentials));
case "witai":
return applyProperties(new witai_js_1.WitAITTSClient(credentials));
case "upliftai":
return applyProperties(new upliftai_js_1.UpliftAITTSClient(credentials));
case "sherpaonnx":
return applyProperties(new sherpaonnx_js_1.SherpaOnnxTTSClient(credentials));
case "sherpaonnx-wasm":
return applyProperties(new sherpaonnx_wasm_js_1.SherpaOnnxWasmTTSClient(credentials));
case "espeak":
return applyProperties(new espeak_js_1.EspeakTTSClient(credentials));
case "espeak-wasm":
return applyProperties(new espeak_wasm_js_1.EspeakWasmTTSClient(credentials));
case "sapi":
return applyProperties(new sapi_js_1.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.`);
}
}