// Drawing data from the score, kept on the page: tracks of rows [t, v…] in time order, and events. // Views read what they need at the playhead time (interpolated state, a trail's recent rows…). import type { VisEvent } from "./protocol.ts"; interface Batch { t0: number; t1: number; stride: number; data: Float64Array; } export class FrameStore { private tracks = new Map(); events: VisEvent[] = []; /** the latest readouts from the score, by piece time */ private infos: { t: number; info: Record }[] = []; add(track: number, stride: number, data: Float64Array) { if (!data.length) return; let list = this.tracks.get(track); if (!list) this.tracks.set(track, (list = [])); list.push({ t0: data[0], t1: data[data.length - stride], stride, data }); } addEvents(e: VisEvent[]) { for (const x of e) this.events.push(x); } addInfo(t: number, info: Record) { this.infos.push({ t, info }); } /** Readouts as of piece time t. */ info(t: number): Record { let best: Record = {}; for (const x of this.infos) if (x.t <= t) best = x.info; return best; } /** Drop what's older than t (keep one row before it, for interpolation). */ trim(t: number) { for (const list of this.tracks.values()) while (list.length > 1 && list[1].t0 <= t && list[0].t1 < t) list.shift(); let i = 0; while (i < this.events.length && this.events[i].t < t) i++; if (i > 0) this.events.splice(0, i); while (this.infos.length > 1 && this.infos[1].t <= t) this.infos.shift(); } /** Last time covered by a track (−∞ if none). */ last(track: number): number { const list = this.tracks.get(track); return list?.length ? list[list.length - 1].t1 : -Infinity; } /** * The track's values at time t, linearly interpolated between rows (clamped at the ends). * Writes into `out` (length stride − 1) and returns it, or null if the track is empty. */ at(track: number, t: number, out: Float64Array): Float64Array | null { const list = this.tracks.get(track); if (!list?.length) return null; // the batch holding t (or the nearest end) let bi = list.length - 1; while (bi > 0 && list[bi].t0 > t) bi--; const b = list[bi], S = b.stride, n = b.data.length / S; const d = b.data; // the row at or before t, by bisection let lo = 0, hi = n - 1; if (t <= d[0]) hi = 0; else if (t >= d[(n - 1) * S]) lo = hi = n - 1; else { while (hi - lo > 1) { const mid = (lo + hi) >> 1; if (d[mid * S] <= t) lo = mid; else hi = mid; } } let ra = lo * S, rb = hi * S, src = d, srcB = d; // past the batch's last row: interpolate toward the next batch's first row if (lo === hi && lo === n - 1 && bi + 1 < list.length && t > d[ra]) { srcB = list[bi + 1].data; rb = 0; } const ta = src[ra], tb = srcB[rb]; const f = tb > ta ? Math.min(1, Math.max(0, (t - ta) / (tb - ta))) : 0; for (let k = 1; k < S; k++) out[k - 1] = src[ra + k] + f * (srcB[rb + k] - src[ra + k]); return out; } /** Call fn for every row with t0 ≤ t ≤ t1, in order (fn gets the array and the row's offset). */ rows(track: number, t0: number, t1: number, fn: (d: Float64Array, o: number) => void) { const list = this.tracks.get(track); if (!list) return; for (const b of list) { if (b.t1 < t0) continue; if (b.t0 > t1) break; const S = b.stride, d = b.data; for (let o = 0; o < d.length; o += S) { const t = d[o]; if (t < t0) continue; if (t > t1) break; fn(d, o); } } } }