AudioCapture PRO
The AudioCapture class taps the microphone via AVAudioEngine and
streams real-time signals to your script: raw PCM frames, RMS / peak
levels, and YIN-based pitch estimates. It is the right tool for
tuners, real-time waveform views, level-triggered logic, and custom
DSP. It can optionally write the captured audio to a WAV file.
Choosing between
AudioRecorderandAudioCapture:
AudioRecorderwrites encoded files (m4a / aac / flac / mp3 / opus) and only exposes power meters viaonLevelUpdate. Pick it for the final-quality recording use case.AudioCaptureexposes the raw waveform plus pitch and level callbacks. Pick it for analysis-driven scripts. It can also save a wav file viasaveTo, which makes it a superset for uncompressed recording.- Don't run both at the same time — they would compete for the same audio input bus.
Features
- Real-time PCM buffer callback (
Float32ArrayorInt16Array). - Real-time pitch detection via YIN with note name and cents offset.
- Real-time RMS / peak level callback at a configurable rate.
- Optional WAV file output (32-bit float, hardware sample rate).
Usage
Setup the audio session
AudioCapture shares the system audio session with the rest of the app.
Activate it once before creating the capture instance:
Create an AudioCapture instance
create requests microphone permission and resolves with the instance.
It rejects if permission is denied.
Listen to PCM buffers
Pitch detection (tuner)
Lightweight level meter
If you only need a VU meter, prefer onLevel over onBuffer — it does
not allocate a typed array per frame.
Start, stop, dispose
stop() halts the engine and closes the wav file (if any). dispose()
also tears down the JS callbacks; call it once you no longer need the
instance to free resources promptly.
Error handling
API Reference
AudioCapture.create(config?)
Requests microphone permission and constructs an instance.
- config.sampleRate (number, optional): Hint for the desired sample
rate. The hardware decides the actual value, exposed via
sampleRate. Default44100. - config.channels (
1 | 2, optional): Channel count hint. Default1. - config.bufferSize (number, optional): Frames per tap. Range
[256, 8192]. Default1024. - config.format (
"float32" | "int16", optional): Sample format foronBuffer.samples. Default"float32". - config.saveTo (string, optional): If set, captured audio is written to this path as a 32-bit float WAV file.
Returns a Promise<AudioCapture>.
AudioCapture.isRunning
Whether the engine is running.
AudioCapture.sampleRate
Hardware sample rate after start().
AudioCapture.channels
Hardware channel count after start().
AudioCapture.start()
Starts the engine. Returns false on failure (no input available,
session conflict, etc. — onError will also fire).
AudioCapture.stop()
Stops the engine and closes the wav file (if any).
AudioCapture.dispose()
Releases the engine, all callbacks, and the wav file.
AudioCapture.onBuffer
PCM buffer callback. Each invocation receives a fresh typed array; safe to retain.
AudioCapture.bufferEmitRate
Buffers per second for onBuffer. 0 = follow the hardware tap rate.
AudioCapture.onPitch
YIN-based pitch estimate callback. frequency === 0 indicates an
unvoiced frame.
AudioCapture.pitchConfig
Pitch detector parameters: minFrequency, maxFrequency, threshold,
emitRate.
AudioCapture.onLevel
RMS / peak level callback (no PCM payload). Cheaper than onBuffer.
AudioCapture.levelEmitRate
Levels per second for onLevel. Default 30.
AudioCapture.onError
Error callback. Fires on engine startup failure or runtime issues.
