MicRecorder
The MicRecorder class provides functionality for recording audio from a microphone with voice activity detection (VAD). It uses the browser’s MediaRecorder API and integrates with a VAD (Voice Activity Detection) system for speech detection.
Overview
The MicRecorder class is a core component that can be used in two ways:
-
As part of MicdropClient: The
MicRecorderis automatically managed byMicdropClientwhen using it for voice conversations.MicdropClientcreates an instance internally and handles all the microphone setup, speech detection, and audio streaming. -
As a standalone component: You can use
MicRecorderdirectly if you only need microphone recording and speech detection functionality without the WebSocket communication and conversation management provided byMicdropClient.
This flexibility allows you to either use the full voice conversation capabilities through MicdropClient, or implement your own custom audio handling using just the microphone recording features of MicRecorder.
Features
- Voice activity detection (VAD)
- Multiple audio format support (ogg, webm, mp4, wav)
- Event-based architecture
- State management
Usage Example
import { Mic, MicRecorder } from '@micdrop/web'
// Create a new recorder instance with VAD config (string, VAD instance, or array)const recorder = new MicRecorder('volume')
// Start the microphone, then listen to itawait Mic.start()await recorder.start(Mic)
// Listen for eventsrecorder.on('StartSpeaking', () => { console.log('User started speaking')})
recorder.on('StopSpeaking', () => { console.log('User stopped speaking')})
recorder.on('Chunk', (chunk: Int16Array) => { // 100 ms of mono PCM16 at 16 kHz, ready to be sent to the server console.log('Received audio chunk:', chunk.length)})State
The recorder maintains a state object with the following properties:
interface MicRecorderState { isStarting: boolean // Whether the recorder is in the process of starting isStarted: boolean // Whether the recorder is currently active isSpeaking: boolean // Whether speech is currently detected}Events
The recorder emits the following events:
Chunk: Emitted when a new audio chunk is available, 100 ms of mono PCM16 at 16 kHzStartSpeaking: Emitted when speech is detectedStopSpeaking: Emitted when speech endsStateChange: Emitted when the recorder’s state changes
Methods
constructor(vadConfig?: VADConfig)
Creates a new MicRecorder instance with the provided VAD config. The config can be:
- A string (
'volume'or'silero') - A VAD instance
- An array of VAD configs for multiple VADs
start(mic: MicSource): Promise<void>
Starts the recorder, listening to the microphone it is given.
stop(): void
Stops the recorder and cleans up resources.
VAD
The MicRecorder class uses a VAD (Voice Activity Detection) system to detect speech. You can access the internal VAD instance via recorder.vad to update options or listen to VAD-specific events.
See VAD documentation for more information and available options.
Technical Details
- Uses a delayed stream to avoid cutting off speech at the beginning of detection
- Audio is recorded in chunks of 100ms when speech is detected
- Default audio settings: 128kbps bitrate
- Supports multiple audio formats with fallback options