Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit a4e30bb49d3464525aa933922da92812f8b680af
parent c7cd5bb7b0744d22b5b298e93aedde70b179055f
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Sun,  1 Nov 2026 13:05:43 -0500

Merge branch 'main' into lane/play-walk-app

Diffstat:
Mumtool/components/projects/review/playEngine/audioBank.ts | 24+++++++++++++-----------
Aumtool/lib/report/bankTrim.mjs | 29+++++++++++++++++++++++++++++
Aumtool/lib/report/bankTrim.test.mjs | 39+++++++++++++++++++++++++++++++++++++++
3 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/umtool/components/projects/review/playEngine/audioBank.ts b/umtool/components/projects/review/playEngine/audioBank.ts @@ -1,3 +1,4 @@ +import { keysToTrim } from "@/lib/report/bankTrim.mjs"; import { timeStretch, wantsStretch } from "@/lib/report/timeStretch.mjs"; import type { MediaRef } from "./engine"; @@ -9,7 +10,9 @@ import type { MediaRef } from "./engine"; // either side, playSchedule.mjs decodeSpanOf), never a window: a window can be // a two-minute excerpt and a cut has fifty clips, so the bank holds the clips // around the playhead and lets the rest go, oldest use first, past a budget -// of decoded bytes (a 20-minute cut never holds 20 minutes of PCM). +// of decoded bytes (a 20-minute cut never holds 20 minutes of PCM). The clips +// the engine last asked for (touch) are never let go, even over the budget: +// one let go is asked for again at once, and the clock holds on it (bankTrim.mjs). // // At a speed but 1x a clip is time-stretched at its own pitch (the bench's // timeStretch.mjs, WSOLA) and the stretched copy played at 1x -- the bench's @@ -37,6 +40,8 @@ export class AudioBank { private slots = new Map<string, Slot>(); private stretched: { key: string; buf: AudioBuffer }[] = []; private clock = 0; + /** The keys the engine asked for last (touch): the clips around the playhead, on the edges in force. */ + private wanted: Set<string> | null = null; constructor( private urlOf: (id: string, media: MediaRef, from: number, to: number) => string, @@ -98,12 +103,14 @@ export class AudioBank { return this.slots.get(AudioBank.keyOf(id, media, span))?.why ?? null; } - /** Mark these as in use now, so the trim keeps them. */ + /** These are the clips wanted now: marked in use, and kept by every trim until the next touch. */ touch(keys: Iterable<string>) { - for (const k of keys) { + this.wanted = new Set(keys); + for (const k of this.wanted) { const s = this.slots.get(k); if (s) s.used = ++this.clock; } + this.trim(); } /** The sound at `rate`: itself at 1x, else a stretched copy (made once, kept for the clips near the playhead). */ @@ -137,19 +144,14 @@ export class AudioBank { return this.slots.size; } - /** Over the budget: the least recently used decodes go (a failure is kept, it is small and says why). */ + /** Over the budget: the least recently used decodes not wanted now go (a failure is kept, it is small and says why). */ private trim() { - const ready = [...this.slots.values()].filter((s) => s.state === "ready").sort((a, b) => a.used - b.used); - let total = this.bytes; - for (const s of ready) { - if (total <= this.budget) break; - this.slots.delete(s.key); - total -= s.bytes; - } + for (const k of keysToTrim([...this.slots.values()], this.wanted, this.budget)) this.slots.delete(k); } clear() { this.slots.clear(); + this.wanted = null; this.stretched = []; } } diff --git a/umtool/lib/report/bankTrim.mjs b/umtool/lib/report/bankTrim.mjs @@ -0,0 +1,29 @@ +// Which decodes the Play view's sound bank lets go (playEngine/audioBank.ts): +// past its budget of decoded bytes, the ones nobody wants any more go first -- +// a clip that left the playhead's window, a span decoded on edges since +// replaced (the manifest's, before the render's came back) -- least recently +// used first; the clips around the playhead are never let go, even over the +// budget. Letting a wanted decode go is a thrash, not a saving: the engine +// asks for it again on its next tick, the clock holds on it ("loading…"), and +// the next decode to land lets it go again. A neighbourhood of long gapped +// clips (each decoded whole, its gaps too) is past three minutes of PCM +// together, which is what kept nt1, lw1 and lj1 loading forever. + +/** + * @param {{ key: string, state: string, bytes: number, used: number }[]} slots + * @param {Set<string> | null} wanted the keys the engine asked for last (null: none said yet -- all may go) + * @param {number} budget + * @returns {string[]} the keys to drop, in order + */ +export function keysToTrim(slots, wanted, budget) { + let total = slots.reduce((n, s) => n + s.bytes, 0); + const out = []; + const ready = slots.filter((s) => s.state === "ready" && !(wanted && wanted.has(s.key))); + ready.sort((a, b) => a.used - b.used); + for (const s of ready) { + if (total <= budget) break; + out.push(s.key); + total -= s.bytes; + } + return out; +} diff --git a/umtool/lib/report/bankTrim.test.mjs b/umtool/lib/report/bankTrim.test.mjs @@ -0,0 +1,39 @@ +// The Play view's sound bank trim (bankTrim.mjs): past the budget the decodes +// nobody wants go first, oldest use first; a wanted one never goes. +// +// Run with: node --test lib/report/bankTrim.test.mjs +import assert from "node:assert/strict"; +import test from "node:test"; + +import { keysToTrim } from "./bankTrim.mjs"; + +const MB = 1024 * 1024; +const slot = (key, mb, used, state = "ready") => ({ key, state, bytes: mb * MB, used }); + +test("under the budget nothing goes", () => { + assert.deepEqual(keysToTrim([slot("a", 10, 1), slot("b", 10, 2)], null, 72 * MB), []); +}); + +test("with nothing said wanted, the least recently used go until under", () => { + assert.deepEqual(keysToTrim([slot("a", 40, 3), slot("b", 40, 1), slot("c", 40, 2)], null, 72 * MB), ["b", "c"]); +}); + +test("a wanted decode never goes, even over the budget (the thrash at nt1/lw1/lj1)", () => { + // nt1 81 s, lw1 88 s, og1 44 s: ~83 MB of PCM wanted together, past 72 MB. + const slots = [slot("og1", 17, 1), slot("nt1", 31, 2), slot("lw1", 34, 3)]; + assert.deepEqual(keysToTrim(slots, new Set(["og1", "nt1", "lw1"]), 72 * MB), []); +}); + +test("stale-edge and out-of-window decodes go first, wanted ones stay", () => { + const slots = [ + slot("lw1|manifest-edges", 34, 9), // used recently, but its edges were replaced + slot("tj2", 4, 1), // left the window + slot("nt1", 31, 2), + slot("lw1|render-edges", 34, 3), + ]; + assert.deepEqual(keysToTrim(slots, new Set(["nt1", "lw1|render-edges"]), 72 * MB), ["tj2", "lw1|manifest-edges"]); +}); + +test("a decode still loading is not trimmed (it has no bytes to give back)", () => { + assert.deepEqual(keysToTrim([slot("a", 0, 1, "loading"), slot("b", 80, 2)], new Set(["b"]), 72 * MB), []); +});