Skip to content

feat(audio-context): Android non-uniform convolution, HRTF panning and Web Audio spec coverage - #289

Merged
triniwiz merged 2 commits into
masterfrom
feat/android-audio-spec
Nov 25, 2026
Merged

triniwiz merged 2 commits into
masterfrom
feat/android-audio-spec

Conversation

@triniwiz

Copy link
Copy Markdown
Member

Brings the Android @nativescript/audio-context graph much closer to the Web Audio spec. The DSP is native (audio_graph.cpp, convolver.cpp, hrtf.cpp), and audio data stays in native memory.

ConvolverNode: non-uniform partitioning. The design follows Chromium's ReverbConvolver.

  • The first 4096 frames of the response are convolved in 128-frame partitions on the render thread, so there is still no latency.
  • Frames 4096–32768 use 2048-frame partitions, and the rest uses 16384-frame partitions.
  • In a realtime context the longer partitions run on a background thread. Each one starts at least two of its blocks into the response, so the worker has a full block of time to finish.
  • In an OfflineAudioContext they run inline, so offline output is deterministic.
  • On a host benchmark, a 10 s true-stereo response renders 10 s of audio in about 130 ms. Uniform 128-frame partitions would need about 30× the work.

PannerNode panningModel = 'HRTF'. This is real HRIR convolution.

  • Dataset: the MIT Media Lab KEMAR "compact" set, 368 measured directions × 2 ears × 128 taps at 44.1 kHz (about 188 KB as int16, in hrtf_kemar.h). Licence: "This data is Copyright 1994 by the MIT Media Laboratory. It is provided free with no restrictions on use, provided the authors are cited when the data is used in any research or commercial application." Citation: Gardner & Martin, HRTF Measurements of a KEMAR Dummy-Head Microphone, MIT Media Lab Technical Report feat(audio-context): Android OfflineAudioContext on the realtime mixer, with suspend/resume and DelayNode #280 (1994). The citation is also in the header.
  • Kernels: each kernel's onset is split off as an interaural delay, as Chromium's HRTFKernel does. The left side mirrors the measured right side.
  • Interpolation: kernels and delays are interpolated bilinearly between elevation rows and azimuths. They are resampled to the context rate with band-limited, gain-preserving resampling.
  • Rendering: direct-form convolution with no latency, and a crossfade over one quantum when the direction changes. Direction is k-rate; distance and cone gains are per frame.
  • Stereo input: the left input feeds the left ear and the right input feeds the right ear, as in Chromium.
  • Tail: the panner keeps ringing for its kernel and delay after its input stops.
  • Removed: the unused custom-HRIR Java API (setHRTF and setHRTFPartitionSize), and a non-spec distance low-pass that every PannerNode had.

Other native fixes

  • StereoPannerNode pans by pan alone. The listener position, distance and cone no longer affect it.
  • AudioParam exponentialRampToValueAtTime, setTargetAtTime and setValueCurveAtTime are now evaluated natively. They used to be approximated with linear ramps. A ramp with no event before it now starts at the time it was called, from the current value.
  • OscillatorNode.detune is added.
  • AudioBufferSourceNode gets detune, loopStart and loopEnd. loop can change during playback. playbackRate × detune is k-rate.
  • PeriodicWave ignores the DC term and is normalized to a peak of 1 unless disableNormalization is set.
  • WaveShaperNode applies oversample '2x' and '4x' with half-band FIR up- and down-sampling.
  • DynamicsCompressorNode:
    • one detector shared by all channels, 6 ms of look-ahead, and the spec's makeup gain (0.6 × the reduction at 0 dB);
    • reduction is a number metered by the render thread.
  • AnalyserNode:
    • Blackman window;
    • smoothing on |X|/N;
    • byte time-domain data is floor(128·(1+x)).
  • currentTime is never negative.
  • Node param scratch buffers are sized once, so pointers taken earlier in a quantum stay valid.

TypeScript (index.android.ts only; common.ts and index.d.ts are unchanged)

  • Node shape: numberOfInputs and numberOfOutputs on every node.
  • Channel constraints: per-node channelCount, channelCountMode and channelInterpretation rules for splitter, merger, panners, convolver, compressor and destination. destination.maxChannelCount is added.
  • Scheduled sources:
    • start() and stop() validate their arguments and call order;
    • ended fires once, asynchronously, and only after start();
    • ended still fires when the listener was attached before .buffer was set.
  • Contexts:
    • OfflineAudioContext validates its constructor arguments and supports oncomplete and the complete event;
    • createBuffer(numberOfChannels, length, sampleRate) works on both context types;
    • a closed context rejects close, resume and suspend with InvalidStateError;
    • getOutputTimestamp() is added;
    • setSinkId rejects with NotFoundError or InvalidStateError and fires sinkchange;
    • decodeAudioData rejects with EncodingError.
  • AudioBuffer: constructor validation, and IndexSizeError for a channel out of range.
  • Node validation: Delay, IIR, Analyser, Panner, PeriodicWave, WaveShaper (the curve is copied), Convolver (sample rate must match), and Oscillator ('custom' only through a PeriodicWave).
  • BiquadFilterNode: getFrequencyResponse is added.
  • AudioParams: defaultValue, minValue and maxValue. The compressor params and the buffer source's playbackRate and detune are fixed at 'k-rate'.

Specs: two new suites, audio.api (29 tests) and audio.dsp (22 tests). They use an Android-only wrapper and are skipped on other platforms. Among them:

  • a 50 000-frame response checked against direct convolution;
  • a realtime long-response timing check;
  • HRTF ITD and ILD, mirror symmetry, elevation and tail;
  • the automation curves;
  • detune and loop points;
  • PeriodicWave normalization;
  • oversampling;
  • the compressor;
  • the analyser.

Pixel 9 Pro emulator, audio suite: 117/117 passing.

Not done

  • ScriptProcessorNode and the MediaStream* nodes: NativeScript has no MediaStream / getUserMedia, and ScriptProcessorNode is deprecated in favour of AudioWorklet.
  • suspend() and resume() still act on the shared realtime engine, so they affect every realtime context.
  • setSinkId({type: 'none'}) is not supported.
  • AudioParam.value still reads back the last value that was set, not the value computed by automation. That getter is in common.ts.

…d Web Audio spec coverage

- ConvolverNode: non-uniformly partitioned convolution like Chromium's ReverbConvolver. A
  128-frame head (first 4096 frames) runs on the render thread with no latency; 2048- and
  16384-frame partitions run on a background thread for realtime contexts, inline offline.
- PannerNode 'HRTF': per-ear kernels and interaural delays from the MIT KEMAR compact set
  (free use with attribution), interpolated by azimuth and elevation, resampled to the
  context rate, crossfaded on direction changes; stereo input pans left/right per ear.
  Replaces the unused custom-HRIR path and the non-spec distance low-pass on panners.
- StereoPannerNode pans by pan alone (no listener, distance or cone).
- AudioParam: native exponentialRamp, setTarget and setValueCurve; ramps with no prior
  event start from the call time and current value.
- OscillatorNode.detune; AudioBufferSourceNode detune, loopStart and loopEnd (k-rate rate).
- PeriodicWave: DC ignored, normalized to peak 1 unless disableNormalization.
- WaveShaperNode '2x'/'4x' oversampling; DynamicsCompressorNode linked detection, 6 ms
  look-ahead, makeup gain and a readable reduction; AnalyserNode Blackman window,
  magnitude smoothing and spec byte conversion; currentTime never negative.
- TS: numberOfInputs/Outputs, per-node channel constraints, start/stop and ended rules,
  OfflineAudioContext validation and complete event, createBuffer signature, AudioBuffer
  validation, closed-context errors, getOutputTimestamp, setSinkId errors and sinkchange,
  node validation, Biquad getFrequencyResponse, AudioParam default/min/max and fixed rates.
- Specs: audio.api and audio.dsp suites.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: db3bc514-12cf-406a-b213-2f734e17f53d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown

ℹ️ Note: npm publishing is disabled for pull requests.
Publishing is performed only from tag pushes via the npm Release workflow.
If you need a test publish, merge to master — pushes publish pr-tagged prereleases.

@triniwiz
triniwiz merged commit 92cdd28 into master Nov 25, 2026
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants