Skip to content

refactor(stems): extract the buffer transport + #audio shims to src/transport.js - #35

Merged
byrongamatos merged 3 commits into
mainfrom
feat/es-module-split-transport
Jul 8, 2026
Merged

byrongamatos merged 3 commits into
mainfrom
feat/es-module-split-transport

Conversation

@byrongamatos

@byrongamatos byrongamatos commented Jul 8, 2026 •

Copy link
Copy Markdown
Collaborator

What

Step 11 — the last big cut. Moves the top of the playback engine out of main.js into src/transport.js:

  • playhead clock: transportPlayhead / transportPlayheadRaw (off the AudioContext clock)
  • source lifecycle: startSources / workletStartSources / stopSources
  • transport control: transportPlay / transportPause / transportSeek / setTransportRate / lightStop / handleNaturalEnd / dispatchAudioEvent
  • the shared worklet handler onWorkletMessage
  • installAudioShims + the nativeCore{Paused,Pause,Time} helpers (the #audio play/pause/currentTime/duration delegation)

main.js 1840 → 1412 lines.

Clean upper layer — no seam needed

Unlike streaming, this cluster 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 the now-dead main.js imports (resumeCtx, repositionStream, streamOffsetBuffered).

Tests

ios-play-pause-shim.test.mjs + test_manifest.py's shim assertions now read src/transport.js (the shims' new home) — the same retarget done for the earlier screen.js → main.js move. Full suite green: node 45/45, pytest 10/10. Codex preflight: 0.

CHANGELOG: step 11 entry added.

On-device smoke pending — this moved the whole transport + takeover layer, so verify: takeover (all stems, live sliders), play/pause, seek/scrub, speed change, natural end.

Still open (follow-up, not this PR)

Copilot's immutable-per-generation track hardening + token-guard unit tests are streaming.js concerns (the pump internals are private to streaming.js) — tracked for a dedicated small PR, kept out of this clean move.

Summary by CodeRabbit

  • New Features

    • Added a dedicated playback “buffer transport” module to centralize playhead timing, transport controls (play/pause/seek/rate), and worklet message coordination.
    • Added/centralized #audio transport shims for consistent currentTime, paused, duration, play, and pause behavior.
  • Bug Fixes

    • Improved robustness of shim installation via guarded updates to avoid failures in stricter runtimes, and ensured transport play/pause/seek state stays aligned.
  • Tests

    • Updated iOS shim and manifest-related tests to validate the transport layer instead of the main entrypoint.

…ransport.js

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) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 8, 2026 14:23
@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1ec08905-2913-46a8-bf41-5f8fae692e00

📥 Commits

Reviewing files that changed from the base of the PR and between f680186 and a89183d.

📒 Files selected for processing (1)
  • CHANGELOG.md
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md

📝 Walkthrough

Walkthrough

This PR extracts buffer transport and #audio shim behavior into src/transport.js, updates src/main.js to import that logic, and retargets tests and changelog text to the new module.

Changes

Buffer transport extraction

Layer / File(s) Summary
Transport playhead and source control
src/transport.js
Implements worklet messaging, playhead calculation, and source start/stop logic for worklet and per-stem buffer modes.
Transport controls
src/transport.js
Implements play, pause, seek, rate, light stop, natural-end handling, and audio event dispatching.
#audio shim installation
src/transport.js
Implements native core wrappers and installAudioShims() to route #audio methods and properties through the transport.
main.js wiring and cleanup
src/main.js
Updates imports to use transport.js, removes inline transport logic, and adjusts surrounding comments.
Test and changelog updates
tests/ios-play-pause-shim.test.mjs, tests/test_manifest.py, CHANGELOG.md
Tests now read source from src/transport.js, and the changelog documents the extraction step.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Main as src/main.js
  participant Transport as src/transport.js
  participant AudioContext
  participant Sources

  Main->>Transport: transportPlay()/transportSeek()/transportPause()
  Transport->>AudioContext: read currentTime / resume
  Transport->>Sources: stopSources()/startSources(offset)
  Sources-->>Transport: playback state
  Transport-->>Main: play/pause/seek events
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main refactor: moving buffer transport and #audio shims into src/transport.js.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/es-module-split-transport

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the Stems playback engine by extracting the buffer transport and #audio shim layer out of src/main.js into a dedicated src/transport.js module, while retargeting existing tests and documenting the step in the changelog.

Changes:

  • Added src/transport.js containing transport playhead math, source lifecycle control, worklet message handling, and #audio play/pause/currentTime shims.
  • Updated src/main.js to import transport/shim functions from src/transport.js and removed the inlined transport implementation.
  • Retargeted shim-related tests (Python + Node) and updated the changelog entry for step 11.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/test_manifest.py Updates shim assertions to read src/transport.js instead of src/main.js.
tests/ios-play-pause-shim.test.mjs Retargets the iOS play/pause shim regression test to src/transport.js.
src/transport.js New module housing buffer transport orchestration and #audio delegation shims.
src/main.js Removes extracted transport code and wires streaming hooks + transport imports.
CHANGELOG.md Adds step 11 release note describing the extraction.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CHANGELOG.md Resolved
Address Copilot on #35 — align the changelog with the PR (smoke pending);
'recommended before release' matches the not-yet-verified phrasing used for
earlier steps.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/transport.js`:
- Around line 285-291: `handleNaturalEnd()` is still processing queued worklet
`ended` messages after an intentional pause/stop, which can incorrectly dispatch
`ended` and move the playhead to `duration`. Update `handleNaturalEnd()` in
`src/transport.js` to ignore stale natural-end events unless playback is still
active (for example by checking `transport.playing`/current active state before
updating `baseOffset`, stopping sources, or calling
`dispatchAudioEvent('ended')`). Keep the guard aligned with the stop/pause paths
used by `transportPause()` and `lightStop()`.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e157dab4-c3c2-4885-a3d5-ba6e8b764c06

📥 Commits

Reviewing files that changed from the base of the PR and between b634bd9 and 0b2bcd9.

⛔ Files ignored due to path filters (1)
  • tests/__pycache__/test_manifest.cpython-314-pytest-9.0.3.pyc is excluded by !**/*.pyc
📒 Files selected for processing (5)
  • CHANGELOG.md
  • src/main.js
  • src/transport.js
  • tests/ios-play-pause-shim.test.mjs
  • tests/test_manifest.py

Comment thread src/transport.js Resolved
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@byrongamatos
byrongamatos merged commit 165c840 into main Jul 8, 2026
2 of 3 checks passed
byrongamatos added a commit that referenced this pull request Jul 8, 2026
…eek guard (#36)

* fix(stems): guard handleNaturalEnd on playback; test + document the seek guard

Step 11.1 — the deferred streaming/transport hardening.

1. handleNaturalEnd() now returns unless transport.playing. In worklet mode a
   queued worklet 'ended' can arrive just after transportPause()/lightStop()
   flipped playing=false (stopSources posts 'stop' but can't unqueue an in-flight
   'ended'), which would dispatch a spurious 'ended' and jump the playhead to
   duration. Pre-existing (CodeRabbit on #35); buffer mode was already safe since
   stopSources nulls src.onended before stop(). No genuine natural-end enters with
   playing===false (it's set false inside the handler).

2. Export appendRound + tests/streaming.test.mjs cases pinning the step-9.1
   seek-token guard: posts only when the captured token matches ST.streamSeekToken,
   drops the block (no post, frontier unchanged) otherwise.

3. Document the synchronous t.done=true invariant in cancelStreamReaders that
   keeps reader-reopen seek-safe. This is why Copilot's immutable-per-generation
   track refactor isn't needed: the steal is unreachable given that invariant
   (readers reopen only after a network fetch, a macrotask after the cancelled
   read resolves with t.done already true) plus appendRound's per-track guard.

node 47/47, pytest 10/10. Codex 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(stems): decouple appendRound guard test from STREAM_CHUNK_FRAMES

Address Copilot on #36 — assert a block was posted and the frontier advanced by
exactly the posted frame count, instead of hard-coding 8192.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

2 participants