Skip to content

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

fix(skill): picture-book sound tools catch up with the episodes

yzr2188b8f2a39a
diff --git a/.claude/skills/papermorph/scripts/export.py b/.claude/skills/papermorph/scripts/export.py
index 9d5006d..fe8d1cc 100644
--- a/.claude/skills/papermorph/scripts/export.py
+++ b/.claude/skills/papermorph/scripts/export.py
@@ -1,15 +1,19 @@
#!/usr/bin/env python3
-"""Export a chapter's lesson as an MP4 video with an .srt caption file, for video sites.
+"""Export a chapter (a lesson or a picture-book episode) as an MP4 video with an .srt caption file, for video sites.
- uv run --with playwright export.py ch12 --url http://localhost:8765/<book>/ --out OUT
+ uv run --with playwright --with numpy export.py ch12 --url http://localhost:8765/<book>/ --out OUT
... --fps 30 --height 1080 --link https://example.org/<book>/ch12/
The engine draws every frame as a pure function of the beat time, so frames are rendered by seeking, not by
-recording playback: the video is exact and the same on every run. Question beats (quick checks and practice) are
-left out; the video ends on a card saying they are in the interactive lesson (--link adds its address).
-Captions are not burned in: upload OUT/chNN.srt with the video. Needs ffmpeg; serve the site over HTTP first.
+recording playback: the video is exact and the same on every run. The sound is mixed the way the page plays it:
+each beat's clip from its `pre` on, and in a picture book the sfx() effects (cut at the end of their scene, as a
+scene change stops them) and the BGM tracks with their crossfades, ducking and MUSIC_OFF. Question beats (quick
+checks and practice) are left out, and then the video ends on a card saying they are in the interactive lesson
+(--link adds its address). Captions are not burned in: upload OUT/chNN.srt with the video. Needs ffmpeg; serve the
+site over HTTP first.
"""
-import argparse, asyncio, json, subprocess, sys, tempfile
+import argparse, asyncio, functools, json, subprocess, sys, tempfile, wave
+import numpy as np
from pathlib import Path
from playwright.async_api import async_playwright
@@ -30,6 +34,23 @@ END_CARD = """(link) => {
}"""
+RATE = 44100
+
+
[email protected]_cache(maxsize=None)
+def decode(src):
+ """Any audio file or URL as float32 stereo samples at RATE."""
+ raw = subprocess.run(['ffmpeg', '-v', 'error', '-i', src, '-f', 'f32le', '-ac', '2', '-ar', str(RATE), '-'],
+ capture_output=True, check=True).stdout
+ return np.frombuffer(raw, np.float32).reshape(-1, 2)
+
+
+def put(track, at, clip, gain=1.0, stop=None):
+ """Add clip into track from `at` seconds, cut at `stop` seconds."""
+ i = round(at * RATE); j = min(len(track), i + len(clip), round(stop * RATE) if stop is not None else len(track))
+ if j > i: track[i:j] += clip[:j - i] * gain
+
+
def srt_time(t):
ms = round(t * 1000)
return f'{ms // 3600000:02d}:{ms // 60000 % 60:02d}:{ms // 1000 % 60:02d},{ms % 1000:03d}'
@@ -54,33 +75,63 @@ async def main(a):
await pg.goto(base + '?beat=0&t=0'); await pg.wait_for_timeout(500)
if errs:
sys.exit(f'page errors: {errs}')
- info = await pg.evaluate("({lang: CHAPTER.language || 'en', beats: BEATS.map(b => ({id: b.id, ask: !!b.ask}))})")
+ info = await pg.evaluate("""({lang: CHAPTER.language || 'en', beats: BEATS.map(b => ({id: b.id, ask: !!b.ask, pre: b.pre || 0})),
+ sfx: typeof SFX === 'undefined' ? [] : SFX.map(e => ({...e, src: new URL(`audio/sfx/${e.name}.mp3`, location.href).href})),
+ bgm: typeof BGM === 'undefined' ? [] : BGM.map(m => ({src: m.a.src, beats: m.beats, gain: m.gain}))})""")
beats = [(i, x['id']) for i, x in enumerate(info['beats']) if not x['ask']]
+ asks = len(beats) < len(info['beats'])
box = await pg.locator('#stage').bounding_box()
clip = {k: box[k] for k in ('x', 'y', 'width', 'height')}
audio_dir = Path(tempfile.mkdtemp(prefix='export-'))
video = out / f'{a.chapter}.mp4'
- # Pass 1: the audio track, each beat's clip padded to the beat's length on screen.
- lengths, cues, offset = [], [], 0.0
+ # 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, music_off, cues, offset = [], [], [], [], 0.0
for i, bid in beats:
await open_beat(pg, base, i)
end, T = await pg.evaluate('[P.end, TIMINGS[BEATS[P.i].id]]')
- lengths.append(end)
- for k, (t, line) in enumerate(T['cues']):
- stop = T['cues'][k + 1][0] if k + 1 < len(T['cues']) else T['dur']
- cues.append((offset + t, offset + stop, line))
+ pre = info['beats'][i]['pre']
+ lengths.append(end); starts.append(offset)
+ music_off.append(await pg.evaluate('([id, n, fps]) => Array.from({length: n}, (_, f) => !!window.MUSIC_OFF?.[id]?.(f / fps))',
+ [bid, round(end * a.fps), a.fps]))
+ for k, c in enumerate(T['cues']): # [t, text, end?]: the page shows it at pre + t
+ stop = c[2] if len(c) > 2 else T['cues'][k + 1][0] if k + 1 < len(T['cues']) else T['dur']
+ cues.append((offset + pre + c[0], offset + pre + stop, c[1]))
offset += end
- total = offset + END_SECONDS
- site_audio = [f'{a.url}{a.chapter}/audio/{info["lang"]}/{bid}.mp3' for _, bid in beats]
- inputs, chains = [], []
- for k, src in enumerate(site_audio):
- inputs += ['-i', src]
- chains.append(f'[{k}:a]aformat=sample_rates=44100:channel_layouts=stereo,apad=whole_dur={lengths[k]:.3f},'
- f'atrim=0:{lengths[k]:.3f}[a{k}]')
- mix = ''.join(f'[a{k}]' for k in range(len(beats))) + f'concat=n={len(beats)}:v=0:a=1,apad=whole_dur={total:.3f}[out]'
+ total = offset + end_s
+ 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])
+ for e in info['sfx']: # effects sit at half volume under the voices; a new scene stops them
+ if e['beat'] == bid and 0 <= e['at'] < lengths[k]:
+ put(track, o + e['at'], decode(e['src']), .5 * e['vol'], o + lengths[k])
+ # music: the page's bgmTick, frame by frame: 0.8 s crossfades, ducked to 30 % in a scene with voices
+ fps, n = a.fps, sum(len(m) for m in music_off)
+ frames_id = [bid for k, (_, bid) in enumerate(beats) for _ in music_off[k]]
+ frames_t = [f / fps for m in music_off for f in range(len(m))]
+ frames_off = [x for m in music_off for x in m]
+ beat_len = {bid: lengths[k] for k, (_, bid) in enumerate(beats)}
+ for m in info['bgm']:
+ song, v, duck, pos = decode(m['src']), 0.0, .3, 0.0
+ gain = np.zeros(n); at = np.zeros(n, int)
+ for f in range(n):
+ bid, t = frames_id[f], frames_t[f]
+ current = bid in m['beats'] and not frames_off[f]
+ v = max(0.0, min(1.0, v + (1 if current else -1) / fps / .8))
+ duck += max(-1 / fps, min(1 / fps, .3 - duck))
+ if current: pos = sum(beat_len.get(b, 0) for b in m['beats'][:m['beats'].index(bid)]) + t
+ else: pos += 1 / fps
+ gain[f], at[f] = v * m['gain'] * duck, round(pos * RATE) % len(song)
+ step = RATE / fps
+ for f in np.nonzero(gain)[0]:
+ i = round(f * step); seg = np.take(song, np.arange(at[f], at[f] + round(step)), axis=0, mode='wrap')
+ put(track, i / RATE, seg, gain[f])
+ peak = float(np.abs(track).max())
wav = audio_dir / 'track.wav'
- subprocess.run(['ffmpeg', '-v', 'error', '-y', *inputs, '-filter_complex', ';'.join(chains) + ';' + mix,
- '-map', '[out]', str(wav)], check=True)
+ with wave.open(str(wav), 'wb') as w:
+ w.setnchannels(2); w.setsampwidth(2); w.setframerate(RATE)
+ w.writeframes((np.clip(track, -1, 1) * 32767).astype('<i2').tobytes())
# Pass 2: frames, piped into the encoder with the audio track.
enc = subprocess.Popen(['ffmpeg', '-v', 'error', '-y', '-f', 'image2pipe', '-framerate', str(a.fps), '-c:v', 'mjpeg',
'-i', '-', '-i', str(wav), '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-crf', '20',
@@ -95,10 +146,11 @@ async def main(a):
enc.stdin.write(await pg.screenshot(clip=clip, type='jpeg', quality=92))
frames += 1
print(f'{bid}: {n} frames', flush=True)
- await pg.evaluate(END_CARD, a.link or '')
- end = await pg.screenshot(clip=clip, type='jpeg', quality=92)
- for _ in range(END_SECONDS * a.fps):
- enc.stdin.write(end)
+ if end_s:
+ await pg.evaluate(END_CARD, a.link or '')
+ end = await pg.screenshot(clip=clip, type='jpeg', quality=92)
+ for _ in range(end_s * a.fps):
+ enc.stdin.write(end)
enc.stdin.close(); enc.wait()
await b.close()
with open(out / f'{a.chapter}.srt', 'w', encoding='utf-8') as f:
@@ -106,7 +158,8 @@ async def main(a):
f.write(f'{k}\n{srt_time(t0)} --> {srt_time(t1)}\n{line}\n\n')
if errs or enc.returncode:
sys.exit(f'failed: {errs[:3]} encoder {enc.returncode}')
- print(f'{video} ({total / 60:.1f} min, {frames} frames, {len(beats)} beats; question beats left out) and {a.chapter}.srt')
+ print(f'{video} ({total / 60:.1f} min, {frames} frames, {len(beats)} beats{"; question beats left out" if asks else ""}; '
+ f'sound peak {peak:.2f}{" (clipped)" if peak > 1 else ""}) and {a.chapter}.srt')
if __name__ == '__main__':
diff --git a/.claude/skills/papermorph/scripts/join.py b/.claude/skills/papermorph/scripts/join.py
index 0cac55f..c65ecdc 100644
--- a/.claude/skills/papermorph/scripts/join.py
+++ b/.claude/skills/papermorph/scripts/join.py
@@ -1,18 +1,27 @@
#!/usr/bin/env python3
"""Join several narration clips into one clip per scene, so a whole scene plays as one beat.
- python3 join.py site/<book>/chNN/audio/en scene1=s01,s02,s02b scene2=s03,[email protected]+1.5,s05
+ python3 join.py site/<book>/chNN/audio/en scene1=s01,s02,s02b,hush1:4 scene2=s03,[email protected]+1.5,s05
+ python3 join.py ... --words content/<book>/chNN/narration.en.json # also every word's time, for scene authors
Each argument is SCENE=CLIP,CLIP,...; a clip may be CLIP@T+S@T+S: S seconds of silence inserted T seconds into it
-(room for an action in the middle of a line). A short pause (0.35 s) goes between clips. Writes AUDIO_DIR/SCENE.mp3 and
+(room for an action in the middle of a line; T may be 0, silence before the line), and NAME:S is S seconds of
+silence with no clip of its own (a stare or a tag after the last line; its start is in `parts` like a clip's). A
+hole belongs after a word's sound has ended, not where the next word starts. A short pause (0.35 s) goes between clips. Writes AUDIO_DIR/SCENE.mp3 and
AUDIO_DIR/shots.js: TIMINGS.SCENE = {dur, marks, cues, parts} with each clip's start in `parts` (marks become
"clip.mark") and the caption cues moved along; the mouth cues of clips with holes are moved too (MOUTHS.clip). Load
-shots.js after timings.js and mouths.js. Run after tts.py and mouths.py; needs ffmpeg.
+shots.js after timings.js and mouths.js. Captions start 0.12 s after Edge's word times (the sound starts that much
+later; mouths.py moves its cues the same) and end at the first pause of 0.45 s or more after their words.
+With --words, writes NARRATION.words.txt next to the narration (outside the site): each clip's start, its holes and
+every word's heard time in the joined scene clip, which a scene's author times actions against (the beat clock is the
+scene's `pre` plus these). Keep the join command in a script beside the book's notes and rerun it after any change.
+Run after tts.py and mouths.py; needs ffmpeg.
"""
import json, re, subprocess, sys, pathlib, tempfile
if len(sys.argv) < 3 or sys.argv[1] in ('-h', '--help'):
print(__doc__); sys.exit(0 if sys.argv[1:2] in (['-h'], ['--help']) else 2)
GAP = .35
+LAG = .12 # Edge's word times run ~0.12–0.25 s ahead of the sound: captions (and mouths.py's cues) start this much later
d = pathlib.Path(sys.argv[1]); T = json.loads((d / 'timings.js').read_text().split('=', 1)[1].rstrip().rstrip(';'))
def speech_end(f): # where a clip's sound stops (the start of its trailing silence), in seconds
err = subprocess.run(['ffmpeg', '-v', 'info', '-i', str(f), '-af', 'silencedetect=n=-40dB:d=0.15', '-f', 'null', '-'], capture_output=True, text=True).stderr
@@ -21,28 +30,41 @@ def speech_end(f): # where a clip's sound stops (the start of its trailing sil
dur = float(subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', str(f)], capture_output=True, text=True).stdout)
# the last silence runs to the end of the file (ffmpeg may or may not print its end there)
return starts[-1] if starts and (len(ends) < len(starts) or ends[-1] > dur - .05) and starts[-1] > 0 else dur
+def pauses(f): # start of every pause of 0.45 s or more inside a clip: a caption ends at the pause after its words
+ err = subprocess.run(['ffmpeg', '-v', 'info', '-i', str(f), '-af', 'silencedetect=n=-40dB:d=0.45', '-f', 'null', '-'], capture_output=True, text=True).stderr
+ return [float(x) for x in re.findall(r'silence_start: ([\d.]+)', err)]
+args, words = sys.argv[2:], None
+if '--words' in args:
+ k = args.index('--words'); words = pathlib.Path(args[k + 1]); del args[k:k + 2]
+def silence(path, sec):
+ subprocess.run(['ffmpeg', '-v', 'error', '-y', '-f', 'lavfi', '-i', 'anullsrc=r=24000:cl=mono', '-t', str(sec), '-b:a', '64k', str(path)], check=True)
out, H = {}, {} # H: every shot's holes, for the mouths
-for arg in sys.argv[2:]:
+for arg in args:
shot, beats = arg.split('='); beats = beats.split(',')
holes = {} # beat -> [(at, seconds)]: silences inserted inside a clip, room for action
for i, b in enumerate(beats):
+ if ':' in b: # NAME:S, a stretch of silence
+ name, sec = b.split(':'); beats[i] = name; holes[name] = []
+ T[name] = {'dur': float(sec), 'marks': {}, 'cues': [], 'silent': True}; continue
name, *ins = b.split('@'); beats[i] = name
- holes[name] = [tuple(map(float, x.split('+'))) for x in ins]
+ holes[name] = sorted(tuple(map(float, x.split('+'))) for x in ins)
with tempfile.TemporaryDirectory() as tmp:
def piece(b): # the clip's file, cut at its holes with silence between
+ if T[b].get('silent'):
+ f = pathlib.Path(tmp) / f'{b}.mp3'; silence(f, T[b]['dur']); return [str(f)]
if not holes[b]: return [str((d / f'{b}.mp3').resolve())]
out, start = [], 0
for k, (at, sec) in enumerate(holes[b] + [(None, 0)]):
seg = pathlib.Path(tmp) / f'{b}-{k}.mp3'
- cmd = ['ffmpeg', '-v', 'error', '-y', '-i', str(d / f'{b}.mp3'), '-ss', str(start)] + (['-to', str(at)] if at is not None else []) + ['-c:a', 'libmp3lame', '-b:a', '64k', str(seg)]
- subprocess.run(cmd, check=True); out.append(str(seg))
+ if at is None or at > start: # a hole at 0 (or two at one place) has no sound before it
+ cmd = ['ffmpeg', '-v', 'error', '-y', '-i', str(d / f'{b}.mp3'), '-ss', str(start)] + (['-to', str(at)] if at is not None else []) + ['-c:a', 'libmp3lame', '-b:a', '64k', str(seg)]
+ subprocess.run(cmd, check=True); out.append(str(seg))
if at is not None:
sil = pathlib.Path(tmp) / f'{b}-s{k}.mp3'
- subprocess.run(['ffmpeg', '-v', 'error', '-y', '-f', 'lavfi', '-i', 'anullsrc=r=24000:cl=mono', '-t', str(sec), '-b:a', '64k', str(sil)], check=True); out.append(str(sil)); start = at
+ silence(sil, sec); out.append(str(sil)); start = at
return out
shift = lambda b, t: t + sum(sec for at, sec in holes[b] if t >= at)
- gap = pathlib.Path(tmp) / 'gap.mp3'
- subprocess.run(['ffmpeg', '-v', 'error', '-y', '-f', 'lavfi', '-i', 'anullsrc=r=24000:cl=mono', '-t', str(GAP), '-b:a', '64k', str(gap)], check=True)
+ gap = pathlib.Path(tmp) / 'gap.mp3'; silence(gap, GAP)
lst = pathlib.Path(tmp) / 'list.txt'
lst.write_text(''.join(''.join(f"file '{f}'\n" for f in piece(b)) + (f"file '{gap}'\n" if i < len(beats) - 1 else '') for i, b in enumerate(beats)))
subprocess.run(['ffmpeg', '-v', 'error', '-y', '-f', 'concat', '-safe', '0', '-i', str(lst), '-c:a', 'libmp3lame', '-b:a', '64k', str(d / f'{shot}.mp3')], check=True)
@@ -52,11 +74,12 @@ for arg in sys.argv[2:]:
# a caption at 0 (a voice with no word timings) still belongs after a hole that opens the clip; each one ends
# .3 s after its words do (the next caption, a hole between it and the next, or the clip's last sound), so it does
# not hang over the action; a hole inside its own sentence keeps it up
- cs, last = T[b]['cues'], speech_end(d / f'{b}.mp3')
+ if T[b].get('silent'): at += T[b]['dur'] + GAP; continue
+ cs, last, ps = T[b]['cues'], speech_end(d / f'{b}.mp3'), pauses(d / f'{b}.mp3')
for k, (t, c) in enumerate(cs):
t = max(t, .1); nxt = cs[k + 1][0] if k + 1 < len(cs) else None
- stop = min([last] + ([nxt] + [h for h, _ in holes[b] if t < h < nxt] if nxt is not None else []))
- cues.append([round(shift(b, t) + at, 3), c, round(shift(b, stop - 1e-3) + at + .3, 3)] + [1] * bool(T[b].get('forced')))
+ stop = min([last] + ([nxt] + [h for h, _ in holes[b] if t < h < nxt] if nxt is not None else []) + [x for x in ps if t + .15 < x < (nxt or 1e9)])
+ cues.append([round(shift(b, t) + at + LAG, 3), c, round(shift(b, stop - 1e-3) + at + .3, 3)] + [1] * bool(T[b].get('forced')))
marks.update({f'{b}.{k}': round(shift(b, v) + at, 3) for k, v in T[b]['marks'].items()})
at += T[b]['dur'] + sum(sec for _, sec in holes[b]) + GAP
dur = float(subprocess.run(['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', str(d / f'{shot}.mp3')], capture_output=True, text=True).stdout)
@@ -68,4 +91,13 @@ sh = lambda b, t: t + sum(sec for at, sec in H[b] if t >= at)
moved = {b: sorted([[round(sh(b, t), 3), k] for t, k in M[b]] + [[round(sh(b, at - 1e-3), 3), 'X'] for at, _ in H[b]])
for b in H if H[b] and b in M}
(d / 'shots.js').write_text(''.join(f'TIMINGS.{k} = {json.dumps(v)};\n' for k, v in out.items()) + ''.join(f'MOUTHS.{b} = {json.dumps(v)};\n' for b, v in moved.items()))
+if words: # every word where it is heard in its scene clip
+ W, lines = json.loads(words.with_suffix('.timings.json').read_text()), []
+ for sc, v in out.items():
+ lines.append(f"{sc} dur {v['dur']} (scene-clip times; the beat clock adds the scene's pre)")
+ for b, p in v['parts'].items():
+ hs = ' '.join(f'[hole {round(p + sh(b, at - 1e-6), 2)}…{round(p + sh(b, at - 1e-6) + sec, 2)}]' for at, sec in H.get(b, []))
+ ws = W.get(b, {}).get('words', [])
+ lines.append(f" {b} @{p} {hs} | " + (' '.join(f'{w}@{round(p + sh(b, t) + LAG, 2)}' for t, w in ws) or '(silence)'))
+ words.with_suffix('.words.txt').write_text('\n'.join(lines) + '\n')
print({k: (v['dur'], v['parts']) for k, v in out.items()})
diff --git a/.claude/skills/papermorph/scripts/mouths.py b/.claude/skills/papermorph/scripts/mouths.py
index 80cb0b3..fc79dd9 100644
--- a/.claude/skills/papermorph/scripts/mouths.py
+++ b/.claude/skills/papermorph/scripts/mouths.py
@@ -7,10 +7,12 @@ Reads the .timings.json cache that tts.py wrote next to the narration file and w
(window.MOUTHS = {beat: [[seconds, shape], ...]}). Shapes follow the cartoon set Rhubarb uses: A closed (m b p),
B teeth/most consonants, C open (e i), D wide (a), E round (o), F pucker (oo w u), G f/v, H l, X rest. Each word's
letters become shapes spread over the word's time; a pause between words closes the mouth. Letters are read as
-English spelling; for another language the rules below are a rough guide only. Run it after tts.py (and before
-join.py, which moves the cues of clips it joins).
+English spelling; for another language the rules below are a rough guide only, and a word with no Latin letters
+(Chinese, Japanese) opens and closes once per character. Edge's word times run about 0.12 s ahead of the sound, so
+every cue is moved that much later; when the clip AUDIO_DIR/<beat>.mp3 exists, the mouth shuts where its sound
+stops. Run it after tts.py (and before join.py, which moves the cues of clips it joins). Needs ffmpeg.
"""
-import json, re, sys, pathlib
+import json, re, subprocess, sys, pathlib
if len(sys.argv) < 3 or sys.argv[1] in ('-h', '--help'):
print(__doc__); sys.exit(0 if sys.argv[1:2] in (['-h'], ['--help']) else 2)
src, out = pathlib.Path(sys.argv[1]), pathlib.Path(sys.argv[2])
@@ -24,8 +26,14 @@ def shapes(word):
for k, v in RULES:
if w.startswith(k, i): s.append(v); i += len(k); break
else: s.append('B'); i += 1
+ if not w: s = ['DCEBDCB'[k % 7] for k in range(max(1, len(re.sub(r'[\s、。,!?…ー・っッゃゅょャュョ]', '', word))))]
out = [x for j, x in enumerate(s) if j == 0 or x != s[j - 1]]
return out or ['B']
+LAG = .12 # Edge's word times run ahead of the sound (join.py has the same)
+def speech_end(f): # where the clip's sound stops: the mouth shuts there, not .55 s after the last word starts
+ err = subprocess.run(['ffmpeg', '-v', 'info', '-i', str(f), '-af', 'silencedetect=n=-40dB:d=0.15', '-f', 'null', '-'], capture_output=True, text=True).stderr
+ st = [float(x) for x in re.findall(r'silence_start: ([\d.]+)', err) if float(x) > .2]
+ return st[-1] if st else 1e9
res = {}
for beat, e in cache.items():
words, dur, cues = e['words'], e['dur'], []
@@ -35,6 +43,10 @@ for beat, e in cache.items():
cues += [[round(t + k * step, 3), x] for k, x in enumerate(sh)]
if j + 1 < len(words) and words[j + 1][0] - end > .12: cues.append([round(end, 3), 'X']) # a pause closes the mouth
cues.append([round(min(dur, (words[-1][0] + .55) if words else 0), 3), 'X'])
- res[beat] = cues
+ f = out / f'{beat}.mp3'
+ if f.exists():
+ se = speech_end(f) - LAG + .05 # (LAG is added below)
+ cues = [c for c in cues if c[0] < se] + [[round(se, 3), 'X']] if se < cues[-1][0] else cues
+ res[beat] = [[round(t + LAG, 3), k] for t, k in cues]
(out / 'mouths.js').write_text('window.MOUTHS = ' + json.dumps(res, separators=(',', ':')) + ';\n')
print(len(res), 'beats;', next(iter(res.items()), None))