@msb235/blits 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +163 -0
- package/README.md +32 -8
- package/dist/bare.js +7 -1
- package/dist/book.d.ts +8 -1
- package/dist/book.js +35 -4
- package/dist/chain.d.ts +4 -3
- package/dist/chain.js +10 -6
- package/dist/channels.d.ts +12 -19
- package/dist/channels.js +18 -118
- package/dist/clock.d.ts +12 -5
- package/dist/clock.js +13 -5
- package/dist/closed.d.ts +15 -0
- package/dist/closed.js +144 -0
- package/dist/color.d.ts +61 -0
- package/dist/color.js +229 -0
- package/dist/columns.js +2 -1
- package/dist/crowd.d.ts +4 -4
- package/dist/crowd.js +11 -8
- package/dist/cue.d.ts +2 -0
- package/dist/cue.js +78 -27
- package/dist/due.js +5 -2
- package/dist/fade.d.ts +13 -0
- package/dist/fade.js +55 -4
- package/dist/fill.d.ts +1 -1
- package/dist/fill.js +17 -10
- package/dist/fit.d.ts +144 -0
- package/dist/fit.js +181 -0
- package/dist/fold.d.ts +3 -3
- package/dist/fold.js +25 -18
- package/dist/gather.d.ts +6 -0
- package/dist/gather.js +18 -0
- package/dist/handle.d.ts +27 -2
- package/dist/handle.js +60 -11
- package/dist/held.js +7 -3
- package/dist/history.d.ts +15 -2
- package/dist/history.js +54 -9
- package/dist/hosts.d.ts +2 -2
- package/dist/hosts.js +19 -6
- package/dist/index.d.ts +10 -2
- package/dist/index.js +6 -1
- package/dist/lane.d.ts +4 -4
- package/dist/lane.js +5 -5
- package/dist/lanes.d.ts +32 -9
- package/dist/lanes.js +53 -24
- package/dist/locus.d.ts +3 -3
- package/dist/locus.js +1 -1
- package/dist/marks.d.ts +3 -0
- package/dist/marks.js +81 -13
- package/dist/meet.d.ts +3 -2
- package/dist/meet.js +15 -3
- package/dist/mixer.d.ts +55 -35
- package/dist/mixer.js +173 -83
- package/dist/motion.d.ts +2 -199
- package/dist/motion.js +2 -652
- package/dist/motions.d.ts +266 -0
- package/dist/motions.js +724 -0
- package/dist/move.d.ts +1 -1
- package/dist/move.js +27 -11
- package/dist/owner.d.ts +2 -2
- package/dist/owner.js +5 -5
- package/dist/pace.d.ts +2 -0
- package/dist/pace.js +8 -0
- package/dist/pack.d.ts +31 -0
- package/dist/pack.js +65 -0
- package/dist/paging.d.ts +63 -0
- package/dist/paging.js +110 -0
- package/dist/patch.d.ts +3 -0
- package/dist/patch.js +2 -0
- package/dist/place.js +74 -25
- package/dist/project.d.ts +7 -4
- package/dist/project.js +117 -78
- package/dist/pull.js +4 -3
- package/dist/qualify.js +4 -2
- package/dist/revive.d.ts +58 -0
- package/dist/revive.js +158 -0
- package/dist/sample.d.ts +1 -1
- package/dist/sample.js +6 -1
- package/dist/seek.d.ts +3 -0
- package/dist/seek.js +311 -0
- package/dist/signals.d.ts +3 -2
- package/dist/signals.js +12 -6
- package/dist/spans.d.ts +29 -0
- package/dist/spans.js +165 -0
- package/dist/strays.d.ts +1 -1
- package/dist/strays.js +21 -0
- package/dist/tape.d.ts +21 -0
- package/dist/tape.js +65 -0
- package/dist/ticker.d.ts +73 -0
- package/dist/ticker.js +115 -0
- package/dist/transport.d.ts +121 -0
- package/dist/transport.js +271 -0
- package/dist/types.d.ts +427 -50
- package/dist/unpage.d.ts +8 -0
- package/dist/unpage.js +163 -0
- package/dist/voice.d.ts +78 -12
- package/dist/voice.js +126 -12
- package/dist/weigh.js +2 -2
- package/docs/schema.html +140 -48
- package/package.json +2 -1
package/dist/motions.js
ADDED
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
import { closed } from './closed.js';
|
|
2
|
+
import { absent, Numbers } from './numbers.js';
|
|
3
|
+
import { reading } from './reading.js';
|
|
4
|
+
import { Store } from './store.js';
|
|
5
|
+
import { sloped } from './tweened.js';
|
|
6
|
+
/**
|
|
7
|
+
* A patch's law, `[form, settle, k1, k2, k3]`: which closed form its stretches follow, its `settle`,
|
|
8
|
+
* and up to three constants that form reads. It heads the patch's `runs`, so `solve` finds them in
|
|
9
|
+
* the buffer a sample already reads rather than in an object of their own.
|
|
10
|
+
*/
|
|
11
|
+
const HEAD = 5;
|
|
12
|
+
/** A run that shares nothing with the patch's own. */
|
|
13
|
+
const runOf = (r) => ({
|
|
14
|
+
flags: r.flags,
|
|
15
|
+
stretches: r.stretches.map((seg) => ({ ...seg })),
|
|
16
|
+
pending: r.pending?.map((c) => ({ ...c })),
|
|
17
|
+
});
|
|
18
|
+
/** `segs`, in time order, ahead of `list`: those older than its first, one per time. */
|
|
19
|
+
function ahead(segs, list) {
|
|
20
|
+
const first = list[0].at;
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const seg of [...segs].sort((x, y) => x.at - y.at))
|
|
23
|
+
if (seg.at < first && out[out.length - 1]?.at !== seg.at)
|
|
24
|
+
out.push(seg);
|
|
25
|
+
return out.length === 0 ? list : [...out, ...list];
|
|
26
|
+
}
|
|
27
|
+
/** Where every patch's `sample` leaves a value on its way to the mix; none outlives its call. */
|
|
28
|
+
let shared = { xs: new Float64Array(4), vs: new Float64Array(4) };
|
|
29
|
+
/** What a patch's `xs` and `vs` are before its first subject sets its axes. */
|
|
30
|
+
const unsized = new Float64Array(0);
|
|
31
|
+
/**
|
|
32
|
+
* A subject's flags: its value is a number; it has changes pending; it has older stretches; the
|
|
33
|
+
* stretch it is playing was found landed, which holds until another replaces it.
|
|
34
|
+
*/
|
|
35
|
+
const SCALAR = 1;
|
|
36
|
+
const PENDING = 2;
|
|
37
|
+
const OLDER = 4;
|
|
38
|
+
const LANDED = 8;
|
|
39
|
+
const wrongKind = (channel, holdsNumber) => new Error(`blits: channel ${channel} holds ${holdsNumber ? 'a number' : 'an array'}, and this motion patch gives a subject ${holdsNumber ? 'an array' : 'a number'}`);
|
|
40
|
+
/**
|
|
41
|
+
* A motion patch's subjects: the stretch each is playing, in one flat array by a number the patch
|
|
42
|
+
* gives it, with earlier stretches a read back may reach and changes not yet applied beside them.
|
|
43
|
+
* The one copy, read by `at` and by a mix's lane alike.
|
|
44
|
+
*
|
|
45
|
+
* After the law, each subject's run of `stride` numbers is its release time, its flags, a tween's
|
|
46
|
+
* seconds for the stretch, then `x0`, `v0` and `to` per axis. A live sample reads this one buffer and nothing else the patch owns: at
|
|
47
|
+
* 10k springs of one subject each, a buffer per field, a map lookup per sample and the law in an
|
|
48
|
+
* object of its own took a call from 130 ns to 490.
|
|
49
|
+
*/
|
|
50
|
+
export class Motions {
|
|
51
|
+
shape;
|
|
52
|
+
numbers = new Numbers(this);
|
|
53
|
+
/** Numbers by subject once a second is numbered: till then the one subject is found by `numbers`. */
|
|
54
|
+
slots = new Store();
|
|
55
|
+
/** Axes per subject; -1 until the first subject sets it. */
|
|
56
|
+
n = -1;
|
|
57
|
+
cap = 0;
|
|
58
|
+
stride = 0;
|
|
59
|
+
/**
|
|
60
|
+
* The law, then each subject's run; empty until the first subject, the law read from `shape`.
|
|
61
|
+
* Read outside, at `bareAt`, and written only here.
|
|
62
|
+
*/
|
|
63
|
+
runs = unsized;
|
|
64
|
+
/**
|
|
65
|
+
* Where `sample` leaves a subject's position and velocity, per axis. Shared by every patch, so
|
|
66
|
+
* read it before the next sample.
|
|
67
|
+
*/
|
|
68
|
+
xs = unsized;
|
|
69
|
+
vs = unsized;
|
|
70
|
+
/**
|
|
71
|
+
* By subject number, changes not yet applied and earlier stretches a read back may reach: null
|
|
72
|
+
* until a subject first has one, since many patches are never changed. Each starts with room for
|
|
73
|
+
* one: an empty array written at 0 reserves 17, and most patches have one subject.
|
|
74
|
+
*/
|
|
75
|
+
pending = null;
|
|
76
|
+
older = null;
|
|
77
|
+
/** By subject number, the change that released the stretch it is playing; null until one has. */
|
|
78
|
+
causes = null;
|
|
79
|
+
/**
|
|
80
|
+
* The mix playing the patch and its voice's id, which an untimed change and a `read` with no time
|
|
81
|
+
* ask for the latest frame. Set by the mix that cues the patch, and cleared when its voice retires.
|
|
82
|
+
*/
|
|
83
|
+
owner = null;
|
|
84
|
+
ownerId = -1;
|
|
85
|
+
/** Whether the channel it was cued on holds a number; undefined until it is cued. */
|
|
86
|
+
holdsNumber = undefined;
|
|
87
|
+
channel = '';
|
|
88
|
+
/**
|
|
89
|
+
* Records what the channel the patch is cued on holds, refusing a value of the other kind: at once
|
|
90
|
+
* where every subject's value is one kind, else on the first sample of a subject whose value is not.
|
|
91
|
+
*/
|
|
92
|
+
cuedOn(channel, holdsNumber) {
|
|
93
|
+
const every = this.shape.scalarEvery();
|
|
94
|
+
if (every !== undefined && every !== holdsNumber)
|
|
95
|
+
throw wrongKind(channel, holdsNumber);
|
|
96
|
+
for (let s = 0; s < this.numbers.size; s++)
|
|
97
|
+
if (this.numbers.subject(s) !== absent &&
|
|
98
|
+
((this.runs[this.base(s) + 1] & SCALAR) !== 0) !== holdsNumber)
|
|
99
|
+
throw wrongKind(channel, holdsNumber);
|
|
100
|
+
this.holdsNumber = holdsNumber;
|
|
101
|
+
this.channel = channel;
|
|
102
|
+
}
|
|
103
|
+
frame(subject) {
|
|
104
|
+
return this.owner === null ? Number.NaN : this.owner.frame(this.ownerId, subject);
|
|
105
|
+
}
|
|
106
|
+
constructor(shape) {
|
|
107
|
+
this.shape = shape;
|
|
108
|
+
}
|
|
109
|
+
/** The patch's number for `subject`, made with its first stretch on first ask. */
|
|
110
|
+
slot(subject) {
|
|
111
|
+
const known = this.known(subject);
|
|
112
|
+
if (known !== undefined)
|
|
113
|
+
return known;
|
|
114
|
+
const x = this.shape.from(subject);
|
|
115
|
+
const v = this.shape.velocity(subject, x);
|
|
116
|
+
const to = this.shape.aim(x, v, null, subject);
|
|
117
|
+
const n = this.n < 0 ? x.length : this.n;
|
|
118
|
+
this.check(x, n);
|
|
119
|
+
this.check(v, n);
|
|
120
|
+
this.check(to, n);
|
|
121
|
+
const scalar = this.shape.scalar(subject);
|
|
122
|
+
if (this.holdsNumber !== undefined && scalar !== this.holdsNumber)
|
|
123
|
+
throw wrongKind(this.channel, this.holdsNumber);
|
|
124
|
+
if (this.n < 0)
|
|
125
|
+
this.size(n);
|
|
126
|
+
const s = this.take(subject);
|
|
127
|
+
this.runs[this.base(s) + 1] = scalar ? SCALAR : 0;
|
|
128
|
+
this.write(s, { at: 0, ms: this.shape.ms?.(subject) ?? 0, x0: x, v0: v, to });
|
|
129
|
+
return s;
|
|
130
|
+
}
|
|
131
|
+
/** Sets the axes every subject moves on, at the first. */
|
|
132
|
+
size(n) {
|
|
133
|
+
this.n = n;
|
|
134
|
+
this.stride = 3 + 3 * n;
|
|
135
|
+
if (shared.xs.length < n)
|
|
136
|
+
shared = { xs: new Float64Array(n), vs: new Float64Array(n) };
|
|
137
|
+
this.xs = shared.xs;
|
|
138
|
+
this.vs = shared.vs;
|
|
139
|
+
}
|
|
140
|
+
/** Numbers `subject`, with room for its run. */
|
|
141
|
+
take(subject) {
|
|
142
|
+
const s = this.numbers.take(subject);
|
|
143
|
+
if (s === 1) {
|
|
144
|
+
const first = this.numbers.subject(0);
|
|
145
|
+
if (first !== absent)
|
|
146
|
+
this.slots.set(first, 0);
|
|
147
|
+
}
|
|
148
|
+
if (this.numbers.size > 1)
|
|
149
|
+
this.slots.set(subject, s);
|
|
150
|
+
this.grow(s + 1);
|
|
151
|
+
return s;
|
|
152
|
+
}
|
|
153
|
+
/** The patch's number for `subject`, undefined where it has none. */
|
|
154
|
+
known(subject) {
|
|
155
|
+
if (this.numbers.size !== 1)
|
|
156
|
+
return this.slots.get(subject);
|
|
157
|
+
const only = this.numbers.subject(0);
|
|
158
|
+
return only !== absent && (only === subject || Object.is(only, subject)) ? 0 : undefined;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Subjects released at a mix time a rewind may still reach, with their run as it stood, so a
|
|
162
|
+
* rewind before the release plays them on from there. Empty without history.
|
|
163
|
+
*/
|
|
164
|
+
released = [];
|
|
165
|
+
/**
|
|
166
|
+
* Forgets a subject, so a later ask starts it afresh from `from`. Given the mix time `at`, under
|
|
167
|
+
* history, its run is kept for a rewind before then, until `reach` passes it.
|
|
168
|
+
*/
|
|
169
|
+
release(subject, at, reach = Number.NEGATIVE_INFINITY) {
|
|
170
|
+
const s = this.known(subject);
|
|
171
|
+
if (s === undefined)
|
|
172
|
+
return;
|
|
173
|
+
if (at !== undefined) {
|
|
174
|
+
const kept = [];
|
|
175
|
+
for (const e of this.released)
|
|
176
|
+
if (e.at >= reach)
|
|
177
|
+
kept.push(e);
|
|
178
|
+
else
|
|
179
|
+
this.owner?.page(this.ownerId, 'released', e.subject, [{ at: e.at, data: runOf(e) }]);
|
|
180
|
+
this.released = kept;
|
|
181
|
+
this.released.push({ subject, at, ...this.runAt(s) });
|
|
182
|
+
}
|
|
183
|
+
this.slots.delete(subject);
|
|
184
|
+
this.numbers.release(s);
|
|
185
|
+
}
|
|
186
|
+
/** Puts back each subject released after mix time `t`, as it stood when released. */
|
|
187
|
+
unrelease(t) {
|
|
188
|
+
const back = this.released.filter((e) => e.at > t).sort((a, b) => a.at - b.at);
|
|
189
|
+
if (back.length === 0)
|
|
190
|
+
return;
|
|
191
|
+
this.released = this.released.filter((e) => e.at <= t);
|
|
192
|
+
const done = new Set();
|
|
193
|
+
for (const e of back) {
|
|
194
|
+
if (done.has(e.subject))
|
|
195
|
+
continue;
|
|
196
|
+
done.add(e.subject);
|
|
197
|
+
this.unpack(e.subject, e);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** Subject `s`'s run as it stands. */
|
|
201
|
+
runAt(s) {
|
|
202
|
+
const older = this.older?.[s];
|
|
203
|
+
const pending = this.pending?.[s];
|
|
204
|
+
return {
|
|
205
|
+
flags: this.runs[this.base(s) + 1],
|
|
206
|
+
stretches: older === undefined ? [this.latest(s)] : [...older, this.latest(s)],
|
|
207
|
+
pending: pending === undefined ? undefined : [...pending],
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
/** A subject's run as plain data, for a voice leaving memory; undefined where it has none. */
|
|
211
|
+
pack(subject) {
|
|
212
|
+
const s = this.known(subject);
|
|
213
|
+
return s === undefined ? undefined : runOf(this.runAt(s));
|
|
214
|
+
}
|
|
215
|
+
/** Puts a subject's run back as `pack` or a release left it, replacing any it has now. */
|
|
216
|
+
unpack(subject, run) {
|
|
217
|
+
const now = this.known(subject);
|
|
218
|
+
if (now !== undefined) {
|
|
219
|
+
this.slots.delete(subject);
|
|
220
|
+
this.numbers.release(now);
|
|
221
|
+
}
|
|
222
|
+
if (this.n < 0)
|
|
223
|
+
this.size(run.stretches[0].x0.length);
|
|
224
|
+
const s = this.take(subject);
|
|
225
|
+
this.runs[this.base(s) + 1] = run.flags & ~(OLDER | PENDING);
|
|
226
|
+
const latest = run.stretches[run.stretches.length - 1];
|
|
227
|
+
this.write(s, latest);
|
|
228
|
+
if (run.stretches.length > 1) {
|
|
229
|
+
if (this.older === null)
|
|
230
|
+
this.older = [undefined];
|
|
231
|
+
this.older[s] = run.stretches.slice(0, -1);
|
|
232
|
+
this.flag(s, OLDER, true);
|
|
233
|
+
}
|
|
234
|
+
if (run.pending !== undefined) {
|
|
235
|
+
if (this.pending === null)
|
|
236
|
+
this.pending = [undefined];
|
|
237
|
+
this.pending[s] = run.pending;
|
|
238
|
+
this.flag(s, PENDING, true);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/** A released run a history store gave back, for a rewind to before its release. */
|
|
242
|
+
reclaim(subject, at, run) {
|
|
243
|
+
if (this.released.some((e) => e.at === at && Object.is(e.subject, subject)))
|
|
244
|
+
return;
|
|
245
|
+
this.released.push({ subject, at, ...run });
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Stretches a history store gave back, each by the mix time it was in force by: each life of the
|
|
249
|
+
* subject, ended by a release, takes those from its own, ahead of the earliest it still keeps.
|
|
250
|
+
*/
|
|
251
|
+
restretch(subject, back) {
|
|
252
|
+
const lives = this.released
|
|
253
|
+
.filter((e) => Object.is(e.subject, subject))
|
|
254
|
+
.sort((x, y) => x.at - y.at);
|
|
255
|
+
let from = Number.NEGATIVE_INFINITY;
|
|
256
|
+
const within = (to) => back.filter((r) => r.at > from && r.at <= to).map((r) => r.data);
|
|
257
|
+
for (const e of lives) {
|
|
258
|
+
e.stretches = ahead(within(e.at), e.stretches);
|
|
259
|
+
from = e.at;
|
|
260
|
+
}
|
|
261
|
+
const s = this.known(subject);
|
|
262
|
+
if (s === undefined)
|
|
263
|
+
return;
|
|
264
|
+
const older = this.older?.[s] ?? [];
|
|
265
|
+
const list = ahead(within(Number.POSITIVE_INFINITY), [...older, this.latest(s)]);
|
|
266
|
+
if (list.length === older.length + 1)
|
|
267
|
+
return;
|
|
268
|
+
if (this.older === null)
|
|
269
|
+
this.older = [undefined];
|
|
270
|
+
this.older[s] = list.slice(0, -1);
|
|
271
|
+
this.flag(s, OLDER, true);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Told, with `watchId`, whenever a subject's stretch or what is pending for it changes, so a mix
|
|
275
|
+
* keeping a copy of it knows to read it again. Set by the mix, cleared when its voice retires. An
|
|
276
|
+
* owner and an id, where a callback cost a closure and its context per patch.
|
|
277
|
+
*/
|
|
278
|
+
watcher = null;
|
|
279
|
+
watchId = -1;
|
|
280
|
+
/** The easing a tween's stretches follow; undefined for every other shape. */
|
|
281
|
+
get ease() {
|
|
282
|
+
return this.shape.ease;
|
|
283
|
+
}
|
|
284
|
+
/** The patch's kind and constants. */
|
|
285
|
+
spec() {
|
|
286
|
+
return this.shape.spec();
|
|
287
|
+
}
|
|
288
|
+
/** A copy of the patch's law, `[form, settle, k1, k2, k3]`. */
|
|
289
|
+
law() {
|
|
290
|
+
return this.cap === 0 ? Float64Array.from(this.shape.law) : this.runs.slice(0, HEAD);
|
|
291
|
+
}
|
|
292
|
+
/** Whether `law` holds this patch's law, number for number, as `Object.is` compares them. */
|
|
293
|
+
hasLaw(law) {
|
|
294
|
+
const own = this.cap === 0 ? this.shape.law : this.runs;
|
|
295
|
+
for (let i = 0; i < HEAD; i++)
|
|
296
|
+
if (!Object.is(law[i], own[i]))
|
|
297
|
+
return false;
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Copies subject `s`'s current stretch into `out` from `o` as its release time, its seconds, then
|
|
302
|
+
* `x0`, `v0` and `to` per axis; false, copying nothing, while it has a change pending or an
|
|
303
|
+
* earlier stretch kept, which only `sample` handles.
|
|
304
|
+
*/
|
|
305
|
+
stretchInto(s, out, o) {
|
|
306
|
+
const runs = this.runs;
|
|
307
|
+
const b = this.base(s);
|
|
308
|
+
if ((runs[b + 1] & (PENDING | OLDER)) !== 0)
|
|
309
|
+
return false;
|
|
310
|
+
out[o] = runs[b];
|
|
311
|
+
out[o + 1] = runs[b + 2];
|
|
312
|
+
const n3 = 3 * this.n;
|
|
313
|
+
for (let i = 0; i < n3; i++)
|
|
314
|
+
out[o + 2 + i] = runs[b + 3 + i];
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
/** Whether subject `s`'s value is a number rather than an array. */
|
|
318
|
+
scalar(s) {
|
|
319
|
+
return (this.runs[this.base(s) + 1] & SCALAR) !== 0;
|
|
320
|
+
}
|
|
321
|
+
/** The value `at` hands the mix: the first axis for a number, a fresh array otherwise. */
|
|
322
|
+
value(s, xs) {
|
|
323
|
+
if ((this.runs[this.base(s) + 1] & SCALAR) !== 0)
|
|
324
|
+
return xs[0];
|
|
325
|
+
const n = this.n;
|
|
326
|
+
const out = new Array(n);
|
|
327
|
+
for (let i = 0; i < n; i++)
|
|
328
|
+
out[i] = xs[i];
|
|
329
|
+
return out;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Position and velocity at voice time `t` into `xo` and `vo`, a tween's velocity left at 0. A live
|
|
333
|
+
* read stamps every untimed change with `t` and commits every change due by then, in time order;
|
|
334
|
+
* a projection's read applies the timed ones to a copy and commits nothing.
|
|
335
|
+
*/
|
|
336
|
+
sample(s, t, xo, vo) {
|
|
337
|
+
if (reading.live) {
|
|
338
|
+
const runs = this.runs;
|
|
339
|
+
const b = this.base(s);
|
|
340
|
+
if (runs[b + 1] & PENDING)
|
|
341
|
+
this.commit(s, t);
|
|
342
|
+
if (runs[b + 1] & OLDER)
|
|
343
|
+
this.prune(s);
|
|
344
|
+
const at = runs[b];
|
|
345
|
+
if (t < at && runs[b + 1] & OLDER)
|
|
346
|
+
this.evaluateSegment(this.playing(s, t), t, xo, vo);
|
|
347
|
+
else {
|
|
348
|
+
const x = b + 3;
|
|
349
|
+
const ms = runs[b + 2];
|
|
350
|
+
this.evaluate(at, ms, runs, x, runs, x + this.n, runs, x + 2 * this.n, t, xo, vo);
|
|
351
|
+
}
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
this.peek(s, t, xo, vo);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Where subject `s`'s run starts in `runs` (its release time, then flags, seconds, `x0`, `v0`
|
|
358
|
+
* and `to`) while it has nothing pending and no earlier stretch kept; -1 for any other.
|
|
359
|
+
*/
|
|
360
|
+
bareAt(s) {
|
|
361
|
+
const b = this.base(s);
|
|
362
|
+
return (this.runs[b + 1] & (PENDING | OLDER)) !== 0 ? -1 : b;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* A live `sample` at `t` for a subject with nothing pending and no earlier stretch kept, true
|
|
366
|
+
* then; false, writing nothing, for any other, which `quiet` and `sample` take instead.
|
|
367
|
+
*/
|
|
368
|
+
sampleBare(s, t, xo, vo) {
|
|
369
|
+
const runs = this.runs;
|
|
370
|
+
const b = this.bareAt(s);
|
|
371
|
+
if (b < 0)
|
|
372
|
+
return false;
|
|
373
|
+
const x = b + 3;
|
|
374
|
+
const n = this.n;
|
|
375
|
+
this.evaluate(runs[b], runs[b + 2], runs, x, runs, x + n, runs, x + 2 * n, t, xo, vo);
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Whether every subject has landed on its target at its voice time at the latest frame, as a
|
|
380
|
+
* sample there would find; one with a change waiting has not. Commits nothing.
|
|
381
|
+
*/
|
|
382
|
+
landed() {
|
|
383
|
+
const runs = this.runs;
|
|
384
|
+
const n = this.n;
|
|
385
|
+
for (let s = 0; s < this.numbers.size; s++) {
|
|
386
|
+
const b = this.base(s);
|
|
387
|
+
const flags = runs[b + 1];
|
|
388
|
+
if ((flags & (LANDED | PENDING)) === LANDED)
|
|
389
|
+
continue;
|
|
390
|
+
const subject = this.numbers.subject(s);
|
|
391
|
+
if (subject === absent)
|
|
392
|
+
continue;
|
|
393
|
+
if (flags & PENDING)
|
|
394
|
+
return false;
|
|
395
|
+
const at = runs[b];
|
|
396
|
+
const t = this.frame(subject);
|
|
397
|
+
if (!(t >= at))
|
|
398
|
+
return false;
|
|
399
|
+
const x = b + 3;
|
|
400
|
+
const ms = runs[b + 2];
|
|
401
|
+
if (!this.evaluate(at, ms, runs, x, runs, x + n, runs, x + 2 * n, t, this.xs, this.vs))
|
|
402
|
+
return false;
|
|
403
|
+
runs[b + 1] = flags | LANDED;
|
|
404
|
+
}
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Whether a live `sample` at `t` would leave the subject's state as it is: nothing to stamp or
|
|
409
|
+
* commit by then, and nothing older than `reading.horizon` to let go of.
|
|
410
|
+
*/
|
|
411
|
+
quiet(s, t) {
|
|
412
|
+
const runs = this.runs;
|
|
413
|
+
const b = this.base(s);
|
|
414
|
+
const flags = runs[b + 1];
|
|
415
|
+
if (flags & PENDING) {
|
|
416
|
+
const list = this.pending[s];
|
|
417
|
+
const first = list[0].at;
|
|
418
|
+
if (first === undefined || first <= t || list[list.length - 1].at === undefined)
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
if (flags & OLDER) {
|
|
422
|
+
const list = this.older[s];
|
|
423
|
+
const next = list.length > 1 ? list[1].at : runs[b];
|
|
424
|
+
if (next <= reading.horizon)
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
/** Position and velocity at `at`, default the latest frame; undefined until there is one. */
|
|
430
|
+
read(subject, at) {
|
|
431
|
+
const s = this.known(subject);
|
|
432
|
+
if (s === undefined)
|
|
433
|
+
return undefined;
|
|
434
|
+
const when = at ?? this.frame(subject);
|
|
435
|
+
if (Number.isNaN(when))
|
|
436
|
+
return undefined;
|
|
437
|
+
const x = new Float64Array(this.n);
|
|
438
|
+
const v = new Float64Array(this.n);
|
|
439
|
+
sloped(true);
|
|
440
|
+
try {
|
|
441
|
+
this.peek(s, when, x, v);
|
|
442
|
+
}
|
|
443
|
+
finally {
|
|
444
|
+
sloped(false);
|
|
445
|
+
}
|
|
446
|
+
return { x, v, s };
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Queues a retarget or push, untimed at the latest frame, for `subject`'s next read past its time.
|
|
450
|
+
* The queue keeps the changes with a time in time order, ahead of any still waiting for one.
|
|
451
|
+
*/
|
|
452
|
+
change(subject, c) {
|
|
453
|
+
this.owner?.revive(this.ownerId, subject);
|
|
454
|
+
const s = this.slot(subject);
|
|
455
|
+
this.check(c.to, this.n);
|
|
456
|
+
this.check(c.v, this.n);
|
|
457
|
+
c.made = this.owner === null ? Number.NaN : this.owner.now();
|
|
458
|
+
if (c.at === undefined) {
|
|
459
|
+
const at = this.frame(subject);
|
|
460
|
+
if (!Number.isNaN(at))
|
|
461
|
+
c.at = at;
|
|
462
|
+
}
|
|
463
|
+
const list = this.pending?.[s];
|
|
464
|
+
if (list === undefined) {
|
|
465
|
+
if (this.pending === null)
|
|
466
|
+
this.pending = [undefined];
|
|
467
|
+
this.pending[s] = [c];
|
|
468
|
+
this.flag(s, PENDING, true);
|
|
469
|
+
}
|
|
470
|
+
else if (c.at === undefined)
|
|
471
|
+
list.push(c);
|
|
472
|
+
else
|
|
473
|
+
this.queue(list, c, c.at);
|
|
474
|
+
reading.moved++;
|
|
475
|
+
const made = { at: c.at, to: c.to, v: c.v };
|
|
476
|
+
this.owner?.changed(this.ownerId, this, subject, made);
|
|
477
|
+
}
|
|
478
|
+
/** Files a change with a time after every one due by then, ahead of those still waiting for one. */
|
|
479
|
+
queue(list, c, at) {
|
|
480
|
+
let i = list.length;
|
|
481
|
+
while (i > 0) {
|
|
482
|
+
const before = list[i - 1].at;
|
|
483
|
+
if (before !== undefined && before <= at)
|
|
484
|
+
break;
|
|
485
|
+
i--;
|
|
486
|
+
}
|
|
487
|
+
list.splice(i, 0, c);
|
|
488
|
+
}
|
|
489
|
+
/** Every change due by `t` applied to a copy of the stretch playing then, committing nothing. */
|
|
490
|
+
peek(s, t, xo, vo) {
|
|
491
|
+
let seg = this.playing(s, t);
|
|
492
|
+
const list = this.pending?.[s];
|
|
493
|
+
if (list !== undefined)
|
|
494
|
+
for (const change of list) {
|
|
495
|
+
const a = change.at;
|
|
496
|
+
if (a === undefined || !(a <= t))
|
|
497
|
+
break;
|
|
498
|
+
if (a >= seg.at)
|
|
499
|
+
seg = this.applied(s, seg, change, a);
|
|
500
|
+
}
|
|
501
|
+
this.evaluateSegment(seg, t, xo, vo);
|
|
502
|
+
}
|
|
503
|
+
check(a, n) {
|
|
504
|
+
if (a !== undefined && a.length !== n)
|
|
505
|
+
throw new Error('blits: a motion patch moves every subject on the same number of axes');
|
|
506
|
+
}
|
|
507
|
+
flag(s, bit, on) {
|
|
508
|
+
this.watcher?.stretchChanged(this.watchId, s);
|
|
509
|
+
const i = this.base(s) + 1;
|
|
510
|
+
const flags = this.runs[i];
|
|
511
|
+
this.runs[i] = on ? flags | bit : flags & ~bit;
|
|
512
|
+
}
|
|
513
|
+
forget(s) {
|
|
514
|
+
if (this.pending !== null)
|
|
515
|
+
this.pending[s] = undefined;
|
|
516
|
+
if (this.older !== null)
|
|
517
|
+
this.older[s] = undefined;
|
|
518
|
+
if (this.causes !== null)
|
|
519
|
+
this.causes[s] = undefined;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Takes back every change made after mix time `t`: each subject plays from its earliest stretch
|
|
523
|
+
* kept, with the changes made by then queued to be applied again.
|
|
524
|
+
*/
|
|
525
|
+
rewind(t) {
|
|
526
|
+
this.unrelease(t);
|
|
527
|
+
for (let s = 0; s < this.numbers.size; s++)
|
|
528
|
+
if (this.numbers.subject(s) !== absent)
|
|
529
|
+
this.cut(s, t);
|
|
530
|
+
}
|
|
531
|
+
cut(s, t) {
|
|
532
|
+
const older = this.older?.[s];
|
|
533
|
+
const pending = this.pending?.[s];
|
|
534
|
+
const current = this.latest(s);
|
|
535
|
+
const stretches = older === undefined ? [current] : [...older, current];
|
|
536
|
+
const kept = [];
|
|
537
|
+
let cut = false;
|
|
538
|
+
for (const c of [...stretches.slice(1).map((seg) => seg.change), ...(pending ?? [])])
|
|
539
|
+
if (c === undefined || c.made === undefined || !(c.made > t)) {
|
|
540
|
+
if (c !== undefined)
|
|
541
|
+
kept.push(c);
|
|
542
|
+
}
|
|
543
|
+
else
|
|
544
|
+
cut = true;
|
|
545
|
+
if (!cut)
|
|
546
|
+
return;
|
|
547
|
+
if (older !== undefined) {
|
|
548
|
+
this.older[s] = undefined;
|
|
549
|
+
this.flag(s, OLDER, false);
|
|
550
|
+
}
|
|
551
|
+
this.write(s, stretches[0]);
|
|
552
|
+
// Applied again in the order they take effect; one never stamped waits for the next read.
|
|
553
|
+
const timed = kept
|
|
554
|
+
.filter((c) => c.at !== undefined)
|
|
555
|
+
.sort((a, b) => a.at - b.at);
|
|
556
|
+
const list = [...timed, ...kept.filter((c) => c.at === undefined)];
|
|
557
|
+
if (list.length === 0) {
|
|
558
|
+
if (pending !== undefined) {
|
|
559
|
+
this.pending[s] = undefined;
|
|
560
|
+
this.flag(s, PENDING, false);
|
|
561
|
+
}
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (this.pending === null)
|
|
565
|
+
this.pending = [undefined];
|
|
566
|
+
this.pending[s] = list;
|
|
567
|
+
this.flag(s, PENDING, true);
|
|
568
|
+
}
|
|
569
|
+
/** Where subject `s`'s run starts in `runs`. */
|
|
570
|
+
base(s) {
|
|
571
|
+
return HEAD + s * this.stride;
|
|
572
|
+
}
|
|
573
|
+
grow(size) {
|
|
574
|
+
if (size <= this.cap)
|
|
575
|
+
return;
|
|
576
|
+
const cap = Math.max(size, this.cap * 2);
|
|
577
|
+
const runs = new Float64Array(HEAD + cap * this.stride);
|
|
578
|
+
runs.set(this.cap === 0 ? this.shape.law : this.runs);
|
|
579
|
+
this.runs = runs;
|
|
580
|
+
this.cap = cap;
|
|
581
|
+
}
|
|
582
|
+
write(s, seg) {
|
|
583
|
+
const n = this.n;
|
|
584
|
+
const b = this.base(s);
|
|
585
|
+
this.watcher?.stretchChanged(this.watchId, s);
|
|
586
|
+
if (seg.change !== undefined || this.causes !== null) {
|
|
587
|
+
this.causes ??= [];
|
|
588
|
+
this.causes[s] = seg.change;
|
|
589
|
+
}
|
|
590
|
+
this.runs[b] = seg.at;
|
|
591
|
+
this.runs[b + 1] = this.runs[b + 1] & ~LANDED;
|
|
592
|
+
this.runs[b + 2] = seg.ms;
|
|
593
|
+
for (let i = 0; i < n; i++) {
|
|
594
|
+
this.runs[b + 3 + i] = seg.x0[i];
|
|
595
|
+
this.runs[b + 3 + n + i] = seg.v0[i];
|
|
596
|
+
this.runs[b + 3 + 2 * n + i] = seg.to[i];
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
/** The stretch subject `s` is playing now, as an object. */
|
|
600
|
+
latest(s) {
|
|
601
|
+
const n = this.n;
|
|
602
|
+
const x = this.base(s) + 3;
|
|
603
|
+
return {
|
|
604
|
+
at: this.runs[this.base(s)],
|
|
605
|
+
ms: this.runs[this.base(s) + 2],
|
|
606
|
+
x0: Array.from(this.runs.subarray(x, x + n)),
|
|
607
|
+
v0: Array.from(this.runs.subarray(x + n, x + 2 * n)),
|
|
608
|
+
to: Array.from(this.runs.subarray(x + 2 * n, x + 3 * n)),
|
|
609
|
+
change: this.causes?.[s],
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
/** The stretch playing at voice time `t`: the latest released by then, else the first. */
|
|
613
|
+
playing(s, t) {
|
|
614
|
+
const list = this.older?.[s];
|
|
615
|
+
if (list === undefined || t >= this.runs[this.base(s)])
|
|
616
|
+
return this.latest(s);
|
|
617
|
+
if (list[0].at > t)
|
|
618
|
+
return list[0];
|
|
619
|
+
let lo = 0;
|
|
620
|
+
let hi = list.length - 1;
|
|
621
|
+
while (lo < hi) {
|
|
622
|
+
const mid = (lo + hi + 1) >> 1;
|
|
623
|
+
if (list[mid].at <= t)
|
|
624
|
+
lo = mid;
|
|
625
|
+
else
|
|
626
|
+
hi = mid - 1;
|
|
627
|
+
}
|
|
628
|
+
return list[lo];
|
|
629
|
+
}
|
|
630
|
+
applied(s, seg, change, at) {
|
|
631
|
+
const x = new Float64Array(this.n);
|
|
632
|
+
const v = new Float64Array(this.n);
|
|
633
|
+
this.evaluateSegment(seg, at, x, v);
|
|
634
|
+
const xs = Array.from(x);
|
|
635
|
+
const vs = change.v ?? Array.from(v);
|
|
636
|
+
const subject = this.numbers.subject(s);
|
|
637
|
+
const known = subject === absent ? undefined : subject;
|
|
638
|
+
const to = this.shape.aim(xs, vs, change.to ?? seg.to, known);
|
|
639
|
+
const ms = subject === absent ? seg.ms : (this.shape.ms?.(known) ?? 0);
|
|
640
|
+
return { at, ms, x0: xs, v0: vs, to, change };
|
|
641
|
+
}
|
|
642
|
+
commit(s, t) {
|
|
643
|
+
const list = this.pending[s];
|
|
644
|
+
// The first read of a subject no frame had met: those waiting for a time take this one.
|
|
645
|
+
if (list[list.length - 1].at === undefined) {
|
|
646
|
+
let i = list.length;
|
|
647
|
+
while (i > 0 && list[i - 1].at === undefined)
|
|
648
|
+
i--;
|
|
649
|
+
for (const c of list.splice(i)) {
|
|
650
|
+
c.at = t;
|
|
651
|
+
this.queue(list, c, t);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
while (list.length > 0) {
|
|
655
|
+
const change = list[0];
|
|
656
|
+
const a = change.at;
|
|
657
|
+
if (a > t)
|
|
658
|
+
break;
|
|
659
|
+
list.shift();
|
|
660
|
+
this.insert(s, this.applied(s, this.playing(s, a), change, a));
|
|
661
|
+
}
|
|
662
|
+
if (list.length === 0) {
|
|
663
|
+
this.pending[s] = undefined;
|
|
664
|
+
this.flag(s, PENDING, false);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
insert(s, seg) {
|
|
668
|
+
let list = this.older?.[s];
|
|
669
|
+
if (list === undefined) {
|
|
670
|
+
list = [];
|
|
671
|
+
if (this.older === null)
|
|
672
|
+
this.older = [undefined];
|
|
673
|
+
this.older[s] = list;
|
|
674
|
+
this.flag(s, OLDER, true);
|
|
675
|
+
}
|
|
676
|
+
if (seg.at >= this.runs[this.base(s)]) {
|
|
677
|
+
list.push(this.latest(s));
|
|
678
|
+
this.write(s, seg);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
let i = list.length;
|
|
682
|
+
while (i > 0 && list[i - 1].at > seg.at)
|
|
683
|
+
i--;
|
|
684
|
+
list.splice(i, 0, seg);
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* The mix time a stretch was in force by, which a store keys it under: when the change that
|
|
688
|
+
* ended it was made, which falls in the same life of the subject.
|
|
689
|
+
*/
|
|
690
|
+
made(next) {
|
|
691
|
+
const made = next.change?.made;
|
|
692
|
+
return made === undefined || Number.isNaN(made) ? (this.owner?.now() ?? Number.NaN) : made;
|
|
693
|
+
}
|
|
694
|
+
/** Lets go of stretches older than the one in force at `reading.horizon`. */
|
|
695
|
+
prune(s) {
|
|
696
|
+
const list = this.older?.[s];
|
|
697
|
+
if (list === undefined)
|
|
698
|
+
return;
|
|
699
|
+
let n = 0;
|
|
700
|
+
while (n < list.length &&
|
|
701
|
+
(n + 1 < list.length ? list[n + 1].at : this.runs[this.base(s)]) <=
|
|
702
|
+
reading.horizon)
|
|
703
|
+
n++;
|
|
704
|
+
if (n === 0)
|
|
705
|
+
return;
|
|
706
|
+
const out = list.splice(0, n);
|
|
707
|
+
const subject = this.numbers.subject(s);
|
|
708
|
+
if (this.owner !== null && subject !== absent)
|
|
709
|
+
this.owner.page(this.ownerId, 'stretch', subject, out.map((seg, i) => ({
|
|
710
|
+
at: this.made(out[i + 1] ?? list[0] ?? this.latest(s)),
|
|
711
|
+
data: seg,
|
|
712
|
+
})));
|
|
713
|
+
if (list.length === 0) {
|
|
714
|
+
this.older[s] = undefined;
|
|
715
|
+
this.flag(s, OLDER, false);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
evaluateSegment(seg, t, xo, vo) {
|
|
719
|
+
this.evaluate(seg.at, seg.ms, seg.x0, 0, seg.v0, 0, seg.to, 0, t, xo, vo);
|
|
720
|
+
}
|
|
721
|
+
evaluate(at, ms, x0, x, v0, v, to, g, t, xo, vo) {
|
|
722
|
+
return closed(this.runs, this.shape.ease, this.n, at, ms, x0, x, v0, v, to, g, t, xo, vo);
|
|
723
|
+
}
|
|
724
|
+
}
|