// What the Play view decodes of each clip's sound (playSchedule.mjs decodesOf, // bankTrim.mjs coveringSlot): a gapped clip per kept piece, never its gaps; // pieces under a second apart as one; and one decode per clip across the // manifest's edges and the render's -- the render's play from the decode made // while they were measured, found by what it covers. // // Run with: node --test lib/report/playDecodes.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { coveringSlot } from "./bankTrim.mjs"; import { playPlan } from "./playCut.mjs"; import { EDGE_SLACK, MERGE_GAP, audioSpans, decodesOf, scheduleFrom } from "./playSchedule.mjs"; const WIN = { from: 0, to: 600 }; /** The engine's prefetch, as arithmetic: each span's decode found by what it covers, else asked for (a new slot). */ function resolveAll(slots, id, decodes) { const asked = []; const keys = decodes.map((d) => { const have = coveringSlot(slots, `${id}|`, d.need); if (have) return have; const key = `${id}|${d.ask.from}|${d.ask.to}`; if (!slots.some((s) => s.key === key)) { slots.push({ key, state: "loading", used: slots.length, span: d.ask }); asked.push(d.ask); } return key; }); return { keys, asked }; } const secondsOf = (asks) => Math.round(asks.reduce((n, a) => n + (a.to - a.from), 0) * 1000) / 1000; const spansOf = (plan, id) => audioSpans(plan).filter((s) => s.id === id); // lw1's shape: an 88 s span of which about 25 s plays (three gaps). const lw1 = { id: "lw1", type: "clip", video: "v", start: 100, end: 188, gaps: [{ from: 106, to: 130 }, { from: 138, to: 160 }, { from: 166, to: 183 }] }; const lw1Plan = (cuts = null) => playPlan([{ id: "lw1", start: 0, duration: 25 }], [lw1], { leadIn: 0, cuts }); test("a gapped clip is decoded per kept piece: the 25 s it plays and the pads, never its 63 s of gaps", () => { const spans = spansOf(lw1Plan(), "lw1"); assert.deepEqual(spans.map((s) => s.piece), [0, 1, 2, 3], "one span per piece, each carrying its piece"); const ds = decodesOf(spans, { within: WIN }); const { asked } = resolveAll([], "lw1", ds); assert.equal(asked.length, 4, "four pieces, four decodes"); assert.deepEqual(asked, [{ from: 99.75, to: 106.25 }, { from: 129.75, to: 138.25 }, { from: 159.75, to: 166.25 }, { from: 182.75, to: 188.25 }]); assert.equal(secondsOf(asked), 25 + 4 * 0.5, "the kept seconds and a quarter second either side of each"); // Each node reads inside its own decode. for (const d of ds) assert.ok(d.ask.from <= d.need.from && d.ask.to >= d.need.to); }); test("pieces under MERGE_GAP apart share one decode; further apart they do not", () => { const spans = [{ src: 10, srcTo: 14 }, { src: 14.6, srcTo: 18 }, { src: 25, srcTo: 30 }]; const ds = decodesOf(spans, { within: WIN }); assert.ok(MERGE_GAP > 0.6 && MERGE_GAP < 7); assert.deepEqual(ds.map((d) => d.ask), [{ from: 9.75, to: 18.25 }, { from: 9.75, to: 18.25 }, { from: 24.75, to: 30.25 }]); assert.equal(resolveAll([], "x", ds).asked.length, 2, "two requests for three pieces"); }); test("a decode stays inside the cached window (the route clamps to it), and a node's seconds with it", () => { const ds = decodesOf([{ src: 0.1, srcTo: 5 }], { within: { from: 0.05, to: 4.9 }, slack: EDGE_SLACK }); assert.deepEqual(ds[0].ask, { from: 0.05, to: 4.9 }); assert.deepEqual(ds[0].need, { from: 0.1, to: 4.9 }); assert.equal(coveringSlot([{ key: "x|a", state: "ready", used: 1, span: ds[0].ask }], "x|", ds[0].need), "x|a", "covered: never asked again"); }); // The render snaps each clip's edges to a silence: up to a second or so either way (cutPoints.mjs). const plain = { id: "nt1", type: "clip", video: "v", start: 200, end: 281 }; const plainPlan = (cuts = null) => playPlan([{ id: "nt1", start: 0, duration: 81 }], [plain], { leadIn: 0, cuts }); test("measuring, then the render's edges: ONE decode of a plain clip -- the render's play from the wide one", () => { const slots = []; // While the render's edges are measured: the manifest's, EDGE_SLACK wider at both ends. const first = resolveAll(slots, "nt1", decodesOf(spansOf(plainPlan(), "nt1"), { slack: EDGE_SLACK, within: WIN })); assert.equal(first.asked.length, 1); assert.deepEqual(first.asked[0], { from: 200 - 0.25 - EDGE_SLACK, to: 281 + 0.25 + EDGE_SLACK }); slots[0].state = "ready"; // The render's land: the start 1.1 s earlier, the end 1.4 s later. const render = plainPlan(new Map([["nt1", { cutStart: 198.9, cutEnd: 282.4 }]])); const second = resolveAll(slots, "nt1", decodesOf(spansOf(render, "nt1"), { within: WIN })); assert.equal(second.asked.length, 0, "nothing asked for again"); assert.deepEqual(second.keys, [slots[0].key], "the render's span plays from the manifest's decode"); // A node from any second of it reads inside that decode (offset src − its first second, as the engine starts it). for (const p of scheduleFrom(audioSpans(render), 30)) { assert.ok(p.src >= slots[0].span.from && p.srcTo <= slots[0].span.to); } }); test("without the slack the render's wider edges would be a second decode (why it is there)", () => { const slots = []; resolveAll(slots, "nt1", decodesOf(spansOf(plainPlan(), "nt1"), { within: WIN })); const render = plainPlan(new Map([["nt1", { cutStart: 198.9, cutEnd: 282.4 }]])); assert.equal(resolveAll(slots, "nt1", decodesOf(spansOf(render, "nt1"), { within: WIN })).asked.length, 1); }); test("a gapped clip across the two sets of edges: its pieces decoded once, the outer ones wide", () => { const slots = []; const first = resolveAll(slots, "lw1", decodesOf(spansOf(lw1Plan(), "lw1"), { slack: EDGE_SLACK, within: WIN })); assert.equal(first.asked.length, 4); assert.equal(first.asked[0].from, 100 - 0.25 - EDGE_SLACK, "the first piece wide at the clip's start"); assert.equal(first.asked[3].to, 188 + 0.25 + EDGE_SLACK, "the last piece wide at its end"); assert.equal(first.asked[1].from, 129.75, "an inner piece only padded: a gap's edges are exact"); // The render's: its edges snapped, its gaps on the frame grid (within a frame of the manifest's). const render = lw1Plan(new Map([["lw1", { cutStart: 99.2, cutEnd: 189.1, gaps: [{ from: 106.0, to: 130.033 }, { from: 137.967, to: 160.0 }, { from: 166.033, to: 183.0 }], }]])); const second = resolveAll(slots, "lw1", decodesOf(spansOf(render, "lw1"), { within: WIN })); assert.equal(second.asked.length, 0, "every render piece inside a decode already made"); assert.deepEqual(second.keys, first.keys); assert.ok(secondsOf(first.asked) < 25 + 4 * 0.5 + 2 * EDGE_SLACK + 1e-9, "the kept seconds, the pads and the slack: not the 88 s span"); }); test("a render that cuts differently (a montage's phrase: no gaps) asks only for what nothing covers", () => { const slots = []; resolveAll(slots, "lw1", decodesOf(spansOf(lw1Plan(), "lw1"), { slack: EDGE_SLACK, within: WIN })); const render = lw1Plan(new Map([["lw1", { cutStart: 101, cutEnd: 105.5 }]])); assert.equal(resolveAll(slots, "lw1", decodesOf(spansOf(render, "lw1"), { within: WIN })).asked.length, 0, "inside the first piece's decode"); const wide = lw1Plan(new Map([["lw1", { cutStart: 101, cutEnd: 140 }]])); assert.equal(resolveAll(slots, "lw1", decodesOf(spansOf(wide, "lw1"), { within: WIN })).asked.length, 1, "across a gap no piece covers: asked once"); });