Skip to content

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

fix(skill): review, bake and image scripts for picture books

yzraa25824833e5
diff --git a/.claude/skills/papermorph/scripts/bake_puppets.py b/.claude/skills/papermorph/scripts/bake_puppets.py
index 14a94ca..20a64c6 100644
--- a/.claude/skills/papermorph/scripts/bake_puppets.py
+++ b/.claude/skills/papermorph/scripts/bake_puppets.py
@@ -1,14 +1,17 @@
#!/usr/bin/env python3
"""Bake every paper piece a picture book uses (puppets and props, every scene of every chapter) to bitmaps.
- uv run --with playwright --with pillow python bake_puppets.py site/<book> [--url http://localhost:8790/site/<book>/] [--clean]
+ python3 SKILL/scripts/serve.py 8790 site &
+ uv run --with playwright --with pillow python bake_puppets.py site/<book> [--url http://localhost:8790/<book>/] [--clean]
The page would otherwise run the paper filter on each piece at startup. This opens every chNN of the book with ?bake
(which ignores existing bitmaps), seeks it to its last beat (which builds every scene before it, so every piece is asked
for once), and saves each piece to SITE/img/puppets/<hash>.webp, listed in baked.js (window.BAKED) that paper.js reads.
-Run it with a preview server up (python3 SKILL/scripts/serve.py 8790 site) after changing a puppet or prop drawing.
---url is the book's address as the server shows it (default http://localhost:8790/); files this run did not produce are
-kept (a piece another chapter or an earlier run made) unless --clean removes them first. Needs Playwright's Chromium.
+Run it after changing a puppet or prop drawing, or after adding a chapter. A piece is found by its drawing and its
+bounding box, and the box of text or of a rotated piece differs between browsers, so the pages are opened in Chromium
+and in Firefox and both sets are kept. Pieces already baked are not encoded again, and files this run did not produce
+are kept (a piece another chapter or an earlier run made); --clean (after changing the paper filter itself) re-encodes
+every piece and then removes the files no chapter asked for. --url defaults to the book under http://localhost:8790/.
"""
import argparse, base64, io, json, pathlib
from PIL import Image
@@ -24,32 +27,37 @@ READY = """async () => {
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
- ap.add_argument('site'); ap.add_argument('--url', default='http://localhost:8790/'); ap.add_argument('--clean', action='store_true')
+ ap.add_argument('site'); ap.add_argument('--url'); ap.add_argument('--clean', action='store_true')
a = ap.parse_args()
- site = pathlib.Path(a.site); out = site / 'img/puppets'; url = a.url.rstrip('/') + '/'
- got = []
+ site = pathlib.Path(a.site); out = site / 'img/puppets'
+ url = (a.url or f'http://localhost:8790/{site.resolve().name}/').rstrip('/') + '/'
+ got = {}
with sync_playwright() as p:
- b = p.chromium.launch(); pg = b.new_page(viewport={'width': 1600, 'height': 900})
- for ch in sorted(d.name for d in site.glob('ch[0-9][0-9]')):
- pg.goto(f'{url}{ch}/?bake', timeout=600000)
- last = pg.evaluate('BEATS.length - 1')
- pg.goto(f'{url}{ch}/?bake&beat={last}&t=0.1', timeout=600000)
- got += pg.evaluate(READY)
- print(ch, 'beat', last, len(got), 'pieces so far')
- b.close()
+ for kind in (p.chromium, p.firefox):
+ b = kind.launch(); pg = b.new_page(viewport={'width': 1600, 'height': 900})
+ for ch in sorted(d.name for d in site.glob('ch[0-9][0-9]')):
+ pg.goto(f'{url}{ch}/?bake', timeout=600000)
+ last = pg.evaluate('BEATS.length - 1')
+ pg.goto(f'{url}{ch}/?bake&beat={last}&t=0.1', timeout=600000)
+ got.update(pg.evaluate(READY))
+ print(kind.name, ch, 'beat', last, len(got), 'pieces so far', flush=True)
+ b.close()
out.mkdir(parents=True, exist_ok=True)
- if a.clean:
- for f in out.glob('*.webp'): f.unlink()
man_path = out / 'baked.js'
man = {}
if man_path.exists() and not a.clean:
man = {h: f for h, f in json.loads(man_path.read_text().split('=', 1)[1].rstrip().rstrip(';')).items() if (out / f).exists()}
- for h, data in got:
+ new = 0
+ for h, data in got.items():
f = out / f'{h}.webp'
+ if h in man and not a.clean: continue # the same key is the same drawing: re-encoding only adds noise
Image.open(io.BytesIO(base64.b64decode(data.split(',')[1]))).save(f, quality=88, method=6)
- man[h] = f.name
+ man[h] = f.name; new += 1
man_path.write_text('window.BAKED = ' + json.dumps(man) + ';\n')
- print(len(got), 'pieces baked,', len(man), 'listed,', sum((out / f).stat().st_size for f in man.values()) // 1024, 'KB')
+ if a.clean: # only now, so the site never lists a file that is gone
+ for f in out.glob('*.webp'):
+ if f.name not in man.values(): f.unlink()
+ print(len(got), 'pieces asked for,', new, 'encoded,', len(man), 'listed,', sum((out / f).stat().st_size for f in man.values()) // 1024, 'KB')
if __name__ == '__main__':
diff --git a/.claude/skills/papermorph/scripts/bake_sets.py b/.claude/skills/papermorph/scripts/bake_sets.py
index 2cd32e9..63b78bc 100644
--- a/.claude/skills/papermorph/scripts/bake_sets.py
+++ b/.claude/skills/papermorph/scripts/bake_sets.py
@@ -1,16 +1,18 @@
#!/usr/bin/env python3
"""Bake a picture book's SVG set sketches to WebP layers (plus a blurred -soft copy for shallow focus).
- uv run --with playwright --with pillow python bake_sets.py books/<book>/art/sets.js site/<book> [SET ...]
+ uv run --with playwright --with pillow python bake_sets.py books/<book>/art/sets.js site/<book> [SET ...] [--ref DIR]
SETS.JS (template: assets/templates/picture-book/sets.js) defines window.SETS = {setName: () => [layer, ...]} and
window.SET_DEFS (an SVG <defs> string with the paper filters). A layer is {name, k, box: [x, y, w, h] in world px,
scale, svg, soft?}: k is the parallax factor (1 = the puppets' plane), scale the bitmap's pixels per world px, soft:
false skips the blurred copy. Writes SITE/img/sets/<set>/<layer>.webp, <layer>-soft.webp and the placement manifest
-SITE/img/sets/sets.js that paper.js reads. With SET names only those sets are baked and the manifest keeps the others.
+SITE/img/sets/sets.js that paper.js reads. The manifest keeps every set it already lists: without SET names only the
+sets it does not list yet are baked (one that is there may have been painted since), and with SET names those are baked
+again. --ref DIR also writes each baked set's layers as one 1600x900 picture, DIR/ref-<set>.png.
The sketches are a finished look on their own (free) and the composition reference an image model repaints (give the
-baked picture to paint.py, then put the result in with painted.py). Needs Playwright's Chromium.
+--ref picture to paint.py, then put the result in with painted.py). Needs Playwright's Chromium.
"""
import asyncio, base64, io, json, pathlib, sys
from PIL import Image
@@ -27,31 +29,39 @@ JS = """async ([name, i, soft]) => {
}"""
-async def main(sets_js, site, only):
- out = pathlib.Path(site) / 'img/sets'
+async def main(sets_js, site, only, ref):
+ out = pathlib.Path(site) / 'img/sets'; old = out / 'sets.js'
+ man = json.loads(old.read_text().split('=', 1)[1].rstrip().rstrip(';')) if old.exists() else {}
async with async_playwright() as p:
b = await p.chromium.launch(); pg = await b.new_page()
await pg.add_script_tag(path=str(pathlib.Path(sets_js).resolve()))
- names = only or await pg.evaluate('Object.keys(SETS)')
- man = {}
+ known = await pg.evaluate('Object.keys(SETS)')
+ if set(only) - set(known): sys.exit(f'not in {sets_js}: {sorted(set(only) - set(known))}')
+ names = only or [n for n in known if n not in man]
+ if not names: print('every set is in the manifest already: name the ones to bake again')
for n in names:
+ frame = Image.new('RGBA', (1600, 900))
layers = await pg.evaluate(f'SETS[{json.dumps(n)}]().map(l => ({{name: l.name, k: l.k, box: l.box, soft: l.soft !== false}}))')
(out / n).mkdir(parents=True, exist_ok=True)
for i, L in enumerate(layers):
for soft in ([False, True] if L['soft'] else [False]):
png = await pg.evaluate(JS, [n, i, soft])
f = out / n / (L['name'] + ('-soft' if soft else '') + '.webp')
- Image.open(io.BytesIO(base64.b64decode(png.split(',')[1]))).save(f, quality=86, method=6)
+ im = Image.open(io.BytesIO(base64.b64decode(png.split(',')[1]))); im.save(f, quality=86, method=6)
print(f.relative_to(out), f.stat().st_size // 1024, 'KB')
+ if not soft:
+ x, y, w, h = L['box']; lay = im.convert('RGBA').resize((w, h)); frame.paste(lay, (x, y), lay)
man[n] = layers
- old = out / 'sets.js'
- if old.exists() and only:
- man = {**json.loads(old.read_text().split('=', 1)[1].rstrip().rstrip(';')), **man}
+ if ref:
+ pathlib.Path(ref).mkdir(parents=True, exist_ok=True); frame.convert('RGB').save(pathlib.Path(ref) / f'ref-{n}.png')
old.write_text('window.SETS = ' + json.dumps(man) + ';\n')
await b.close()
if __name__ == '__main__':
- if len(sys.argv) < 3:
- print(__doc__); sys.exit(0 if sys.argv[1:] in (['-h'], ['--help']) else 2)
- asyncio.run(main(sys.argv[1], sys.argv[2], sys.argv[3:]))
+ args, ref = sys.argv[1:], None
+ if '--ref' in args:
+ k = args.index('--ref'); ref = args[k + 1]; del args[k:k + 2]
+ if len(args) < 2:
+ print(__doc__); sys.exit(0 if args in (['-h'], ['--help']) else 2)
+ asyncio.run(main(args[0], args[1], args[2:], ref))
diff --git a/.claude/skills/papermorph/scripts/check_blank.py b/.claude/skills/papermorph/scripts/check_blank.py
index 5aef543..06d3929 100644
--- a/.claude/skills/papermorph/scripts/check_blank.py
+++ b/.claude/skills/papermorph/scripts/check_blank.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""Find opening blanks: the narration has started but the picture is still empty.
- python3 -m http.server 8765 -d site &
+ python3 SKILL/scripts/serve.py 8765 site &
uv run --with playwright check_blank.py site # every chapter of the book in site/
uv run --with playwright check_blank.py site/history ch05 ch06 --url http://localhost:8765/history/
LIMIT=0.6 uv run --with playwright check_blank.py site # stricter
@@ -19,7 +19,7 @@ LIMIT = float(os.environ.get("LIMIT", "1.0"))
PROBE = r"""
(i) => {
seek(i, false);
- const b = BEATS[i], tm = TIMINGS[b.id], first = (tm.cues[0] || [0])[0];
+ const b = BEATS[i], tm = TIMINGS[b.id], pre = b.pre || 0, first = pre + (tm.cues[0] || [0])[0]; // the speech starts `pre` s in
const visible = () => { // drawn leaves whose opacity, multiplied up to the scene, is above .05
let n = 0;
for (const e of scene.querySelectorAll('text,path,circle,rect,ellipse,line,polygon,image')) {
@@ -32,8 +32,8 @@ PROBE = r"""
return n;
};
const out = [];
- for (let t = 0; t <= Math.min(tm.dur, 12); t += .2) { P.t = t; evalTo(t); out.push([+t.toFixed(1), visible()]); }
- return { id: b.id, ask: !!b.ask, first, dur: tm.dur, out };
+ for (let t = 0; t <= Math.min(P.end, pre + 12); t += .2) { P.t = t; evalTo(t); out.push([+t.toFixed(1), visible()]); }
+ return { id: b.id, ask: !!b.ask, first, dur: P.end, out };
}
"""
diff --git a/.claude/skills/papermorph/scripts/engine.py b/.claude/skills/papermorph/scripts/engine.py
index 36b0ef2..7a8da4f 100644
--- a/.claude/skills/papermorph/scripts/engine.py
+++ b/.claude/skills/papermorph/scripts/engine.py
@@ -58,7 +58,7 @@ def books(target: Path):
if (target / 'lib' / 'engine.js').exists():
return [target]
found = sorted(p.parent.parent for p in target.glob('*/lib/engine.js'))
- if not found and (target / 'lib').is_dir(): # e.g. site/ with the bookshelf's lib/, before any book exists
+ if not found and (target / 'lib').is_dir() and any((target / 'lib').iterdir()): # e.g. site/ with the bookshelf's lib/, before any book exists (an empty lib/ is a new book's)
sys.exit(f'{target}/lib has no engine.js and no book folder below it has one: pass the book folder itself')
return found or [target] # or a new book
diff --git a/.claude/skills/papermorph/scripts/paint.py b/.claude/skills/papermorph/scripts/paint.py
index 02a5af0..ea85673 100644
--- a/.claude/skills/papermorph/scripts/paint.py
+++ b/.claude/skills/papermorph/scripts/paint.py
@@ -15,7 +15,7 @@ Each call prints the cost OpenRouter reports and appends one line to the ledger
separated; the ledger and its folder are created). Test the cheapest quality first and total the ledger before and
after a batch, so the spend is known.
"""
-import argparse, base64, datetime, json, os, pathlib, sys, urllib.request, urllib.error
+import argparse, base64, datetime, json, mimetypes, os, pathlib, sys, urllib.request, urllib.error
def total(path):
@@ -54,7 +54,7 @@ def main():
else:
body["resolution"] = "2K"
if a.refs:
- body["input_references"] = [{"type": "image_url", "image_url": {"url": "data:image/png;base64," + base64.b64encode(pathlib.Path(r).read_bytes()).decode()}} for r in a.refs]
+ body["input_references"] = [{"type": "image_url", "image_url": {"url": f"data:{mimetypes.guess_type(r)[0] or 'image/png'};base64," + base64.b64encode(pathlib.Path(r).read_bytes()).decode()}} for r in a.refs]
req = urllib.request.Request("https://openrouter.ai/api/v1/images", json.dumps(body).encode(),
{"Authorization": f"Bearer {key()}", "Content-Type": "application/json"})
try:
diff --git a/.claude/skills/papermorph/scripts/painted.py b/.claude/skills/papermorph/scripts/painted.py
index 93589f8..3e2a252 100644
--- a/.claude/skills/papermorph/scripts/painted.py
+++ b/.claude/skills/papermorph/scripts/painted.py
@@ -7,7 +7,10 @@ Replaces the SVG sketch of a set (bake_sets.py) by its painted layers. For every
written to SITE/img/sets/<set>/<layer>.webp with a blurred <layer>-soft.webp for close-ups, and the set's entry in
SITE/img/sets/sets.js is replaced. Full-frame pictures are resized to the 1600x900 frame and grown by mirrored, blurred
edges (`pad`) so a shaking or zooming camera never sees past them; a `cut` layer is a transparent picture cropped to its
-opaque part and placed by its left x, bottom y and width, so characters can stand behind it. Words for signs and boards
+opaque part and placed by its left x, bottom y and width, so characters can stand behind it. A layer's `k` is its
+parallax factor as in the sketch (1, the puppets' plane, if left out). The padding mirrors the picture's edges; a layer
+with "edge": "stretch" repeats its outermost pixels instead (when something near the edge, a post box, would show
+twice). Words for signs and boards
(the model leaves them blank) are written onto the picture. Props go to SITE/img/props/<name>.webp. Run bake_sets.py
first (it rewrites sets.js); run this again after any change. Paths in the manifest are relative to the manifest.
@@ -19,7 +22,7 @@ first (it rewrites sets.js); run this again after any change. Paths in the manif
{"name": "bg", "file": "bg-street.png",
"words": [{"x": 584, "y": 449, "size": 60, "text": "OPEN", "colour": "#f6d68a", "font": "sign"}]},
{"name": "cart", "file": "cart.png", "cut": {"x": 1010, "bottom": 800, "width": 470}},
- {"name": "wall", "file": "wall.png", "box": [0, 0, 1600, 900]} (a box in world px: x, y, w, h)
+ {"name": "wall", "file": "wall.png", "box": [0, 0, 1600, 900], "k": 0.85} (a box in world px: x, y, w, h)
]},
"props": {"bun": "p-bun.png"} (largest side 800 px)
}
@@ -59,8 +62,15 @@ def words(im, items, ox, oy, fonts, base):
d.text((w['x'] - ox, w['y'] - oy), w['text'], fill=w.get('colour', '#ffffff'), font=font, anchor='mm')
-def padded(im, pad):
- px, py = pad; W, H = im.size; out = Image.new('RGBA', (W + 2 * px, H + 2 * py))
+def padded(im, pad, edge='mirror'):
+ px, py = pad; W, H = im.size
+ if edge == 'stretch': # the outermost pixels repeated outwards (columns, then rows), softened
+ out = Image.new('RGBA', (W + 2 * px, H + 2 * py)); out.paste(im, (px, py))
+ out.paste(im.crop((0, 0, 1, H)).resize((px, H)), (0, py)); out.paste(im.crop((W - 1, 0, W, H)).resize((px, H)), (W + px, py))
+ out.paste(out.crop((0, py, W + 2 * px, py + 1)).resize((W + 2 * px, py)), (0, 0))
+ out.paste(out.crop((0, py + H - 1, W + 2 * px, py + H)).resize((W + 2 * px, py)), (0, py + H))
+ out = out.filter(ImageFilter.GaussianBlur(12)); out.paste(im, (px, py)); return out
+ out = Image.new('RGBA', (W + 2 * px, H + 2 * py))
out.paste(im.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.FLIP_TOP_BOTTOM).resize((W + 2 * px, H + 2 * py)).filter(ImageFilter.GaussianBlur(12)))
for dx, dy, flip, box in [(-px, 0, Image.FLIP_LEFT_RIGHT, (0, 0, px, H)), (W, 0, Image.FLIP_LEFT_RIGHT, (W - px, 0, W, H)),
(0, -py, Image.FLIP_TOP_BOTTOM, (0, 0, W, py)), (0, H, Image.FLIP_TOP_BOTTOM, (0, H - py, W, H))]:
@@ -78,6 +88,9 @@ def main(manifest, site):
W, H = m.get('frame', [1600, 900]); pad = m.get('pad', [60, 34]); fonts = m.get('fonts', {})
sets_js = pathlib.Path(site) / 'img/sets/sets.js'
sets = json.loads(sets_js.read_text().split('=', 1)[1].strip().rstrip(';')) if sets_js.exists() else {}
+ missing = [L['file'] for layers in m.get('sets', {}).values() for L in layers if not (base / L['file']).exists()]
+ missing += [src for src in m.get('props', {}).values() if not (base / src).exists()]
+ if missing: sys.exit(f'missing pictures (nothing changed): {missing}')
for name, layers in m.get('sets', {}).items():
out = sets_js.parent / name; out.mkdir(parents=True, exist_ok=True)
for f in out.glob('*.webp'): f.unlink()
@@ -91,7 +104,7 @@ def main(manifest, site):
b = L['box']; im = im.resize((b[2], b[3]), Image.LANCZOS); words(im, L.get('words', []), b[0], b[1], fonts, base)
else:
im = im.resize((W, H), Image.LANCZOS); words(im, L.get('words', []), 0, 0, fonts, base)
- im = padded(im, pad); b = [-pad[0], -pad[1], W + 2 * pad[0], H + 2 * pad[1]]
+ im = padded(im, pad, L.get('edge', 'mirror')); b = [-pad[0], -pad[1], W + 2 * pad[0], H + 2 * pad[1]]
im.save(out / f"{L['name']}.webp", quality=88, method=6)
im.filter(ImageFilter.GaussianBlur(6)).save(out / f"{L['name']}-soft.webp", quality=80, method=6)
entry.append({'name': L['name'], 'k': L.get('k', 1), 'box': b, 'soft': True})
diff --git a/.claude/skills/papermorph/scripts/perf.py b/.claude/skills/papermorph/scripts/perf.py
index 2fe01f8..b0e1a8d 100644
--- a/.claude/skills/papermorph/scripts/perf.py
+++ b/.claude/skills/papermorph/scripts/perf.py
@@ -26,7 +26,7 @@ async def main(ch, out, base):
pg = await b.new_page(viewport={"width": 1512, "height": 982})
await pg.goto(f"{base}{ch}/?beat=0&t=0"); await pg.wait_for_timeout(800)
await pg.evaluate(SETUP)
- beats = await pg.evaluate("BEATS.map(b => [b.id, !!b.ask, TIMINGS[b.id].dur])")
+ beats = await pg.evaluate("BEATS.map(b => [b.id, !!b.ask, (b.pre || 0) + TIMINGS[b.id].dur])")
res = []
for i, (bid, ask, dur) in enumerate(beats):
if ask: continue
diff --git a/.claude/skills/papermorph/scripts/sfx.py b/.claude/skills/papermorph/scripts/sfx.py
index 5ad7475..0708fd5 100644
--- a/.claude/skills/papermorph/scripts/sfx.py
+++ b/.claude/skills/papermorph/scripts/sfx.py
@@ -3,7 +3,7 @@
uv run --with numpy python sfx.py site/<book>/ch01/audio/sfx knock door rain # these effects, OUT_DIR/<name>.mp3
uv run --with numpy python sfx.py site/<book>/ch01/audio/sfx all # every recipe
- python3 sfx.py --list # names with a one-line description
+ uv run --with numpy python sfx.py --list # names with a one-line description
Needs ffmpeg. Every file is faded (5 ms) and peak-normalised to -3 dBFS; each recipe is seeded by its name, so a name
always renders the same sound. Play them from a chapter with sfx(beat, seconds, 'name', volume) (paper-cutout kit,
@@ -14,7 +14,10 @@ lp1/hp1/bpf filters, pluck, pink, drops), and list it in RECIPES. Keep them soft
as "scary rain" and a bare sine as a phone, so layer a low thump under a click, shape every sound with a decay, and
listen before using one. Recipes: sting growl gulp door tick bell whoosh fly violin crickets chair (character and
house sounds), knock chop bang bonk thud crash (wood, axe, glass, a wardrobe door), plop sniff chomp burp lick bark
-(eating and a dog), chime beep card coin (a convenience store), rain and rain-short (gentle rain).
+(eating and a dog), chime beep card coin (a convenience store), rain and rain-short (gentle rain), creak latch sash
+shutter flap clang scrape (doors, windows, a shutter, cardboard, a steel bowl), inhale inhale-short (a breath in),
+patter patter1 step1 (light steps, one step, a grown-up's shoe), honk gulls (a penguin or goose, seagulls), and beds:
+wind (8 s), surf (30 s), room (3 s room tone that tiles every 2.6 s).
"""
import math
import os
@@ -437,11 +440,163 @@ def rain(dur=5.0, fade=.35):
return y * np.minimum(t / fade, 1) * np.minimum((dur - t) / fade, 1)
+def creak():
+ """A door hinge creaking open a crack, 1.1 s: uneven stick-slip clicks ringing a wooden body, loudest mid-way."""
+ y, at = np.zeros(int(1.1 * SR)), .02
+ while at < 1.05:
+ k = t_of(.03)
+ click = bpf(noise(len(k)), 420, q=6) + .7 * bpf(noise(len(k)), 900, q=8)
+ place(y, unit(click) * np.exp(-k / .008) * (.5 + .5 * np.sin(np.pi * at / 1.1)), at)
+ at += 1 / (14 + 26 * np.sin(np.pi * at / 1.1) * (.7 + .3 * rng.random())) # 14-40 clicks a second
+ return y
+
+
+def latch():
+ """A door latch clicking shut: two small metal clicks."""
+ y = np.zeros(int(.25 * SR))
+ for at, g in [(0, 1), (.06, .6)]:
+ t = t_of(.05)
+ place(y, unit(bpf(noise(len(t)), 3200, q=3)) * np.exp(-t / .006) * g, at)
+ return y
+
+
+def clang():
+ """An empty steel bowl dropped on stone: a bright ring, then quick rattles as it settles."""
+ y = np.zeros(int(.9 * SR))
+ for at, g, d in [(0, 1, .35), (.16, .55, .18), (.27, .35, .12), (.34, .2, .08)]:
+ t = t_of(d)
+ ring = sum(np.sin(2 * np.pi * f * t) * a for f, a in [(1180, 1), (1730, .6), (2610, .4), (3300, .25)])
+ place(y, unit(ring + .3 * hp1(noise(len(t)), 2000) * np.exp(-t / .004)) * np.exp(-t / (d / 3)) * g, at)
+ return y
+
+
+def shutter():
+ """A rolling steel shutter rattling down (1 s of slats) and banging on the ground."""
+ y = np.zeros(int(1.3 * SR))
+ for i in range(22):
+ t = t_of(.05)
+ place(y, unit(bpf(noise(len(t)), 1500 + 300 * (i % 3), q=4)) * np.exp(-t / .01) * (.35 + .3 * (i % 2)), i * .045)
+ t = t_of(.3)
+ place(y, (unit(bpf(noise(len(t)), 300, q=1)) + unit(np.sin(2 * np.pi * 90 * t))) * np.exp(-t / .07) * .7, 1.0)
+ return y
+
+
+def wind():
+ """A soft cold wind, 8 s bed: low noise around 500 Hz swelling in slow gusts."""
+ t = t_of(8.0)
+ n = len(t)
+ f = np.fft.rfftfreq(n, 1 / SR)
+ f[0] = 1
+ w = np.fft.irfft(np.fft.rfft(noise(n)) / np.sqrt(f) * np.exp(-((np.log(f) - np.log(500)) ** 2) / 1.2), n)
+ return unit(w) * (.45 + .55 * np.sin(np.pi * t / 4) ** 2) * np.minimum(t / 1.0, 1) * np.minimum((8 - t) / 1.2, 1)
+
+
+def room():
+ """Room tone, 3 s: an extractor hum (100 Hz) under a low wash; 0.4 s fades, so tiles every 2.6 s overlap smoothly."""
+ t = t_of(3.0)
+ n = len(t)
+ f = np.fft.rfftfreq(n, 1 / SR)
+ f[0] = 1
+ wash = np.fft.irfft(np.fft.rfft(noise(n)) / f * (f > 40) / (1 + (f / 600) ** 2), n)
+ hum = np.sin(2 * np.pi * 100 * t) + .4 * np.sin(2 * np.pi * 200 * t + 1)
+ return (unit(wash) * .8 + unit(hum) * .25) * np.minimum(t / .4, 1) * np.minimum((3 - t) / .4, 1)
+
+
+def inhale(dur=1.4):
+ """A big breath in through the nose: band noise swelling and rising (500 -> 1500 Hz), then a 0.12 s fade."""
+ t = t_of(dur)
+ return unit(bpf(noise(len(t)), 500 + 1000 * t / dur, q=1.5)) * np.minimum(t / (dur * .8), 1) ** 1.5 * np.minimum((dur - t) / .12, 1)
+
+
+def scrape():
+ """Metal scraped, 0.28 s: a short rasp ringing a steel bowl (2.1 / 3.4 / 5 kHz)."""
+ t = t_of(.28)
+ n = noise(len(t)) * (1 + .6 * np.sin(2 * np.pi * 28 * t))
+ v = bpf(n, 2100, q=8) + .7 * bpf(n, 3400, q=10) + .4 * bpf(n, 5000, q=10) + .3 * bpf(n, 900, q=1)
+ return unit(v) * np.sin(np.pi * t / .28) ** .8
+
+
+def flap():
+ """A cardboard flap dropping back: a dull papery slap, 0.15 s."""
+ t = t_of(.15)
+ return unit(bpf(noise(len(t)), 420, q=1.2)) * np.minimum(t / .004, 1) * np.exp(-t / .035)
+
+
+def sash():
+ """A sash window shoved up: a wooden slide (band noise rising) ending in a knock."""
+ t = t_of(.5)
+ y = unit(bpf(noise(len(t)), 500 + 900 * t / .5, q=2)) * np.minimum(t / .05, 1) * (.4 + .6 * t / .5)
+ k = t_of(.08)
+ place(y, unit(bpf(noise(len(k)), 700, q=3)) * np.exp(-k / .02), .42)
+ return y
+
+
+def honk():
+ """A penguin's (or goose's) bray: a nasal saw falling 470 -> 300 Hz with a quick wobble, through two formants; two pulses."""
+ y = np.zeros(int(.75 * SR))
+ for at, d, f0 in [(0, .16, 470), (.2, .42, 420)]:
+ t = t_of(d)
+ f = f0 - 120 * t / d + 18 * np.sin(2 * np.pi * 24 * t)
+ s = 2 * ((np.cumsum(f) / SR) % 1) - 1 # naive saw
+ v = bpf(s, 950, q=3) + .6 * bpf(s, 2300, q=4) + .2 * s
+ place(y, unit(v) * np.sin(np.pi * np.minimum(t / d, 1)) ** .6 * (.7 if at == 0 else 1), at)
+ return y
+
+
+def gulls():
+ """Three distant gull cries, 3 s: a falling 'kee-ow' with a little rasp, softened by distance."""
+ y = np.zeros(3 * SR)
+ for at, f0, g in [(.1, 1900, 1), (.55, 1750, .8), (1.9, 2000, .6)]:
+ t = t_of(.38)
+ f = f0 - 700 * (t / .38) ** 1.5 + 40 * np.sin(2 * np.pi * 30 * t)
+ ph = np.cumsum(f) / SR
+ v = np.sin(2 * np.pi * ph) + .35 * np.sin(4 * np.pi * ph) + .05 * noise(len(t))
+ place(y, lp1(v, 3000) * np.sin(np.pi * t / .38) ** .8 * g, at)
+ return y
+
+
+def surf(dur=30.0, fade_in=1.0, fade_out=1.5):
+ """Soft surf, 30 s: a low noise wash swelling and drawing back every 3 s.
+
+ One long file covers a scene (a short loop of separate plays gaps or doubles at every join); shaped in the
+ frequency domain, so no filter run-in."""
+ t = t_of(dur)
+ n = len(t)
+ f = np.fft.rfftfreq(n, 1 / SR)
+ f[0] = 1
+ w = np.fft.irfft(np.fft.rfft(noise(n)) / np.sqrt(f) / (1 + (f / 1100) ** 2) * (f > 60), n)
+ return unit(w) * (.4 + .6 * np.sin(np.pi * t / 3) ** 2) * np.minimum(t / fade_in, 1) * np.minimum((dur - t) / fade_out, 1)
+
+
+def patter(steps=12, gap=1 / 6):
+ """Light footsteps on stone (a small animal or a child): flat little slaps, six a second, 2 s."""
+ y = np.zeros(int((.06 + steps * gap) * SR))
+ for k in range(steps):
+ d = t_of(.06)
+ hit = unit(bpf(noise(len(d)), 900 + 150 * (k % 2), q=1.5) * np.exp(-d / .012)) * .6 + unit(np.sin(2 * np.pi * 180 * d) * np.exp(-d / .02)) * .4
+ place(y, hit * (.75 + .25 * (k % 2)), .02 + k * gap)
+ return y
+
+
+def step1():
+ """One grown-up's shoe on pavement, 0.2 s: a soft low thud under a short dull scuff (lower than patter)."""
+ y, d = np.zeros(int(.2 * SR)), t_of(.14)
+ thud = unit(np.sin(2 * np.pi * (130 - 60 * d / .14) * d) * np.exp(-d / .035))
+ scuff = unit(bpf(noise(len(d)), 1500, q=1.2) * np.exp(-d / .02) * np.minimum(d / .004, 1))
+ place(y, .6 * thud + .5 * scuff, .01)
+ return y
+
+
RECIPES = {name: globals()[name] for name in (
"sting growl gulp door tick bell whoosh fly violin crickets chair knock chop bang bonk thud crash plop sniff "
- "chomp burp lick bark chime beep card coin rain").split()}
+ "chomp burp lick bark chime beep card coin rain creak latch clang shutter sash flap scrape inhale wind room "
+ "honk gulls surf patter step1").split()}
RECIPES["rain-short"] = lambda: rain(1.15, .12)
RECIPES["rain-short"].__doc__ = "About one second of the same rain, for a quick cut."
+RECIPES["inhale-short"] = lambda: inhale(.85)
+RECIPES["inhale-short"].__doc__ = "A quicker breath in, 0.85 s."
+RECIPES["patter1"] = lambda: patter(1)
+RECIPES["patter1"].__doc__ = "One light step of the same patter."
def write_mp3(path, y):
@@ -468,7 +623,7 @@ def main(argv):
if argv == ["--list"]:
for name, fn in RECIPES.items():
doc = (fn.__doc__ or "").strip().splitlines()
- print(f"{name:12s}{doc[0] if doc else ''}")
+ print(f"{name:14s}{doc[0] if doc else ''}")
return
if len(argv) < 2 or argv[0] in ("-h", "--help"):
print(__doc__)
diff --git a/.claude/skills/papermorph/scripts/shot.py b/.claude/skills/papermorph/scripts/shot.py
index c0fff70..1496418 100644
--- a/.claude/skills/papermorph/scripts/shot.py
+++ b/.claude/skills/papermorph/scripts/shot.py
@@ -30,7 +30,7 @@ async def main(a):
if errs:
print("page errors:", errs); await b.close(); return 1
n = await pg.evaluate("BEATS.length")
- durs = await pg.evaluate("BEATS.map(b => TIMINGS[b.id].dur)")
+ durs = await pg.evaluate("BEATS.map(b => (b.pre || 0) + TIMINGS[b.id].dur)") # a picture book's scene starts `pre` s before its speech
specs = a.specs or [f"{i}:{t}" for t in ("open", "end", *a.moments) for i in range(n)]
groups = {}
for s in specs:
@@ -41,7 +41,9 @@ async def main(a):
f = a.out / f"{a.chapter}_b{i:02d}_{t}.png"
await pg.screenshot(path=str(f))
groups.setdefault(t, []).append(f)
+ live = await pg.evaluate("typeof bakedJobs === 'undefined' ? 0 : bakedJobs.size")
await b.close()
+ if live: print(f"{live} paper pieces were baked live in the page: run bake_puppets.py")
for t, fs in groups.items():
for k in range(0, len(fs), 4):
sheet = Image.new("RGB", (1600, 960), "black")
diff --git a/.claude/skills/papermorph/scripts/sprites.py b/.claude/skills/papermorph/scripts/sprites.py
index 926dbea..dba748f 100644
--- a/.claude/skills/papermorph/scripts/sprites.py
+++ b/.claude/skills/papermorph/scripts/sprites.py
@@ -40,6 +40,8 @@ def art(rows, pal):
def sheet(anims):
"""anims: {name: [Image, ...]} -> (sheet, layout)."""
w, h = next(iter(anims.values()))[0].size
+ odd = [f"{n}[{k}] {f.size}" for n, fs in anims.items() for k, f in enumerate(fs) if f.size != (w, h)]
+ if odd: raise ValueError(f"every frame must be {w}x{h}: {odd[:5]}")
cols = max(len(f) for f in anims.values())
S = Image.new("RGBA", (w * cols, h * len(anims)))
layout = {"w": w, "h": h, "anims": {}}
diff --git a/.claude/skills/papermorph/scripts/still.py b/.claude/skills/papermorph/scripts/still.py
index 7ccf158..9d67223 100644
--- a/.claude/skills/papermorph/scripts/still.py
+++ b/.claude/skills/papermorph/scripts/still.py
@@ -28,8 +28,8 @@ async def main():
r = await pg.evaluate("(() => { const r = document.getElementById('stage').getBoundingClientRect(); return {x: r.x, y: r.y, width: r.width, height: r.height}; })()")
for i, bid in enumerate(ids):
if asks[i] or (a.beats and bid not in a.beats): continue
- dur = await pg.evaluate(f"TIMINGS['{bid}'].dur")
await pg.evaluate(f"seek({i}, false)"); await pg.wait_for_timeout(200)
+ dur = await pg.evaluate("P.end") # the whole beat: a picture book's `pre` and the tail after the speech
prev, diffs, t = None, [], 0.0
while t <= dur + 1e-6:
await pg.evaluate(f"P.t = {t}; evalTo({t})"); await pg.wait_for_timeout(40)
diff --git a/.claude/skills/papermorph/scripts/strip.py b/.claude/skills/papermorph/scripts/strip.py
index adff11c..0955e45 100644
--- a/.claude/skills/papermorph/scripts/strip.py
+++ b/.claude/skills/papermorph/scripts/strip.py
@@ -3,7 +3,8 @@
uv run --with playwright --with pillow --with numpy strip.py ch12 OUT [specs ...] [--sheet NAME] [--url URL] [--browser chromium]
- push:2.3,dir,end chosen moments of beat 'push': seconds, a narration mark (dir, dir+0.5), mid or end
+ push:2.3,dir,end chosen moments of beat 'push': seconds on the beat clock, a narration mark (dir, dir+0.5,
+ s04.ba1 in a joined scene), mid or end of the whole beat
push/0.25 a motion strip: every 0.25 s across the beat (push alone: every 1 s)
push/0.25@w also a motion WebP of the beat (0.25 s steps)
push/0.2@o also an onion skin: the strip's frames laid over each other in one picture, later ones
@@ -43,7 +44,11 @@ async def main(a):
pg = await b.new_page(viewport={"width": 1600, "height": 960})
errs = []; pg.on("pageerror", lambda e: errs.append(str(e)))
await pg.goto(f"{a.url}{a.chapter}/?beat=0&t=0"); await pg.wait_for_timeout(800)
+ at = {"i": -1, "t": 0.0}
async def shot(t, scale):
+ if t < at["t"]: # tweens that have finished stay finished: going back needs the beat rebuilt
+ await pg.evaluate(f"seek({at['i']}, false)")
+ at["t"] = t
await pg.evaluate(f"P.t = {t}; evalTo({t})"); await pg.wait_for_timeout(60)
r = await pg.evaluate("(() => { const r = document.getElementById('stage').getBoundingClientRect(); return [r.x, r.y, r.width, r.height]; })()")
await pg.screenshot(path=str(tmp), clip={"x": r[0], "y": r[1], "width": r[2], "height": r[3]})
@@ -58,13 +63,15 @@ async def main(a):
bid, moments, step = m.groups()
i = await pg.evaluate(f"BEATS.findIndex(b => b.id === '{bid}')")
if i < 0: sys.exit(f"no beat {bid!r}")
- tm = await pg.evaluate(f"TIMINGS['{bid}']"); dur = tm["dur"]
+ tm = await pg.evaluate(f"TIMINGS['{bid}']")
+ await pg.evaluate(f"seek({i}, false)"); await pg.wait_for_timeout(300); at.update(i=i, t=0.0)
+ dur, pre = await pg.evaluate("[P.end, BEATS[P.i].pre || 0]") # the whole beat; marks start after `pre`
def when(x):
- mk = re.fullmatch(r"([A-Za-z_]\w*)([+-][\d.]+)?", x)
+ mk = re.fullmatch(r"([A-Za-z_][\w.]*?)([+-][\d.]+)?", x)
if x in ("mid", "end"): return dur / 2 if x == "mid" else dur - .05
- if mk and mk[1] in tm.get("marks", {}): return tm["marks"][mk[1]] + float(mk[2] or 0)
+ if mk and mk[1] in tm.get("marks", {}): return pre + tm["marks"][mk[1]] + float(mk[2] or 0)
+ if mk: sys.exit(f"no mark {mk[1]!r} in {bid}")
return float(x)
- await pg.evaluate(f"seek({i}, false)"); await pg.wait_for_timeout(300)
st = float(step or 1)
ts = [when(x) for x in moments.split(",")] if moments else [k * st for k in range(int(dur / st) + 1)]
ims, raw = [], []
diff --git a/.claude/skills/papermorph/scripts/tts.py b/.claude/skills/papermorph/scripts/tts.py
index 6013a44..7439787 100644
--- a/.claude/skills/papermorph/scripts/tts.py
+++ b/.claude/skills/papermorph/scripts/tts.py
@@ -1,7 +1,7 @@
"""Generate narration audio and word-mark timings with Edge TTS or Kokoro.
Usage (from the project root; SKILL is this skill's folder):
- uv run --with edge-tts $SKILL/scripts/tts.py content/ch01/narration.en.json site/ch01/audio/en
+ uv run --with edge-tts $SKILL/scripts/tts.py content/<book>/ch01/narration.en.json site/<book>/ch01/audio/en
uv run --python 3.12 --with kokoro $SKILL/scripts/tts.py ... # narration with "engine": "kokoro"
The narration file names the engine ("edge", the default, or "kokoro"), the voice and the rate. Edge needs network