Skip to content

FishAudioTTS

Fish Audio TTS provider using native fetch with msgpack-encoded requests.

Defined in: src/providers/tts/fishaudio/FishAudioTTS.ts:274

Fish Audio TTS provider using native fetch with msgpack-encoded requests.

Remarks

This is a REST-based provider: each synthesize() call makes a single HTTP POST to Fish Audio’s POST /v1/tts endpoint with a MessagePack request body (Content-Type: application/msgpack) and the model header selecting the model generation. The API streams back raw audio bytes (Transfer-Encoding: chunked), which this provider buffers into a Blob.

Peer dependency: requires @msgpack/msgpack (optional peer, loaded lazily in initialize()). Fish Audio also accepts JSON for text-only requests, but inline reference audio for voice cloning needs binary encoding, so msgpack is used for all requests.

Audio flow: Text -> msgpack encode -> Fish Audio REST API -> Complete audio Blob

Example

import { FishAudioTTS } from 'composite-voice';

const tts = new FishAudioTTS({
  apiKey: 'fa_xxxxxxxxxxxx',
  referenceId: 'your-voice-id',
  model: 's2.1-pro',
  format: 'mp3',
});

await tts.initialize();
const audioBlob = await tts.synthesize('Hello, world!');
// Play the audio blob, e.g., via an <audio> element

See

  • RestTTSProvider - The base class this provider extends.
  • FishAudioTTSConfig - Configuration options for this provider.

Extends

  • RestTTSProvider

Constructors

Constructor

new FishAudioTTS(config, logger?): FishAudioTTS;

Defined in: src/providers/tts/fishaudio/FishAudioTTS.ts:285

Creates a new FishAudioTTS provider instance.

Parameters

ParameterTypeDescription
configFishAudioTTSConfigConfiguration for the Fish Audio TTS provider.
logger?LoggerOptional logger instance for debug and diagnostic output.

Returns

FishAudioTTS

Overrides

RestTTSProvider.constructor

Properties

PropertyModifierTypeDefault valueDescriptionOverridesInherited fromDefined in
audioCallback?protected(chunk) => voidundefinedCallback registered by the SDK or consumer to receive audio chunks. Set via onAudio.-RestTTSProvider.audioCallbacksrc/providers/base/BaseTTSProvider.ts:84
configpublicFishAudioTTSConfigundefinedTTS-specific provider configuration.RestTTSProvider.config-src/providers/tts/fishaudio/FishAudioTTS.ts:275
initializedprotectedbooleanfalseTracks whether initialize has completed successfully.-RestTTSProvider.initializedsrc/providers/base/BaseProvider.ts:97
loggerprotectedLoggerundefinedScoped logger instance for this provider.-RestTTSProvider.loggersrc/providers/base/BaseProvider.ts:94
metadataCallback?protected(metadata) => voidundefinedCallback registered by the SDK or consumer to receive audio metadata. Set via onMetadata.-RestTTSProvider.metadataCallbacksrc/providers/base/BaseTTSProvider.ts:90
rolesreadonlyreadonly ProviderRole[]undefinedTTS providers cover the 'tts' pipeline role by default.-RestTTSProvider.rolessrc/providers/base/BaseTTSProvider.ts:75
typereadonlyProviderTypeundefinedCommunication transport this provider uses ('rest' or 'websocket').-RestTTSProvider.typesrc/providers/base/BaseProvider.ts:74

Accessors

isProxyMode

Get Signature

get protected isProxyMode(): boolean;

Defined in: src/providers/base/BaseProvider.ts:286

Whether the provider is in proxy mode.

Returns

boolean

true when proxyUrl is set.

Inherited from

RestTTSProvider.isProxyMode

Methods

assertAuth()

protected assertAuth(): void;

Defined in: src/providers/base/BaseProvider.ts:272

Validate that auth is configured (either apiKey or proxyUrl).

Returns

void

Remarks

Call this in onInitialize() for any provider that requires external authentication. Native providers (NativeSTT, NativeTTS) and in-browser providers (WebLLM) should NOT call this method.

Throws

ProviderInitializationError Thrown when neither apiKey nor proxyUrl is set.

Inherited from

RestTTSProvider.assertAuth

assertReady()

protected assertReady(): void;

Defined in: src/providers/base/BaseProvider.ts:255

Guard that throws if the provider has not been initialized.

Returns

void

Remarks

Call at the start of any method that requires the provider to be ready.

Throws

Error Thrown with a descriptive message when initialized is false.

Inherited from

RestTTSProvider.assertReady

dispose()

dispose(): Promise<void>;

Defined in: src/providers/base/BaseProvider.ts:154

Clean up resources and dispose of the provider.

Returns

Promise<void>

Remarks

Delegates to the subclass hook onDispose and resets the initialized flag. If the provider is not initialized, the call is a no-op.

Throws

Re-throws any error raised by onDispose.

Inherited from

RestTTSProvider.dispose

emitAudio()

protected emitAudio(chunk): void;

Defined in: src/providers/base/BaseTTSProvider.ts:190

Emit a synthesized audio chunk to the registered callback.

Parameters

ParameterTypeDescription
chunkAudioChunkThe audio chunk to emit.

Returns

void

Remarks

Subclasses call this method for each chunk of audio produced during synthesis. If no callback has been registered the chunk is silently dropped.

Inherited from

RestTTSProvider.emitAudio

emitMetadata()

protected emitMetadata(metadata): void;

Defined in: src/providers/base/BaseTTSProvider.ts:206

Emit audio metadata to the registered callback.

Parameters

ParameterTypeDescription
metadataAudioMetadataThe audio metadata to emit.

Returns

void

Remarks

Typically called once at the start of synthesis when the provider knows the output format. If no callback has been registered the metadata is silently dropped.

Inherited from

RestTTSProvider.emitMetadata

finalize()

finalize(): Promise<void>;

Defined in: src/providers/base/RestTTSProvider.ts:109

Finalize synthesis by sending the accumulated text to the REST API.

Returns

Promise<void>

Remarks

Calls synthesize with the full accumulated text. The buffer is cleared after synthesis completes (or on error).

Inherited from

RestTTSProvider.finalize

getConfig()

getConfig(): TTSProviderConfig;

Defined in: src/providers/base/BaseTTSProvider.ts:217

Get a shallow copy of the current TTS configuration.

Returns

TTSProviderConfig

A new TTSProviderConfig object.

Inherited from

RestTTSProvider.getConfig

initialize()

initialize(): Promise<void>;

Defined in: src/providers/base/BaseProvider.ts:127

Initialize the provider, making it ready for use.

Returns

Promise<void>

Remarks

Calls the subclass hook onInitialize. If the provider has already been initialized the call is a no-op.

Throws

ProviderInitializationError Thrown when onInitialize rejects. The original error is wrapped with the provider class name for diagnostics.

Inherited from

RestTTSProvider.initialize

isAudioReady()

isAudioReady(chunk): boolean;

Defined in: src/providers/base/BaseTTSProvider.ts:145

Is this audio chunk ready for playback?

Parameters

ParameterTypeDescription
chunkAudioChunkThe audio chunk to check.

Returns

boolean

true when the chunk has valid audio data.

Remarks

The orchestrator calls this to validate audio chunks before passing them to the audio player. The default implementation checks that the chunk has non-zero data.

Inherited from

RestTTSProvider.isAudioReady

isReady()

isReady(): boolean;

Defined in: src/providers/base/BaseProvider.ts:178

Check whether the provider has been initialized and is ready.

Returns

boolean

true when initialize has completed successfully and dispose has not yet been called.

Inherited from

RestTTSProvider.isReady

onAudio()

onAudio(callback): void;

Defined in: src/providers/base/BaseTTSProvider.ts:161

Register a callback to receive synthesized audio chunks.

Parameters

ParameterTypeDescription
callback(chunk) => voidFunction invoked with each AudioChunk.

Returns

void

Remarks

All TTS providers — regardless of transport — deliver audio through this callback. CompositeVoice registers it during pipeline setup so that audio data flows into the AudioPlayer.

Inherited from

RestTTSProvider.onAudio

onConfigUpdate()

protected onConfigUpdate(_config): void;

Defined in: src/providers/base/BaseProvider.ts:242

Hook called after updateConfig merges new values.

Parameters

ParameterTypeDescription
_configPartial<BaseProviderConfig>The partial configuration that was merged.

Returns

void

Remarks

The default implementation is a no-op. Override in subclasses to react to runtime configuration changes (e.g. reconnect with a new API key).

Inherited from

RestTTSProvider.onConfigUpdate

onDispose()

protected onDispose(): Promise<void>;

Defined in: src/providers/tts/fishaudio/FishAudioTTS.ts:357

Disposes the provider and releases the HTTP client and encoder.

Returns

Promise<void>

Overrides

RestTTSProvider.onDispose

onInitialize()

protected onInitialize(): Promise<void>;

Defined in: src/providers/tts/fishaudio/FishAudioTTS.ts:296

Initializes the msgpack encoder and HTTP client for the Fish Audio API.

Returns

Promise<void>

Throws

ProviderInitializationError if neither apiKey nor proxyUrl is configured.

Throws

ProviderInitializationError if the optional peer dependency @msgpack/msgpack is not installed.

Overrides

RestTTSProvider.onInitialize

onMetadata()

onMetadata(callback): void;

Defined in: src/providers/base/BaseTTSProvider.ts:176

Register a callback to receive audio metadata.

Parameters

ParameterTypeDescription
callback(metadata) => voidFunction invoked with AudioMetadata when available.

Returns

void

Remarks

Metadata (sample rate, encoding, channels, etc.) helps the AudioPlayer configure playback correctly. Providers may emit metadata once at the start of synthesis but are not required to.

Inherited from

RestTTSProvider.onMetadata

processChunk()

processChunk(text): void;

Defined in: src/providers/base/RestTTSProvider.ts:98

Process a text chunk by accumulating it into the buffer.

Parameters

ParameterTypeDescription
textstringA piece of text to accumulate.

Returns

void

Remarks

For REST providers, text is buffered until finalize is called, at which point the full text is sent to synthesize.

Inherited from

RestTTSProvider.processChunk

resolveApiKey()

protected resolveApiKey(): Promise<string>;

Defined in: src/providers/base/BaseProvider.ts:321

Resolve the API key, calling the factory if apiKey is a function.

Returns

Promise<string>

The resolved API key string, or 'proxy' in proxy mode.

Inherited from

RestTTSProvider.resolveApiKey

resolveAuthHeader()

protected resolveAuthHeader(defaultAuthType?): Promise<string | undefined>;

Defined in: src/providers/base/BaseProvider.ts:366

Resolve Authorization header value for the configured auth type.

Parameters

ParameterTypeDefault valueDescription
defaultAuthType"token" | "bearer"'token'The default auth type for this provider.

Returns

Promise<string | undefined>

The Authorization header value, or undefined in proxy mode.

Remarks

If apiKey is a factory function it is called to get a fresh token. Returns the header value for REST or server-side WebSocket connections:

  • 'token''Token <apiKey>'
  • 'bearer''Bearer <apiKey>'

Returns undefined in proxy mode.

Inherited from

RestTTSProvider.resolveAuthHeader

resolveBaseUrl()

protected resolveBaseUrl(defaultUrl?): string | undefined;

Defined in: src/providers/base/BaseProvider.ts:307

Resolve the base URL for this provider.

Parameters

ParameterTypeDescription
defaultUrl?stringThe provider’s default API URL. Pass undefined to let the underlying SDK use its own default.

Returns

string | undefined

The resolved URL, or undefined when all sources are unset.

Remarks

Priority: proxyUrl > endpoint > defaultUrl.

For WebSocket providers (this.type === 'websocket'), the proxy URL’s http(s) scheme is automatically converted to ws(s).

When no URL is configured and defaultUrl is undefined, the return value is undefined — this lets SDK-based providers (Anthropic, OpenAI) fall back to their own built-in defaults.

Inherited from

RestTTSProvider.resolveBaseUrl

resolveWsProtocols()

protected resolveWsProtocols(defaultAuthType?): Promise<string[] | undefined>;

Defined in: src/providers/base/BaseProvider.ts:342

Resolve WebSocket subprotocol for authentication.

Parameters

ParameterTypeDefault valueDescription
defaultAuthType"token" | "bearer"'token'The default auth type for this provider.

Returns

Promise<string[] | undefined>

Subprotocol array for new WebSocket(url, protocols), or undefined.

Remarks

If apiKey is a factory function it is called to get a fresh token. Returns the subprotocol array for direct mode based on authType:

  • 'token'['token', apiKey] (Deepgram default)
  • 'bearer'['bearer', apiKey] (OAuth/Bearer tokens)

Returns undefined in proxy mode (no client-side auth needed).

Inherited from

RestTTSProvider.resolveWsProtocols

synthesize()

synthesize(text): Promise<Blob>;

Defined in: src/providers/tts/fishaudio/FishAudioTTS.ts:372

Synthesizes text to audio using the Fish Audio TTS REST API.

Parameters

ParameterTypeDescription
textstringThe text to synthesize into speech.

Returns

Promise<Blob>

A Blob containing the synthesized audio in the configured format.

Throws

Error if the provider is not initialized.

Throws

Error if the Fish Audio API request fails.

Overrides

RestTTSProvider.synthesize

updateConfig()

updateConfig(config): void;

Defined in: src/providers/base/BaseProvider.ts:201

Merge partial configuration updates into the current config.

Parameters

ParameterTypeDescription
configPartial<BaseProviderConfig>A partial configuration object whose keys will overwrite existing values.

Returns

void

Remarks

After merging, the subclass hook onConfigUpdate is called so providers can react to changed values at runtime.

Inherited from

RestTTSProvider.updateConfig

© 2026 CompositeVoice. All rights reserved.

Font size
Contrast
Motion
Transparency