An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
feat(engine): engine-v2.2, sound effects and music in the engine
yzr74f39fee6e3c
diff --git a/.claude/skills/papermorph/SKILL.md b/.claude/skills/papermorph/SKILL.mdindex fa827f4..ca9c5af 100644--- a/.claude/skills/papermorph/SKILL.md+++ b/.claude/skills/papermorph/SKILL.md@@ -47,7 +47,7 @@ site/<book>/ the static book (only this is delivered)chNN/index.html, audio/<lang>/ a lesson, its beat MP3s + timings.js```-A picture book adds `books/<book>/story.md` (the user's telling, verbatim), `storyboard.md` and `art/` (`sets.js` with the set sketches, generated pictures, `painted.json`, `costs.tsv`); its `site/<book>/lib/` holds the paper kit (`paper.js`, `cast.js`, `shared.js` from `assets/templates/picture-book/`, which needs engine-v2.1 or later) and `img/` the baked sets, props and puppet pieces; each episode `chNN/` has one script per scene and `audio/<lang>/`, `audio/sfx/`, `audio/bgm/`.+A picture book adds `books/<book>/story.md` (the user's telling, verbatim), `storyboard.md` and `art/` (`sets.js` with the set sketches, generated pictures, `painted.json`, `costs.tsv`); its `site/<book>/lib/` holds the paper kit (`paper.js`, `cast.js`, `shared.js` from `assets/templates/picture-book/`, which needs engine-v2.2 or later: `sfx()` and `music()` are in the engine) and `img/` the baked sets, props and puppet pieces; each episode `chNN/` has one script per scene and `audio/<lang>/`, `audio/sfx/`, `audio/bgm/`.`SKILL` means this skill's absolute folder; `<book>` is the book slug, `<lang>` its primary language code. Needs: `uv`, `ffmpeg`/`ffprobe`, Edge TTS network access (or Kokoro, local: `uv run --python 3.12 --with kokoro`), github.com for `engine.py` when the repository's `engine/` folder is not at hand, and Playwright browsers (`uv run --with playwright playwright install chromium firefox`; where Playwright's Chromium does not run, set `CHROME` to an installed Chrome, Chromium, Brave or Edge). Preview: `python3 SKILL/scripts/serve.py 8765 site` (no browser caching).diff --git a/.claude/skills/papermorph/assets/templates/picture-book/chapter.html b/.claude/skills/papermorph/assets/templates/picture-book/chapter.htmlindex 12851f0..3ed93ab 100644--- a/.claude/skills/papermorph/assets/templates/picture-book/chapter.html+++ b/.claude/skills/papermorph/assets/templates/picture-book/chapter.html@@ -34,36 +34,11 @@ const BEATS = [].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));/* ---------- background music ----------- One track per listed scene group: [file in audio/bgm/ without .mp3, [scene ids], gain]. A track's position is the- time since its first scene began, so pausing and seeking keep it in step; 0.8 s crossfades between tracks; the music- ducks to 30 % while anyone speaks and sits at 70 % otherwise. Leave BGM empty for a silent film; a scene can mute its- music for a moment with window.MUSIC_OFF = { sceneId: t => boolean }. */-const BGM = [/* ['theme', ['scene1', 'scene2'], .3] */]- .map(([n, beats, gain]) => ({ n, beats, gain, a: Object.assign(new Audio(`audio/bgm/${n}.mp3`), { preload: 'auto', loop: true, volume: 0 }), v: 0 }));-const beatLen = id => { const b = BEATS.find(x => x.id === id); return (b.pre || 0) + TIMINGS[id].dur + .6; };-let lastTick = performance.now(), duck = .3;-function bgmTick(now) {- const dt = Math.min(.1, (now - lastTick) / 1000); lastTick = now;- const id = BEATS[P.i]?.id, on = P.started && P.playing && !P.waiting && !P.done;- // the music ducks well under the voices for the whole of a scene that has them (its silent opening and tail too, so- // it does not swell between lines), easing over about half a second- duck += Math.max(-dt, Math.min(dt, (P.audio ? .3 : .7) - duck));- const vol = +(document.getElementById('volume')?.value ?? 1) * duck;- for (const m of BGM) {- const k = m.beats.indexOf(id), current = on && k >= 0 && !(window.MUSIC_OFF?.[id]?.(P.t));- m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / .8));- m.a.volume = m.v * m.gain * vol;- if (m.a.playbackRate !== (P.rate || 1)) m.a.playbackRate = P.rate || 1; // in step at 0.75× and 1.5× too, no re-seeking- if (current) {- const want = (m.beats.slice(0, k).reduce((s, b) => s + beatLen(b), 0) + P.t) % (m.a.duration || 1e9);- if (Math.abs(m.a.currentTime - want) > .5) m.a.currentTime = want;- if (m.a.paused) m.a.play().catch(() => {});- } else if (m.v === 0 && !m.a.paused) m.a.pause();- }- if (!on && !P.done) for (const m of BGM) m.a.pause();- requestAnimationFrame(bgmTick);-}-requestAnimationFrame(bgmTick);+ The engine's music(file, [scene ids], gain, fade): one looping track per scene group, placed by the time since its+ first scene began, so pausing and seeking keep it in step; 0.8 s crossfades between tracks (or `fade` s); ducked to+ 30 % under the voices. Leave it out for a silent film; a scene can mute its music for a moment with+ window.MUSIC_OFF = { sceneId: t => boolean }. */+// music('audio/bgm/theme.mp3', ['scene1', 'scene2'], .3);// Build every puppet and prop once at load, off screen, so their bitmaps are ready before the first beat needs them.paperWarm(g => { kidPuppet(g); });boot();diff --git a/.claude/skills/papermorph/assets/templates/picture-book/lib/shared.js b/.claude/skills/papermorph/assets/templates/picture-book/lib/shared.jsindex f514505..c280127 100644--- a/.claude/skills/papermorph/assets/templates/picture-book/lib/shared.js+++ b/.claude/skills/papermorph/assets/templates/picture-book/lib/shared.js@@ -17,43 +17,3 @@ const chew = q => onTwos(q) > 0 ? 'chew' : 'A';/* ---------- painted props (img/props/<name>.webp, from painted.py) ---------- */// A picture at (x, y), w by h, to hold in a puppet's hand (holds: { key: svg }) or drop into prop().const artSVG = (name, x, y, w, h) => `<image href="../img/props/${name}.webp" x="${x}" y="${y}" width="${w}" height="${h}"/>`;--/* ---------- sound effects ----------- sfx(beat, at, name, vol): play audio/sfx/<name>.mp3 (SKILL/scripts/sfx.py) when the clock of beat `beat` passes `at`- in normal playback (seeking past it does not fire it). Pausing holds them; a seek stops them. They play at half volume under the voices. */-const SFX = [], sfxOn = [];-const SFX_BED = {}; // file name -> length in s, for long ambience called once (a seek into the middle starts it part-way)-// A scene function runs again at every seek, replay and Home: the same effect (beat, time, name) is listed once.-const sfx = (beat, at, name, vol = 1, bed = SFX_BED[name] || 0) => {- const e = SFX.find(x => x.beat === beat && x.at === at && x.name === name);- if (e) Object.assign(e, { vol, bed }); else SFX.push({ beat, at, name, vol, bed });-};-const sfxVolume = () => Math.min(1, Math.max(0, +(document.getElementById('volume')?.value ?? 1)));-let sfxLast = { i: -1, t: 0 };-function sfxTick() {- const id = BEATS[P.i]?.id, on = P.started && P.playing && !P.waiting && !P.done;- // the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or a new scene,- // and what was playing belongs to another moment- const same = sfxLast.i === P.i && P.t >= sfxLast.t && P.t - Math.max(0, sfxLast.t) < .5;- if (!same) while (sfxOn.length) sfxOn.pop().pause();- // a scene that has just begun (or begins again) fires its effects from 0 on, those at 0 too; a seek fires nothing it skipped- const from = same ? sfxLast.t : P.t < .25 ? -1 : P.t;- const seeked = !same || sfxLast.pend; // pend: a seek that happened while paused, now playing- if (on)- for (const e of SFX) if (e.beat === id && (e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) {- const a = new Audio(`audio/sfx/${e.name}.mp3`);- a._vol = Math.min(1, Math.max(0, .5 * e.vol)); // effects sit under the voices (vol 2 is full volume); the slider is applied every tick below- a.volume = a._vol * sfxVolume();- a.playbackRate = P.rate || 1;- a.play().catch(() => {}); sfxOn.push(a); a.onended = () => sfxOn.splice(sfxOn.indexOf(a), 1);- if (e.at <= from || e.at > P.t) a.addEventListener('loadedmetadata', () => { a.currentTime = P.t - e.at; }, { once: true }); // a long bed joined mid-way- }- const v = sfxVolume();- for (const a of sfxOn) { a.playbackRate = P.rate || 1; if (a.volume !== a._vol * v) a.volume = a._vol * v; // the slider reaches effects and beds already playing- if (on === a.paused && !a.ended) on ? a.play().catch(() => {}) : a.pause(); } // a pause holds them, play resumes- // the music follows the playback speed too (BGM is the chapter's own list, when it has one)- if (typeof BGM !== 'undefined') for (const m of BGM) if (m.a.playbackRate !== (P.rate || 1)) m.a.playbackRate = P.rate || 1;- sfxLast = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!sfxLast.pend) };- requestAnimationFrame(sfxTick);-}-addEventListener('load', () => requestAnimationFrame(sfxTick)); // after the chapter's own scripts define BEATSdiff --git a/.claude/skills/papermorph/references/engine.md b/.claude/skills/papermorph/references/engine.mdindex 74abe55..96609f1 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.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.1`; what an older copy lacks is marked *(v2.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.+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.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,6 +109,8 @@ 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)*. `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). 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 % under the voice, or 70 % in the music-only `pre` of the beats in `window.MUSIC_UP_IN_PRE` (`true` for all), crossfades over `fade` s (0.8), 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 click or key, so ducks, fades and the volume slider work on phones too; a pause holds them and a seek stops the effects. `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.Treat saved progress as optional: malformed JSON, `null` or a non-object value starts with empty progress; only an array of positive integer chapter numbers is used for completion.diff --git a/.claude/skills/papermorph/references/sound.md b/.claude/skills/papermorph/references/sound.mdindex be5b175..1eaf284 100644--- a/.claude/skills/papermorph/references/sound.md+++ b/.claude/skills/papermorph/references/sound.md@@ -16,13 +16,13 @@ Voices carry the story; everything else sits under them. In early cuts the music`sfx.py` synthesizes effects as small MP3s, free and consistent (knock, door, creak, bell, chop, crash, coin, rain, wind, room tone, surf, gulls, crickets, footsteps (`patter`, `step1`), a dog and more; `--list` names them all). A sound the list lacks goes in the book's own `books/<book>/sfx_recipes.py`, passed with `--recipes`: each top-level function without required arguments is a recipe named after it, returning samples at `sfx.SR`, built from sfx's own helpers, its docstring's first line shown by `--list` (the script's header has the details). Keep them soft and natural: the first rain was a high hiss and read as scary; pink noise with patter and taps on umbrellas read as rain.-- Place an effect on the scene clock with `sfx(scene, t, name, volume)`; it plays at half volume under the voices, a pause holds it and the next scene stops it.+- Place an effect on the scene clock with the engine's `sfx(scene, t, name, volume)`; it plays at half volume under the voices, a pause holds it and the next scene stops it.- Cut an effect to its shot: a six-second rain under a one-second shot spills into the next.- A long bed (wind, room tone, the 10 s `birds` of a park) is listed in `SFX_BED` with its length (`SFX_BED.birds = 10`), so a seek into the middle joins it part-way; a bed that loops overlaps its seams by about half a second, and room tone runs under the silences too, where its absence is heard first.## Music-Music is optional and goes under everything: about 70 % of its own level, and 30 % for the whole of a scene that has voices (its silent opening and tail too, so it does not swell between lines), fading in and out over most of a second. Give a scene silence when silence is the joke: `window.MUSIC_OFF = { scene: t => … }` fades it out for a stare. Don't let music stop dead in the middle of a scene; carry it to the end or fade it, and bring an earlier theme back when the story returns to an earlier place. Use tracks the book may redistribute (generated for the book, or openly licensed) and credit them.+Music is optional and goes under everything: `music('audio/bgm/theme.mp3', [scene ids], gain, fade)` in the chapter page, one call per track. It sits at 30 % of its own level for the whole of a scene with voices (its silent opening and tail too, so it does not swell between lines); `window.MUSIC_UP_IN_PRE = ['scene']` lifts it to 70 % in that scene's music-only opening. Tracks crossfade over `fade` seconds (0.8 by default). Give a scene silence when silence is the joke: `window.MUSIC_OFF = { scene: t => … }` fades it out for a stare. Don't let music stop dead in the middle of a scene; carry it to the end or fade it, and bring an earlier theme back when the story returns to an earlier place. Use tracks the book may redistribute (generated for the book, or openly licensed) and credit them.## Captionsdiff --git a/.claude/skills/papermorph/scripts/engine.py b/.claude/skills/papermorph/scripts/engine.pyindex 038bb2a..a435666 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.requestfrom pathlib import Path-DEFAULT = 'DozenTwelve/[email protected]' # follows the latest compatible release+DEFAULT = 'DozenTwelve/[email protected]' # follows the latest compatible releaseOWN = 'book.css'LATEX = {'vendor/mathjax/tex-svg-full.js': 'mathjax.js', 'vendor/mathjax/LICENSE': 'mathjax-LICENSE.txt'}diff --git a/.claude/skills/papermorph/scripts/playlog.py b/.claude/skills/papermorph/scripts/playlog.pyindex 0a0dcd8..166b243 100644--- a/.claude/skills/papermorph/scripts/playlog.py+++ b/.claude/skills/papermorph/scripts/playlog.py@@ -41,6 +41,12 @@ HTMLMediaElement.prototype.play = function () {} catch {}return play0.apply(this, arguments);};+// the engine's effects play through Web Audio, not an element+const start0 = AudioBufferSourceNode.prototype.start;+AudioBufferSourceNode.prototype.start = function () {+ try { if (this._sfx && P.playing) __log.push({ kind: 'sfx', what: this._sfx, i: P.i, t: P.t, vol: this._vol }); } catch {}+ return start0.apply(this, arguments);+};addEventListener('DOMContentLoaded', () => {const c = document.getElementById('caption'); if (!c) return;let last = '';diff --git a/engine/engine.js b/engine/engine.jsindex 6ad6d52..8fe15c8 100644--- a/engine/engine.js+++ b/engine/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/elementary-algebra/lib/engine.js b/site/elementary-algebra/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/elementary-algebra/lib/engine.js+++ b/site/elementary-algebra/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/joke-collection/lib/engine.js b/site/joke-collection/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/joke-collection/lib/engine.js+++ b/site/joke-collection/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/little-red-riding-hood/lib/engine.js b/site/little-red-riding-hood/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/little-red-riding-hood/lib/engine.js+++ b/site/little-red-riding-hood/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/madness/ch01/index.html b/site/madness/ch01/index.htmlindex 760a02d..783209e 100644--- a/site/madness/ch01/index.html+++ b/site/madness/ch01/index.html@@ -799,7 +799,7 @@ const BEATS = [/* ---------- music ----------Public-domain recordings (books/madness/music/SOURCES.tsv), one per part of the film, loudness-matched (-20 LUFS).- Each plays through its beats from where it is; a 3 s crossfade at each change of part; ducked to 30 % under the voice+ The engine's music(): a 3 s crossfade at each change of part; ducked to 30 % under the voiceand up to 70 % in the opening's music-only lead-in. export.py mixes it by the same rules (fade, MUSIC_UP_IN_PRE). */window.MUSIC_UP_IN_PRE = ['open']; // only the title's lead-in; the fool speaks in another beat's lead-inconst PARTS = [@@ -811,33 +811,7 @@ const PARTS = [['m6-mozart-40', ['pinel', 'painting', 'tuke', 'triumph', 'turn', 'watch2', 'tea', 'moral', 'anti'], .5],['m7-satie', ['silenced', 'few', 'goya', 'nietzsche', 'vangogh', 'work', 'question', 'pascal2', 'last'], .55],];-const BGM = PARTS.map(([n, beats, gain]) => ({ n, beats, gain, fade: 3,- a: Object.assign(new Audio(`audio/bgm/${n}.mp3`), { preload: 'auto', volume: 0 }), v: 0 }));-// where the track should be: the beats before this one in its part (picture before the line, narration, hold) plus now.-// Only used after a jump; in plain playback the track runs on by itself, so beats that hold longer never make it skip.-const beatLen = id => { const b = BEATS.find(x => x.id === id); return (b.pre || 0) + TIMINGS[id].dur + HOLD; };-let lastTick = performance.now(), duck = .3, was = { i: -1, t: 0 };-function bgmTick(now) {- const dt = Math.min(.1, (now - lastTick) / 1000); lastTick = now;- const id = BEATS[P.i]?.id, on = P.started && P.playing && !P.waiting && !P.done;- const flowing = (P.i === was.i && P.t >= was.t && P.t - was.t < .5) || (P.i === was.i + 1 && P.t < .5);- was = { i: P.i, t: P.t };- duck += Math.max(-dt, Math.min(dt, (P.t < (BEATS[P.i]?.pre || 0) && MUSIC_UP_IN_PRE.includes(id) ? .7 : .3) - duck));- const vol = +(document.getElementById('volume')?.value ?? 1) * duck;- for (const m of BGM) {- const k = m.beats.indexOf(id), current = on && k >= 0;- m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));- m.a.volume = Math.min(1, m.v * m.gain * vol);- if (current) {- const want = (m.beats.slice(0, k).reduce((s, b) => s + beatLen(b), 0) + P.t) % (m.a.duration || 1e9);- if (!flowing && Math.abs(m.a.currentTime - want) > .5) m.a.currentTime = want;- if (m.a.paused) m.a.play().catch(() => {});- } else if (m.v === 0 && !m.a.paused) m.a.pause();- }- if (!on && !P.done) for (const m of BGM) m.a.pause();- requestAnimationFrame(bgmTick);-}-requestAnimationFrame(bgmTick);+PARTS.forEach(([n, beats, gain]) => music(`audio/bgm/${n}.mp3`, beats, gain, 3));// the zoomed print's centre in Hogarth's plate 8 so the two ladies sit mid-frame (at zoom 2.6) and the print fills itconst HOG = [-220, 203];diff --git a/site/madness/lib/engine.js b/site/madness/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/madness/lib/engine.js+++ b/site/madness/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/madness/lib/shared.js b/site/madness/lib/shared.jsindex f514505..c280127 100644--- a/site/madness/lib/shared.js+++ b/site/madness/lib/shared.js@@ -17,43 +17,3 @@ const chew = q => onTwos(q) > 0 ? 'chew' : 'A';/* ---------- painted props (img/props/<name>.webp, from painted.py) ---------- */// A picture at (x, y), w by h, to hold in a puppet's hand (holds: { key: svg }) or drop into prop().const artSVG = (name, x, y, w, h) => `<image href="../img/props/${name}.webp" x="${x}" y="${y}" width="${w}" height="${h}"/>`;--/* ---------- sound effects ----------- sfx(beat, at, name, vol): play audio/sfx/<name>.mp3 (SKILL/scripts/sfx.py) when the clock of beat `beat` passes `at`- in normal playback (seeking past it does not fire it). Pausing holds them; a seek stops them. They play at half volume under the voices. */-const SFX = [], sfxOn = [];-const SFX_BED = {}; // file name -> length in s, for long ambience called once (a seek into the middle starts it part-way)-// A scene function runs again at every seek, replay and Home: the same effect (beat, time, name) is listed once.-const sfx = (beat, at, name, vol = 1, bed = SFX_BED[name] || 0) => {- const e = SFX.find(x => x.beat === beat && x.at === at && x.name === name);- if (e) Object.assign(e, { vol, bed }); else SFX.push({ beat, at, name, vol, bed });-};-const sfxVolume = () => Math.min(1, Math.max(0, +(document.getElementById('volume')?.value ?? 1)));-let sfxLast = { i: -1, t: 0 };-function sfxTick() {- const id = BEATS[P.i]?.id, on = P.started && P.playing && !P.waiting && !P.done;- // the clock ran on (or stood still, paused) in the same beat; anything else is a seek, a replay or a new scene,- // and what was playing belongs to another moment- const same = sfxLast.i === P.i && P.t >= sfxLast.t && P.t - Math.max(0, sfxLast.t) < .5;- if (!same) while (sfxOn.length) sfxOn.pop().pause();- // a scene that has just begun (or begins again) fires its effects from 0 on, those at 0 too; a seek fires nothing it skipped- const from = same ? sfxLast.t : P.t < .25 ? -1 : P.t;- const seeked = !same || sfxLast.pend; // pend: a seek that happened while paused, now playing- if (on)- for (const e of SFX) if (e.beat === id && (e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) {- const a = new Audio(`audio/sfx/${e.name}.mp3`);- a._vol = Math.min(1, Math.max(0, .5 * e.vol)); // effects sit under the voices (vol 2 is full volume); the slider is applied every tick below- a.volume = a._vol * sfxVolume();- a.playbackRate = P.rate || 1;- a.play().catch(() => {}); sfxOn.push(a); a.onended = () => sfxOn.splice(sfxOn.indexOf(a), 1);- if (e.at <= from || e.at > P.t) a.addEventListener('loadedmetadata', () => { a.currentTime = P.t - e.at; }, { once: true }); // a long bed joined mid-way- }- const v = sfxVolume();- for (const a of sfxOn) { a.playbackRate = P.rate || 1; if (a.volume !== a._vol * v) a.volume = a._vol * v; // the slider reaches effects and beds already playing- if (on === a.paused && !a.ended) on ? a.play().catch(() => {}) : a.pause(); } // a pause holds them, play resumes- // the music follows the playback speed too (BGM is the chapter's own list, when it has one)- if (typeof BGM !== 'undefined') for (const m of BGM) if (m.a.playbackRate !== (P.rate || 1)) m.a.playbackRate = P.rate || 1;- sfxLast = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!sfxLast.pend) };- requestAnimationFrame(sfxTick);-}-addEventListener('load', () => requestAnimationFrame(sfxTick)); // after the chapter's own scripts define BEATSdiff --git a/site/math-notebook/lib/engine.js b/site/math-notebook/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/math-notebook/lib/engine.js+++ b/site/math-notebook/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/physics-notebook/lib/engine.js b/site/physics-notebook/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/physics-notebook/lib/engine.js+++ b/site/physics-notebook/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/science/lib/engine.js b/site/science/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/science/lib/engine.js+++ b/site/science/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);diff --git a/site/three-little-pigs/lib/engine.js b/site/three-little-pigs/lib/engine.jsindex 6ad6d52..8fe15c8 100644--- a/site/three-little-pigs/lib/engine.js+++ b/site/three-little-pigs/lib/engine.js@@ -1162,6 +1162,104 @@ const blanks = rowsIn => (body, api) => {};};+/* ---------- sound effects and music ----------+ sfx(beat, at, name, vol, bed): play audio/sfx/<name>.mp3 when the clock of beat `beat` passes `at` in normal playback+ (a seek past it fires nothing), at half volume (vol 2 is full). A long ambience is listed in SFX_BED with its length+ (SFX_BED.birds = 10): a seek into it joins it part-way. A beat function runs again at every seek; an effect is listed once.+ music(file, beats, gain, fade): a looping track under the listed beats (all of them by default). Its place is the+ length of its earlier beats plus the clock. It ducks to 30 % under the voice, or 70 % in the music-only lead-in (`pre`)+ of the beats in window.MUSIC_UP_IN_PRE (true: every beat). It crossfades over `fade` s, and+ window.MUSIC_OFF = { beat: t => true } fades it out for a moment.+ export.py reads SFX, BGM and these settings, and mixes the film by the same rules.+ Effects and music go through one Web Audio graph: ducks, fades and the volume slider work on phones too (iOS ignores+ an element's .volume), and effects are decoded before they are due. A pause holds everything; a seek stops the effects. */+const SND = { sfx: [], bed: {}, bgm: [], len: [], on: [], buf: {}, ctx: null, out: null, last: { i: -1, t: 0 }, duck: .3 };+// window properties, not declarations: a book with its own older sfx() or BGM declares them over these+Object.assign(window, { SFX: SND.sfx, SFX_BED: SND.bed, BGM: SND.bgm,+ sfx(beat, at, name, vol = 1, bed = SND.bed[name] || 0) {+ const e = SND.sfx.find(x => x.beat === beat && x.at === at && x.name === name);+ if (e) Object.assign(e, { vol, bed }); else SND.sfx.push({ beat, at, name, vol, bed });+ sfxLoad(name);+ },+ music(file, beats = BEATS.map(b => b.id), gain = 1, fade = .8) {+ const m = { n: file, beats, gain, fade, v: 0, a: Object.assign(new Audio(file), { preload: 'auto', loop: true, volume: 0 }) };+ SND.bgm.push(m);+ if (SND.ctx) bgmWire(m);+ },+});+function sfxLoad(name) {+ const s = SND.buf[name] ??= { data: fetch(`audio/sfx/${name}.mp3`).then(r => r.ok ? r.arrayBuffer() : null).catch(() => null) };+ if (SND.ctx && !s.decoded) s.decoded = s.data.then(d => d && SND.ctx.decodeAudioData(d)).then(b => { s.b = b; }, () => {});+}+function bgmWire(m) {+ m.g = SND.ctx.createGain();+ SND.ctx.createMediaElementSource(m.a).connect(m.g).connect(SND.out);+ m.a.volume = 1;+}+// 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 (navigator.audioSession) navigator.audioSession.type = 'playback'; // iOS: the silent switch must not mute the film+ SND.ctx = new AudioContext();+ SND.out = SND.ctx.createGain();+ SND.out.connect(SND.ctx.destination);+ for (const n in SND.buf) sfxLoad(n);+ for (const m of SND.bgm) bgmWire(m);+ }+ if (SND.ctx?.state === 'suspended') SND.ctx.resume();+ for (const m of SND.bgm) if (!m.blessed) { // iOS lets an element play later only if it once played inside a click+ m.blessed = true; m.a.muted = true;+ m.a.play().then(() => { m.a.pause(); m.a.muted = false; }, () => { m.a.muted = false; });+ }+}+addEventListener('pointerdown', soundUnlock, true);+addEventListener('keydown', 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;+ const held = P.started && !P.playing && !P.done, ctx = SND.ctx;+ if (ctx?.state === 'running' && held) ctx.suspend();+ else if (ctx?.state === 'suspended' && !held) ctx.resume();+ // 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;+ if (!same) while (SND.on.length) SND.on.pop().stop();+ // 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;+ if (on && ctx) for (const e of SND.sfx) {+ const b = SND.buf[e.name]?.b;+ if (!b || e.beat !== id || !(e.at > from && e.at <= P.t || seeked && e.bed && e.at < P.t && P.t < e.at + e.bed)) continue;+ const src = ctx.createBufferSource(), g = ctx.createGain();+ src.buffer = b; src._sfx = e.name; g.gain.value = src._vol = .5 * e.vol;+ src.connect(g).connect(SND.out);+ src.start(0, Math.max(0, P.t - e.at));+ SND.on.push(src);+ src.onended = () => { const k = SND.on.indexOf(src); if (k >= 0) SND.on.splice(k, 1); };+ }+ SND.last = { i: P.i, t: on ? P.t : from, pend: !on && (!same || !!L.pend) };+ if (!SND.bgm.length) return;+ const vol = +($('volume')?.value ?? 1), up = window.MUSIC_UP_IN_PRE;+ if (SND.out) SND.out.gain.value = vol;+ SND.duck += Math.max(-dt, Math.min(dt, (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);+ m.v = Math.max(0, Math.min(1, m.v + (current ? 1 : -1) * dt / m.fade));+ const g = m.v * m.gain * SND.duck;+ if (m.g) m.g.gain.value = g; else m.a.volume = Math.min(1, g * vol);+ 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;+ let d = m.a.currentTime - want;+ if (d > dur / 2) d -= dur; else if (d < -dur / 2) d += dur;+ // far off (a jump, a start): seek; a little off (a slow start, a stall): run up to 5 % faster or slower to catch up+ if (m.a.paused || Math.abs(d) > .3) { m.a.currentTime = want; d = 0; }+ const rate = P.rate * (Math.abs(d) > .03 ? 1 - Math.max(-.05, Math.min(.05, d)) : 1);+ if (Math.abs(m.a.playbackRate - rate) > .001) m.a.playbackRate = rate;+ if (m.a.paused) m.a.play().catch(() => {});+ } else if (m.v === 0 && !m.a.paused) m.a.pause();+ }+ if (!on && !P.done) for (const m of SND.bgm) m.a.pause();+}+/* ---------- player ---------- */const P = { i: 0, t: 0, end: 1, playing: false, waiting: false, done: false, started: false, audio: null, afail: false, stall: 0, cleanup: [], rate: 1, nextClip: null };function runBeat(i) {@@ -1178,7 +1276,7 @@ function runBeat(i) {const end = Math.max(pre + tm.dur, ...TW.map(w => w.t0 + w.dur)) + HOLD;const tail = TW.some(w => w.tail === true);for (const w of TW) if (w.tail === true || tail && w.tail === 'follow') w.dur = end - w.t0;- return end;+ return SND.len[i] = end; // the music places itself by how long each beat really runs}const HOLD = .6;// A beat that clears the picture must not leave it empty while the narration has started.@@ -1331,6 +1429,7 @@ function frame(now) {evalTo(P.t);if (P.t >= P.end) beatDone();}+ soundTick(dt);syncLinks();ui();requestAnimationFrame(frame);