MicrophoneInput
Browser audio input provider that captures audio from the microphone.
Defined in: src/providers/input/MicrophoneInput.ts:149
Browser audio input provider that captures audio from the microphone.
Remarks
MicrophoneInput adapts the SDK’s existing AudioCapture to the AudioInputProvider interface, making microphone capture a first-class pipeline role. It delegates all operations to AudioCapture:
| AudioInputProvider method | AudioCapture method / behavior |
|---|---|
start() | start(callback) |
stop() | stop() |
pause() | pause() |
resume() | resume() |
isActive() | isCapturing() |
onAudio(callback) | Stores callback for chunk delivery |
getMetadata() | Returns metadata from config |
Raw ArrayBuffer chunks from AudioCapture are wrapped into AudioChunk objects with timestamps and sequence numbers.
Data-flow diagram:
Microphone ──getUserMedia──> AudioCapture ──callback──> MicrophoneInput
|
wrap as AudioChunk
|
v
onAudio callback
|
v
InputQueue -> STT
Example
import { MicrophoneInput } from 'composite-voice';
import type { AudioChunk } from 'composite-voice';
// Create with custom microphone settings
const input = new MicrophoneInput({
sampleRate: 16000,
format: 'pcm',
channels: 1,
echoCancellation: true,
});
// Use in a 5-provider pipeline
const voice = new CompositeVoice({
providers: [
input,
new DeepgramSTT({ apiKey: '...' }),
new AnthropicLLM({ model: 'claude-sonnet-4-20250514', apiKey: '...' }),
new DeepgramTTS({ apiKey: '...' }),
new BrowserAudioOutput(),
],
});
await voice.initialize();
await voice.startListening();
See
- AudioCapture for the underlying Web Audio API implementation
- AudioInputProvider for the interface contract
- MicrophoneInputConfig for configuration options
- BufferInput for the server-side counterpart
Implements
Constructors
Constructor
new MicrophoneInput(config?): MicrophoneInput;
Defined in: src/providers/input/MicrophoneInput.ts:211
Creates a new MicrophoneInput instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | Partial<MicrophoneInputConfig> | Optional audio input configuration. Missing fields use defaults from DEFAULT_AUDIO_INPUT_CONFIG. |
Returns
MicrophoneInput
Remarks
Instantiates the internal AudioCapture with the provided configuration. The provider starts in an uninitialized state; call initialize() before use.
Example
// Default configuration
const input = new MicrophoneInput();
// Custom microphone settings
const input = new MicrophoneInput({
sampleRate: 16000,
format: 'pcm',
channels: 1,
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true,
});
Properties
| Property | Modifier | Type | Default value | Description | Defined in |
|---|---|---|---|---|---|
roles | readonly | readonly ProviderRole[] | undefined | Pipeline roles covered by this provider. Remarks MicrophoneInput is a single-role provider covering only the 'input' slot. Unlike NativeSTT (which covers 'input' + 'stt'), this provider handles audio capture only and requires a separate STT provider. | src/providers/input/MicrophoneInput.ts:161 |
type | readonly | ProviderType | 'rest' | The communication type this provider uses. See ProviderType | src/providers/input/MicrophoneInput.ts:151 |
Methods
dispose()
dispose(): Promise<void>;
Defined in: src/providers/input/MicrophoneInput.ts:256
Dispose of the provider and release all resources.
Returns
Promise<void>
Remarks
Delegates to AudioCapture.dispose(), which stops capture, closes the AudioContext, and releases the media stream. After disposal the instance should not be reused.
Implementation of
getConfig()
getConfig(): Partial<MicrophoneInputConfig>;
Defined in: src/providers/input/MicrophoneInput.ts:286
Get a copy of the current audio input configuration.
Returns
Partial<MicrophoneInputConfig>
A shallow copy of the current AudioInputConfig from the underlying AudioCapture.
getMetadata()
getMetadata(): AudioMetadata;
Defined in: src/providers/input/MicrophoneInput.ts:431
Get the audio format metadata for the captured audio.
Returns
The AudioMetadata describing the captured audio format.
Remarks
Returns metadata derived from the configuration: sampleRate from config (default 16000), encoding as 'linear16' (AudioCapture always outputs 16-bit PCM), channels from config (default 1), and bitDepth of 16.
Used by the pipeline to auto-configure the downstream STT provider via configureSTTFromMetadata().
See
Implementation of
AudioInputProvider.getMetadata
initialize()
initialize(): Promise<void>;
Defined in: src/providers/input/MicrophoneInput.ts:237
Initialize the provider, making it ready for capture.
Returns
Promise<void>
Remarks
Sets the initialized flag. If already initialized, this is a no-op. The actual microphone permission and AudioContext setup happen in start(), not here.
Implementation of
isActive()
isActive(): boolean;
Defined in: src/providers/input/MicrophoneInput.ts:390
Check whether the microphone is actively capturing audio.
Returns
boolean
true when audio is actively being captured from the microphone.
Remarks
Delegates to AudioCapture.isCapturing(). Returns true only when the capture state is 'active'.
See
Implementation of
isReady()
isReady(): boolean;
Defined in: src/providers/input/MicrophoneInput.ts:276
Check whether the provider has been initialized and is ready for use.
Returns
boolean
true when initialize has completed and dispose has not yet been called.
Implementation of
onAudio()
onAudio(callback): void;
Defined in: src/providers/input/MicrophoneInput.ts:410
Register a callback to receive audio chunks.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | (chunk) => void | Function invoked with each AudioChunk when audio is captured from the microphone. |
Returns
void
Remarks
Only one callback can be registered at a time; subsequent calls replace the previous callback. Must be called before start() so chunks are delivered.
Each chunk is an AudioChunk containing the raw audio data, metadata, timestamp, and a monotonically increasing sequence number.
See
Implementation of
pause()
pause(): void;
Defined in: src/providers/input/MicrophoneInput.ts:361
Temporarily pause microphone capture.
Returns
void
Remarks
Delegates to AudioCapture.pause(), which suspends the AudioContext. No audio data is delivered while paused. Resume with resume().
See
Implementation of
resume()
resume(): void;
Defined in: src/providers/input/MicrophoneInput.ts:375
Resume microphone capture after a pause.
Returns
void
Remarks
Delegates to AudioCapture.resume(), which resumes the suspended AudioContext.
See
Implementation of
start()
start(): void;
Defined in: src/providers/input/MicrophoneInput.ts:321
Start capturing audio from the microphone.
Returns
void
Remarks
Delegates to AudioCapture.start(callback), providing an internal callback that wraps raw ArrayBuffer chunks into AudioChunk objects with a timestamp and sequence number before forwarding them to the registered callback.
A callback must be registered via onAudio() before calling start().
See
Implementation of
stop()
stop(): void;
Defined in: src/providers/input/MicrophoneInput.ts:346
Stop capturing audio from the microphone.
Returns
void
Remarks
Delegates to AudioCapture.stop(). The provider can be restarted with start().
See
Implementation of
updateConfig()
updateConfig(config): void;
Defined in: src/providers/input/MicrophoneInput.ts:301
Update the audio input configuration at runtime.
Parameters
| Parameter | Type | Description |
|---|---|---|
config | Partial<MicrophoneInputConfig> | Partial configuration to merge with current settings. |
Returns
void
Remarks
Delegates to AudioCapture.updateConfig(). Configuration changes take effect on the next call to start(). They do not affect an active capture session.