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

140 lines (139 loc) 4.5 kB
import { AbstractTTSClient } from "../core/abstract-tts"; import type { SpeakOptions, TTSCredentials, UnifiedVoice, WordBoundaryCallback } from "../types"; /** * Extended options for ElevenLabs TTS */ export interface ElevenLabsTTSOptions extends SpeakOptions { format?: "mp3" | "wav"; useTimestamps?: boolean; } /** * ElevenLabs TTS credentials */ export interface ElevenLabsCredentials extends TTSCredentials { /** * ElevenLabs API key */ apiKey?: string; } /** * ElevenLabs character alignment data */ export interface ElevenLabsAlignment { characters: string[]; character_start_times_seconds: number[]; character_end_times_seconds: number[]; } /** * ElevenLabs API response with timestamps */ export interface ElevenLabsTimestampResponse { audio_base64: string; alignment: ElevenLabsAlignment; normalized_alignment?: ElevenLabsAlignment; } /** * ElevenLabs TTS client */ export declare class ElevenLabsTTSClient extends AbstractTTSClient { /** * ElevenLabs API key */ private apiKey; /** * Base URL for ElevenLabs API */ private baseUrl; /** * Create a new ElevenLabs TTS client * @param credentials ElevenLabs credentials */ constructor(credentials?: ElevenLabsCredentials); /** * Check if the credentials are valid * @returns Promise resolving to true if credentials are valid, false otherwise */ checkCredentials(): Promise<boolean>; /** * Perform a tiny synthesis to detect quota/Unauthorized issues up-front * Returns false if quota is exceeded or API key is unauthorized for synthesis */ private _quotaProbe; /** * Get the list of required credential types for this engine * @returns Array of required credential field names */ protected getRequiredCredentials(): string[]; /** * Get available voices from the provider * @returns Promise resolving to an array of voice objects */ protected _getVoices(): Promise<any[]>; /** * Prepare text for synthesis by stripping SSML tags * @param text Text to prepare * @param options Synthesis options * @returns Prepared text */ private prepareText; /** * Convert text to audio bytes * @param text Text to synthesize * @param options Synthesis options * @returns Promise resolving to audio bytes */ synthToBytes(text: string, options?: ElevenLabsTTSOptions): Promise<Uint8Array>; /** * Synthesize text to a byte stream * @param text Text to synthesize * @param options Synthesis options * @returns Promise resolving to an object containing the audio stream and word boundaries array */ synthToBytestream(text: string, options?: ElevenLabsTTSOptions): Promise<{ audioStream: ReadableStream<Uint8Array>; wordBoundaries: Array<{ text: string; offset: number; duration: number; }>; }>; /** * Call ElevenLabs API with timestamps endpoint * @param text Text to synthesize * @param voiceId Voice ID to use * @returns Promise resolving to timestamp response */ private synthWithTimestamps; /** * Convert character-level timing data to word boundaries * @param text Original text * @param alignment Character alignment data from ElevenLabs * @returns Array of word boundary objects */ private convertCharacterTimingToWordBoundaries; /** * Start playback with word boundary callbacks * @param text Text to speak * @param callback Callback function for word boundaries * @param options Synthesis options */ startPlaybackWithCallbacks(text: string, callback: WordBoundaryCallback, options?: SpeakOptions): Promise<void>; /** * Map ElevenLabs voice objects to unified format * @param rawVoices Array of ElevenLabs voice objects * @returns Promise resolving to an array of unified voice objects */ protected _mapVoicesToUnified(rawVoices: any[]): Promise<UnifiedVoice[]>; /** * Get voice by ID * @param voiceId Voice ID * @returns Promise resolving to voice details */ getVoice(voiceId: string): Promise<UnifiedVoice | null>; /** * Convert MP3 audio data to WAV format using the audio converter utility * @param mp3Data MP3 audio data from ElevenLabs * @returns WAV audio data */ private convertMp3ToWav; }