import json, wave, numpy as np, soundfile as sf script = json.load(open("script.json")) best = {b["i"]: b["seed"] for b in json.load(open("best.json"))} OVR = {0: "out3/00_v0_s31", 4: "out3/04_v0_s22", 7: "out3/07_v0_s31", 15: "out3/15_v0_s23"} SR = 24000 LEAD, GAP_S, GAP_SCENE, TAIL = 0.4, 0.3, 0.65, 1.5 def sil(d): return np.zeros(int(d * SR), np.float32) def trim(a): env = np.convolve(np.abs(a), np.ones(240) / 240, mode="same") idx = np.where(env > 0.012)[0] a = a[max(0, idx[0] - 600): idx[-1] + 4800] f = int(0.02 * SR); a[:f] *= np.linspace(0, 1, f); a[-f:] *= np.linspace(1, 0, f) return a out = [sil(LEAD)]; t = LEAD; timing = []; k = 0 for sc in script: sents = []; sc_start = t for i, text in enumerate(sc["s"]): src = f"/home/claude/agent1/tts/{OVR[k]}.wav" if k in OVR else f"/home/claude/agent1/tts/out/s{best[k]}/{k:02d}.wav" a, sr = sf.read(src, dtype="float32"); k += 1 assert sr == SR a = trim(a) a = a / max(1e-6, np.percentile(np.abs(a), 99.5)) * 0.7 # even loudness per line 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 = np.clip(a / np.abs(a).max() * 0.89, -1, 1) with wave.open("voice_gooya.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_gooya.json", "w"), ensure_ascii=False, indent=1) print("total", round(t, 2))