From f5e7202f3f4f4502270d3e9610e8768e5a90f41b Mon Sep 17 00:00:00 2001 From: Benjamin Hardy Date: Mon, 2 Nov 2026 22:26:19 +0100 Subject: [PATCH] Implemented new audio engine and deck engine --- .gitignore | 3 + CLAUDE.md | 65 +- frontend/rubberband/README.md | 30 + frontend/rubberband/build.sh | 48 ++ frontend/rubberband/glue.c | 48 ++ frontend/src/components/PrepStrip.tsx | 164 ++--- frontend/src/lib/audio/context.ts | 51 ++ frontend/src/lib/audio/deckCore.test.ts | 293 +++++++++ frontend/src/lib/audio/deckCore.ts | 607 ++++++++++++++++++ frontend/src/lib/audio/deckEngine.ts | 211 ++++++ .../src/lib/audio/deckProcessor.worklet.ts | 156 +++++ frontend/src/lib/audio/position.ts | 27 + frontend/src/lib/audio/rubberband.wasm | Bin 0 -> 387873 bytes frontend/src/lib/audio/stretcher.ts | 178 +++++ frontend/src/lib/decodeAlign.ts | 3 +- frontend/src/lib/playbackEngine.ts | 200 ------ frontend/src/lib/rawWave.ts | 30 +- frontend/src/lib/scratchEngine.ts | 223 ------- frontend/src/lib/waveform.ts | 34 +- 19 files changed, 1794 insertions(+), 577 deletions(-) create mode 100644 frontend/rubberband/README.md create mode 100755 frontend/rubberband/build.sh create mode 100644 frontend/rubberband/glue.c create mode 100644 frontend/src/lib/audio/context.ts create mode 100644 frontend/src/lib/audio/deckCore.test.ts create mode 100644 frontend/src/lib/audio/deckCore.ts create mode 100644 frontend/src/lib/audio/deckEngine.ts create mode 100644 frontend/src/lib/audio/deckProcessor.worklet.ts create mode 100644 frontend/src/lib/audio/position.ts create mode 100755 frontend/src/lib/audio/rubberband.wasm create mode 100644 frontend/src/lib/audio/stretcher.ts delete mode 100644 frontend/src/lib/playbackEngine.ts delete mode 100644 frontend/src/lib/scratchEngine.ts diff --git a/.gitignore b/.gitignore index 958c676..5d69fde 100644 --- a/.gitignore +++ b/.gitignore @@ -51,3 +51,6 @@ backend/dist/ # ---- Stem engine (PyInstaller onedir build) ---- engine/build/ engine/dist/ + +# ---- Rubber Band build (frontend/rubberband/build.sh downloads + compiles here) ---- +frontend/rubberband/upstream/ diff --git a/CLAUDE.md b/CLAUDE.md index 57200e6..bfe41ea 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1334,11 +1334,10 @@ Two independent apps that talk over HTTP: - **Stem playback** (decided 2026-09-30): a stem FILE always loads its four stems (`trackStemsFor` / `trackStemUrl` in `api.ts`) and the deck plays their SUM, as Traktor's stem deck does — the mix stream is never decoded, - which also saves a full-length buffer. `PlaybackEngine.load` takes several - buffers: one source + gain each, all started at ONE scheduled context - time (so they stay sample-locked through seeks and loops), mute/solo as - ramped gains, never a restart. `ScratchEngine.loadStems` keeps ONE summed - copy of the audible stems (not four), re-summed in slices on a mute change. + which also saves a full-length buffer. The deck's worklet (`lib/audio/`) + takes several stems and reads them all at ONE position, so they stay + sample-locked through seeks, loops and scratch (which hears what is + audible); mute/solo are ramped gains, never a restart. `analyzeStems` returns the overview's three-band columns (of the sum) plus one amplitude lane per stem, each normalised to its own 99.5th percentile but never below 5% of the loudest (a silent stem stays silent). @@ -1347,8 +1346,10 @@ Two independent apps that talk over HTTP: Q W E R mute stems 1-4, Shift+ solos. **Solo is not a mode**: it SETS the mutes (every other stem muted), so each stem stays freely mutable after it; soloing the one stem still playing unmutes all. Mutes reset per track. Memory is - the cost: ~140 MB per stem for 6 minutes, i.e. the same four buffers as - the file holds streams, and no more. + the cost: ~127 MB of stereo PCM per stem for 6 minutes in the worklet, + plus a half-size mono copy each on the main thread for the raw waveform — + ~6 stereo-track units for a stem track (it was 5: four buffers plus a + summed scratch copy), 1.5 for a plain track (it was 2). - **Prep strip** (DJ deck across the top of the window): `PrepStrip` owns it, in three rows. **Header**: cover, title/artist · album · genre, a Read-only badge, the readout well (ELAPSED / REMAIN to tenths, KEY, and `BpmReadout` — @@ -1384,7 +1385,7 @@ Two independent apps that talk over HTTP: outline (`drawLoop(…, idle)`), so the toggle shows what it would restore. **‹ › (and ←/→) MOVE an engaged loop by its own length**, Traktor-style, and only jump the playhead when no loop is engaged. The move sets the new - loop BEFORE seeking: `PlaybackEngine.seek` honours the loop in force, so + loop BEFORE seeking: the deck's seek honours the loop in force, so seeking first wraps the playhead back into the old loop. - **Memory cues** (decided 2026-10-16, [the discussion](.claude/discussions/discuss-memory-cues-2026-10-16.md)): Mem mode replaces the pads with `MemoryCueStrip` — chips in TIME order, @@ -1406,8 +1407,8 @@ Two independent apps that talk over HTTP: type dropdown or delete button. Rename re-sets the slot with its own start/type/length plus the new name, since `set_cue` replaces a slot. A non-editable cue gets the same menu disabled, not no menu. - Prep libs live in `src/lib/`: `playbackEngine.ts` (Web Audio, seamless loops), - `scratchEngine.ts` (drag-to-scratch), `waveform.ts` (frequency-colored + Prep libs live in `src/lib/`: `audio/` (the deck's worklet engine: + playback, loops, scratch, varispeed, keylock), `waveform.ts` (frequency-colored analysis + paint), `beatgrid.ts` (the marker-list beat math — every beat/bar/snap/jump calculation goes through it), `cues.ts` (draw cues/loop/beatgrid/cue-point). See "Prep engine" below. @@ -2711,19 +2712,37 @@ diff — no COLLECTION splicing needed. The prep strip plays every track through the **Web Audio API** (a plain `