RecorderWorklet
Requires react-native-audio-worklets, react-native-worklets >= 0.10.0, and react-native-audio-api >= 1.1.0. See the Worklets introduction for installation.
RecorderWorklet hands snapshots of what an AudioRecorder captures to your worklet on the UI runtime, with no AudioContext. Use it to animate a level meter or a spectrum while recording. It builds snapshots the same way as WorkletNode, from the recorder's input instead of an audio graph.
Two things differ from WorkletNode:
- The snapshot is mono. Whatever the input's channel count, its channels are mixed down to one before the snapshot is taken.
- The sample rate is the input's, not a context's. It depends on the device and the route (built-in microphone, headset, Bluetooth) and can change mid-recording, so read it from the callback's
sampleRateargument rather than assuming one: a snapshot spansbufferLength / sampleRateseconds, and in frequency-domain mode binicoversi * sampleRate / (bufferLength * 2)Hz.
Constructor
import { RecorderWorklet } from 'react-native-audio-worklets';
const meter = new RecorderWorklet(callback, 1024);
// or with explicit options:
const spectrum = new RecorderWorklet(callback, {
domain: 'frequency-domain',
bufferLength: 512,
smoothingTimeConstant: 0.6,
});
recorder.addConsumer(meter);
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | RecorderWorkletCallback | Worklet invoked on the UI runtime when a snapshot is ready. Must include the 'worklet' directive. |
options | number | WorkletNodeOptions | Optional. The same options as WorkletNode. Defaults: time-domain, bufferLength: 1024. |
As with WorkletNode, compute what you need inside the callback or copy audioData: its memory is refilled for the next snapshot.
RecorderWorkletCallback
type RecorderWorkletCallback = (audioData: Float32Array, sampleRate: number) => void;
| Argument | Description |
|---|---|
audioData | The snapshot, as in WorkletNodeCallback. Time-domain: mono PCM (bufferLength samples). Frequency-domain: linear magnitude spectrum (bufferLength bins; FFT size bufferLength * 2). |
sampleRate | The input's sample rate for this snapshot, in Hz. It changes when the input route does, so read it on every call: a snapshot spans bufferLength / sampleRate seconds, and bin i covers i * sampleRate / (bufferLength * 2) Hz. |
Errors
| Error type | Condition |
|---|---|
NotSupportedError | react-native-audio-worklets native module not installed, worklet extensions are not linked, or New Architecture is disabled. |
NotSupportedError | react-native-worklets is missing or below the supported version (>= 0.10.0). |
IndexSizeError | bufferLength or smoothingTimeConstant is out of range. |
How buffering works
The recorder hands each captured buffer to the worklet, whatever the buffer's size. For each buffer:
- Mix down. The buffer is mixed down to mono and added to a window that always holds the newest frames:
bufferLengthframes in time domain,bufferLength * 2in frequency domain. - Snapshot. A snapshot is due every
bufferLengthframes. Time-domain snapshots are back to back: each one holds thebufferLengthsamples since the previous one. Frequency-domain snapshots overlap by half: each is the spectrum (window, FFT, magnitude smoothing) of the newestbufferLength * 2frames, reduced tobufferLengthbins. - Deliver. The callback is scheduled on the UI runtime with the snapshot. Only one callback runs at a time. While it runs, snapshots that fall due are skipped rather than queued, and the next one is sent with the first buffer captured after the callback returns, holding the newest frames. A slow callback sees fewer snapshots, never stale ones, and the recording is not affected.
What this means for your callback:
- Rate. At most
sampleRate / bufferLengthcallbacks a second: about 47 forbufferLength: 1024at 48 kHz, or about 23 for2048. Only one snapshot is sent per captured buffer, so when the input delivers buffers longer thanbufferLength(common on Android), the rate drops to one per buffer. For a display refreshed every frame, abufferLengththat gives one snapshot per frame or less is enough. - Window length. A time-domain snapshot covers
bufferLength / sampleRateseconds; a frequency-domain one covers twice that and refreshes twice per window. - First snapshot. It arrives once a full window has been captured:
bufferLengthframes in time domain,bufferLength * 2in frequency domain. The same applies after the input changes format mid-recording (a new route, a different sample rate). - Not every sample. Snapshots are for display. Under load, buffers can be dropped before they reach the worklet, so don't use a
RecorderWorkletto capture or save the audio itself.
Example
A level meter for whatever the recorder captures:
import { AudioRecorder } from 'react-native-audio-api';
import { RecorderWorklet } from 'react-native-audio-worklets';
import { useSharedValue, withSpring } from 'react-native-reanimated';
const recorder = new AudioRecorder();
function LevelMeter() {
const level = useSharedValue(0);
useEffect(() => {
const meter = new RecorderWorklet((audioData) => {
'worklet';
let sum = 0;
for (let i = 0; i < audioData.length; i++) {
sum += audioData[i] * audioData[i];
}
level.value = withSpring(Math.min(Math.sqrt(sum / audioData.length) * 4, 1));
}, 1024);
recorder.addConsumer(meter);
return () => recorder.removeConsumer(meter);
}, [level]);
// ...
}
Properties
| Property | Type | Description |
|---|---|---|
bufferLength Read only | number | Snapshot size passed to the callback (set at construction). In frequency-domain mode, the internal FFT size is bufferLength * 2. |
smoothingTimeConstant | number | Exponential smoothing of linear magnitude bins (0..1, default 0.8). Meaningful in frequency-domain mode. |