/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / stream / frames.ts
107 lines · 3.6 KBBlameHistoryRaw
1// Drawing data from the score, kept on the page: tracks of rows [t, v…] in time order, and events.
2// Views read what they need at the playhead time (interpolated state, a trail's recent rows…).
4import type { VisEvent } from "./protocol.ts";
6interface Batch {
7 t0: number;
8 t1: number;
9 stride: number;
10 data: Float64Array;
13export class FrameStore {
14 private tracks = new Map<number, Batch[]>();
15 events: VisEvent[] = [];
16 /** the latest readouts from the score, by piece time */
17 private infos: { t: number; info: Record<string, string> }[] = [];
19 add(track: number, stride: number, data: Float64Array) {
20 if (!data.length) return;
21 let list = this.tracks.get(track);
22 if (!list) this.tracks.set(track, (list = []));
23 list.push({ t0: data[0], t1: data[data.length - stride], stride, data });
24 }
26 addEvents(e: VisEvent[]) {
27 for (const x of e) this.events.push(x);
28 }
30 addInfo(t: number, info: Record<string, string>) {
31 this.infos.push({ t, info });
32 }
34 /** Readouts as of piece time t. */
35 info(t: number): Record<string, string> {
36 let best: Record<string, string> = {};
37 for (const x of this.infos) if (x.t <= t) best = x.info;
38 return best;
39 }
41 /** Drop what's older than t (keep one row before it, for interpolation). */
42 trim(t: number) {
43 for (const list of this.tracks.values()) while (list.length > 1 && list[1].t0 <= t && list[0].t1 < t) list.shift();
44 let i = 0;
45 while (i < this.events.length && this.events[i].t < t) i++;
46 if (i > 0) this.events.splice(0, i);
47 while (this.infos.length > 1 && this.infos[1].t <= t) this.infos.shift();
48 }
50 /** Last time covered by a track (−∞ if none). */
51 last(track: number): number {
52 const list = this.tracks.get(track);
53 return list?.length ? list[list.length - 1].t1 : -Infinity;
54 }
56 /**
57 * The track's values at time t, linearly interpolated between rows (clamped at the ends).
58 * Writes into `out` (length stride − 1) and returns it, or null if the track is empty.
59 */
60 at(track: number, t: number, out: Float64Array): Float64Array | null {
61 const list = this.tracks.get(track);
62 if (!list?.length) return null;
63 // the batch holding t (or the nearest end)
64 let bi = list.length - 1;
65 while (bi > 0 && list[bi].t0 > t) bi--;
66 const b = list[bi], S = b.stride, n = b.data.length / S;
67 const d = b.data;
68 // the row at or before t, by bisection
69 let lo = 0, hi = n - 1;
70 if (t <= d[0]) hi = 0;
71 else if (t >= d[(n - 1) * S]) lo = hi = n - 1;
72 else {
73 while (hi - lo > 1) {
74 const mid = (lo + hi) >> 1;
75 if (d[mid * S] <= t) lo = mid;
76 else hi = mid;
77 }
78 }
79 let ra = lo * S, rb = hi * S, src = d, srcB = d;
80 // past the batch's last row: interpolate toward the next batch's first row
81 if (lo === hi && lo === n - 1 && bi + 1 < list.length && t > d[ra]) {
82 srcB = list[bi + 1].data;
83 rb = 0;
84 }
85 const ta = src[ra], tb = srcB[rb];
86 const f = tb > ta ? Math.min(1, Math.max(0, (t - ta) / (tb - ta))) : 0;
87 for (let k = 1; k < S; k++) out[k - 1] = src[ra + k] + f * (srcB[rb + k] - src[ra + k]);
88 return out;
89 }
91 /** Call fn for every row with t0 ≤ t ≤ t1, in order (fn gets the array and the row's offset). */
92 rows(track: number, t0: number, t1: number, fn: (d: Float64Array, o: number) => void) {
93 const list = this.tracks.get(track);
94 if (!list) return;
95 for (const b of list) {
96 if (b.t1 < t0) continue;
97 if (b.t0 > t1) break;
98 const S = b.stride, d = b.data;
99 for (let o = 0; o < d.length; o += S) {
100 const t = d[o];
101 if (t < t0) continue;
102 if (t > t1) break;
103 fn(d, o);
104 }
105 }
106 }
↑↓ move↵openescclose