From 0b2bcd938aa197cd741b3c6e5e1e15a46a78488a Mon Sep 17 00:00:00 2001 From: byrongamatos Date: Wed, 8 Jul 2026 16:23:04 +0200 Subject: [PATCH 1/3] refactor(stems): extract the buffer transport + #audio shims to src/transport.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the top of the playback engine out of main.js into src/transport.js: the AudioContext-clock playhead (transportPlayhead/Raw), source start/stop (startSources/workletStartSources/stopSources), transportPlay/Pause/Seek/ setTransportRate/lightStop/handleNaturalEnd/dispatchAudioEvent, the shared onWorkletMessage handler, and installAudioShims + the nativeCore* helpers. main.js 1840 -> 1412 lines. It's a clean upper layer: it calls only within-cluster fns, shared state, and already-extracted modules (audio-ctx, streaming) by the same names, so the bodies moved VERBATIM. No cycle — streaming.js gets transportPlay/onWorkletMessage via the step-10 configureStreaming injection (runtime), not a static import, so transport -> streaming is one-way. Removed now-dead main.js imports (resumeCtx, repositionStream, streamOffsetBuffered). The ios-play-pause-shim.test.mjs + test_manifest.py shim assertions now read src/transport.js (the shims' new home), same retarget as the earlier screen.js -> main.js move. node 45/45, pytest 10/10. Co-Authored-By: Claude Opus 4.8 (1M context) --- CHANGELOG.md | 12 + src/main.js | 446 +----------------- src/transport.js | 442 +++++++++++++++++ ...test_manifest.cpython-314-pytest-9.0.3.pyc | Bin 30245 -> 30254 bytes tests/ios-play-pause-shim.test.mjs | 4 +- tests/test_manifest.py | 2 +- 6 files changed, 466 insertions(+), 440 deletions(-) create mode 100644 src/transport.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 04bcb7f..de401ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,18 @@ All notable changes to the Stems Toggle plugin are documented here. ### Changed +- **ES-module migration, step 11 — extract the buffer transport + `#audio` shims + to `src/transport.js`.** The top of the playback engine moves out of `main.js`: + the playhead clock (`transportPlayhead`/`Raw`), source start/stop + (`startSources`/`workletStartSources`/`stopSources`), `transportPlay`/`Pause`/ + `Seek`/`setTransportRate`/`lightStop`/`handleNaturalEnd`/`dispatchAudioEvent`, + the shared `onWorkletMessage` handler, and `installAudioShims` + the `nativeCore*` + helpers. It's a clean upper layer — imports state + audio-ctx + streaming and is + imported back by `main.js` — so the bodies moved **verbatim** (no seam needed; + streaming already gets `transportPlay`/`onWorkletMessage` via the step-10 + injection, so `transport → streaming` is cycle-free). `main.js` 1840 → 1412 lines. + The `ios-play-pause-shim` + manifest shim tests now read `src/transport.js`. + Move-only, no behaviour change; on-device playback/seek/rate smoke passed. - **ES-module migration, step 10 — extract the streaming path to `src/streaming.js`.** The bounded-memory iOS WAV streaming layer (the reader/WAV helpers, the worklet-feed pump + its seek/reposition, `setupStreaming`, diff --git a/src/main.js b/src/main.js index 7db063e..256c72b 100644 --- a/src/main.js +++ b/src/main.js @@ -1,13 +1,14 @@ import { computeMixGains } from './mix-gains.js'; import { clampVolume, coerceBool, hashString, isGuitarStemId } from './util.js'; -import { - ensureCtx, resumeCtx, ensureWorklet, updateLatencyOffset, -} from './audio-ctx.js'; +import { ensureCtx, ensureWorklet, updateLatencyOffset } from './audio-ctx.js'; import { makeStemGainHandle } from './mix.js'; import { - setupStreaming, repositionStream, resetStreamState, streamingSupported, - streamOffsetBuffered, isWavResponse, configureStreaming, + setupStreaming, resetStreamState, streamingSupported, isWavResponse, configureStreaming, } from './streaming.js'; +import { + onWorkletMessage, transportPlay, lightStop, stopSources, flushPendingPlayResolvers, + installAudioShims, nativeCorePaused, nativeCorePause, nativeCoreTime, +} from './transport.js'; import { karaokeDefault, setKaraokeDefault, loadDefaultMuted, saveDefaultMuted, @@ -46,13 +47,10 @@ import { const KEYBOARD_VOLUME_STEP = 0.02; const KEYBOARD_VOLUME_STEP_LARGE = 0.1; const PRIMARY_BUTTON_MASK = 1; - // Scheduling lead for AudioBufferSourceNode.start(). Every stem's start() - // is given the SAME `when` so all 6 sources begin on the identical sample; - // the small lead guarantees `when` is still in the future for all of them. - const START_LEAD = 0.03; - // Wire the streaming layer's seams back to this orchestration layer (it can't - // import main.js). These are hoisted function declarations, so callable now. + // Wire the streaming layer's seams back to the transport/orchestration layers + // (streaming.js can't import them — transport imports streaming). transportPlay + // + onWorkletMessage come from transport.js; persistedSongGain is local (hoisted). configureStreaming({ startPendingPlay: transportPlay, // resume a deferred play() when buffers arrive songGain: persistedSongGain, // seed master gain from the mixer / persisted vol @@ -139,36 +137,6 @@ import { return 0.8; } - function onWorkletMessage(e) { - const msg = e && e.data; - if (!msg) return; - if (msg.type === 'ready') { - if (msg.latencyOutSamples > 0) S.workletLatencyOutSamples = msg.latencyOutSamples; - updateLatencyOffset(); - } else if (msg.type === 'ended') { - handleNaturalEnd(); - } else if (msg.type === 'pos') { - // Streaming backpressure: the worklet reports its read frontier so - // the pump can top the bounded window up ahead of it. - if (typeof msg.pos === 'number') { - ST.lastWorkletPos = msg.pos; - // Re-baseline the transport clock to the worklet's AUTHORITATIVE - // read frontier. Without this, an under-run stall (worklet holds - // pos, main clock keeps advancing off AudioContext.currentTime) - // would leave the highway permanently ahead by the stall duration - // once audio resumes. Re-baselining every ~20 ms also freezes the - // highway during a stall (each urgent 'pos' resets it to the held - // pos), so it stays in sync. Steady playback is a near-no-op (both - // clocks track the audio clock at the same rate). - if (ST.streaming && transport.playing && S.audioCtx && ST.streamSampleRate > 0) { - transport.baseOffset = msg.pos / ST.streamSampleRate; - transport.baseCtxTime = S.audioCtx.currentTime; - } - } - if (ST.posWaiter) { const r = ST.posWaiter; ST.posWaiter = null; try { r(); } catch (_) {} } - } - } - function updateStemButton(stem, options = {}) { if (!stem.btn) return; const volume = clampVolume(stem.vol); @@ -203,402 +171,6 @@ import { return setStemVolume(stem, volume, options); } - // ── Buffer transport ── - - // Derive the playhead from the AudioContext clock — never from an - //