"""实测 Python 内核的流式合成:首块多久到、一共几块。 跑法: VOLC_KEY=xxx python scripts/verify-tts-stream.py 注意:输出全部用 ASCII。Windows 控制台默认 GBK,print 个 ✓ 就能让整个脚本崩掉 (真崩过),验收脚本不能因为这种原因失败。 """ import os import sys import time sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'android', 'kernel')) import speech # noqa: E402 key = os.environ.get('VOLC_KEY', '').strip() if not key: print(' 需要 VOLC_KEY 环境变量') sys.exit(2) TEXT = ('人工智能正在改变我们生活的方方面面。从早上叫醒我们的智能音箱,' '到通勤路上推荐路线的导航软件,再到工作中帮我们整理资料的助手。') failed = 0 for encoding in ('pcm', 'mp3'): settings = { 'speech': { 'ttsEngine': 'volcv3', 'volcApiKey': key, 'ttsVoice': 'zh_female_vv_uranus_bigtts', 'volcEncoding': encoding, 'ttsSpeed': 1.0, }, } t0 = time.time() chunks = 0 total = 0 first_at = None mime = '' try: for mime, chunk in speech.synthesize_stream(TEXT, settings): chunks += 1 total += len(chunk) if first_at is None: first_at = time.time() - t0 except speech.SpeechError as exc: print(f' [{encoding}] 失败: {exc}') failed += 1 continue last_at = time.time() - t0 # 首块明显早于末块 = 边生成边下发。用 0.6 当阈值: # 真流式下首块通常只占总时长的两成左右,攒完再发的话两者几乎相等。 streamed = first_at is not None and first_at < last_at * 0.6 if not streamed: failed += 1 print(f' [{encoding}] mime={mime} {chunks} 块 / {total} 字节') print(f' 首块 {first_at * 1000:.0f}ms 末块 {last_at * 1000:.0f}ms ' f'{"[OK] 边生成边给" if streamed else "[FAIL] 疑似攒完才给"}') print(f'\n {"全部通过" if failed == 0 else str(failed) + " 项失败"}') sys.exit(1 if failed else 0)