Skip to content

An AI skill that turns books into animated, narrated, interactive web experiences

fix(engine): a beat under music gives back the time its clock stood still

yzr0c47290d6db3
diff --git a/.claude/skills/papermorph/references/engine.md b/.claude/skills/papermorph/references/engine.md
index cbeffa3..72fb791 100644
--- a/.claude/skills/papermorph/references/engine.md
+++ b/.claude/skills/papermorph/references/engine.md
@@ -110,7 +110,7 @@ quiz(position, [{ id: 'c-mean', prompt: ['Find the mean of ', $m('7, 3, 9'), '.'
Space play/pause, and once the chapter has ended replay *(v2.1)*, ←/→ previous/next beat (Shift+←/→ during a question), Home restart, C captions (a question's narration is captioned too, above its band or beside its corner card, until the narration ends), `<`/`>` speed (0.75–1.5×, remembered), volume (remembered, except muted), F full screen, ? help. Clicking a step segment jumps to that point of the beat (`seekInto(i, frac)`); the next beat's clip preloads while one plays. `seek(i, play)` rebuilds and jumps; `start(i, play)` begins a beat from the current picture. While audio plays, the beat clock follows `audio.currentTime`. In a hidden tab (another tab or window in front) a timer keeps the clock going, so the chapter plays on from beat to beat *(v2.3.1; an older engine stops at the end of the beat until the tab is shown again)*. `?beat=N&t=S` in the URL opens a frozen frame for review, without captions (N is kept within the chapter's steps); on a question step the answer box takes focus only once Space starts the lesson. With `?review` in the URL, N pauses and asks for a note on the current moment and Shift+N copies the chapter's notes, one line each with the `?beat=N&t=S` that reopens it, so a reviewer can collect notes while watching and hand them over at once. After the end, on the finish card or a film's last frame, Enter opens `CHAPTER.next` (replays when there is none) and R replays.
-**Sound effects and music** *(v2.2)*, in films only: in a chapter with a question, `sfx()` and `music()` are silent, unless the chapter sets `CHAPTER.film = true` *(v2.3)*: a film with interactive moments (a documentary where the reader fires a rifle). There, while a question beat waits for the reader after its narration, the music plays on (it rises to 70 %, as in a lead-in) and is not pulled back for the time it ran on, and a click on its step bar seeks into it like any other beat; the next beat's tracks take over as usual (`fade` 0 is a cut). `sfx(beat, t, name, vol)` plays `audio/sfx/<name>.mp3` when that beat's clock passes `t` in normal playback (a seek past it fires nothing), at half volume (vol 2 is full); one still loading when it is due plays late, joined part-way, for up to a second. A long ambience listed in `SFX_BED` with its length (`SFX_BED.birds = 10`) is joined part-way after a seek. `music(file, beats, gain, fade)` is a looping track under the listed beats (all of them by default). Its place is the length of its earlier beats as they really ran plus the clock. It ducks to 30 % for the whole beat, voice or not, and rises to 70 % only in the `pre` lead-in of the beats in `window.MUSIC_UP_IN_PRE` (`true` for all); it crossfades over `fade` s (0.8; 0 is a cut), and `window.MUSIC_OFF = { beat: t => true }` fades it out for a moment. Both go through one Web Audio graph started by the reader's first touch, click or key, so ducks, fades and the volume slider reach them on phones too (on iOS the slider does not reach the narration). A pause holds the music's level and a seek stops the effects. When a clip loading or buffering holds the clock, the music runs on, then eases back (5 % slower or faster), or jumps back once if it is more than a second out. `export.py` reads `SFX`, `BGM` and these settings and mixes the film by the same rules.
+**Sound effects and music** *(v2.2)*, in films only: in a chapter with a question, `sfx()` and `music()` are silent, unless the chapter sets `CHAPTER.film = true` *(v2.3)*: a film with interactive moments (a documentary where the reader fires a rifle). There, while a question beat waits for the reader after its narration, the music plays on (it rises to 70 %, as in a lead-in) and is not pulled back for the time it ran on, and a click on its step bar seeks into it like any other beat; the next beat's tracks take over as usual (`fade` 0 is a cut). `sfx(beat, t, name, vol)` plays `audio/sfx/<name>.mp3` when that beat's clock passes `t` in normal playback (a seek past it fires nothing), at half volume (vol 2 is full); one still loading when it is due plays late, joined part-way, for up to a second. A long ambience listed in `SFX_BED` with its length (`SFX_BED.birds = 10`) is joined part-way after a seek. `music(file, beats, gain, fade)` is a looping track under the listed beats (all of them by default). Its place is the length of its earlier beats as they really ran plus the clock; a beat under music gives back from its closing hold the time the clock stood still (a clip getting going), so the film keeps the music's time. It ducks to 30 % for the whole beat, voice or not, and rises to 70 % only in the `pre` lead-in of the beats in `window.MUSIC_UP_IN_PRE` (`true` for all); it crossfades over `fade` s (0.8; 0 is a cut), and `window.MUSIC_OFF = { beat: t => true }` fades it out for a moment. Both go through one Web Audio graph started by the reader's first touch, click or key, so ducks, fades and the volume slider reach them on phones too (on iOS the slider does not reach the narration). A pause holds the music's level and a seek stops the effects. When a clip loading or buffering holds the clock, the music runs on, then eases back (5 % slower or faster), or jumps back once if it is more than a second out. `export.py` reads `SFX`, `BGM` and these settings and mixes the film by the same rules.
Progress is saved under `animebook:progress:<book-path>`; the cover and a picture book's series page read the same key. A book can set another key with `--progress-key` in `lib/book.css`; its cover must then read that key. Books served in different folders have separate saved places and completion lists.
diff --git a/engine/engine.js b/engine/engine.js
index 0082c18..68b5f5a 100644
--- a/engine/engine.js
+++ b/engine/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/elementary-algebra/lib/engine.js b/site/elementary-algebra/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/elementary-algebra/lib/engine.js
+++ b/site/elementary-algebra/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/joke-collection/lib/engine.js b/site/joke-collection/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/joke-collection/lib/engine.js
+++ b/site/joke-collection/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/little-red-riding-hood/lib/engine.js b/site/little-red-riding-hood/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/little-red-riding-hood/lib/engine.js
+++ b/site/little-red-riding-hood/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/madness/lib/engine.js b/site/madness/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/madness/lib/engine.js
+++ b/site/madness/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/math-notebook/lib/engine.js b/site/math-notebook/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/math-notebook/lib/engine.js
+++ b/site/math-notebook/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/physics-notebook/lib/engine.js b/site/physics-notebook/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/physics-notebook/lib/engine.js
+++ b/site/physics-notebook/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/science/lib/engine.js b/site/science/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/science/lib/engine.js
+++ b/site/science/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/shooting-an-elephant/lib/engine.js b/site/shooting-an-elephant/lib/engine.js
index 4d3817c..3fcca19 100644
--- a/site/shooting-an-elephant/lib/engine.js
+++ b/site/shooting-an-elephant/lib/engine.js
@@ -1356,7 +1356,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1450,7 +1450,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1461,7 +1461,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();
diff --git a/site/three-little-pigs/lib/engine.js b/site/three-little-pigs/lib/engine.js
index 0082c18..68b5f5a 100644
--- a/site/three-little-pigs/lib/engine.js
+++ b/site/three-little-pigs/lib/engine.js
@@ -1362,7 +1362,7 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
- Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0 });
+ Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
const a = P.audio = beatClip(i, true);
@@ -1456,7 +1456,7 @@ function step(now, most) {
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
if (P.playing && !P.waiting && !P.done) {
- const a = P.audio;
+ const a = P.audio, t0 = P.t;
// While the clip plays, its clock drives the picture; before it starts or after it ends, wall time does.
const pre = BEATS[P.i].pre || 0;
if (a && P.t < pre) { // the picture's lead before the narration, on wall time; then the clip starts
@@ -1467,7 +1467,12 @@ function step(now, most) {
else if ((P.stall += dt) > (a.readyState < 2 ? 8 : 15)) soundFailed(); // nothing to play after 8 s, or stuck for 15
} else P.t += dt * P.rate;
evalTo(P.t);
- if (P.t >= P.end) beatDone();
+ // P.lost: how long the clock stood still while the film played (a clip getting going, buffering). Music runs on
+ // through it, so a beat under music gives that time back from its closing hold: each beat then lasts as long in
+ // real time as on the music's timeline, and the music stays with the picture instead of drifting ahead beat by beat.
+ P.lost += dt * P.rate - (P.t - t0);
+ const back = SND.bgm.some(m => m.beats.includes(BEATS[P.i].id)) ? Math.max(0, Math.min(HOLD, P.lost)) : 0;
+ if (P.t >= P.end - back) beatDone();
}
soundTick(dt);
syncLinks();