diff --git a/CHANGELOG.md b/CHANGELOG.md index 04bcb7f..3a8261c 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/takeover 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 - //