js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
110 lines (109 loc) • 3.99 kB
TypeScript
/**
* SherpaOnnx WebAssembly TTS Client
*
* This client uses the WebAssembly build of SherpaOnnx for browser environments
* where native modules cannot be used.
*/
import { AbstractTTSClient } from "../core/abstract-tts";
import type { SpeakOptions, TTSCredentials, UnifiedVoice, WordBoundaryCallback } from "../types";
/**
* SherpaOnnx WebAssembly TTS Client
*
* This client uses the WebAssembly build of SherpaOnnx for browser environments
* where native modules cannot be used.
*/
export declare class SherpaOnnxWasmTTSClient extends AbstractTTSClient {
private wasmModule;
private tts;
private sampleRate;
private baseDir;
private wasmPath;
private wasmLoaded;
/**
* Create a new SherpaOnnx WebAssembly TTS client
* @param credentials Optional credentials object
*/
constructor(credentials?: TTSCredentials);
/**
* Get the default models directory
* @returns Path to the default models directory
*/
private _getDefaultModelsDir;
/**
* Check if the credentials are valid
* @returns Promise resolving to true if credentials are valid
*/
checkCredentials(): Promise<boolean>;
/**
* Get available voices
* @returns Promise resolving to an array of unified voice objects
*/
protected _getVoices(): Promise<UnifiedVoice[]>;
/**
* Initialize the WebAssembly module
* @param wasmUrl URL to the WebAssembly file
* @returns Promise resolving when the module is initialized
*/
initializeWasm(_wasmUrl: string): Promise<void>;
/**
* Synthesize text to speech and return the audio as a byte array
* @param text Text to synthesize
* @param options Options for synthesis
* @returns Promise resolving to a byte array of audio data
*/
synthToBytes(text: string, _options?: SpeakOptions): Promise<Uint8Array>;
/**
* Convert audio samples to the requested format
* @param samples Float32Array of audio samples
* @returns Uint8Array of audio data in the requested format
*/
private _convertAudioFormat;
/**
* Mock implementation for synthToBytes
* @returns Promise resolving to a byte array of audio data
*/
private _mockSynthToBytes;
/**
* Synthesize text to speech and stream the audio
* @param text Text to synthesize
* @param onAudioBuffer Callback for audio buffers
* @param onStart Callback for when synthesis starts
* @param onEnd Callback for when synthesis ends
* @param onWord Callback for word boundary events
* @param options Options for synthesis
* @returns Promise resolving when synthesis is complete
*/
synthToStream(text: string, onAudioBuffer: (audioBuffer: Uint8Array) => void, onStart?: () => void, onEnd?: () => void, onWord?: WordBoundaryCallback, options?: SpeakOptions): Promise<void>;
/**
* Synthesize text to speech and save to a file
* @param text Text to synthesize
* @param filename Filename to save as
* @param format Audio format (mp3 or wav)
* @param options Options for synthesis
* @returns Promise resolving when synthesis is complete
*/
synthToFile(text: string, filename: string, format?: "mp3" | "wav", options?: SpeakOptions): Promise<void>;
/**
* Get a property value
* @param property Property name
* @returns Property value
*/
getProperty(property: string): any;
/**
* Set a property value
* @param property Property name
* @param value Property value
*/
setProperty(property: string, value: any): void;
/**
* Clean up resources
*/
dispose(): void;
/**
* Synthesize text to a byte stream
* @param text Text to synthesize
* @param options Options for synthesis
* @returns Promise resolving to a readable stream of audio bytes
*/
synthToBytestream(text: string, options?: SpeakOptions): Promise<ReadableStream<Uint8Array>>;
}