Skip to main content
Version: Next

RecorderWorklet

warning

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 sampleRate argument rather than assuming one: a snapshot spans bufferLength / sampleRate seconds, and in frequency-domain mode bin i covers i * 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​

ParameterTypeDescription
callbackRecorderWorkletCallbackWorklet invoked on the UI runtime when a snapshot is ready. Must include the 'worklet' directive.
optionsnumber | WorkletNodeOptionsOptional. 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;
ArgumentDescription
audioDataThe snapshot, as in WorkletNodeCallback. Time-domain: mono PCM (bufferLength samples). Frequency-domain: linear magnitude spectrum (bufferLength bins; FFT size bufferLength * 2).
sampleRateThe 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 typeCondition
NotSupportedErrorreact-native-audio-worklets native module not installed, worklet extensions are not linked, or New Architecture is disabled.
NotSupportedErrorreact-native-worklets is missing or below the supported version (>= 0.10.0).
IndexSizeErrorbufferLength 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:

  1. Mix down. The buffer is mixed down to mono and added to a window that always holds the newest frames: bufferLength frames in time domain, bufferLength * 2 in frequency domain.
  2. Snapshot. A snapshot is due every bufferLength frames. Time-domain snapshots are back to back: each one holds the bufferLength samples since the previous one. Frequency-domain snapshots overlap by half: each is the spectrum (window, FFT, magnitude smoothing) of the newest bufferLength * 2 frames, reduced to bufferLength bins.
  3. 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 / bufferLength callbacks a second: about 47 for bufferLength: 1024 at 48 kHz, or about 23 for 2048. Only one snapshot is sent per captured buffer, so when the input delivers buffers longer than bufferLength (common on Android), the rate drops to one per buffer. For a display refreshed every frame, a bufferLength that gives one snapshot per frame or less is enough.
  • Window length. A time-domain snapshot covers bufferLength / sampleRate seconds; a frequency-domain one covers twice that and refreshes twice per window.
  • First snapshot. It arrives once a full window has been captured: bufferLength frames in time domain, bufferLength * 2 in 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 RecorderWorklet to 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​

PropertyTypeDescription
bufferLength
Read only
numberSnapshot size passed to the callback (set at construction). In frequency-domain mode, the internal FFT size is bufferLength * 2.
smoothingTimeConstantnumberExponential smoothing of linear magnitude bins (0..1, default 0.8). Meaningful in frequency-domain mode.