修:语音读一半就不读了(收尾死线写死 15 秒)

用户报"读一半就不读了"。先量上游确认不是它截断 —— 结果上游完全正常:

    42 字 →  375304 字节 →  7.8 秒音频
   210 字 → 1861802 字节 → 38.8 秒音频
   840 字 → 6856642 字节 → 142.8 秒音频

(PCM 24kHz 单声道 16bit = 48000 字节/秒)

所以问题在客户端,而且数字对得严丝合缝:一段 210 字的回答有 38.8 秒音频,
而 PcmPlayer.drainAndRelease 里是个**写死的 15 秒死线** ——

    val deadline = System.currentTimeMillis() + 15_000
    while (...) { if (played >= totalFrames) break; sleep(50) }
    stop()   // ← 超时就 stop(),把还没播完的音频砍掉

15 秒一到就 stop(),用户听到的就是"读一半就不读了"。回复越长砍得越早。

修:按音频长度算等待时间,不再写死。

    expectMs = totalFrames * 1000 / SAMPLE_RATE + 5000

多给 5 秒余量应付播放头更新延迟。用户中途点停止时,playbackHeadPosition
会抛(轨道已释放)→ break,照样立刻收工。

顺带给 mock 加了 MOCK_REPLY_CHARS:**有一类 bug 只有长内容才暴露**
(这次的"播一半被砍"就是),拿短回复测永远是绿的。
    MOCK_REPLY_CHARS=400 node scripts/mock-llm.mjs

关于验证:写了个自动量"播放时长"的脚本,但这台设备的 UI 自动化不稳
(打字/点击经常打空,消息压根发不出去),测出来的"没播放"是假的,
所以删掉了 —— 与其留个会误报的验收脚本,不如不留。
这次的证据是"上游时长 vs 客户端死线"的硬数字对比,很确定;
真机上播完没播完还得靠耳朵确认。

附:测试过程中临时改过设备上的 baseUrl/autoRead,跑完已完整恢复
(provider/baseUrl/model/Key/昵称/18 个会话/语音 Key 都核对过了)。
This commit is contained in:
l
2026-10-10 03:30:00 +08:00
parent c506f999c0
commit 27b62ce962
3 changed files with 103 additions and 3 deletions

View File

@@ -9,6 +9,26 @@ import http from 'node:http';
const PORT = Number(process.env.MOCK_PORT || 5299);
/**
* 回复长度(字符数)。默认是原来那段短回答。
*
* 为什么做成可配:有一类 bug **只有长内容才暴露** —— 比如朗读播到一半被砍
* (收尾死线写死 15 秒,而一段 210 字的回答有 38.8 秒音频)。
* 拿短回复测永远是绿的,必须能造出长回复。
*
* MOCK_REPLY_CHARS=400 node scripts/mock-llm.mjs
*/
const REPLY_CHARS = Number(process.env.MOCK_REPLY_CHARS || 0);
/** 造一段指定字数的中文回复 */
function longReply(chars) {
const unit = '人工智能正在改变我们生活的方方面面,从早上叫醒我们的智能音箱,'
+ '到通勤路上推荐路线的导航软件,再到工作中帮我们整理资料的助手。';
let out = '';
while (out.length < chars) out += unit;
return out.slice(0, chars);
}
function sse(res, obj) {
res.write(`data: ${JSON.stringify(obj)}\n\n`);
}
@@ -180,7 +200,9 @@ const server = http.createServer(async (req, res) => {
}
const toolText = toolMsg ? String(toolMsg.content) : '(没拿到工具结果)';
const answer = `我用 calc 工具算了一下:${toolText}\n\n所以 (1+2)*3 = 9。这条回答是端到端跑通的:界面 → 内核 → 工具调用 → 流式返回。`;
const answer = REPLY_CHARS > 0
? longReply(REPLY_CHARS)
: `我用 calc 工具算了一下:${toolText}\n\n所以 (1+2)*3 = 9。这条回答是端到端跑通的:界面 → 内核 → 工具调用 → 流式返回。`;
for (const ch of answer) {
sse(res, { choices: [{ delta: { content: ch } }] });
await sleep(12);

64
scripts/probe-long-tts.py Normal file
View File

@@ -0,0 +1,64 @@
"""长文本合成:确认上游会不会截断。
"读一半就不读了"有两个可能:客户端播到一半被砍,或者上游只合成了一半。
这两个得分开验 —— 先量上游。
用法: 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}')