js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
183 lines (182 loc) • 5.96 kB
TypeScript
import { AbstractTTSClient } from "../core/abstract-tts";
import { SpeakOptions, TTSCredentials, UnifiedVoice } from "../types";
import { WordBoundary } from "../utils/word-timing-estimator";
/**
* PlayHT TTS Client Credentials
*/
export interface PlayHTTTSCredentials extends TTSCredentials {
/** PlayHT API Key */
apiKey?: string;
/** PlayHT User ID */
userId?: string;
}
/**
* Extended options for PlayHT TTS
*/
export interface PlayHTTTSOptions extends SpeakOptions {
/**
* Output directory for audio files
*/
outputDir?: string;
/**
* Output file name
*/
outputFile?: string;
/**
* Callback for word boundary events
*/
onWord?: (wordBoundary: WordBoundary) => void;
/**
* Whether to return word boundaries
*/
returnWordBoundaries?: boolean;
/**
* Callback for end of speech event
*/
onEnd?: () => void;
}
/**
* PlayHT Voice Engine
*/
export type PlayHTVoiceEngine = "PlayHT1.0" | "PlayHT2.0" | null;
/**
* PlayHT TTS Client
*
* This client uses the PlayHT API to convert text to speech.
* It supports streaming audio but does not support SSML.
* Word boundaries are estimated since PlayHT doesn't provide word events.
*/
export declare class PlayHTTTSClient extends AbstractTTSClient {
private apiKey;
private userId;
private voice;
private voiceEngine;
private outputFormat;
private lastWordBoundaries;
/**
* Create a new PlayHT TTS Client
* @param credentials PlayHT API credentials
*/
constructor(credentials?: PlayHTTTSCredentials);
/**
* Check if the credentials are valid
* @returns Promise resolving to true if credentials are valid, false otherwise
*/
checkCredentials(): Promise<boolean>;
/**
* Fetch voices from the PlayHT API
* @returns Promise resolving to an array of PlayHT voice objects
*/
private _fetchVoices;
/**
* Get available voices
* @returns Promise resolving to an array of unified voice objects
*/
protected _getVoices(): Promise<UnifiedVoice[]>;
/**
* Map PlayHT voice objects to unified format
* @param rawVoices Array of PlayHT 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): void;
/**
* Auto-detect voice engine based on voice ID
* @param voiceId Voice ID to analyze
*/
private autoDetectVoiceEngine;
/**
* Set the voice engine to use for synthesis
* @param engine Voice engine to use
*/
setVoiceEngine(engine: PlayHTVoiceEngine): void;
/**
* Set the output format
* @param format Output format (wav, mp3)
*/
setOutputFormat(format: string): 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;
/**
* Get the last word boundaries
* @returns Array of word boundary objects
*/
getLastWordBoundaries(): WordBoundary[];
/**
* Set the last word boundaries
* @param wordBoundaries Array of word boundary objects
*/
setLastWordBoundaries(wordBoundaries: WordBoundary[]): void;
/**
* Convert text to speech
* @param text Text to convert to speech
* @param options TTS options
* @returns Promise resolving to the path of the generated audio file
*/
textToSpeech(text: string, options?: PlayHTTTSOptions): Promise<string>;
/**
* Convert text to speech with streaming
* @param text Text to convert to speech
* @param options TTS options
* @returns Promise resolving to the path of the generated audio file
*/
textToSpeechStreaming(text: string, options?: PlayHTTTSOptions): Promise<string>;
/**
* Convert SSML to speech (not supported by PlayHT)
* @param ssml SSML to convert to speech
* @param options TTS options
* @returns Promise resolving to the path of the generated audio file
*/
ssmlToSpeech(_ssml: string, _options?: PlayHTTTSOptions): Promise<string>;
/**
* Convert SSML to speech with streaming (not supported by PlayHT)
* @param ssml SSML to convert to speech
* @param options TTS options
* @returns Promise resolving to the path of the generated audio file
*/
ssmlToSpeechStreaming(_ssml: string, _options?: PlayHTTTSOptions): Promise<string>;
/**
* Synthesize text to audio and save it to a file
* @param text Text or SSML to synthesize
* @param filename Filename to save as
* @param format Audio format (mp3 or wav)
* @param options Synthesis options
*/
synthToFile(text: string, filename: string, format?: "mp3" | "wav", // Default to MP3 for PlayHT
options?: PlayHTTTSOptions): Promise<void>;
/**
* Synthesize text to audio bytes
* @param text Text to synthesize
* @param options Synthesis options
* @returns Promise resolving to audio bytes
*/
synthToBytes(text: string, options?: PlayHTTTSOptions): Promise<Buffer>;
/**
* Synthesize text to audio byte stream
* @param text Text to synthesize
* @param options Synthesis options
* @returns Promise resolving to an object containing the audio stream and an empty word boundaries array.
*/
synthToBytestream(text: string, _options?: SpeakOptions): Promise<{
audioStream: ReadableStream<Uint8Array>;
wordBoundaries: Array<{
text: string;
offset: number;
duration: number;
}>;
}>;
}