// 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, as they were then) is past three // minutes of PCM together, which is what kept nt1, lw1 and lj1 loading // forever. (A gapped clip is decoded per kept piece now, and each clip once // across the two sets of edges: `coveringSlot`, below.) /** * @param {{ key: string, state: string, bytes: number, used: number }[]} slots * @param {Set | 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; } /** * The decode a node can play from: one of a clip's slots (same clip, same * window, same bytes -- `prefix`) whose span covers the source seconds it * reads. A decode is never asked for twice to play a span inside one already * held: the manifest's edges and the render's (playSchedule.mjs decodesOf, * EDGE_SLACK), a seek, a re-plan. Ready first, then one under way, then a * failed one (so the node plays silent rather than holding the clock); the * last used among equals. null: none covers it. * * @param {{ key: string, state: string, used: number, span: { from: number, to: number } }[]} slots * @param {string} prefix * @param {{ from: number, to: number }} need */ export function coveringSlot(slots, prefix, need) { const rank = { ready: 0, loading: 1, failed: 2 }; let best = null; for (const s of slots) { if (!s.key.startsWith(prefix)) continue; if (!(s.span.from <= need.from + 1e-6 && s.span.to >= need.to - 1e-6)) continue; const r = rank[/** @type {"ready" | "loading" | "failed"} */ (s.state)] ?? 3; if (!best || r < best.r || (r === best.r && s.used > best.s.used)) best = { s, r }; } return best ? best.s.key : null; }