Repository navigation
feat(transport): gapless loop wrap + render-pipeline cleanup - #14
Merged
Merged
Conversation
Eliminates the audible click on every loop wrap and reduces main-thread
churn during playback. Two phases in one PR.
Phase A — Gapless audio loop
Master audio was a plain <audio> element; loop wrap fired
store.seek(loop.start) from the RAF tick, which translated into
audioElement.currentTime = loop.start. HTMLMediaElement seeks are
never gapless — the browser pauses the decoder, repoints, resumes.
That's the click.
New architecture: two <audio> elements with the same src, both routed
through MediaElementAudioSourceNode → individual GainNode →
masterGain → destination. Active side plays; idle side parks at
loop.start. Within 50 ms of the wrap point, the RAF tick arms a
sample-accurate gain crossfade in the AudioContext (8 ms linearRamp).
Crossfade fires on the audio render thread — no main-thread work, no
decoder reset. RAM cost is constant (just two element decoder
buffers), so 1h+ takes work the same as 5-min songs.
OP-1 loop-shift (pendingWrapAt) preserved: idle re-parks on new
loop.start, active plays through to OLD loop.end where the crossfade
fires.
Bonus: frame-budget watchdog in preview-runtime drops one preview
frame after a tick > 14 ms, so a video-seek burst can't cascade into
audio scheduler delay. Audio sacred, video can compensate.
Phase A.5 — Glitch probe + bench
AudioWorklet that monitors the master gain for sample-to-sample
amplitude jumps > 0.3, posts to window.__loopGlitches. Opt-in via
?perf=1 / localStorage.tk1Probe / window.__tk1ProbeGlitches.
Playwright bench script (bench-loop-glitch.mjs) seeds a 30s sine
fixture, sets a 1s loop, plays 12s, expects zero glitches. Vor/Nach-
Vergleich gegen main als harter Beweis.
Phase B — Render pipeline cleanup
- Compositor.tsx: collapse the two selectors so the component only
re-renders on cam-id flip, not on every 60 Hz currentTime tick.
- preview-runtime.ts: dirty-flag pool.setCams() — only reconcile
when clips list or cams URL map reference actually changed (used
to allocate a fresh VideoCam[] every frame).
- TransportBar / SyncTuner / OverlaysPanel: remove currentTime
subscriptions used only inside click/keyboard handlers; read
imperatively via useEditorStore.getState() at action time. Also
stops the keyboard-listener from re-binding 60×/sec via useEffect
deps.
- TransportClock and FxHardwarePanel intentionally still subscribe —
they animate at frame rate.
Tests
- 12 new tests for OffsetScheduler.ts (loopWrapTime, shouldArmCrossfade)
- useAudioMaster.test.tsx rewritten with WebAudio mocks; 14 tests
covering both legacy behavior + new crossfade semantics
- 7 new tests in preview-runtime.test.ts (3 dirty-flag, 4 watchdog)
- 657 / 657 unit tests green; lint clean (pre-existing wasm-pack
errors only).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Was wird gefixt
Beim bar-aligned Loopen war ein winziger Click an jedem Wrap hörbar — auch ohne CPU-Druck. Ursache: der Master-Audio-Pfad lief über ein einzelnes
<audio>-Element, und der Loop-Wrap war ein main-threadaudioElement.currentTime = loop.start-Write.HTMLMediaElement-Seeks sind niemals gapless (Browser pausiert intern den Decoder, repositioniert auf einen Keyframe, resumed). Das war architektonisch, kein Performance-Problem.Dazu kam Render-Pipeline-Churn: Compositor und mehrere Panels haben sich auf
playback.currentTimedirekt subscribet → 60Hz Re-Renders während Playback, viele davon auf Werte die nur in Click-Handlern gebraucht werden.pool.setCams()lief jeden Tick mit frischerVideoCam[]-Allocation.Phase A — Gapless Audio Loop
Zwei
<audio>-Elemente mit gleichersrc, beide viaMediaElementAudioSourceNodedurch eigeneGainNodes in einenmasterGaingeroutet:Aktive Seite spielt; idle-Seite ist auf
loop.startgeparkt. Innerhalb von 50ms vor dem Wrap armed der RAF-Tick einen sample-genauen 8mslinearRampToValueAtTimeCrossfade im AudioContext. Der Crossfade läuft auf dem Audio-Render-Thread — null Main-Thread-Arbeit beim Wrap, kein Decoder-Reset, kein Click. Beide<audio>-Decoder-Buffer ≈ 10–20MB Konstant — funktioniert für 5-Min-Songs UND >1h-Takes gleichermaßen.OP-1-Loop-Shift (
pendingWrapAt) bleibt funktional: idle re-parkt auf neuemloop.start, aktiv spielt durch bis zum altenloop.endwo der Crossfade fired.Bonus: Frame-Budget-Watchdog in
preview-runtime.tsskipt einen Preview-Frame nach einem Tick > 14ms, damit ein Video-Seek-Burst nicht in Audio-Scheduler-Verzögerung kaskadiert. Audio sacred, Video darf kompensieren.Phase A.5 — Glitch-Probe + Bench
public/loop-glitch-probe.worklet.js: AudioWorklet mit Sample-zu-Sample-Derivat-Check (Threshold 0.3 ≈ -10dBFS Sprung), quantum-boundary-aware. Opt-in via?perf=1/localStorage.tk1Probe=1/window.__tk1ProbeGlitches=true.scripts/bench-loop-glitch.mjs: Playwright-Skript. Generiert 30s Sinus-Fixture, seedet Job in IndexedDB+OPFS, setzt 1s-Loop, spielt 12s, dumptwindow.__loopGlitches. Vor/Nach-Vergleich gegenmainals harter Beweis.Phase B — Render-Pipeline-Cleanup
Compositor.tsx: zwei Selektoren auf einen reduziert. Re-rendert jetzt nur beim tatsächlichen Cam-ID-Flip (Zustand'sObject.isshort-circuitet auf den zurückgegebenen Strings).preview-runtime.ts: dirty-flag aufclips-Ref +cams-Ref.pool.setCams()läuft nur wenn der Store tatsächlich Clips/Cams ändert. Spart per-frame Heap-Allocation.currentTime-Subscriptions entfernt, die nur in Click/Keyboard-Handlern gebraucht wurden. Imperativ viauseEditorStore.getState()an der Action-Stelle. Bonus: TransportBar's Keyboard-Listener wird nicht mehr 60×/sec re-bound.Was bewusst NICHT in diesem PR ist
VideoDecodermit Frame-Cache. Eigene Iteration.<video>-Ping-Pong pro Cam — würde nur den Loop-Wrap fixen, nicht andere Sprung-Arten.Test plan
OffsetScheduler.test.ts, 14 inuseAudioMaster.test.tsxrewritten, 7 inpreview-runtime.test.ts)tsc --noEmitclean (pre-existing wasm-pack-Fehler bleiben, sind Build-Artefakt-Issue im Worktree)loop.endbench-loop-glitch.mjsgegenmain(alter Pfad): erwartet ≥5 Glitches in 12s. Gegen Branch: erwartet 0.🤖 Generated with Claude Code