Skip to content

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

feat(engine): v2.3, films with interactive moments

DozenTwelvec293f32edfed
diff --git a/.claude/skills/papermorph/assets/templates/documentary/style.json b/.claude/skills/papermorph/assets/templates/documentary/style.json
index c331c2a..c6cc3fc 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.2" },
+ "engine": { "source": "DozenTwelve/Papermorph", "ref": "engine-v2.3" },
"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 c26df88..c4eb7a0 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.2" },
+ "engine": { "source": "DozenTwelve/Papermorph", "ref": "engine-v2.3" },
"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 4df2281..de41248 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.2`; `@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.2`; what an older copy lacks is marked *(v2.1)* or *(v2.2)*, 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.3`; `@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.3`; what an older copy lacks is marked *(v2.1)*, *(v2.2)* or *(v2.3)*, 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.
@@ -109,7 +109,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`. `?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. `sfx(beat, t, name, vol)` plays `audio/sfx/<name>.mp3` when that beat's clock passes `t` in normal playback (a seek past it fires nothing), at half volume (vol 2 is full); one still loading when it is due plays late, joined part-way, for up to a second. A long ambience listed in `SFX_BED` with its length (`SFX_BED.birds = 10`) is joined part-way after a seek. `music(file, beats, gain, fade)` is a looping track under the listed beats (all of them by default). Its place is the length of its earlier beats as they really ran plus the clock. It ducks to 30 % for the whole beat, voice or not, and rises to 70 % only in the `pre` lead-in of the beats in `window.MUSIC_UP_IN_PRE` (`true` for all); it crossfades over `fade` s (0.8; 0 is a cut), and `window.MUSIC_OFF = { beat: t => true }` fades it out for a moment. Both go through one Web Audio graph started by the reader's first touch, click or key, so ducks, fades and the volume slider reach them on phones too (on iOS the slider does not reach the narration). A pause holds the music's level and a seek stops the effects. When a clip loading or buffering holds the clock, the music runs on, then eases back (5 % slower or faster), or jumps back once if it is more than a second out. `export.py` reads `SFX`, `BGM` and these settings and mixes the film by the same rules.
+**Sound effects and music** *(v2.2)*, in films only: in a chapter with a question, `sfx()` and `music()` are silent, unless the chapter sets `CHAPTER.film = true` *(v2.3)*: a film with interactive moments (a documentary where the reader fires a rifle). There, while a question beat waits for the reader after its narration, the music plays on (it rises to 70 %, as in a lead-in) and is not pulled back for the time it ran on, and a click on its step bar seeks into it like any other beat; the next beat's tracks take over as usual (`fade` 0 is a cut). `sfx(beat, t, name, vol)` plays `audio/sfx/<name>.mp3` when that beat's clock passes `t` in normal playback (a seek past it fires nothing), at half volume (vol 2 is full); one still loading when it is due plays late, joined part-way, for up to a second. A long ambience listed in `SFX_BED` with its length (`SFX_BED.birds = 10`) is joined part-way after a seek. `music(file, beats, gain, fade)` is a looping track under the listed beats (all of them by default). Its place is the length of its earlier beats as they really ran plus the clock. It ducks to 30 % for the whole beat, voice or not, and rises to 70 % only in the `pre` lead-in of the beats in `window.MUSIC_UP_IN_PRE` (`true` for all); it crossfades over `fade` s (0.8; 0 is a cut), and `window.MUSIC_OFF = { beat: t => true }` fades it out for a moment. Both go through one Web Audio graph started by the reader's first touch, click or key, so ducks, fades and the volume slider reach them on phones too (on iOS the slider does not reach the narration). A pause holds the music's level and a seek stops the effects. When a clip loading or buffering holds the clock, the music runs on, then eases back (5 % slower or faster), or jumps back once if it is more than a second out. `export.py` reads `SFX`, `BGM` and these settings and mixes the film by the same rules.
Progress is saved under `animebook:progress:<book-path>`; the cover and a picture book's series page read the same key. A book can set another key with `--progress-key` in `lib/book.css`; its cover must then read that key. Books served in different folders have separate saved places and completion lists.
diff --git a/.claude/skills/papermorph/references/site.md b/.claude/skills/papermorph/references/site.md
index 9e4f435..53e2efe 100644
--- a/.claude/skills/papermorph/references/site.md
+++ b/.claude/skills/papermorph/references/site.md
@@ -132,4 +132,4 @@ Narration and on-screen text are written in one language first. A second languag
## Video export (optional)
-`uv run --with playwright --with numpy SKILL/scripts/export.py chNN --url http://localhost:8765/<book>/ --out OUT --link <lesson URL>` renders the beats to `OUT/chNN.mp4` (1080p, 30 fps) plus `OUT/chNN.srt` for upload with it. Frames come from seeking the beat clock and the sound is mixed by the page's own rules (each beat's `pre`, and in a picture book its effects and music), so the video matches the page; question beats are left out and a lesson's video ends on a card pointing to the interactive lesson. It takes about 1.6× the video's length. Video files are large (an episode at 1080p is tens of megabytes, more than a static host such as Cloudflare Pages takes per file): upload them to the video site, not into `site/`.
+`uv run --with playwright --with numpy SKILL/scripts/export.py chNN --url http://localhost:8765/<book>/ --out OUT --link <lesson URL>` renders the beats to `OUT/chNN.mp4` (1080p, 30 fps) plus `OUT/chNN.srt` for upload with it. Frames come from seeking the beat clock and the sound is mixed by the page's own rules (each beat's `pre`, and in a picture book its effects and music), so the video matches the page; question beats are left out and a lesson's video ends on a card pointing to the interactive lesson (in a film with `CHAPTER.film`, they stay in: each holds its picture for `hold` s, then the next beat plays as if the reader had acted). It takes about 1.6× the video's length. Video files are large (an episode at 1080p is tens of megabytes, more than a static host such as Cloudflare Pages takes per file): upload them to the video site, not into `site/`.
diff --git a/.claude/skills/papermorph/scripts/engine.py b/.claude/skills/papermorph/scripts/engine.py
index a435666..15c36ba 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/.claude/skills/papermorph/scripts/export.py b/.claude/skills/papermorph/scripts/export.py
index 07d0aae..0da396e 100644
--- a/.claude/skills/papermorph/scripts/export.py
+++ b/.claude/skills/papermorph/scripts/export.py
@@ -9,7 +9,8 @@ recording playback: the video is exact and the same on every run. The sound is m
each beat's clip from its `pre` on, and in a film (a picture book or a documentary) the sfx() effects (cut at the end
of their scene, as a scene change stops them) and the BGM tracks with their crossfades, ducking and MUSIC_OFF. Question beats (quick
checks and practice) are left out, and then the video ends on a card saying they are in the interactive lesson
-(--link adds its address). Captions are not burned in: upload OUT/chNN.srt with the video. Needs ffmpeg; serve the
+(--link adds its address); in a film with interactive moments (CHAPTER.film, e.g. the reader fires a rifle) they stay in,
+each holding its last picture for its `hold` seconds (1.5 by default) before the next beat plays as if the reader had acted. Captions are not burned in: upload OUT/chNN.srt with the video. Needs ffmpeg; serve the
site over HTTP first.
"""
import argparse, asyncio, functools, json, shutil, subprocess, sys, tempfile, wave
@@ -79,10 +80,13 @@ async def main(a):
await pg.goto(base + '?beat=0&t=0'); await pg.wait_for_timeout(500)
if errs:
sys.exit(f'page errors: {errs}')
- info = await pg.evaluate("""({lang: CHAPTER.language || 'en', beats: BEATS.map(b => ({id: b.id, ask: !!b.ask, pre: b.pre || 0})),
+ info = await pg.evaluate("""({lang: CHAPTER.language || 'en', film: !!CHAPTER.film,
+ beats: BEATS.map(b => ({id: b.id, ask: !!b.ask, pre: b.pre || 0, hold: b.hold ?? 1.5})),
bgm: typeof BGM === 'undefined' ? [] : BGM.map(m => ({src: m.a.src, beats: m.beats, gain: m.gain, fade: m.fade ?? .8})),
upInPre: window.MUSIC_UP_IN_PRE || false})""")
- beats = [(i, x['id']) for i, x in enumerate(info['beats']) if not x['ask']]
+ # a film's question beats (CHAPTER.film: the reader acts, e.g. fires) stay in the video: their picture holds `hold` s
+ # (BEATS[i].hold, 1.5 by default) where the page waits for the reader, then the next beat plays as if they had acted
+ beats = [(i, x['id']) for i, x in enumerate(info['beats']) if not x['ask'] or info['film']]
info['sfx'] = []
asks = len(beats) < len(info['beats'])
box = await pg.locator('#stage').bounding_box()
@@ -95,6 +99,7 @@ async def main(a):
for i, bid in beats:
await open_beat(pg, base, i)
end, T = await pg.evaluate('[P.end, TIMINGS[BEATS[P.i].id]]')
+ if info['beats'][i]['ask']: end += info['beats'][i]['hold']
# a scene lists its sfx() when its beat is built, so they are collected beat by beat
for e in await pg.evaluate(SFX_JS):
if e not in info['sfx']:
diff --git a/README.md b/README.md
index 7e035a7..182bac4 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.2`. 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.3`. 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.js b/engine/engine.js
index c4fd1d0..bdd729c 100644
--- a/engine/engine.js
+++ b/engine/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/elementary-algebra/lib/engine.js b/site/elementary-algebra/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/elementary-algebra/lib/engine.js
+++ b/site/elementary-algebra/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/joke-collection/lib/engine.js b/site/joke-collection/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/joke-collection/lib/engine.js
+++ b/site/joke-collection/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/little-red-riding-hood/lib/engine.js b/site/little-red-riding-hood/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/little-red-riding-hood/lib/engine.js
+++ b/site/little-red-riding-hood/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/madness/lib/engine.js b/site/madness/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/madness/lib/engine.js
+++ b/site/madness/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/math-notebook/lib/engine.js b/site/math-notebook/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/math-notebook/lib/engine.js
+++ b/site/math-notebook/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/physics-notebook/lib/engine.js b/site/physics-notebook/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/physics-notebook/lib/engine.js
+++ b/site/physics-notebook/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/science/lib/engine.js b/site/science/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/science/lib/engine.js
+++ b/site/science/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));
diff --git a/site/three-little-pigs/lib/engine.js b/site/three-little-pigs/lib/engine.js
index c4fd1d0..bdd729c 100644
--- a/site/three-little-pigs/lib/engine.js
+++ b/site/three-little-pigs/lib/engine.js
@@ -1210,7 +1210,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 (!SND.ctx && window.AudioContext && !BEATS.some(b => b.ask)) {
+ 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();
SND.out = SND.ctx.createGain();
@@ -1228,6 +1228,8 @@ function soundUnlock() {
for (const type of ['pointerdown', 'touchend', 'click', 'keydown']) addEventListener(type, soundUnlock, true);
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
+ const wait = P.started && P.playing && P.waiting && !P.done && !!CHAPTER.film;
const held = P.started && !P.playing && !P.done, ctx = SND.ctx;
if (ctx?.state === 'running' && held) ctx.suspend();
else if (ctx && ctx.state !== 'running' && ctx.state !== 'closed' && !held) ctx.resume().catch(() => {});
@@ -1250,9 +1252,11 @@ function soundTick(dt) {
if (!SND.bgm.length) return;
// 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, (P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
+ if (!held) SND.duck += Math.max(-dt, Math.min(dt, (wait || P.t < pre && (up === true || up?.includes?.(id)) ? .7 : .3) - SND.duck));
for (const m of SND.bgm) {
- const k = m.beats.indexOf(id), current = on && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ const k = m.beats.indexOf(id), current = (on || wait) && k >= 0 && !window.MUSIC_OFF?.[id]?.(P.t);
+ if (jump) m.off = 0; // a seek or a replay puts every track back where the film's own timing says
+ if (wait && current) m.off = (m.off || 0) + dt * P.rate; // the time it ran on while the reader decided
m.cur = current;
if (!held) m.v = m.fade > 0 ? Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade)) : +current;
const g = m.v * m.gain * SND.duck;
@@ -1260,7 +1264,7 @@ function soundTick(dt) {
if (current) {
// where the film is: the beats before this one in the track as they really ran (P.end), plus now
const dur = m.a.duration || Infinity;
- const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t) % dur;
+ const want = (m.beats.slice(0, k).reduce((s, b) => s + (SND.len[BEATS.findIndex(x => x.id === b)] || 0), 0) + P.t + (m.off || 0)) % dur;
let d = m.a.currentTime - want;
if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;
// a start, a jump, or more than a second out once the clock runs again: seek. Less: ease back while the clock runs,
@@ -1280,7 +1284,7 @@ function soundTick(dt) {
if (m.v === 0 && !m.a.paused) m.a.pause();
}
}
- if (!on && !P.done) for (const m of SND.bgm) m.a.pause();
+ if (!on && !wait && !P.done) for (const m of SND.bgm) m.a.pause();
}
/* ---------- player ---------- */
@@ -1344,7 +1348,7 @@ function seek(i, play = P.playing) {
// Jump to a point inside beat i (frac of its narration); question beats always start at their beginning.
function seekInto(i, frac) {
seek(i, true);
- const pre = BEATS[i].pre || 0, t = BEATS[i].ask ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur);
+ const pre = BEATS[i].pre || 0, t = BEATS[i].ask && !CHAPTER.film ? 0 : frac * (pre + TIMINGS[BEATS[i].id].dur); // a film's question beats seek like any other
if (t < .3) return;
P.t = t;
evalTo(t);
@@ -1626,7 +1630,7 @@ function boot() {
$('bHome').href = `../#ch${String(CHAPTER.number).padStart(2, '0')}`;
progress(p => { p.last = CHAPTER.number; });
document.getElementById('stage').setAttribute('aria-label', translate('stageLabelTitled', { title: CHAPTER.title }));
- const film = !BEATS.some(b => b.ask); // a picture book: no questions, so no "lesson" or "quick checks" on its cover
+ const film = !!CHAPTER.film || !BEATS.some(b => b.ask); // a picture book or a film: no "lesson" or "quick checks" on its cover
if (film) { // the help lists no question keys and needs no column titles
$('coverGo').textContent = translate('startFilm');
const shift = [...$('help').querySelectorAll('dt')].find(e => e.textContent.startsWith('Shift'));