#!/usr/bin/env python3 """Streaming client / latency benchmark for stream_server.py. Simulates an LLM emitting text at --cps characters per second (0 = all at once) and measures first-audio latency from the first text message, real-time factor, and playback stalls (buffer underruns) assuming playback starts at first audio. python stream_client.py --voice bank_collection_female --text-file demos/text.txt --cps 15 --output out.wav python stream_client.py --concurrency 4 --cps 15 ... """ import argparse import asyncio import json import statistics import time from pathlib import Path import numpy as np import soundfile as sf import websockets DEFAULT_TEXT = '您好,很抱歉让您久等了。我已经为您查询到,您的订单编号是20261113004567,商品金额为128.50元,实际支付108.50元。请您打开APP查看处理进度。' async def one(url, voice, text, cps, sample_rate, seed, output=None, chunk_chars=1, mode='icl', segment=None, speed=1.0): async with websockets.connect(url, max_size=None) as ws: await ws.send(json.dumps(dict(type='start', voice=voice, sample_rate=sample_rate, seed=seed, mode=mode, speed=speed, **({'segment': segment} if segment else {})))) started = json.loads(await ws.recv()) if started.get('type') != 'started': raise RuntimeError(started) t0 = time.perf_counter() arrivals, chunks, done = [], [], None async def feed(): for i in range(0, len(text), chunk_chars): await ws.send(json.dumps(dict(type='text', text=text[i:i + chunk_chars]))) if cps: await asyncio.sleep(chunk_chars / cps) await ws.send(json.dumps(dict(type='end'))) feeder = asyncio.create_task(feed()) async for message in ws: if isinstance(message, bytes): arrivals.append((time.perf_counter() - t0, len(message) // 2 / sample_rate)) chunks.append(np.frombuffer(message, '