js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
103 lines (102 loc) • 3.7 kB
TypeScript
import { AbstractTTSClient } from "../core/abstract-tts";
import type { SpeakOptions, TTSCredentials, UnifiedVoice, WordBoundaryCallback } from "../types";
/**
* Azure TTS Client Credentials
*/
export interface AzureTTSCredentials extends TTSCredentials {
subscriptionKey: string;
region: string;
}
/**
* Azure TTS Client
*/
export declare class AzureTTSClient extends AbstractTTSClient {
private subscriptionKey;
private region;
private sdk;
private sdkLoadingPromise;
/**
* Create a new Azure TTS client
* @param credentials Azure credentials object with subscriptionKey and region
*/
constructor(credentials: AzureTTSCredentials);
/**
* Get raw voices from Azure
* @returns Promise resolving to an array of unified voice objects
*/
protected _getVoices(): Promise<any[]>;
/**
* Map Azure voice objects to unified format
* @param rawVoices Array of Azure voice objects
* @returns Promise resolving to an array of unified voice objects
*/
protected _mapVoicesToUnified(rawVoices: any[]): Promise<UnifiedVoice[]>;
/**
* Synthesize text to audio bytes
* @param text Text or SSML to synthesize
* @param options Synthesis options
* @returns Promise resolving to audio bytes
*/
synthToBytes(text: string, options?: AzureTTSOptions): Promise<Uint8Array>;
/**
* Synthesize text to a byte stream with word boundary information
* @param text Text or SSML to synthesize
* @param options Synthesis options
* @returns Promise resolving to an object containing the audio stream and word boundary information
*/
synthToBytestream(text: string, options?: AzureTTSOptions): Promise<{
audioStream: ReadableStream<Uint8Array>;
wordBoundaries: Array<{
text: string;
offset: number;
duration: number;
}>;
}>;
/**
* Load the Microsoft Speech SDK dynamically.
* @returns A promise resolving to the SDK module, or null if loading fails or not applicable.
*/
private loadSDK;
/**
* Synthesize speech using the Microsoft Cognitive Services Speech SDK
* @param ssml SSML to synthesize
* @param options Synthesis options
* @param sdkInstance The loaded SDK instance.
* @returns Promise resolving to an object containing the audio stream and word boundary information
*/
private synthToBytestreamWithSDK;
/**
* Synthesize speech using the REST API (no word boundaries)
* @param ssml SSML to synthesize
* @param options Synthesis options
* @returns Promise resolving to an object containing the audio stream and an empty word boundary array
*/
private synthToBytestreamWithREST;
/**
* Start playback with word boundary callbacks
* @param text Text or SSML to speak
* @param callback Callback function for word boundaries
* @param options Synthesis options
*/
startPlaybackWithCallbacks(text: string, callback: WordBoundaryCallback, options?: AzureTTSOptions): Promise<void>;
/**
* Start playback with word boundary callbacks using the SDK
* @param text Text or SSML to speak
* @param callback Callback function for word boundaries
* @param options Synthesis options
*/
private startPlaybackWithCallbacksSDK;
/**
* Prepare SSML for synthesis
* @param text Text or SSML to prepare
* @param options Synthesis options
* @returns SSML ready for synthesis
*/
private prepareSSML;
}
/**
* Extended options for Azure TTS
*/
export interface AzureTTSOptions extends SpeakOptions {
format?: 'mp3' | 'wav';
}