animader-cli 0.3.0 → 0.5.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.
@@ -29,7 +29,7 @@
29
29
  * resume: () => Promise<void>,
30
30
  * suspend: () => Promise<void>,
31
31
  * now: () => number,
32
- * play: (decoded: AudioBuffer, origin: number, stopAt: number) => AudioBufferSourceNode,
32
+ * play: (decoded: AudioBuffer, origin: number, stopAt: number, offset?: number) => AudioBufferSourceNode,
33
33
  * stop: (source: AudioBufferSourceNode) => void,
34
34
  * analyser: AnalyserNode,
35
35
  * }}
@@ -71,13 +71,15 @@ export function createWebAudioPort({ context }) {
71
71
 
72
72
  /**
73
73
  * One fresh single-use source per start, scheduled on the context's
74
- * own timeline: begin at `origin`, STOP at `stopAt` (origin+seconds).
74
+ * own timeline: begin at `origin` from `offset` seconds into the
75
+ * buffer, STOP at `stopAt` (the host end on the context clock). A
76
+ * seek passes its position as the offset; a fresh start passes 0.
75
77
  */
76
- play(decoded, origin, stopAt) {
78
+ play(decoded, origin, stopAt, offset = 0) {
77
79
  const source = context.createBufferSource();
78
80
  source.buffer = decoded;
79
81
  source.connect(analyser);
80
- source.start(origin);
82
+ source.start(origin, offset);
81
83
  source.stop(stopAt);
82
84
  return source;
83
85
  },
package/hello/run.js CHANGED
@@ -137,21 +137,19 @@ export function createPlayer({ pacer, routeWrites, toUniforms, setUniform, setPa
137
137
  }
138
138
 
139
139
  /**
140
- * One tick: the score's writes at the tick's own time, then one frame of
141
- * the program at that time. pacer's route pair splits the frame's entries
142
- * across the buses in wire order (`routeWrites`), and folding one entry at
143
- * a time through `toUniforms` gives each write's value in the shape its bus
144
- * sink takes: a number for one lane, a plain array otherwise. The writes
145
- * reach the sinks in that order, so when two write the same name the later
146
- * one wins, the frame contract's last-word rule. Every write rides
147
- * `redraw: false`, and the tick's one draw carries them. Registry 0 lands
140
+ * Apply one tick's score writes to the sinks, without drawing: the frame
141
+ * contract's eval, route and fold at `redraw: false`, with the later
142
+ * write winning. pacer's route pair splits the frame's entries across the
143
+ * buses in wire order (`routeWrites`), and folding one entry at a time
144
+ * through `toUniforms` gives each write's value in the shape its bus sink
145
+ * takes: a number for one lane, a plain array otherwise. Registry 0 lands
148
146
  * by the target's own reflected name; registry 1 goes to `setParam` when
149
147
  * the page has an audio sink. A declared-only target with no host name
150
148
  * cannot key a by-name fold, so it is not written, exactly as the sinks
151
- * could not have taken it.
149
+ * could not have taken it. Leaves `state.writes` holding this tick's
150
+ * writes for the frame a caller paints afterwards.
152
151
  */
153
- function step() {
154
- const t = state.tick / TICKS_PER_SECOND;
152
+ function applyTick() {
155
153
  const entries = pacer.eval(state.tick * MICROS_PER_TICK);
156
154
  state.writes = [];
157
155
  if (entries) {
@@ -172,19 +170,81 @@ export function createPlayer({ pacer, routeWrites, toUniforms, setUniform, setPa
172
170
  }
173
171
  }
174
172
  }
175
- draw({ time: t });
176
- state.tick += 1;
177
- state.draws += 1;
178
- // pngine drains dx, dy and the wheel sums after each frame
179
- // (vendor/pngine.mjs, the draw path).
173
+ }
174
+
175
+ /** pngine drains dx, dy and the wheel sums after each frame. */
176
+ function drainPointerDeltas() {
180
177
  pointer[4] = 0;
181
178
  pointer[5] = 0;
182
179
  pointer[9] = 0;
183
180
  pointer[10] = 0;
181
+ }
182
+
183
+ /**
184
+ * One tick: the score's writes at the tick's own time, then one frame of
185
+ * the program at that time. Every write rides `redraw: false`, and the
186
+ * tick's one draw carries them. The tick advances after its frame, so the
187
+ * last shown tick is always `tick - 1`.
188
+ */
189
+ function step() {
190
+ const t = state.tick / TICKS_PER_SECOND;
191
+ applyTick();
192
+ draw({ time: t });
193
+ state.tick += 1;
194
+ state.draws += 1;
195
+ drainPointerDeltas();
184
196
  if (onFrame) onFrame(frame());
185
197
  return state.tick;
186
198
  }
187
199
 
200
+ /**
201
+ * Step the score to `tick` without drawing. Every tick's writes and
202
+ * uniforms land at `redraw: false`; the last tick's writes stay in
203
+ * `state.writes` for the frame the caller paints afterwards. No draw, no
204
+ * `draws` count and no `onFrame` announcement: a seek is not a played
205
+ * frame. The clock never moves backwards; a target behind the current
206
+ * tick is a no-op.
207
+ */
208
+ function advanceTo(tick) {
209
+ while (state.tick < tick) {
210
+ applyTick();
211
+ state.tick += 1;
212
+ }
213
+ return state.tick;
214
+ }
215
+
216
+ /**
217
+ * Draw the frame the last advance left: `state.tick - 1` at its own time,
218
+ * without evaluating the score again. The zero tick is the one boundary
219
+ * with no preceding frame, so it paints the zero frame rather than a
220
+ * negative time. A real draw drains the pointer deltas and announces its
221
+ * frame, exactly like `step`'s draw.
222
+ */
223
+ function drawFrame() {
224
+ const shown = state.tick === 0 ? 0 : state.tick - 1;
225
+ draw({ time: shown / TICKS_PER_SECOND });
226
+ state.draws += 1;
227
+ drainPointerDeltas();
228
+ if (onFrame) onFrame(frame());
229
+ }
230
+
231
+ /**
232
+ * Reset and replay silently to `tick`, then paint exactly that frame.
233
+ * The zero tick has no preceding frame: it applies tick 0's own writes
234
+ * (without advancing) so the zero frame is score-exact rather than the
235
+ * stale uniforms the previous frame left on the sinks.
236
+ */
237
+ function seekTo(tick) {
238
+ reset();
239
+ if (tick === 0) {
240
+ applyTick();
241
+ } else {
242
+ advanceTo(tick);
243
+ }
244
+ drawFrame();
245
+ return state.tick;
246
+ }
247
+
188
248
  /** The writes the last tick applied and the lanes it left, as plain values. */
189
249
  function frame() {
190
250
  return {
@@ -247,6 +307,12 @@ export function createPlayer({ pacer, routeWrites, toUniforms, setUniform, setPa
247
307
  step,
248
308
  stepTo,
249
309
  stepAt,
310
+ /** Advance silently to `tick` (no draw, no frame announcement). */
311
+ advanceTo,
312
+ /** Paint the last advanced frame (`tick - 1`), never re-evaluating. */
313
+ draw: drawFrame,
314
+ /** Reset, replay silently to `tick`, then paint that frame. */
315
+ seekTo,
250
316
  tick(n) {
251
317
  for (let i = 0; i < n; i++) step();
252
318
  return state.tick;
package/lib/routes.mjs CHANGED
@@ -25,7 +25,7 @@ const MIME = Object.freeze({
25
25
  '.html': 'text/html; charset=utf-8',
26
26
  });
27
27
 
28
- const PLAYER_FILES = new Set(['index.html', 'page.mjs', 'boot.mjs', 'controller.mjs', 'player.css', 'devchannel.mjs']);
28
+ const PLAYER_FILES = new Set(['index.html', 'page.mjs', 'boot.mjs', 'controller.mjs', 'transport.mjs', 'player.css', 'devchannel.mjs']);
29
29
  const APP_LIB_FILES = new Set(['score.mjs', 'show.mjs', 'soundtrack.mjs']);
30
30
  const APP_ADAPTER_FILES = new Set(['webaudio.mjs']);
31
31
  const HELLO_PLAYER_FILES = new Set(['run.js', 'join.js']);
package/lib/show.mjs CHANGED
@@ -72,9 +72,10 @@ export const SHOW_STATES = Object.freeze([
72
72
  * @param {object} options.audio
73
73
  * the audio port: `resume(): Promise`, `suspend(): Promise` (the
74
74
  * clock freezes once it settles), `now(): number` (a
75
- * context-currentTime-style reader), `play(decoded, origin, stopAt):
76
- * handle` (a fresh single-use source started at `origin` with its
77
- * STOP scheduled at `stopAt`), `stop(handle)`.
75
+ * context-currentTime-style reader), `play(decoded, origin, stopAt,
76
+ * offset): handle` (a fresh single-use source started at `origin` from
77
+ * `offset` seconds into the buffer, with its STOP scheduled at `stopAt`),
78
+ * `stop(handle)`.
78
79
  * @param {(fn: () => void) => () => void} options.scheduleWake
79
80
  * schedules one pump (requestAnimationFrame-shaped) and answers a
80
81
  * cancel function.
@@ -170,6 +171,61 @@ export function createShow({
170
171
  };
171
172
  }
172
173
 
174
+ /**
175
+ * Start one fresh source at `elapsed` seconds on the context clock `now`,
176
+ * unless that position is at/after the host end or past the decoded
177
+ * soundtrack — a soundtrack shorter than the seek point leaves no source
178
+ * at all and the context clock keeps running. The STOP is scheduled at
179
+ * the host end on the same clock (`origin + seconds`), so a seek's source
180
+ * ends with the show, not with the buffer's remaining length.
181
+ */
182
+ function startSource(now, elapsed) {
183
+ const offset = Math.max(0, elapsed);
184
+ if (offset >= seconds) return null;
185
+ const duration = loaded?.decoded?.duration;
186
+ if (typeof duration === 'number' && offset >= duration) return null;
187
+ return audio.play(loaded.decoded, now, origin + seconds, offset);
188
+ }
189
+
190
+ /** A replay failure keeps a structured cause's own fields, like start. */
191
+ function failSeek(cause) {
192
+ const error = cause instanceof ShowError
193
+ ? cause
194
+ : new ShowError('seek_failed', `the show could not seek: ${cause?.message ?? cause}`, { stage: 'seek', cause });
195
+ return failShow(error.code, error.message, {
196
+ stage: error.stage,
197
+ document: error.document,
198
+ diagnostics: error.diagnostics,
199
+ cause: error.cause ?? cause,
200
+ });
201
+ }
202
+
203
+ /** An audio-port failure during a seek takes the transport's error path. */
204
+ function failAudio(message, cause) {
205
+ const error = cause instanceof ShowError
206
+ ? cause
207
+ : new ShowError('audio_failed', `${message}: ${cause?.message ?? cause}`, { stage: 'audio', cause });
208
+ return failShow(error.code, error.message, {
209
+ stage: error.stage,
210
+ document: error.document,
211
+ diagnostics: error.diagnostics,
212
+ cause: error.cause ?? cause,
213
+ });
214
+ }
215
+
216
+ /** Read the clock for a seek; a non-finite read is the loud clock failure. */
217
+ function seekClock(where) {
218
+ const now = clock();
219
+ if (!Number.isFinite(now)) {
220
+ throw failShow(
221
+ 'clock_invalid',
222
+ `the master clock read ${now} ${where}; the show refuses to guess time`,
223
+ { stage: 'clock' },
224
+ );
225
+ }
226
+ return now;
227
+ }
228
+
173
229
  /**
174
230
  * One paint: step every tick the clock has passed, bounded by the
175
231
  * grid's end, then schedule the next paint unless the show is over.
@@ -344,12 +400,120 @@ export function createShow({
344
400
  );
345
401
  }
346
402
  lastNow = now;
403
+ // A seek to a paused landing ended the source; Resume is the gesture
404
+ // that starts sound from the landed tick. A normal pause keeps its
405
+ // suspended source, so it is reused rather than re-created.
406
+ try {
407
+ if (handle === null) {
408
+ if (origin === null) origin = now - loaded.player.state.tick / ticksPerSecond;
409
+ handle = startSource(now, now - origin);
410
+ }
411
+ } catch (cause) {
412
+ if (state === 'error') throw failure;
413
+ throw failAudio('the audio could not restart on resume', cause);
414
+ }
347
415
  announce('playing');
348
416
  schedulePump();
349
417
  return state;
350
418
  });
351
419
  }
352
420
 
421
+ /**
422
+ * The serialized seek: clamp and round to the tick grid, replay the score
423
+ * silently to the landed tick, and take the landing state from the spec's
424
+ * table. A playing seek rebases the origin, restarts the source at the
425
+ * offset and reschedules the pump; a paused landing suspends the context
426
+ * (so the clock is frozen) and starts no sound; the host end is the one
427
+ * terminal landing. A failure takes the transport's existing error path,
428
+ * and a host failure that arrives during a platform await keeps its
429
+ * original identity.
430
+ */
431
+ function seek(tick) {
432
+ return enqueue(async () => {
433
+ if (state === 'loading' || state === 'error' || loaded === null) return state;
434
+ const rounded = Math.round(tick);
435
+ if (!Number.isFinite(rounded)) return state;
436
+ const target = Math.min(totalTicks, Math.max(0, rounded));
437
+ const from = state;
438
+ const position = target / ticksPerSecond;
439
+ endAudio();
440
+ try {
441
+ loaded.player.seekTo(target);
442
+ } catch (cause) {
443
+ if (state === 'error') throw failure;
444
+ throw failSeek(cause);
445
+ }
446
+ // A host failure raised synchronously by the replay (a draw callback,
447
+ // for instance) is already the answer; no landing may repaint over it.
448
+ if (state === 'error') throw failure;
449
+
450
+ // READY at zero is the one landing that stays READY.
451
+ if (from === 'ready' && target === 0) {
452
+ origin = null;
453
+ lastNow = null;
454
+ announce('ready');
455
+ return state;
456
+ }
457
+
458
+ // A playing seek between the ends keeps playing: the context is
459
+ // running, the origin is rebased to now - t, and a fresh source
460
+ // starts at that position.
461
+ if (from === 'playing' && target > 0 && target < totalTicks) {
462
+ try {
463
+ await audio.resume();
464
+ } catch (cause) {
465
+ if (state === 'error') throw failure;
466
+ throw failAudio('the audio could not be resumed for the seek', cause);
467
+ }
468
+ if (state === 'error') throw failure;
469
+ const now = seekClock('at the seek');
470
+ origin = now - position;
471
+ lastNow = now;
472
+ try {
473
+ handle = startSource(now, position);
474
+ } catch (cause) {
475
+ if (state === 'error') throw failure;
476
+ throw failAudio('the audio could not restart at the seek', cause);
477
+ }
478
+ announce('playing');
479
+ schedulePump();
480
+ return state;
481
+ }
482
+
483
+ // A playing seek to the host end is the one ENDED landing.
484
+ if (from === 'playing' && target === totalTicks) {
485
+ const now = seekClock('at the end seek');
486
+ origin = now - position;
487
+ lastNow = now;
488
+ announce('ended');
489
+ return state;
490
+ }
491
+
492
+ // Every other landing is silent: suspend first so the clock is frozen
493
+ // at the rebased origin before the state is announced.
494
+ try {
495
+ await audio.suspend();
496
+ } catch (cause) {
497
+ if (state === 'error') throw failure;
498
+ throw failAudio('the audio could not be suspended for the seek', cause);
499
+ }
500
+ if (state === 'error') throw failure;
501
+ const now = seekClock('at the paused seek');
502
+ origin = now - position;
503
+ lastNow = now;
504
+ if (from === 'press-to-resume') {
505
+ announce('press-to-resume');
506
+ return state;
507
+ }
508
+ if (from === 'ended' || from === 'stopped') {
509
+ announce(target === totalTicks ? 'ended' : 'paused');
510
+ return state;
511
+ }
512
+ announce('paused');
513
+ return state;
514
+ });
515
+ }
516
+
353
517
  /** End the show now: the audio stops and no pump is scheduled again. */
354
518
  function stop() {
355
519
  return enqueue(async () => {
@@ -449,6 +613,7 @@ export function createShow({
449
613
  start,
450
614
  pause,
451
615
  resume,
616
+ seek,
452
617
  stop,
453
618
  restart,
454
619
  fail,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "animader-cli",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "The animader command line: project creation, dev server, build, standalone export and host/project doctor checks",
5
5
  "type": "module",
6
6
  "bin": {
@@ -11,14 +11,14 @@
11
11
  },
12
12
  "license": "CC0-1.0",
13
13
  "dependencies": {
14
- "animader": "0.3.0",
14
+ "animader": "0.5.0",
15
15
  "esbuild": "0.24.2",
16
16
  "@mr_hugo/pacer": "0.2.0",
17
- "pngine": "4.2.0"
17
+ "pngine": "4.2.1"
18
18
  },
19
19
  "optionalDependencies": {
20
20
  "@mr_hugo/pacer-darwin-arm64": "0.2.0",
21
- "@pngine/darwin-arm64": "4.2.0"
21
+ "@pngine/darwin-arm64": "4.2.1"
22
22
  },
23
23
  "files": [
24
24
  "bin",