Skip to content

feat(audio-context): render the Android audio graph as a real node graph - #285

Merged
triniwiz merged 3 commits into
masterfrom
feat/android-audio-graph
Nov 25, 2026
Merged

triniwiz merged 3 commits into
masterfrom
feat/android-audio-graph

Conversation

@triniwiz

Copy link
Copy Markdown
Member

What was wrong

The Android engine had no node graph. Each connect was flattened onto the source voices feeding the node: a voice had one gain, one filter and one panner slot per output, and DelayNode and AudioWorkletNode inputs lived in side maps (voiceDelay_, voiceWorklet_, the emitVoice lambda). Because of that:

  • A node feeding a DelayNode or a worklet input played only there, with no fan-out. A source couldn't fan out to two gains.
  • Chained gains replaced each other instead of multiplying.
  • Wiring depended on call order: a node connected before it had sources was attached as a voice of its target.
  • A DelayNode couldn't be in a feedback loop.
  • source.disconnect(firstGain) in a gain→gain chain didn't silence the source.
  • node.connect(AudioParam) wasn't supported.

Design

The engine now keeps the graph itself and renders it (audio_graph.cpp):

  • Nodes and edges. Every node is a GraphNode with inputs and outputs, each a native bus of up to 32 channels × 128 frames. Java's connect/disconnect become one native command per edge, so connections are made in any order and are cut exactly.
  • Per-quantum rendering. Nodes render a 128-frame quantum at a time in topological order. Each input sums its active connections at its computed channel count (channelCount, channelCountMode, channelInterpretation, with Web Audio defaults per node), using the speaker rules for mono/stereo/quad/5.1 or discrete mixing. Outputs fan out to any number of inputs.
  • Cycles. Tarjan's SCCs find cycles. A DelayNode in a cycle reads its line before its input is written, so its delay is at least one quantum, as in the spec. A cycle without a DelayNode is muted.
  • Audio-rate params. node.connect(AudioParam) adds the source's mono mix to the param every frame. This covers gain, delayTime, ConstantSource offset, oscillator frequency, playbackRate, compressor params, panner params and worklet params.
  • Realtime and offline. Realtime callbacks play from a one-quantum FIFO on the existing frame clock. Offline renders one quantum per call, as before, with the eventfd-woken worklets and suspend/resume unchanged.
  • Node lifetime. A source's node leaves the graph when it ends. A node the app frees (finalizer or release) while something still plays into it stays until that stops, and only then is its native state freed. Destinations go with their context.
  • Memory. All audio stays in native memory, and rendering a quantum allocates nothing on the Java heap.

What changed

  • C++: audio_graph.cpp (new) holds the graph, ordering and per-node DSP: sources, gain, delay, biquad, IIR, panner/HRTF/stereo panner, compressor, waveshaper, convolver (pass-through, as before), analyser (records its own input), splitter/merger, worklets, destination. audio_render.cpp keeps stream setup and commands, without the 3,000-line flattened mixer. The per-voice gain/filter/panner slots, voiceDelay_, voiceWorklet_ and the attach/detach commands and JNI are gone.
  • Java: AudioNode default methods route every connect/disconnect (including connect(AudioParam) and setChannelConfig) to AudioContext.connectNodes and friends. The per-class flattening code and nodeSources bookkeeping are removed. A buffer source that takes a new buffer keeps its connections.
  • JS: connect(param) and disconnect(param) reach native, and channelCount, channelCountMode and channelInterpretation are exposed on Android nodes.
  • README: dropped the Android limits that no longer apply and documented the graph and connect(param).

Specs

  • New audio.graph suite (offline, frame-exact): fan-out from a source to two gains; chained gains multiply; any wiring order; a DelayNode feedback loop echoing at 1, 0.5, 0.25; source.disconnect(node) in a chain silences it; a node fanning out into a delay and the destination; an explicit channelCount 1 gain mixing stereo to mono. The feedback and channelCount specs are skipped on iOS (AVAudioEngine has no cycles; iOS nodes have no channel settings).
  • Removed the Android skips for the two connect(AudioParam) specs.

Android audio group on the Pixel 9 Pro emulator: 52 passed, 0 failed, 0 skipped. That's the 43 from before, the 7 new graph specs and the 2 connect(AudioParam) specs. The graph logic was also exercised off-device under ASan/UBSan with offline renders of the same cases.

…ose, channel mixing and worklet wakeups

- Realtime blocks run on a frame clock instead of the device timestamp, which jittered between
  callbacks, so a source's position and gain automation drifted apart (tens of frames).
- nativeAttachPanner/Biquad/PlaybackRate export their 2-arg overloads under the long JNI name;
  the short name also bound the 4-arg overloads and dropped output/input.
- AudioNode.disconnect calls the Java overloads (disconnectOutput/disconnectTo didn't exist, so
  every targeted disconnect was a full one). A new CMD_DETACH_FROM clears only the voice slots
  holding the target.
- Closing a realtime AudioContext stops and frees its voices and nodes; the shared engine only
  suspends once no realtime context is open. Buffer sources whose buffer is set after creation
  are now created in their context's engine and tracked by it (offline ones were created on the
  realtime engine and never rendered).
- An empty gain attach clears voiceDelay_.
- Buffer sources and worklet outputs up/down-mix with the Web Audio speaker rules (mono, stereo,
  quad, 5.1), so a stereo buffer in a mono context plays (L+R)/2.
- Worklet inputs render at each input's own channelCount from each source's own channels.
- The worklet Worker is woken by an eventfd on its looper per requested quantum instead of a
  2 ms poll (Atomics.notify can't come from the render thread). 400 offline quanta with an
  input worklet: ~1000 ms -> ~130 ms.
- BiquadFilterNode.type has a getter/setter on Android.
Android flattened every connection onto the source voices: one gain, filter and
panner slot per voice output, with DelayNode and worklet inputs in side maps.
A node feeding a delay or a worklet played only there, a source couldn't fan out
to two gains, chained gains replaced each other, wiring depended on the order of
connect calls, a DelayNode couldn't sit in a feedback loop, and
source.disconnect(firstGain) left a gain chain playing.

The engine now keeps the graph itself (audio_graph.cpp). connect/disconnect go
straight to native as edges; each 128-frame quantum renders the nodes in
topological order, each input summing its connections at its computed channel
count with the Web Audio up/down-mix rules. Tarjan's SCCs find cycles: a cycle
through a DelayNode reads the delay line before its input is written (delay at
least one quantum), a cycle without one is muted. node.connect(AudioParam) adds
the source's mono mix to gain-style and panner params every frame. Realtime
callbacks play from a quantum FIFO; offline renders a quantum per call.
Nodes the app frees while something still plays into them stay until it stops.
…graph

# Conflicts:
#	packages/audio-context/README.md
#	packages/audio-context/index.android.ts
#	packages/audio-context/platforms/android/audiocontext-release.aar
#	packages/audio-context/src-native/android/audiocontext/src/main/cpp/audio_context.h
#	packages/audio-context/src-native/android/audiocontext/src/main/cpp/audio_jni.cpp
#	packages/audio-context/src-native/android/audiocontext/src/main/cpp/audio_render.cpp
#	packages/audio-context/src-native/android/audiocontext/src/main/cpp/audio_worklet.cpp
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AnalyserNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioBiquadNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioContext.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioIIRNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioPannerNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/AudioScheduledSourceNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/ChannelMergerNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/ChannelSplitterNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/ConvolverNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/DelayNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/DynamicsCompressorNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/GainNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/WaveShaperNode.java
#	packages/audio-context/src-native/android/audiocontext/src/main/java/org/nativescript/audiocontext/WorkletNode.java
#	tools/demo/canvas/spec/audio-offline-render.ts
@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: 164f470a-5175-40da-8258-c71b42d6b2e9

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 f18e91e into master Nov 25, 2026
36 checks passed
@triniwiz
triniwiz deleted the feat/android-audio-graph branch November 25, 2026 05:13
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.

1 participant