TextToSpeech class

MediaPipe Audio Task that performs neural text-to-speech synthesis in unary and streaming modes via WASM SIMD or WebGPU.

Signature:

export declare class TextToSpeech extends TaskRunner 

Methods

Method Modifiers Description
createFromOptions(wasmFileset, options) static Initializes the Wasm runtime and creates a new TextToSpeech instance based on the provided options.
createFromModelPath(wasmFileset, modelAssetPath) static Initializes the Wasm runtime and creates a new TextToSpeech instance from a model asset path.
createFromModelBuffer(wasmFileset, modelAssetBuffer) static Initializes the Wasm runtime and creates a new TextToSpeech instance from a model asset buffer or stream.
setOptions(options) Sets the options for TextToSpeech and initializes the underlying engine. Options cannot be updated once the engine has been created.
listVoices() Queries available voices bundled in the initialized model.
synthesize(text, options) Synthesizes a complete utterance and returns the full PCM audio buffer.
synthesizeStream(initialText, options, finishInput) Starts a streaming synthesis session returning a TextToSpeechStream (supporting both readNext() and for await (const chunk of stream)).
cancel() Cancels any in-flight unary or streaming synthesis on this TextToSpeech instance.
close() Closes the TextToSpeech instance and releases native engine and Wasm memory.

TextToSpeech.createFromOptions()

Initializes the Wasm runtime and creates a new TextToSpeech instance based on the provided options.

Signature:

static createFromOptions(wasmFileset: WasmFileset, options: TextToSpeechOptions): Promise<TextToSpeech>;

Parameters

Parameter Type Description
wasmFileset WasmFileset A configuration object that provides the location of the Wasm binary and its loader.
options TextToSpeechOptions The options for TextToSpeech. Note that either a path to the model or the model buffer itself must be provided via baseOptions.

Returns:

Promise<TextToSpeech>

TextToSpeech.createFromModelPath()

Initializes the Wasm runtime and creates a new TextToSpeech instance from a model asset path.

Signature:

static createFromModelPath(wasmFileset: WasmFileset, modelAssetPath: string | string): Promise<TextToSpeech>;

Parameters

Parameter Type Description
wasmFileset WasmFileset A configuration object that provides the location of the Wasm binary and its loader.
modelAssetPath string | string The path or URL to the .litertlm model asset.

Returns:

Promise<TextToSpeech>

TextToSpeech.createFromModelBuffer()

Initializes the Wasm runtime and creates a new TextToSpeech instance from a model asset buffer or stream.

Signature:

static createFromModelBuffer(wasmFileset: WasmFileset, modelAssetBuffer: Uint8Array | ReadableStreamDefaultReader): Promise<TextToSpeech>;

Parameters

Parameter Type Description
wasmFileset WasmFileset A configuration object that provides the location of the Wasm binary and its loader.
modelAssetBuffer Uint8Array | ReadableStreamDefaultReader A Uint8Array or ReadableStreamDefaultReader containing the .litertlm model bytes.

Returns:

Promise<TextToSpeech>

TextToSpeech.setOptions()

Sets the options for TextToSpeech and initializes the underlying engine. Options cannot be updated once the engine has been created.

Signature:

setOptions(options: TextToSpeechOptions): Promise<void>;

Parameters

Parameter Type Description
options TextToSpeechOptions The options for TextToSpeech.

Returns:

Promise<void>

TextToSpeech.listVoices()

Queries available voices bundled in the initialized model.

Signature:

listVoices(): Promise<VoiceInfo[]>;

Returns:

Promise<VoiceInfo[]>

TextToSpeech.synthesize()

Synthesizes a complete utterance and returns the full PCM audio buffer.

Signature:

synthesize(text: string, options?: SynthesisOptions): Promise<TextToSpeechResult>;

Parameters

Parameter Type Description
text string The text to synthesize.
options SynthesisOptions Optional per-utterance voice or locale overrides.

Returns:

Promise<TextToSpeechResult>

TextToSpeech.synthesizeStream()

Starts a streaming synthesis session returning a TextToSpeechStream (supporting both readNext() and for await (const chunk of stream)).

Signature:

synthesizeStream(initialText?: string, options?: SynthesisOptions, finishInput?: boolean): Promise<TextToSpeechStream>;

Parameters

Parameter Type Description
initialText string Initial text to synthesize (pass "" when streaming incremental LLM text deltas via stream.pushText()).
options SynthesisOptions Optional per-utterance voice or locale overrides.
finishInput boolean Pass true (default) for single-utterance streaming, or false to keep the input stream open for stream.pushText() and stream.finishInput().

Returns:

Promise<TextToSpeechStream>

TextToSpeech.cancel()

Cancels any in-flight unary or streaming synthesis on this TextToSpeech instance.

Signature:

cancel(): void;

Returns:

void

TextToSpeech.close()

Closes the TextToSpeech instance and releases native engine and Wasm memory.

Signature:

close(): void;

Returns:

void