BrowserAudioOutput
Browser audio output provider that plays audio through the Web Audio API.
Defined in: src/providers/output/BrowserAudioOutput.ts:140
Browser audio output provider that plays audio through the Web Audio API.
Remarks
BrowserAudioOutput adapts the SDK’s existing AudioPlayer to the AudioOutputProvider interface, making speaker playback a first-class pipeline role. It delegates all operations to AudioPlayer:
| AudioOutputProvider method | AudioPlayer method |
|---|---|
configure(metadata) | setMetadata(metadata) |
enqueue(chunk) | addChunk(chunk) |
flush() | waitForCompletion() |
stop() | stop() |
pause() | pause() |
resume() | resume() |
isPlaying() | isPlaying() |
Playback callbacks (onPlaybackStart, onPlaybackEnd, onPlaybackError) are wired through to AudioPlayer.setCallbacks().
Data-flow diagram:
OutputQueue ──enqueue()──> BrowserAudioOutput ──addChunk()──> AudioPlayer
| |
configure() setMetadata()
flush() waitForCompletion()
stop() stop()
Example
import { BrowserAudioOutput } from 'composite-voice';
import type { AudioChunk, AudioMetadata } from 'composite-voice';
// Create with custom buffer settings
const output = new BrowserAudioOutput({
minBufferDuration: 150,
enableSmoothing: true,
});
// Use in a 5-provider pipeline
const voice = new CompositeVoice({
providers: [
new MicrophoneInput(),
new DeepgramSTT({ apiKey: '...' }),
new AnthropicLLM({ model: 'claude-sonnet-4-20250514', apiKey: '...' }),
new DeepgramTTS({ apiKey: '...' }),
output,
],
});
// Register playback lifecycle callbacks
output.onPlaybackStart(() => console.log('Speaking...'));
output.onPlaybackEnd(() => console.log('Done speaking'));
output.onPlaybackError((err) => console.error('Playback error:', err));
See
- AudioPlayer for the underlying Web Audio API implementation
- AudioOutputProvider for the interface contract
- BrowserAudioOutputConfig for configuration options
- NullOutput for the server-side no-op counterpart
Implements
Constructors
Constructor
new BrowserAudioOutput(config?): BrowserAudioOutput;
Defined in: src/providers/output/BrowserAudioOutput.ts:196
Creates a new BrowserAudioOutput instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | Partial<BrowserAudioOutputConfig> | Optional audio output configuration. Missing fields use defaults from DEFAULT_AUDIO_OUTPUT_CONFIG. |
Returns
BrowserAudioOutput
Remarks
Instantiates the internal AudioPlayer with the provided configuration. The provider starts in an uninitialized state; call initialize() before use.
Example
// Default configuration
const output = new BrowserAudioOutput();
// Custom buffer settings
const output = new BrowserAudioOutput({
minBufferDuration: 100,
sampleRate: 24000,
enableSmoothing: true,
});
Properties
| Property | Modifier | Type | Default value | Description | Defined in |
|---|---|---|---|---|---|
roles | readonly | readonly ProviderRole[] | undefined | Pipeline roles covered by this provider. Remarks BrowserAudioOutput is a single-role provider covering only the 'output' slot. Unlike NativeTTS (which covers 'tts' + 'output'), this provider handles playback only and requires a separate TTS provider. | src/providers/output/BrowserAudioOutput.ts:152 |
type | readonly | ProviderType | 'rest' | The communication type this provider uses. See ProviderType | src/providers/output/BrowserAudioOutput.ts:142 |
Methods
configure()
configure(metadata): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:292
Configure the output with audio format metadata from the TTS provider.
Parameters
| Parameter | Type | Description |
|---|---|---|
metadata | AudioMetadata | Format description for the incoming audio stream. |
Returns
void
Remarks
Delegates to AudioPlayer.setMetadata(). This should be called before enqueuing chunks so the player knows how to decode and buffer the incoming audio data.
See
Implementation of
dispose()
dispose(): Promise<void>;
Defined in: src/providers/output/BrowserAudioOutput.ts:230
Dispose of the provider and release all resources.
Returns
Promise<void>
Remarks
Delegates to AudioPlayer.dispose(), which stops playback, closes the AudioContext, and clears callback references. After disposal the instance should not be reused.
Implementation of
enqueue()
enqueue(chunk): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:309
Enqueue an audio chunk for playback.
Parameters
| Parameter | Type | Description |
|---|---|---|
chunk | AudioChunk | Audio data to play. |
Returns
void
Remarks
Delegates to AudioPlayer.addChunk(). Chunks are buffered and played sequentially. The player applies minimum buffer duration gating before starting playback.
See
Implementation of
flush()
flush(): Promise<void>;
Defined in: src/providers/output/BrowserAudioOutput.ts:323
Wait for all enqueued audio to finish playing.
Returns
Promise<void>
Remarks
Delegates to AudioPlayer.waitForCompletion(). Resolves when the player’s internal queue is drained and the state returns to 'idle'.
See
Implementation of
getConfig()
getConfig(): Partial<BrowserAudioOutputConfig>;
Defined in: src/providers/output/BrowserAudioOutput.ts:260
Get a copy of the current audio output configuration.
Returns
Partial<BrowserAudioOutputConfig>
A shallow copy of the current AudioOutputConfig.
initialize()
initialize(): Promise<void>;
Defined in: src/providers/output/BrowserAudioOutput.ts:210
Initialize the provider, making it ready for playback.
Returns
Promise<void>
Remarks
Wires up playback lifecycle callbacks to the underlying AudioPlayer via setCallbacks(). If already initialized, this is a no-op.
Implementation of
AudioOutputProvider.initialize
isPlaying()
isPlaying(): boolean;
Defined in: src/providers/output/BrowserAudioOutput.ts:378
Check whether audio is currently playing.
Returns
boolean
true when audio is actively being played through speakers.
Remarks
Delegates to AudioPlayer.isPlaying(). Returns true only when the player is in the 'playing' state.
See
Implementation of
isReady()
isReady(): boolean;
Defined in: src/providers/output/BrowserAudioOutput.ts:251
Check whether the provider has been initialized and is ready for playback.
Returns
boolean
true when initialize has completed and dispose has not yet been called.
Implementation of
onPlaybackEnd()
onPlaybackEnd(callback): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:411
Register a callback invoked when all audio has finished playing.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | () => void | Function called when playback ends. |
Returns
void
Remarks
The callback is forwarded to AudioPlayer via setCallbacks(). Only one callback can be registered at a time; subsequent calls replace the previous callback.
See
AudioOutputProvider.onPlaybackEnd
Implementation of
AudioOutputProvider.onPlaybackEnd
onPlaybackError()
onPlaybackError(callback): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:428
Register a callback invoked when a playback error occurs.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | (error) => void | Function called with the error. |
Returns
void
Remarks
The callback is forwarded to AudioPlayer via setCallbacks(). Only one callback can be registered at a time; subsequent calls replace the previous callback.
See
AudioOutputProvider.onPlaybackError
Implementation of
AudioOutputProvider.onPlaybackError
onPlaybackStart()
onPlaybackStart(callback): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:394
Register a callback invoked when audio playback begins.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | () => void | Function called when playback starts. |
Returns
void
Remarks
The callback is forwarded to AudioPlayer via setCallbacks(). Only one callback can be registered at a time; subsequent calls replace the previous callback.
See
AudioOutputProvider.onPlaybackStart
Implementation of
AudioOutputProvider.onPlaybackStart
pause()
pause(): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:350
Temporarily pause playback by suspending the AudioContext.
Returns
void
Remarks
Delegates to AudioPlayer.pause(). Playback can be resumed with resume().
See
Implementation of
resume()
resume(): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:363
Resume playback after a pause.
Returns
void
Remarks
Delegates to AudioPlayer.resume().
See
Implementation of
stop()
stop(): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:337
Stop playback immediately and clear any buffered audio.
Returns
void
Remarks
Delegates to AudioPlayer.stop(). This stops the current AudioBufferSourceNode, clears the queue, and invokes the onPlaybackEnd callback.
See
Implementation of
updateConfig()
updateConfig(config): void;
Defined in: src/providers/output/BrowserAudioOutput.ts:273
Update the audio output configuration at runtime.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | Partial<BrowserAudioOutputConfig> | Partial configuration to merge with current settings. |
Returns
void
Remarks
Delegates to AudioPlayer.updateConfig(). Some settings (e.g. sampleRate) only take effect on the next AudioContext.