Skip to content

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

fix(engine): v2.4.1, the review's engine fixes

yzr3620ad46f023
diff --git a/.claude/skills/papermorph/assets/templates/documentary/style.json b/.claude/skills/papermorph/assets/templates/documentary/style.json
index 786a09d..ec17df0 100644
--- a/.claude/skills/papermorph/assets/templates/documentary/style.json
+++ b/.claude/skills/papermorph/assets/templates/documentary/style.json
@@ -15,7 +15,7 @@
"voice": { "engine": "edge", "voice": "en-GB-RyanNeural", "rate": "-4%" },
"music": "public-domain classical recordings, one per part of the film, changing at the turns",
"pacing": { "minutes": 10, "turns": "one argument with five or six turns; one idea or picture every two to four seconds" },
- "engine": { "source": "DozenTwelve/Papermorph", "ref": "engine-v2.4" },
+ "engine": { "source": "DozenTwelve/Papermorph", "ref": "engine-v2.4.1" },
"models": { "script": "claude-opus-5-5", "pictures": "claude-sonnet-5-5", "beats": "claude-sonnet-5-5", "checks": "claude-haiku-5-5", "director": "claude-opus-5-5" },
"approved": [{ "chapter": "ch01", "why": "close-ups on what is named; few words on screen" }]
}
diff --git a/.claude/skills/papermorph/assets/templates/style.json b/.claude/skills/papermorph/assets/templates/style.json
index 77a02bd..764b8c8 100644
--- a/.claude/skills/papermorph/assets/templates/style.json
+++ b/.claude/skills/papermorph/assets/templates/style.json
@@ -14,7 +14,7 @@
},
"voice": { "engine": "edge", "voice": "en-US-AndrewNeural", "rate": "-4%" },
"pacing": { "minutes": [6, 10], "checks": "one or two quick checks inside the lesson, practice at the end" },
- "engine": { "source": "DozenTwelve/Papermorph", "ref": "engine-v2.4" },
+ "engine": { "source": "DozenTwelve/Papermorph", "ref": "engine-v2.4.1" },
"models": { "extract": "claude-haiku-5-5", "narration": "claude-sonnet-5-5", "picture": "claude-opus-5-5", "checks": "claude-haiku-5-5", "review": "claude-sonnet-5-5", "rework": "claude-opus-5-5", "register": "claude-haiku-5-5" },
"scenes": {
"art": "pixel scenes on a dark board, characters about a third of the stage tall",
diff --git a/.claude/skills/papermorph/references/engine.md b/.claude/skills/papermorph/references/engine.md
index 3790002..2028e40 100644
--- a/.claude/skills/papermorph/references/engine.md
+++ b/.claude/skills/papermorph/references/engine.md
@@ -2,7 +2,7 @@
A chapter page loads the engine and its `audio/<lang>/timings.js`, defines `CHAPTER` (including `language` when different from `en`: the audio folder), drawing helpers, `BEATS` and questions, then calls `boot()`. The engine provides the stage, drawing, timeline, question types, guide and player. Use the code's signatures if they differ from this reference. The interface text (buttons, help, cover, finish card) follows `<html lang>`: English, Russian, Chinese and Japanese are in the engine's `TRANSLATIONS`; another language needs a block there.
-Every book keeps a copy of the shared engine in `lib/` (`engine.*`, `math.js`, `volume.*`, `contents.*`, `units.*`), so the book runs on its own. The engine is not part of this skill: it lives in the `engine/` folder of the Papermorph repository (or a fork). `python3 SKILL/scripts/engine.py site` copies it into every book from the repository's `engine/` when it is at hand, otherwise from GitHub (`--check` lists copies that differ, `--from DIR|OWNER/REPO@REF` picks the source, by default the latest compatible release, `engine-v2.4`; `@main` takes the newest, a tag or commit pins it, and `engine-v1` is the last engine with the maths helpers inside `engine.js`); a book's copy is not edited by hand. This reference describes `engine-v2.4`; what an older copy lacks is marked *(v2.1)*, *(v2.2)*, *(v2.3)* or *(v2.4)*, and syncing the book brings it. An engine bug is fixed at the source, then synced. The book's own settings and style overrides live in `lib/book.css`, imported by `engine.css` and never overwritten: `--math-font` for numbers and formulas, `--progress-key` when the cover uses a key other than the default.
+Every book keeps a copy of the shared engine in `lib/` (`engine.*`, `math.js`, `volume.*`, `contents.*`, `units.*`), so the book runs on its own. The engine is not part of this skill: it lives in the `engine/` folder of the Papermorph repository (or a fork). `python3 SKILL/scripts/engine.py site` copies it into every book from the repository's `engine/` when it is at hand, otherwise from GitHub (`--check` lists copies that differ, `--from DIR|OWNER/REPO@REF` picks the source, by default the latest compatible release, `engine-v2.4.1`; `@main` takes the newest, a tag or commit pins it, and `engine-v1` is the last engine with the maths helpers inside `engine.js`); a book's copy is not edited by hand. This reference describes `engine-v2.4.1`; what an older copy lacks is marked *(v2.1)*, *(v2.2)*, *(v2.3)*, *(v2.4)* or *(v2.4.1)*, and syncing the book brings it. An engine bug is fixed at the source, then synced. The book's own settings and style overrides live in `lib/book.css`, imported by `engine.css` and never overwritten: `--math-font` for numbers and formulas, `--progress-key` when the cover uses a key other than the default.
The maths helpers (number lines, tiles, fraction rows, planes, chance objects, the number-set diagram, `pickPoint`, `sorter`) are in `lib/math.js`. A chapter that uses one loads `<script src="../lib/math.js"></script>` right after `engine.js`: it reads the engine's globals as it loads, so never before it and never `async`. `lint.py` names a chapter that uses one without loading it. Use the helpers that fit; draw the rest in the chapter, and move what several chapters use into the book's element library (`lib/<name>.js`, [assets.md](assets.md)) rather than into the engine copy.
@@ -59,7 +59,7 @@ A book whose formulas need more than `M()` parts (indices, sums, matrices, accen
- `m('name', offset)` is the start time of the word after `[[name]]`; `D` is the clip's length. Use marks for actions tied to speech; scene entrances and transitions can use fixed offsets.
- Tweens: `tw(e, to, t0, dur, ease)`, `prog(q => …, t0, dur, ease)` for custom motion (arcs, counters, live redraws), `show(e, t0, dur, ease)`, `hide`, `draw` (same arguments), `pop`, `pulse`, `stream`. Eases: `io`, `out`, `back`, `lin`, `soft`. Continuous motion and cameras: see below.
-- A lesson beat ends after its narration and its last tween, and holds that picture `HOLD` (0.6 s) before the next starts; an `ask` beat waits for `done()`.
+- A lesson beat ends after its narration and its last tween, and holds that picture `HOLD` (0.6 s) before the next starts; an `ask` beat waits for `done()`. A `done()` kept from an earlier start of the same beat (the reader stepped back and replayed it) no longer counts *(v2.4.1)*; a chapter's own handler that acts later can compare `P.gen`, which every start of a beat changes.
- **Opening lead-in**: if a beat calls `panel()` (before 0.5 s) and no element is revealed (faded, scaled or drawn in from nothing) within 0.8 s, `runBeat` moves the opening group (every tween starting within 1.2 s of the first reveal) to start at 0.4 s, so the narration never runs over an empty board. It looks only at reveals: a beat that lays out part of its picture visible at once (axes, empty boxes, a set) and reveals the rest on later words still has that first group pulled to 0.4 s. When those words matter, or a camera shows a picture from the first frame, give the beat `{ lead: false }` in its fourth slot; when every opening in the chapter is timed by hand (a documentary), set `CHAPTER.lead = false` *(v2.2)* instead. To see where a beat's reveals land, put marks on a contact sheet: `strip.py chNN OUT idea:0.4,label,label+0.3,end --sheet storyboard --url http://localhost:8765/<book>/` (a mark plus an offset is the moment the action is timed to).
- **`pre`** in the fourth slot (`{ pre: 1.2 }`) runs the picture that many seconds before the narration starts: an action, then the line. Picture books use it on every scene.
- **The end of a film**: a chapter whose last beat asks nothing (a picture book) is finished when that beat ends: it is marked done and, with `CHAPTER.next`, offers the next chapter (Enter).
@@ -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; 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.
+**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, from its start, 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. Safari and every iOS browser stall a media element at each speed change, so there the music keeps the film's speed and is put back by a seek once it is a quarter second out, aimed ahead by the lag it showed after the last one *(v2.4.1)*. `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/.claude/skills/papermorph/references/sound.md b/.claude/skills/papermorph/references/sound.md
index 29faa1a..72b47a0 100644
--- a/.claude/skills/papermorph/references/sound.md
+++ b/.claude/skills/papermorph/references/sound.md
@@ -5,7 +5,7 @@ Voices carry the story; everything else sits under them. In early cuts the music
## Voices
- **One voice per character.** List them under `voices` in `narration.<lang>.json` and write each line as `{"speaker": name, "text": …}`; `tts.py` synthesizes every line (Edge voices, with rate and pitch: children were adult voices sped up and pitched up). The narrator is a voice too.
-- **Lines in other languages** are spoken in their own language by a native voice (Edge has Japanese, Chinese, Korean and more) and subtitled in the book's language. The caption shows both: the original and the translation in brackets. Write it as the beat's `"sub"`; `tts.py` marks such a line (its voice's locale differs from the narration file's language) so the page shows its subtitle even with captions off, the way films subtitle foreign dialogue.
+- **Lines in other languages** are spoken in their own language by a native voice (Edge has Japanese, Chinese, Korean and more) and subtitled in the book's language. The caption shows both: the original and the translation in brackets. Write it as the beat's `"sub"`; `tts.py` marks such a line (its voice's locale differs from the narration file's language) so the page shows its subtitle even with captions off, the way films subtitle foreign dialogue (engine v2.4.1; an older engine shows it only for a picture book joined by `join.py`).
- **Mouths** follow the words: `mouths.py` turns each line's word timings into mouth shapes (a Chinese or Japanese line opens and shuts once per character), and `lips(q, scene, pre, line…)` picks the shape for the frame. Edge's word times run about a tenth of a second ahead of the sound; `mouths.py` and `join.py` move mouths and captions that much later and shut the mouth where the clip's sound stops, so a lip flap that leads or trails the voice is worth reporting.
- **One clip per scene.** `join.py` joins a scene's lines into one audio clip with short pauses, and inserts silence where the action needs room (`line@T+S`: S seconds of silence T seconds into the line, after a word's sound has ended rather than where the next word begins) or after the last line (`name:S`, a stare or a tag). Gaps for a held stare, a fly landing, a class turning round belong here, not in rushed animation. `--words` writes every word's time in the scene clip beside the narration; scene authors time actions against it (the beat clock adds the scene's `pre`).
- **One script for the whole sound.** Keep the episode's audio steps in one script beside the book's notes (`tools/<book>/audio.sh`) and rerun it after any change: lines replaced by hand, `mouths.py`, level fixes, `join.py` with its holes, any caption edits. One episode's join arguments lived only in a terminal and had to be read back out of `shots.js`.
diff --git a/.claude/skills/papermorph/scripts/engine.py b/.claude/skills/papermorph/scripts/engine.py
index 0060c3c..9cb3e93 100644
--- a/.claude/skills/papermorph/scripts/engine.py
+++ b/.claude/skills/papermorph/scripts/engine.py
@@ -20,7 +20,7 @@ with lib/mathjax.js keeps it in step like the rest.
import argparse, filecmp, io, shutil, sys, tarfile, tempfile, urllib.request
from pathlib import Path
-DEFAULT = 'DozenTwelve/[email protected]' # follows the latest compatible release
+DEFAULT = 'DozenTwelve/[email protected]' # follows the latest compatible release
OWN = 'book.css'
LATEX = {'vendor/mathjax/tex-svg-full.js': 'mathjax.js', 'vendor/mathjax/LICENSE': 'mathjax-LICENSE.txt'}
diff --git a/README.md b/README.md
index 25812cc..8d4c41c 100644
--- a/README.md
+++ b/README.md
@@ -77,7 +77,7 @@ python3 .claude/skills/papermorph/scripts/engine.py site
python3 .claude/skills/papermorph/scripts/engine.py site --from you/your-fork@main
```
-Engine versions are tags (`engine-v2`, `engine-v2.1`, …); a new major number means a book may need a change, described in its [release notes](https://github.com/DozenTwelve/Papermorph/releases). Outside this repository, `engine.py` syncs from the latest compatible release, now `engine-v2.4`. Since `engine-v2` the maths helpers (number lines, tiles, planes, the number-set diagram) are in `lib/math.js`: a chapter that uses them loads `../lib/math.js` right after `engine.js`, and `lint.py` names the chapters that need it. `--from DozenTwelve/Papermorph@engine-v1` keeps the earlier engine.
+Engine versions are tags (`engine-v2`, `engine-v2.1`, …); a new major number means a book may need a change, described in its [release notes](https://github.com/DozenTwelve/Papermorph/releases). Outside this repository, `engine.py` syncs from the latest compatible release, now `engine-v2.4.1`. Since `engine-v2` the maths helpers (number lines, tiles, planes, the number-set diagram) are in `lib/math.js`: a chapter that uses them loads `../lib/math.js` right after `engine.js`, and `lint.py` names the chapters that need it. `--from DozenTwelve/Papermorph@engine-v1` keeps the earlier engine.
## Video and LMS export
diff --git a/engine/engine.css b/engine/engine.css
index 436bdb6..f1c8281 100644
--- a/engine/engine.css
+++ b/engine/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/engine/engine.js b/engine/engine.js
index 1a2efe1..f3e03b6 100644
--- a/engine/engine.js
+++ b/engine/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/elementary-algebra/lib/engine.css b/site/elementary-algebra/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/elementary-algebra/lib/engine.css
+++ b/site/elementary-algebra/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/elementary-algebra/lib/engine.js b/site/elementary-algebra/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/elementary-algebra/lib/engine.js
+++ b/site/elementary-algebra/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/joke-collection/lib/engine.css b/site/joke-collection/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/joke-collection/lib/engine.css
+++ b/site/joke-collection/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/joke-collection/lib/engine.js b/site/joke-collection/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/joke-collection/lib/engine.js
+++ b/site/joke-collection/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/little-red-riding-hood/lib/engine.css b/site/little-red-riding-hood/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/little-red-riding-hood/lib/engine.css
+++ b/site/little-red-riding-hood/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/little-red-riding-hood/lib/engine.js b/site/little-red-riding-hood/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/little-red-riding-hood/lib/engine.js
+++ b/site/little-red-riding-hood/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/madness-shorts/lib/engine.css b/site/madness-shorts/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/madness-shorts/lib/engine.css
+++ b/site/madness-shorts/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/madness-shorts/lib/engine.js b/site/madness-shorts/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/madness-shorts/lib/engine.js
+++ b/site/madness-shorts/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/madness/lib/engine.css b/site/madness/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/madness/lib/engine.css
+++ b/site/madness/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/madness/lib/engine.js b/site/madness/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/madness/lib/engine.js
+++ b/site/madness/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/math-notebook/lib/engine.css b/site/math-notebook/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/math-notebook/lib/engine.css
+++ b/site/math-notebook/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/math-notebook/lib/engine.js b/site/math-notebook/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/math-notebook/lib/engine.js
+++ b/site/math-notebook/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/physics-notebook/lib/engine.css b/site/physics-notebook/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/physics-notebook/lib/engine.css
+++ b/site/physics-notebook/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/physics-notebook/lib/engine.js b/site/physics-notebook/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/physics-notebook/lib/engine.js
+++ b/site/physics-notebook/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/science/lib/engine.css b/site/science/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/science/lib/engine.css
+++ b/site/science/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/science/lib/engine.js b/site/science/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/science/lib/engine.js
+++ b/site/science/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/shooting-an-elephant/lib/engine.css b/site/shooting-an-elephant/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/shooting-an-elephant/lib/engine.css
+++ b/site/shooting-an-elephant/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/shooting-an-elephant/lib/engine.js b/site/shooting-an-elephant/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/shooting-an-elephant/lib/engine.js
+++ b/site/shooting-an-elephant/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}
diff --git a/site/three-little-pigs/lib/engine.css b/site/three-little-pigs/lib/engine.css
index 436bdb6..f1c8281 100644
--- a/site/three-little-pigs/lib/engine.css
+++ b/site/three-little-pigs/lib/engine.css
@@ -23,8 +23,9 @@ button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, [tabindex]:focus-visible { outline: 3px solid var(--task); outline-offset: 3px; }
button[hidden] { display: none; }
-/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. */
-#frame { position: absolute; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
+/* One fixed 1600×960 frame (900 stage + 60 controls), scaled to fit the window. Fixed, not absolute: a frame wider
+ than a phone would otherwise widen the phone's layout viewport, and fit() would scale to that wider width. */
+#frame { position: fixed; left: 50%; top: 50%; width: var(--sw, 1600px); height: calc(var(--sh, 900px) + 60px); transform: translate(-50%, -50%) scale(var(--k, .5));
border-radius: 20px; overflow: hidden; background: var(--board); box-shadow: 0 0 0 1px var(--edge), 0 30px 80px rgba(0, 0, 0, .45); }
#stage { position: absolute; left: 0; top: 0; width: var(--sw, 1600px); height: var(--sh, 900px); display: block;
background: radial-gradient(75% 75% at 50% 45%, rgba(0,0,0,0) 60%, var(--vignette)), var(--grain, none), var(--board); }
@@ -194,6 +195,9 @@ svg.m { display: inline-block; overflow: visible; }
.card.screen:not(.sort):not(.fin) > .body { flex: 0 1 auto; min-height: 0; overflow-y: auto; }
.screen .acts { position: absolute; right: 70px; bottom: 48px; }
.screen .grid .row { grid-template-columns: 200px minmax(0, 1fr) 40px; padding: 9px 0 9px 12px; }
+/* a long label (a system of equations) widens the label column for every row instead of running off the card's left */
+.screen .grid:not(.text) { display: grid; grid-template-columns: minmax(200px, max-content) minmax(0, 1fr) 40px; }
+.screen .grid:not(.text) > .row { grid-column: 1 / -1; grid-template-columns: subgrid; }
.screen .grid.text .row { grid-template-columns: minmax(0, 1fr) auto 40px; }
.screen .grid .opt { font-size: 22px; min-height: 50px; padding: 4px 20px; }
.screen .grid .why { padding-left: 216px; font-size: 20px; }
@@ -260,4 +264,5 @@ a.btn { text-decoration: none; display: inline-block; }
.fin .prompt { margin: 6px 0 14px; font: 500 56px/1.15 "Iowan Old Style", Palatino, Georgia, serif; }
.icon.speed { width: auto; min-width: 44px; padding: 0 10px; border-radius: 22px; font-size: 16px; }
/* An upright stage is seen at about half the scale of a wide one on the same screen: bigger caption text. */
+:root[data-stage="portrait"] .step { width: 120px; } /* the 900-wide bar keeps room for the steps */
:root[data-stage="portrait"] .caption { font-size: 38px; bottom: 60px; }
diff --git a/site/three-little-pigs/lib/engine.js b/site/three-little-pigs/lib/engine.js
index 1a2efe1..f3e03b6 100644
--- a/site/three-little-pigs/lib/engine.js
+++ b/site/three-little-pigs/lib/engine.js
@@ -1216,6 +1216,7 @@ function bgmWire(m) {
}
// A browser starts sound only after a click or a key: the first one builds the graph (films only) and readies the music.
function soundUnlock() {
+ if (typeof BEATS === 'undefined') return; // the chapter's script has not run yet
if (!SND.ctx && window.AudioContext && (!BEATS.some(b => b.ask) || CHAPTER.film)) { // a film with a question (CHAPTER.film) keeps its sound
if (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film
SND.ctx = new AudioContext();
@@ -1232,6 +1233,9 @@ function soundUnlock() {
}
// a touch activates the page on touchend or click, not on pointerdown
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
+// Safari (and every iOS browser) stops a media element for .3-.6 s at each playbackRate change, so easing the music
+// back would only push it further out: there it keeps the film's rate and seeks instead.
+const RATE_STALLS = /AppleWebKit/.test(navigator.userAgent) && !/Chrome\/|Android/.test(navigator.userAgent);
function soundTick(dt) {
const id = BEATS[P.i]?.id, pre = BEATS[P.i]?.pre || 0, on = P.started && P.playing && !P.waiting && !P.done, L = SND.last;
// a film waiting for the reader (CHAPTER.film, a question beat after its narration): the music plays on, louder
@@ -1240,7 +1244,7 @@ function soundTick(dt) {
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
// the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or the next beat
- const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < .5;
+ const same = L.i === P.i && P.t >= L.t && P.t - Math.max(0, L.t) < Math.max(.5, dt * P.rate + .25);
if (!same) { while (SND.on.length) SND.on.pop().stop(); SND.wait = []; }
// a beat that has just begun fires its effects from 0 on; a seek fires nothing it skipped, but joins a bed
const from = same ? L.t : P.t < .25 ? -1 : P.t, seeked = !same || L.pend;
@@ -1255,7 +1259,7 @@ function soundTick(dt) {
SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };
const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;
if (SND.out) SND.out.gain.value = vol;
- if (!SND.bgm.length) return;
+ if (!SND.bgm.length || BEATS.some(b => b.ask) && !CHAPTER.film) return; // a lesson with questions plays no music
// the film's clock moved this frame (a clip loading or buffering holds it), or jumped (a seek, a replay)
const runs = on && same && P.t > L.t, jump = !same && !(L.i === P.i - 1 && P.t < .5);
if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
@@ -1277,12 +1281,19 @@ function soundTick(dt) {
// from .25 s out until .05 s
const ok = runs && !m.a.seeking;
// (once more after a jump: the element plays on while it seeks)
- if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > (m.settle ? .25 : 1)) {
- m.a.currentTime = want; d = 0; m.settle = !ok;
+ // Where a rate change stalls, starting and seeking stall too: once a second the music's lag is learnt (m.lead),
+ // seeks aim that far ahead, and a quarter second out is then worth a seek.
+ let far = m.settle ? .25 : 1;
+ m.sought ??= clock;
+ if (RATE_STALLS && ok && clock - m.sought > 1) {
+ m.sought = clock; m.lead = Math.max(0, Math.min(.6, (m.lead || 0) - d)); far = .25;
+ }
+ if (m.a.paused || jump && Math.abs(d) > .05 || ok && Math.abs(d) > far) {
+ m.a.currentTime = (want + (RATE_STALLS && m.lead || 0)) % dur; d = 0; m.settle = !ok; m.sought = clock;
} else if (ok) m.settle = false;
if (Math.abs(d) > .25) m.fix = true; else if (Math.abs(d) < .05) m.fix = false;
// a frame where the clock stood still keeps the rate it had
- const rate = !m.fix ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
+ const rate = !m.fix || RATE_STALLS ? P.rate : ok ? P.rate * (1 - Math.sign(d) * .05) : m.a.playbackRate;
if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;
if (m.a.paused) m.a.play().catch(() => {});
} else {
@@ -1362,6 +1373,8 @@ function seekInto(i, frac) {
}
function start(i, play) {
stopAudio(); clearCards();
+ // P.gen counts starts: a done() kept from an earlier start of this same beat (a step replayed) no longer counts
+ const gen = P.gen = (P.gen || 0) + 1;
Object.assign(P, { i, t: 0, waiting: false, done: false, afail: false, stall: 0, lost: 0 });
P.end = runBeat(i);
evalTo(0);
@@ -1378,7 +1391,7 @@ function start(i, play) {
};
const b = BEATS[i];
if (b.ask) b.ask(() => {
- if (P.i !== i || P.done) return;
+ if (P.gen !== gen || P.done) return;
evalTo(Infinity);
if (i + 1 < BEATS.length) start(i + 1, true);
else { P.done = true; setPlaying(false); filmDone(); }
@@ -1438,7 +1451,7 @@ function filmDone() {
if (!CHAPTER.next) return;
const next = h('button', 'btn go', [translate('nextChapter'), kbd('Enter')]);
next.onclick = go;
- const c = card({ x: 0, y: 770, w: 0, cls: 'side' }, 'next');
+ const c = card({ x: 0, y: SH - 130, w: 0, cls: 'side' }, 'next');
Object.assign(c.style, { left: 'auto', right: '40px', width: 'auto' });
c.append(h('div', 'acts', [next]));
}
@@ -1447,11 +1460,8 @@ function frame(now) {
frameStep(now, .1);
requestAnimationFrame(frame);
}
-// A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
-// one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
-setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
function frameStep(now, most) {
- const dt = Math.min(most, Math.max(0, now - last) / 1000);
+ const raw = Math.max(0, now - last) / 1000, dt = Math.min(most, raw);
last = now;
clock += dt;
if (FX.length) { evalTo(clock, FX); FX = FX.filter(w => !w.done); }
@@ -1470,7 +1480,7 @@ function frameStep(now, most) {
// 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);
+ P.lost += raw * P.rate - (P.t - t0); // real time: a long frame moves the music by all of it
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();
}
@@ -1511,10 +1521,11 @@ function ui() {
const asking = !!b.ask;
if (P.started && !P.done && !(asking && (P.waiting || $('ui').querySelector('.card.screen')))) {
// a cue is [start, line], [start, line, end] or [start, line, end, 1]: with an end, the caption clears once its
- // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off
+ // words are over; a 1 marks a line in another language, whose subtitle shows even with captions off (as does every
+ // cue of a beat whose timings say forced: true, from tts.py or eleven.py)
for (const [t, line, end, forced] of TIMINGS[b.id].cues) {
if (t + (b.pre || 0) > P.t + .05) break;
- text = (captions || forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
+ text = (captions || forced || TIMINGS[b.id].forced) && !(end != null && P.t > end + (b.pre || 0)) ? line : '';
}
}
if (text !== capText) { capText = text; $('caption').textContent = text; $('caption').hidden = !text; }
@@ -1664,4 +1675,7 @@ function boot() {
if (P.audio) P.audio.currentTime = Math.max(0, P.t - (BEATS[P.i].pre || 0));
} else seek(0, false);
requestAnimationFrame(frame);
+ // A hidden tab gets no animation frames: a timer keeps the film going, so a beat that ends there still starts the next
+ // one (a tab playing sound keeps its timers). Its ticks come late, so they may move the clock further at once.
+ setInterval(() => { if (document.hidden) frameStep(performance.now(), 1.5); }, 250);
}