audio-mic
v1.0.0
Published
Capture audio from microphone in browser/node
Maintainers
Readme
audio-mic
Capture audio from microphone in node or browser.
Usage
import mic from 'audio-mic'
let read = mic({
sampleRate: 44100,
channels: 1,
bitDepth: 16,
// bufferSize: 50, // ring buffer ms (default 50)
// backend: 'miniaudio', // force backend
})
read((err, pcmBuffer) => {
// process chunk
})
read(null) // stop captureNode Readable
import MicReadable from 'audio-mic/stream'
MicReadable({ sampleRate: 44100, channels: 1 }).pipe(dest)Backends
Tried in order; first successful one wins.
| Backend | How | Latency | Install |
|---|---|---|---|
| miniaudio | N-API addon wrapping miniaudio.h | Low | Prebuilt via @audio/mic-* packages |
| process | Pipes from ffmpeg/sox/arecord | High | System tool must be installed |
| null | Silent, maintains timing contract | — | Built-in (CI/headless fallback) |
| mediastream | getUserMedia + AudioWorklet (browser) | Low | Built-in |
API
read = mic(opts?)
Returns a source function. Options:
sampleRate— default44100channels— default1bitDepth—8,16(default),24,32bufferSize— ring buffer in ms, default50backend— force a specific backend
read(cb)
Read PCM data. Callback fires with each captured chunk: (err, buffer) => {}.
read(null)
Stop capture. Closes the audio device.
read.close()
Immediately close the audio device.
read.backend
Name of the active backend ('miniaudio', 'process', 'null', 'mediastream').
Building
npm run build # compile native addon locally
npm test # run testsPublishing
# JS-only change (no native code changed):
npm version patch && git push && git push --tags
npm publish
# Native code changed — rebuild platform packages:
npm version patch && git push && git push --tags
gh run watch # wait for CI
rm -rf artifacts
gh run download --dir artifacts \
-n mic-darwin-arm64 -n mic-darwin-x64 \
-n mic-linux-x64 -n mic-linux-arm64 -n mic-win32-x64
# (fallback) If darwin-x64 CI is unavailable, cross-compile locally:
npx node-gyp@latest rebuild --arch=x64
mkdir -p artifacts/mic-darwin-x64
cp build/Release/mic.node artifacts/mic-darwin-x64/
for pkg in packages/mic-*/; do
cp artifacts/$(basename $pkg)/mic.node $pkg/
(cd $pkg && npm publish)
done
npm publishLicense
MIT
ॐ
