js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
249 lines (248 loc) • 7.89 kB
TypeScript
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 {
/**
* Get comprehensive diagnostics for SherpaOnnx setup
* @returns Detailed diagnostic information
*/
static getDiagnostics(): {
platform: string;
expectedPackage: string | null;
hasMainPackage: boolean;
hasPlatformPackage: boolean;
hasNativeModule: boolean;
environmentVariables: Record<string, string | undefined>;
recommendations: string[];
canRun: boolean;
};
/**
* Path to the model file
*/
private modelPath;
/**
* Voice model ID
*/
private modelId;
/**
* Base directory for models
*/
private baseDir;
/**
* SherpaOnnx TTS instance
*/
private tts;
/**
* Model configuration
*/
private jsonModels;
/**
* 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 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
* @param modelId Optional model ID to determine voice type requirements
* @returns True if all required files exist and are not empty
*/
private checkFilesExist;
/**
* Check if a voice is a Piper voice based on its ID
* @param modelId Voice model ID
* @returns True if this is a Piper voice
*/
private isPiperVoice;
/**
* Check if a voice is a Kokoro voice based on its ID
* @param modelId Voice model ID
* @returns True if this is a Kokoro voice
*/
private isKokoroVoice;
/**
* Check if a voice is a Matcha voice based on its ID
* @param modelId Voice model ID
* @returns True if this is a Matcha voice
*/
private isMatchaVoice;
/**
* Get the model type for a given model ID
* @param modelId Voice model ID
* @returns Model type (vits, kokoro, matcha)
*/
private getModelType;
/**
* Find files matching a pattern in a directory recursively
* @param dir Directory to search
* @param pattern Regex pattern to match
* @returns Array of matching file paths
*/
private findFilesInDirectory;
/**
* Find a specific file in a directory recursively
* @param dir Directory to search
* @param filename Filename to find
* @returns Path to the file or null if not found
*/
private findFileInDirectory;
/**
* Find a specific directory in a directory recursively
* @param dir Directory to search
* @param dirname Directory name to find
* @returns Path to the directory or null if not found
*/
private findDirectoryInDestination;
/**
* Check if a model is from GitHub (archive-based)
* @param modelId Voice model ID
* @returns True if this is a GitHub model
*/
private isGitHubModel;
/**
* Get dict directory from voice directory
* @param voiceDir Voice directory path
* @returns Dict directory path or empty string
*/
private getDictDir;
/**
* Ensure vocoder is downloaded for Matcha models
* @returns Path to vocoder file
*/
private ensureVocoderDownloaded;
/**
* Recursively copy a directory and all its contents
* @param src Source directory path
* @param dest Destination directory path
*/
private copyDirectoryRecursive;
/**
* 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;
/**
* Set the platform-specific library path environment variable for SherpaOnnx
* @returns True if the environment variable was set successfully
*/
private setLibraryPath;
/**
* Initialize the SherpaOnnx TTS engine
* @param modelPath Path to model file
* @param tokensPath Path to tokens file
*/
private initializeTTS;
/**
* 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[]>;
/**
* Get a property value
* @param property Property name
* @returns Property value
*/
getProperty(property: string): any;
/**
* 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 using the Node.js native addon.
* @param text Text to synthesize.
* @param _options Synthesis options (e.g., voice/speaker ID).
* @returns Promise resolving to an object containing the audio stream and an empty word boundaries array.
* @returns Promise resolving to an object containing the audio stream and word boundaries.
*/
synthToBytestream(text: string, _options?: SpeakOptions): Promise<{
audioStream: ReadableStream<Uint8Array>;
wordBoundaries: Array<{
text: string;
offset: number;
duration: number;
}>;
}>;
/**
* Convert Float32Array audio samples to WAV format with proper header
* @param samples Float32Array of audio samples
* @param actualSampleRate Actual sample rate of the audio data
* @returns Uint8Array containing WAV file data
*/
private convertToWav;
/**
* 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>;
}
export { SherpaOnnxTTSClient as SherpaOnnxTTS };