An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
fix(scripts): silent beats ({ dur }, no clip) in export, lint, music and check
yzr78b17186c17e
diff --git a/.claude/skills/papermorph/scripts/check.py b/.claude/skills/papermorph/scripts/check.pyindex 794cc23..a0b767f 100644--- a/.claude/skills/papermorph/scripts/check.py+++ b/.claude/skills/papermorph/scripts/check.py@@ -11,7 +11,8 @@ stall due to ReadPixels" (preserveDrawingBuffer, which the export needs) and thrThen, from the first browser, each beat's voice length (pre + its clip) against its real length (P.end). A beat runs toits last tween plus HOLD (0.6 s), so a tween or a clockRun past D lengthens it silently: the overrun past voice + HOLDis printed, and marked when over 1 s (a helper without the beat's D: clockRun(f, 0, { tail: true }) runs to the end-and never lengthens it). Mirrored beats meant to run alike show here too.+and never lengthens it). Mirrored beats meant to run alike show here too. A silent beat ({ dur: s }, no clip) counts+its dur as its voice and is labelled so."""import argparse, asyncio, re, sysfrom playwright.async_api import async_playwright@@ -34,7 +35,7 @@ async def chapter(pg, cur, url):cur["beat"] = bidawait pg.goto(f"{url}?beat={i}&t=0"); await pg.wait_for_timeout(300)try:- end, pre, dur = await pg.evaluate("[P.end, BEATS[P.i].pre || 0, TIMINGS[BEATS[P.i].id].dur]")+ end, pre, dur, quiet = await pg.evaluate("[P.end, BEATS[P.i].pre || 0, TIMINGS[BEATS[P.i].id].dur, !!TIMINGS[BEATS[P.i].id].silent]")except Exception as e:cur["errs"].append((bid, str(e).splitlines()[0])); continuefor t in (end / 2, end - .05):@@ -42,7 +43,7 @@ async def chapter(pg, cur, url):await pg.evaluate(f"P.t = {t}; evalTo({t})")except Exception as e:cur["errs"].append((f"{bid} t={t:.2f}", str(e).splitlines()[0]))- rows.append((bid, pre + dur, end))+ rows.append((bid + (" (silent)" if quiet else ""), pre + dur, end))return rowsdiff --git a/.claude/skills/papermorph/scripts/export.py b/.claude/skills/papermorph/scripts/export.pyindex d1c8cdd..6d870b8 100644--- a/.claude/skills/papermorph/scripts/export.py+++ b/.claude/skills/papermorph/scripts/export.py@@ -113,10 +113,11 @@ async def main(a):video, part = out / f'{a.chapter}.mp4', out / f'{a.chapter}.tmp.mp4' # renamed to video when the encoder is done# Pass 1: the sound track and the captions, beat by beat at the beat's length on screen.end_s = END_SECONDS if asks or a.link else 0 # a picture book ends on its own last scene- lengths, starts, counts, music_off, cues, offset, waits = [], [], [], [], [], 0.0, {}+ lengths, starts, counts, music_off, cues, offset, waits, silent = [], [], [], [], [], 0.0, {}, set()for i, bid in beats:await open_beat(pg, base, i)end, T = await pg.evaluate('[P.end, TIMINGS[BEATS[P.i].id]]')+ if T.get('silent'): silent.add(bid) # { dur } in the beat's fourth slot: no clip, its sound is silenceif info['beats'][i]['ask']: waits[bid] = end; end += info['beats'][i]['hold'] # from P.end the page waits# a scene lists its sfx() when its beat is built, so they are collected beat by beatfor e in await pg.evaluate(SFX_JS):@@ -136,7 +137,7 @@ async def main(a):track = np.zeros((round(total * RATE) + 1, 2), np.float32)for k, (i, bid) in enumerate(beats):o, pre = starts[k], info['beats'][i]['pre']- put(track, o + pre, decode(f'{a.url}{a.chapter}/audio/{info["lang"]}/{bid}.mp3'), 1, o + lengths[k])+ if bid not in silent: put(track, o + pre, decode(f'{a.url}{a.chapter}/audio/{info["lang"]}/{bid}.mp3'), 1, o + lengths[k])for e in info['sfx']: # effects sit at half volume under the voices; a new scene stops themif e['beat'] == bid and 0 <= e['at'] < lengths[k]:put(track, o + e['at'], decode(e['src']), .5 * e['vol'], o + lengths[k])diff --git a/.claude/skills/papermorph/scripts/lint.py b/.claude/skills/papermorph/scripts/lint.pyindex 20502d9..d8f11b3 100644--- a/.claude/skills/papermorph/scripts/lint.py+++ b/.claude/skills/papermorph/scripts/lint.py@@ -10,7 +10,8 @@ Errors (exit status 1):declared by the engine or another of its scripts; a second const/let/class stops the page script, asecond function silently replaces onemath the chapter uses a maths helper from lib/math.js but does not load it after engine.js- beat a beat id has no entry in audio/<lang>/timings.js or no MP3+ beat a beat id has no entry in audio/<lang>/timings.js or no MP3 (a silent beat, { dur: s } in its fourth slot,+ needs neither)mark m('name') is not a mark of any beatstale the narration's text differs from timings.js, or a narration [[mark]] is missing there: run tts.py againstyle style.json is not valid JSON or lacks schema, tier, book, words, look or voice@@ -115,6 +116,22 @@ def decls(text):return [(name, kind, code.count('\n', 0, at) + 1) for name, kind, at in sorted(out, key=lambda d: d[2])]+def slots(code, start):+ """The top-level slots of the array literal opening at code[start] ('['), in code with strings and comments blanked."""+ depth, out, last = 0, [], start + 1+ for k in range(start, len(code)):+ c = code[k]+ if c in '([{':+ depth += 1+ elif c in ')]}':+ depth -= 1+ if depth == 0:+ return out + [code[last:k]]+ elif c == ',' and depth == 1:+ out.append(code[last:k]); last = k + 1+ return out++def timings(path):t = path.read_text(encoding='utf-8')T = json.loads(t[t.index('{'):t.rindex('}') + 1])@@ -169,14 +186,18 @@ def lint_chapter(ch, content, out):# beats, audio, markslang = (re.search(r"""language:\s*['"](\w+)['"]""", inline) or [None, 'en'])[1]tpath = ch / 'audio' / lang / 'timings.js'- if not tpath.exists():- return err('beat', f'no audio/{lang}/timings.js')- T = timings(tpath)starts = [(m.start(), m.group(1)) for m in BEAT.finditer(inline)]ids = [b for _, b in starts]+ code = depth0(inline)[0]+ silent = {b for pos, b in starts if len(s := slots(code, pos)) > 3 and re.search(r'\bdur\s*:', s[3])} # { dur: s }: no clip+ if not tpath.exists() and not (ids and set(ids) <= silent):+ return err('beat', f'no audio/{lang}/timings.js')+ T = timings(tpath) if tpath.exists() else {}if not ids:warn('beat', "no beats recognised (expected ['id', 'title', (m, D) => …]); marks were not checked")for b in ids:+ if b in silent and b not in T:+ continueif b not in T:err('beat', f'beat {b} has no timings')elif not (ch / 'audio' / lang / f'{b}.mp3').exists():diff --git a/.claude/skills/papermorph/scripts/music.py b/.claude/skills/papermorph/scripts/music.pyindex 223067c..81ce238 100644--- a/.claude/skills/papermorph/scripts/music.py+++ b/.claude/skills/papermorph/scripts/music.py@@ -51,7 +51,7 @@ def chapter_url(url, ch):async def timeline(url):- """[(beat id, P.end, pre)] for every beat, the chapter's language and its first music() file, gain and beat count."""+ """[(beat id, P.end, pre, silent)] for every beat (silent: { dur }, no clip), the chapter's language and its first music() file, gain and beat count."""from playwright.async_api import async_playwrightanswers(url)async with async_playwright() as p:@@ -65,7 +65,7 @@ async def timeline(url):beats = []for i in range(n):await pg.goto(f"{url}?beat={i}&t=0"); await pg.wait_for_timeout(300)- beats.append(tuple(await ask("[BEATS[P.i].id, P.end, BEATS[P.i].pre || 0]")))+ beats.append(tuple(await ask("[BEATS[P.i].id, P.end, BEATS[P.i].pre || 0, !!TIMINGS[BEATS[P.i].id].silent]")))await b.close()if errs: print(f"page errors: {'; '.join(sorted(set(errs))[:3])}", file=sys.stderr)return beats, lang, bgm@@ -112,7 +112,7 @@ def spans(end, at, rests):def cut(a):if a.length is None:beats, _, _ = asyncio.run(timeline(a.url))- a.length = sum(e for _, e, _ in beats)+ a.length = sum(b[1] for b in beats)end, ends = (a.end, None) if a.end is not None else (last_note(a.src), sound_end(a.src))at = a.length - a.shift # where the last note falls in the cutfound = silences(a.src, a.rests, .25, max(0, end - at - 120), at + 120) if a.rests is not None else [] # ponytail: rests up to 2 min before the in-point, enough for a few short ones@@ -155,9 +155,10 @@ def momentary(src, gain=1.0):def gaps(a):import numpy as npbeats, lang, bgm = asyncio.run(timeline(a.url))- L, SR, hop, w = sum(e for _, e, _ in beats), 16000, 800, 6400 # 400 ms windows every 50 ms+ L, SR, hop, w = sum(b[1] for b in beats), 16000, 800, 6400 # 400 ms windows every 50 msvoice, t0 = np.zeros(int((L + 1) * SR), np.float32), 0.0- for bid, end, pre in beats:+ for bid, end, pre, silent in beats:+ if silent: t0 += end; continue # no clip: all of it is a gapx = np.frombuffer(subprocess.run(["ffmpeg", "-v", "error", "-i", f"{a.url}audio/{lang}/{bid}.mp3", "-ac", "1", "-ar", str(SR),"-f", "f32le", "-"], capture_output=True, check=True).stdout, np.float32)i = int((t0 + pre) * SR); x = x[:max(0, min(len(x), int((t0 + end) * SR) - i))]; voice[i:i + len(x)] += x; t0 += end