"""长文本合成:确认上游会不会截断。 "读一半就不读了"有两个可能:客户端播到一半被砍,或者上游只合成了一半。 这两个得分开验 —— 先量上游。 用法: VOLC_KEY=xxx python scripts/probe-long-tts.py """ import os import sys import time sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'android', 'kernel')) import speech # noqa: E402 key = os.environ.get('VOLC_KEY', '').strip() if not key: print(' 需要 VOLC_KEY') sys.exit(2) settings = { 'speech': { 'ttsEngine': 'volcv3', 'volcApiKey': key, 'ttsVoice': 'zh_female_vv_uranus_bigtts', }, } # 三档长度:短句 / 中等 / 很长(模拟一段长回答) BASE = '人工智能正在改变我们生活的方方面面,从早上叫醒我们的智能音箱,到通勤路上的导航软件。' cases = [ ('短(约 40 字)', BASE), ('中(约 200 字)', BASE * 5), ('长(约 800 字)', BASE * 20), ] for name, text in cases: t0 = time.time() chunks = 0 total = 0 first = None err = None try: for _mime, chunk in speech.synthesize_stream(text, settings): chunks += 1 total += len(chunk) if first is None: first = time.time() - t0 except speech.SpeechError as exc: err = str(exc) dur = time.time() - t0 # PCM 24kHz 单声道 16bit = 48000 字节/秒 seconds = total / 48000.0 if total else 0 print(f'\n [{name}] 输入 {len(text)} 字') if err: print(f' 失败: {err}') continue print(f' {chunks} 块 / {total} 字节 → 约 {seconds:.1f} 秒音频') print(f' 首块 {first * 1000:.0f}ms 整段耗时 {dur * 1000:.0f}ms') # 中文正常语速约 4~5 字/秒,拿这个反推合成了多少字,看有没有被截 expect_seconds = len(text) / 4.5 ratio = seconds / expect_seconds if expect_seconds else 0 flag = 'OK' if ratio > 0.7 else '疑似被截断' print(f' 按 4.5 字/秒估算应有 {expect_seconds:.1f} 秒,实际 {seconds:.1f} 秒({ratio:.2f}x)→ {flag}')