Repository navigation
feat(audio-context): render the Android audio graph as a real node graph - #285
Merged
Merged
Conversation
…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
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
|
ℹ️ Note: npm publishing is disabled for pull requests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
The Android engine had no node graph. Each
connectwas 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_, theemitVoicelambda). Because of that: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):GraphNodewith inputs and outputs, each a native bus of up to 32 channels × 128 frames. Java'sconnect/disconnectbecome one native command per edge, so connections are made in any order and are cut exactly.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.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.release) while something still plays into it stays until that stops, and only then is its native state freed. Destinations go with their context.What changed
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.cppkeeps 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.AudioNodedefault methods route every connect/disconnect (includingconnect(AudioParam)andsetChannelConfig) toAudioContext.connectNodesand friends. The per-class flattening code andnodeSourcesbookkeeping are removed. A buffer source that takes a new buffer keeps its connections.connect(param)anddisconnect(param)reach native, andchannelCount,channelCountModeandchannelInterpretationare exposed on Android nodes.connect(param).Specs
audio.graphsuite (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 explicitchannelCount1 gain mixing stereo to mono. The feedback andchannelCountspecs are skipped on iOS (AVAudioEngine has no cycles; iOS nodes have no channel settings).connect(AudioParam)specs.Android
audiogroup 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 2connect(AudioParam)specs. The graph logic was also exercised off-device under ASan/UBSan with offline renders of the same cases.