"""Voxi Lite streaming latency on this machine: time to first audio and playback underruns, sentence vs clause chunking, plus the phrase cache. python tests/latency.py [--threads 4] [--voices payal,arjun] [--out latency.json] For each reply, chunks are rendered in order (as stream() does) while a simulated player starts the moment the first chunk is ready and plays chunks back to back. An underrun is a moment the player has finished and the next chunk is not ready yet (the caller hears a gap); reported as count and total seconds. """ import argparse import json import sys import time from pathlib import Path import numpy as np sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from voxi import SR, VoxiLite # noqa: E402 REPLIES = [ ("hi_bill", "hi", "नमस्ते, मैं Voxi से बोल रही हूँ। आपका बकाया ₹1,250 है, जो 15 तारीख़ तक भरना है। क्या मैं पेमेंट लिंक भेज दूँ?"), ("hi_order", "hi", "आपका ऑर्डर कल शाम तक पहुँच जाएगा और ट्रैकिंग लिंक आपको एसएमएस पर भेज दिया गया है ताकि आप उसे देख सकें।"), ("hi_short", "hi", "जी, बिल्कुल। मैं अभी चेक करती हूँ।"), ("hinglish", "hinglish", "Aapka refund process ho gaya hai, aur paise teen se paanch din mein aapke account mein aa jayenge."), ("ta_agent", "ta", "உங்கள் ஆர்டர் நாளை மாலைக்குள் வந்துவிடும், கண்காணிப்பு இணைப்பு SMS மூலம் அனுப்பப்பட்டுள்ளது. வேறு ஏதாவது உதவி வேண்டுமா?"), ("en_agent", "en", "Thank you for calling. Your refund of rupees 2,400 has been processed and it will reach your account in three to five working days."), ] GREETING = "नमस्ते, मैं Voxi से बोल रही हूँ।" def run(tts, text, lang, voice, chunking): t0 = time.perf_counter(); ready, durs = [], [] for y in tts.stream(text, lang, voice, chunking=chunking): ready.append(time.perf_counter() - t0); durs.append(len(y) / SR) play_end, under, n_under = ready[0], 0.0, 0 for r, d in zip(ready, durs): if r > play_end + 1e-3: under += r - play_end; n_under += 1; play_end = r play_end += d return {"first_s": round(ready[0], 3), "chunks": len(ready), "audio_s": round(sum(durs), 2), "underruns": n_under, "underrun_s": round(under, 3)} def main(): ap = argparse.ArgumentParser() ap.add_argument("--threads", type=int, default=4); ap.add_argument("--voices", default="payal,arjun") ap.add_argument("--out", default="") a = ap.parse_args() tts = VoxiLite(threads=a.threads) tts.synthesize("नमस्ते।", "hi") # warm-up rows = [] for voice in a.voices.split(","): for name, lang, text in REPLIES: for chunking in ("sentence", "clause"): tts._cache.clear() r = {"reply": name, "voice": voice, "chunking": chunking, **run(tts, text, lang, voice, chunking)} rows.append(r); print(json.dumps(r, ensure_ascii=False), flush=True) summ = {} for chunking in ("sentence", "clause"): rs = [r for r in rows if r["chunking"] == chunking] f = [r["first_s"] for r in rs] summ[chunking] = {"first_s_median": round(float(np.median(f)), 3), "first_s_p90": round(float(np.percentile(f, 90)), 3), "first_s_max": round(max(f), 3), "replies_with_underrun": sum(r["underruns"] > 0 for r in rs), "underrun_s_total": round(sum(r["underrun_s"] for r in rs), 2), "n": len(rs)} tts._cache.clear() tts.warm([GREETING], "hi", a.voices.split(",")) t0 = time.perf_counter(); next(tts.stream(GREETING, "hi", a.voices.split(",")[0])); cached = time.perf_counter() - t0 summ["cached_greeting_first_s"] = round(cached, 4) print("SUMMARY", json.dumps(summ), flush=True) if a.out: Path(a.out).write_text(json.dumps({"summary": summ, "rows": rows}, indent=1, ensure_ascii=False), encoding="utf-8") if __name__ == "__main__": main()