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

197 lines (196 loc) 6.8 kB
import { AbstractTTSClient } from "../core/abstract-tts.js"; import * as SSMLUtils from "../core/ssml-utils.js"; import * as SpeechMarkdown from "../markdown/converter.js"; import { base64ToUint8Array } from "../utils/base64-utils.js"; import { getFetch } from "../utils/fetch-utils.js"; import { toIso639_3, toLanguageDisplay } from "../utils/language-utils.js"; const fetch = getFetch(); export class ResembleTTSClient extends AbstractTTSClient { constructor(credentials = {}) { super(credentials); Object.defineProperty(this, "apiKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "baseUrl", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.apiKey = credentials.apiKey || process.env.RESEMBLE_API_KEY || ""; this.baseUrl = credentials.baseURL || "https://f.cluster.resemble.ai"; this.voiceId = ""; this._models = [ { id: "default", features: ["streaming", "inline-voice-cloning", "open-source"] }, ]; this.sampleRate = 22050; this.applyCredentialProperties(credentials); } applyCredentialProperties(credentials) { const rawProps = credentials.properties ?? credentials.propertiesJson ?? credentials.propertiesJSON; if (rawProps) { let parsed = null; if (typeof rawProps === "string") { try { parsed = JSON.parse(rawProps); } catch { /* ignore */ } } else if (typeof rawProps === "object") { parsed = rawProps; } if (parsed) { for (const [key, value] of Object.entries(parsed)) { this.setProperty(key, value); } } } } async prepareText(text, options) { let processedText = text; if (options?.useSpeechMarkdown && SpeechMarkdown.isSpeechMarkdown(processedText)) { const ssml = await SpeechMarkdown.toSSML(processedText, "w3c"); processedText = SSMLUtils.stripSSML(ssml); } if (SSMLUtils.isSSML(processedText)) { processedText = SSMLUtils.stripSSML(processedText); } return processedText; } setVoice(voiceId) { this.voiceId = voiceId; } getProperty(property) { switch (property) { case "voice": return this.voiceId; default: return super.getProperty(property); } } setProperty(property, value) { switch (property) { case "voice": this.setVoice(value); break; default: super.setProperty(property, value); break; } } async checkCredentials() { if (!this.apiKey) return false; try { const response = await fetch(`${this.baseUrl}/v2/voices`, { method: "GET", headers: { Authorization: this.apiKey, }, }); return response.ok; } catch { return false; } } getRequiredCredentials() { return ["apiKey"]; } async _getVoices() { try { const response = await fetch(`${this.baseUrl}/v2/voices`, { method: "GET", headers: { Authorization: this.apiKey, }, }); if (!response.ok) return []; const data = await response.json(); return Array.isArray(data) ? data : []; } catch { return []; } } async _mapVoicesToUnified(rawVoices) { return rawVoices.map((voice) => ({ id: voice.uuid || voice.id || voice.voice_uuid, name: voice.name || voice.uuid || voice.id, gender: (voice.gender || "Unknown"), languageCodes: [ { bcp47: voice.language || "en-US", iso639_3: toIso639_3(voice.language || "en-US"), display: toLanguageDisplay(voice.language || "en-US"), }, ], provider: "resemble", })); } async synthToBytes(text, options = {}) { const preparedText = await this.prepareText(text, options); const voiceId = options.voice || this.voiceId; const body = { ...options.providerOptions, voice_uuid: voiceId, data: preparedText, }; const response = await fetch(`${this.baseUrl}/synthesize`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: this.apiKey, }, body: JSON.stringify(body), }); if (!response.ok) { const errorText = await response.text(); throw new Error(`Resemble API error: ${response.status} ${response.statusText} - ${errorText}`); } const json = (await response.json()); const bytes = base64ToUint8Array(json.audio_content); this._createEstimatedWordTimings(preparedText); return bytes; } async synthToBytestream(text, options = {}) { const preparedText = await this.prepareText(text, options); const voiceId = options.voice || this.voiceId; const body = { ...options.providerOptions, voice_uuid: voiceId, data: preparedText, }; const response = await fetch(`${this.baseUrl}/stream`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: this.apiKey, }, body: JSON.stringify(body), }); if (!response.ok) { const errorText = await response.text(); throw new Error(`Resemble API error: ${response.status} ${response.statusText} - ${errorText}`); } if (!response.body) { const bytes = await this.synthToBytes(text, options); const readableStream = new ReadableStream({ start(controller) { controller.enqueue(bytes); controller.close(); }, }); return { audioStream: readableStream, wordBoundaries: [] }; } return { audioStream: response.body, wordBoundaries: [] }; } }