"""Synthetic ASR training speech (no external API): VieNeu-TTS (Apache-2.0; vieneu==3.8.3, 2026-09-23) reads the clean synth texts (names, products, places of the 11 new apps) in voices cloned from TRAIN audio (dev300 excluded). Writes 16 kHz wavs + /extra.jsonl {audio, text} for asr_ft.py --extra. Resumable. usage: python tts_gen.py --root DATA --raw synth_v5_raw.jsonl synth4_raw.jsonl --exclude dev300.txt --n 4000 --out tts_v1 """ import argparse import json import random import time from pathlib import Path import numpy as np import soundfile as sf def main(): ap = argparse.ArgumentParser() ap.add_argument("--root", required=True) ap.add_argument("--raw", nargs="+", required=True) ap.add_argument("--exclude", required=True) ap.add_argument("--n", type=int, default=4000) ap.add_argument("--out", required=True) ap.add_argument("--limit-sec", type=float, default=0, help="stop after this many seconds (0 = no limit)") a = ap.parse_args() rng = random.Random(0) root, out = Path(a.root), Path(a.out) (out / "wav").mkdir(parents=True, exist_ok=True) texts = [] for f in a.raw: for l in open(f, encoding="utf-8"): for t in json.loads(l)["item"]["_meta"].get("clean", {}).values(): if 3 <= len(t.split()) <= 45: texts.append(t.strip()) texts = list(dict.fromkeys(texts)) rng.shuffle(texts) texts = texts[: a.n] excl = {l.strip() for l in open(a.exclude)} refs = [] for l in open(root / "train" / "train.jsonl", encoding="utf-8"): r = json.loads(l) if r["id"] in excl: continue p = root / "train" / r["audio"] info = sf.info(p) if 3.0 <= info.duration <= 9.0: refs.append(str(p)) print(f"{len(texts)} texts, {len(refs)} reference voices", flush=True) from vieneu import Vieneu tts = Vieneu() done_f = out / "extra.jsonl" done = {json.loads(l)["audio"] for l in open(done_f, encoding="utf-8")} if done_f.exists() else set() fo = open(done_f, "a", encoding="utf-8") t0, n_new = time.time(), 0 for i, t in enumerate(texts): wp = str((out / "wav" / f"{i:05d}.wav").resolve()) if wp in done: continue ref = refs[(i * 7919) % len(refs)] try: audio = tts.infer(t, ref_audio=ref) tts.save(audio, str(out / "tmp.wav")) # let the library write its own sample rate, then resample wav, sr = sf.read(str(out / "tmp.wav"), dtype="float32") wav = wav.mean(axis=1) if wav.ndim == 2 else wav if sr != 16000: import librosa wav = librosa.resample(wav, orig_sr=sr, target_sr=16000) if not (0.5 < len(wav) / 16000 < 30): continue sf.write(wp, wav, 16000) fo.write(json.dumps({"audio": wp, "text": t, "ref": ref}, ensure_ascii=False) + "\n") fo.flush() n_new += 1 except Exception as e: print("skip", i, repr(e)[:120], flush=True) if n_new and n_new % 50 == 0: el = time.time() - t0 print(f"{len(done) + n_new}/{len(texts)} {el:.0f}s ({el / n_new:.2f} s/utt)", flush=True) if a.limit_sec and time.time() - t0 > a.limit_sec: break print("TTS_DONE", len(done) + n_new, flush=True) if __name__ == "__main__": main()