Files
JIANT/tools/video/tutorial_voice.py
T
Claude 30a50ddac5 The tutorial film (Spanish): script, voice-over, footage, the page
tools/video/tutorial_script.py: 17 chapters, each line its words and the step
of the chapter's graphic. tutorial_voice.py speaks them (Kokoro, local ONNX
text-to-speech) and lays out the timeline; capture_tutorial.c plays a dark,
evolving piece on the real engines arranged by the chapters while the screen
goes to what each line explains; tutorial.html + tutorial_body.js draw each
frame (black, Chakra Petch, the heat palette, the beings from the design
studies, the FM-1's real screen, an animated graphic a chapter, subtitles).
film.mjs: the tutorial film, AUDIO= the mix, PREVIEW_PRE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-10-16 09:49:14 +00:00

49 lines
2.6 KiB
Python

# SPDX-License-Identifier: GPL-3.0-only
# (JIANT 0.8.0) the tutorial film's voice-over: each line of tools/video/tutorial_script.py spoken (Kokoro, an ONNX
# text-to-speech model, run locally: pip install kokoro-onnx soundfile; the model and voices from the kokoro-onnx
# releases in $KOKORO, default ~/tts), laid end to end with pauses; the timeline the capture and the film follow.
# python3 tools/video/tutorial_voice.py build/tut [voice] [speed]
# -> build/tut/voice.wav (24 kHz mono), build/tut/timeline.json, build/tut/plan.txt (capture_tutorial.c's chapters)
import json, os, sys
from pathlib import Path
import numpy as np
import soundfile as sf
sys.path.insert(0, str(Path(__file__).resolve().parent))
from tutorial_script import CHAPTERS, say
from kokoro_onnx import Kokoro
D = Path(sys.argv[1]); D.mkdir(parents=True, exist_ok=True)
VOICE = sys.argv[2] if len(sys.argv) > 2 else 'em_alex'
SPEED = float(sys.argv[3]) if len(sys.argv) > 3 else 0.94
KD = Path(os.environ.get('KOKORO', Path.home() / 'tts'))
k = Kokoro(str(KD / 'kokoro.onnx'), str(KD / 'voices.bin'))
SR = 24000
LEAD, LINE_GAP, CH_GAP, TAIL = 4.0, 0.55, 2.2, 9.0 # (the intro's beings form first; the outro breathes after)
out, t, chapters = [np.zeros(int(LEAD * SR), np.float32)], LEAD, []
for ci, c in enumerate(CHAPTERS):
ch = dict(id=c['id'], k=c['k'], title=c['title'], g=c['g'], start=t, lines=[])
for text, cue in c['lines']:
a, sr = k.create(say(text), voice=VOICE, speed=SPEED, lang='es')
assert sr == SR
a = a.astype(np.float32)
nz = np.nonzero(np.abs(a) > 0.004)[0] # (the model's own silence at both ends trimmed)
if len(nz): a = a[max(0, nz[0] - 240): nz[-1] + 1200]
ch['lines'].append(dict(text=text, cue=cue, start=round(t, 3), end=round(t + len(a) / SR, 3)))
out.append(a); t += len(a) / SR
out.append(np.zeros(int(LINE_GAP * SR), np.float32)); t += LINE_GAP
print(f'{c["id"]:8s} {t:7.2f} {text[:60]}')
gap = CH_GAP if ci + 1 < len(CHAPTERS) else TAIL
out.append(np.zeros(int(gap * SR), np.float32)); t += gap
ch['end'] = round(t, 3)
chapters.append(ch)
v = np.concatenate(out)
v = v / (np.abs(v).max() + 1e-9) * 0.89
sf.write(D / 'voice.wav', v, SR)
json.dump(dict(end=round(t, 3), voice=VOICE, chapters=chapters), open(D / 'timeline.json', 'w'), ensure_ascii=False, indent=1)
with open(D / 'plan.txt', 'w') as f: # capture_tutorial.c: "seconds chapter cue" a line
f.write(f'{t:.3f} end 0\n')
for ch in chapters:
for ln in ch['lines']:
f.write(f'{ln["start"]:.3f} {ch["id"]} {ln["cue"]}\n')
print(D / 'voice.wav', f'{t:.1f} s')