Mic
Mic is the microphone, as a single object shared by the whole app. It captures
audio, reports how loud the room is, and lists the inputs the system offers.
The recording itself is done by a driver: the Web Audio API in a browser, the
native audio engine on a phone. Importing @micdrop/web or
@micdrop/react-native installs the right one, so there is nothing to wire.
Usage Example
import { Mic } from '@micdrop/web'
// Ask for the microphone permission and start capturingawait Mic.start()
// Or record from a specific inputawait Mic.start(deviceId)
// The level, in decibels between -Infinity and 0, about ten times a secondMic.on('Volume', (volume: number) => { console.log('Current level:', volume)})
// Stop capturing and release the microphoneawait Mic.stop()Starting and stopping a call already does this for you, see
Start/Stop Call. Reach for Mic when you need the
microphone on its own, for a level meter before the call begins for instance.
API Reference
Methods
start(deviceId?: string): Promise<void>
Asks for the microphone permission and starts capturing, from the given input or from the system one.
stop(): Promise<void>
Stops capturing and releases the microphone.
getDevices(): Promise<MicdropDevice[]>
Lists the inputs the system offers, each with an id, a label to show and its
kind. A browser only reveals the labels once a stream is running.
setDriver(driver: MicDriver): void
Records with another library. See Using another audio library.
Properties
isStarted: boolean
Whether the microphone is capturing.
deviceId: string | undefined
The input in use, when the system reports one.
volume: number
The last level measured, in decibels between -Infinity and 0.
Events
Volume: [number]
The level of the captured audio, about ten times a second. It is the loudest frequency rather than the overall energy, which is what separates a voice from a room, see Voice Activity Detection.
Frames: [Float32Array, number]
A batch of mono samples in the -1..1 range, with their sample rate. This is the raw capture, before any chunking.
DeviceChange: []
The system gained or lost an audio device.
Error: [Error]
Capture failed.