import json, wave, sys, numpy as np from kokoro_onnx import Kokoro voice = sys.argv[1] if len(sys.argv) > 1 else "af_heart" speed = float(sys.argv[2]) if len(sys.argv) > 2 else 1.0 K = Kokoro("/home/claude/dl/kokoro/kokoro-v1.0.onnx", "/home/claude/dl/kokoro/voices-v1.0.bin") script = json.load(open("script_en.json")) LEAD, GAP_S, GAP_SCENE, TAIL = 0.4, 0.3, 0.65, 1.5 out = []; t = 0; timing = []; SR = 24000 def sil(d): return np.zeros(int(d * SR), np.float32) out.append(sil(LEAD)); t = LEAD for sc in script: sents = []; sc_start = t for i, text in enumerate(sc["s"]): a, SR = K.create(text, voice=voice, speed=speed, lang="en-us") a = np.asarray(a, np.float32) idx = np.where(np.abs(a) > 0.01)[0]; a = a[max(0, idx[0] - 300): idx[-1] + 2400] sents.append({"text": text, "start": round(t, 3), "dur": round(len(a) / SR, 3)}) out.append(a); t += len(a) / SR g = GAP_S if i < len(sc["s"]) - 1 else GAP_SCENE out.append(sil(g)); t += g timing.append({"id": sc["id"], "start": round(sc_start, 3), "end": round(t, 3), "s": sents}) out.append(sil(TAIL)); t += TAIL; timing[-1]["end"] = round(t, 3) a = np.concatenate(out); a = a / np.abs(a).max() * 0.89 with wave.open("voice_en.wav", "wb") as w: w.setnchannels(1); w.setsampwidth(2); w.setframerate(SR); w.writeframes((a * 32767).astype(np.int16).tobytes()) json.dump({"total": round(t, 3), "scenes": timing}, open("timing_en.json", "w"), indent=1) print("total", round(t, 2))