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

171 lines (170 loc) 5.03 kB
import { AbstractTTSClient } from "../core/abstract-tts"; import type { SpeakOptions, TTSCredentials, UnifiedVoice } from "../types"; /** * SherpaOnnx TTS credentials */ export interface SherpaOnnxTTSCredentials extends TTSCredentials { /** * Path to model file or directory */ modelPath?: string; /** * Path to tokens file */ tokensPath?: string; /** * Voice model ID */ modelId?: string; /** * If true, skip automatic download of default model */ noDefaultDownload?: boolean; } /** * SherpaOnnx TTS client */ export declare class SherpaOnnxTTSClient extends AbstractTTSClient { /** * Path to the model file */ private modelPath; /** * Voice model ID */ private modelId; /** * Base directory for models */ private baseDir; /** * SherpaOnnx TTS instance */ private tts; /** * Sample rate */ private sampleRate; /** * Model configuration */ private jsonModels; /** * Path to the models file */ private static readonly MODELS_FILE; /** * Create a new SherpaOnnx TTS client * @param credentials SherpaOnnx credentials */ constructor(credentials: SherpaOnnxTTSCredentials); /** * Load models and voices from the JSON configuration file * @returns Record of model configurations */ private loadModelsAndVoices; /** * Download the models file from the repository */ private downloadModelsFile; /** * Download a file from a URL to a destination path * @param url URL to download from * @param destination Destination path * @returns Promise resolving when the download is complete */ private downloadFile; /** * Extract a tar.bz2 archive to a destination directory * @param archivePath Path to the archive file * @param destinationDir Destination directory * @returns Promise resolving to a map of extracted file paths */ private extractTarBz2; /** * Check if model and token files exist * @param modelPath Path to model file * @param tokensPath Path to tokens file * @returns True if both files exist and are not empty */ private checkFilesExist; /** * Get dict_dir from extracted model * @param destinationDir Destination directory * @returns Path to dict_dir */ private getDictDir; /** * Download model and token files to voice-specific directory * @param destinationDir Base directory for model files * @param modelId Voice model ID * @returns Tuple of (model_path, tokens_path, lexicon_path, dict_dir) */ private downloadModelAndTokens; /** * Check if model exists and download if not * @param modelId Voice model ID * @returns Tuple of (model_path, tokens_path, lexicon_path, dict_dir) */ private checkAndDownloadModel; /** * Initialize the SherpaOnnx TTS engine * @param modelPath Path to model file * @param tokensPath Path to tokens file */ private initializeTTS; /** * Estimate word boundaries from text * @param text Text to estimate word boundaries for * @param audioDuration Duration of the audio in seconds * @returns Array of word boundaries */ private estimateWordBoundaries; /** * Get available voices from the provider * @returns Promise resolving to an array of voice objects */ protected _getVoices(): Promise<any[]>; /** * Map SherpaOnnx voice objects to unified format * @param rawVoices Array of SherpaOnnx voice objects * @returns Promise resolving to an array of unified voice objects */ protected _mapVoicesToUnified(rawVoices: any[]): Promise<UnifiedVoice[]>; /** * Set the voice to use for synthesis * @param voiceId Voice ID to use */ setVoice(voiceId: string): Promise<void>; /** * Convert text to audio bytes * @param text Text to synthesize * @returns Promise resolving to audio bytes */ synthToBytes(text: string): Promise<Uint8Array>; /** * Synthesize text to a byte stream with word boundaries * @param text Text to synthesize * @param options Synthesis options * @returns Promise resolving to a readable stream of audio bytes with word boundaries */ synthToBytestream(text: string, options?: SpeakOptions): Promise<ReadableStream<Uint8Array> | { audioStream: ReadableStream<Uint8Array>; wordBoundaries: Array<{ text: string; offset: number; duration: number; }>; }>; /** * Strip SSML tags from text * @param text Text with SSML tags * @returns Plain text without SSML tags */ private stripSSML; /** * Check if credentials are valid * @returns Promise resolving to true if credentials are valid */ checkCredentials(): Promise<boolean>; }