Replace postMessage with SharedArrayBuffer for lock-free audio transfer#1295
Draft
Replace postMessage with SharedArrayBuffer for lock-free audio transfer#1295
Conversation
Audio decoded on the main thread was sent to the AudioWorklet via postMessage with transferred Float32Array buffers, dropping samples when the worklet was busy. This replaces that path with a SharedRingBuffer backed by SharedArrayBuffer for lock-free, zero-copy audio transfer via Atomics. - SharedRingBuffer supports timestamp-based insertion, out-of-order writes, gap zero-filling, and overflow protection (READ advance) - Reader skips ahead when buffered data exceeds LATENCY target - Buffer starts at 1s capacity, re-allocates when LATENCY grows - Main thread polls shared state (timestamp, stalled) instead of receiving postMessage from worklet https://claude.ai/code/session_01DyHBcF8vEs1wtbJubRVExp
…h SAB + postMessage fallback The previous commit replaced the postMessage audio path outright. This re-introduces the postMessage transport as a fallback for environments without SharedArrayBuffer (no cross-origin isolation), and hides both transports behind a single AudioBuffer interface so the Decoder no longer needs to know which is in use. - buffer.ts: new AudioBuffer interface, supportsSharedArrayBuffer() detector, and createAudioBuffer() factory. Wraps SharedRingBuffer or AudioRingBuffer depending on environment support. - render.ts: messages split into init-shared / init-post plus data/latency/state for the fallback path. - render-worklet.ts: handles both init paths; state messages are only sent in post mode (shared mode polls shared memory directly). - decoder.ts: uses AudioBuffer via createAudioBuffer(); no direct knowledge of either transport.
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.
Summary
Replaces the message-passing audio transfer mechanism with a lock-free shared ring buffer implementation using SharedArrayBuffer, eliminating sample drops and reducing latency overhead in audio worklet communication.
Key Changes
New SharedRingBuffer implementation (
shared-ring-buffer.ts):setLatency()Comprehensive test suite (
shared-ring-buffer.test.ts):Decoder refactoring (
decoder.ts):insert()instead ofpostMessage()AudioWorklet simplification (
render-worklet.ts):process()to directly read from SharedRingBufferMessage protocol cleanup (
render.ts):DataandLatencymessage typesInitmessage now carries SharedRingBufferInit data (samples and control SharedArrayBuffers)ToMainstate messages in favor of atomic pollingNotable Implementation Details
Atomics.load,Atomics.store) for lock-free coordinationi32Max,slot()) to handle wrapping with signed 32-bit integershttps://claude.ai/code/session_01DyHBcF8vEs1wtbJubRVExp