Skip to content

fix(audio-context): Android scheduling clock, targeted disconnect, close, channel mixing and worklet wakeups - #284

Merged
triniwiz merged 1 commit into
masterfrom
feat/android-audio-fixes
Nov 25, 2026
Merged

triniwiz merged 1 commit into
masterfrom
feat/android-audio-fixes

Conversation

@triniwiz

Copy link
Copy Markdown
Member

What was wrong (Android)

  • Realtime scheduling drift. Each render block took its start time from the stream's device timestamp, which jitters between callbacks. A buffer source moves forward one frame per rendered frame, but gain automation follows that absolute time, so the two drifted apart by tens of frames. That broke 'buffer source start(when) lines up with gain automation'.
  • Output and input indices dropped. nativeAttachPanner, nativeAttachBiquad and nativeAttachPlaybackRate exported their 2-arg overloads under the short JNI name. That name also binds the 4-arg overloads, so output/input were lost. For example, a splitter output connected to a filter filtered the wrong channel.
  • disconnect(target|output) always disconnected everything. It called Java disconnectTo/disconnectOutput, which don't exist, then fell back to a full disconnect.
  • AudioContext.close() only suspended the shared engine. The context's voices kept playing in the next context. This caused 'gain follows setValueAtTime…', 'oscillator sounds only between…' and the two realtime worklet specs to fail.
  • Buffer set after creation in an offline context was never rendered. A buffer source whose buffer was set after creation was created on the realtime engine, so an offline render never played it. Its realtime voice was also invisible to close().
  • voiceDelay_ was never cleared. Disconnecting a source from a DelayNode left it routed through the delay.
  • Stereo buffer in a mono context played only the left channel.
  • Worklet inputs were mixed at the context's channel count. They were then up- or down-mixed to channelCount, so in a mono context a stereo source reached the worklet as its mono mix. Only mono and stereo were handled.
  • The worklet Worker polled its ring every 2 ms. This slowed offline renders with input worklets.
  • BiquadFilterNode.type had no getter or setter.

What changed

  • Realtime blocks run on a frame clock that re-anchors to the monotonic clock after a stall or restart. currentTime reads the same clock.
  • The three JNI functions use long names, as nativeAttachGain already does.
  • JS disconnect calls the existing Java overloads. Every node's targeted disconnect goes through AudioContext.disconnectNode, which sends a new CMD_DETACH_FROM. That command clears only the voice's gain, filter or panner slots, delay route or worklet route that hold the target, at the given output.
  • AudioContextInstance.closeAsync stops the context's tracks and frees its gains, filters, panners and destination. Buffers are kept. The engine suspends only when no realtime context is left open. switchBufferSource creates the new voice in its context's engine and registers it with the context.
  • An empty gain attach also clears voiceDelay_.
  • A shared speakerMix helper implements the Web Audio speaker up/down-mix for mono, stereo, quad and 5.1. Other channel counts mix discretely. Buffer sources, worklet outputs and mono sources all use it.
  • Voices routed into a worklet input render at that input's channelCount, so the ring gets each input already mixed.
  • WorkletWaker registers an eventfd on the Worker thread's looper. The engine writes to it after each requested quantum and when a worklet closes, and the Worker's drain runs on its own thread. Atomics.notify isn't an option because the render thread can't call it. Where the waker isn't available (iOS), the bridge keeps polling. In a 400-quantum offline render with an input worklet, time dropped from about 1000 ms to about 130 ms.
  • BiquadFilterNode.type gets/sets through new AudioBiquadNode.getType/setType.
  • README worklet notes updated.

Specs

New:

  • audio.offline.render
    • a splitter output into a filter filters only that channel (skipped on iOS: iOS doesn't filter a single splitter output)
    • disconnect(node) cuts only that connection
    • a source disconnected from a delay plays undelayed
    • a stereo buffer plays its mono down-mix in a mono context
  • audio.scheduling
    • close stops the context's sources
  • audio.worklet
    • mixes each input to its own channelCount with the speaker rules: 11 cases covering mono, stereo, quad and 5.1, in a mono context (skipped on iOS: iOS mixes an input at the context's channel count first)

Results:

  • Android audio suite (Pixel 9 Pro emulator): 43 passed, 0 failed, 2 skipped. Before: 32 passed, 5 failed. The 2 skips are connect(AudioParam), which Android doesn't support.
  • iOS audio suite (simulator): 43 passed, 0 failed, 2 skipped. These are the 39 existing specs plus the 4 new ones that run on iOS.

…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.
@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: 72a05f84-6890-463f-bb6b-88e19bcca5c9

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

@triniwiz
triniwiz merged commit 16ff853 into master Nov 25, 2026
30 of 31 checks passed
@triniwiz
triniwiz deleted the feat/android-audio-fixes branch November 25, 2026 00:30
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