diff --git a/android/app/src/main/java/com/doubao/lynxclone/speech/AudioIo.kt b/android/app/src/main/java/com/doubao/lynxclone/speech/AudioIo.kt index 1a68639..c36cdee 100644 --- a/android/app/src/main/java/com/doubao/lynxclone/speech/AudioIo.kt +++ b/android/app/src/main/java/com/doubao/lynxclone/speech/AudioIo.kt @@ -545,17 +545,31 @@ class PcmPlayer { * 放完收尾。 * * ★ 不能立刻 stop():MODE_STREAM 下 stop() 会**把缓冲区里还没播的音频丢掉**, - * 最后半句就没了。这里等播放头追上来再释放。 + * 最后半句就没了。所以这里等播放头追上再释放。 + * + * ★★ 等多久必须**按音频长度算出来**,不能写死。 + * 之前这里是个 15 秒的死线,结果:一段 210 字的回答有 38.8 秒音频 + * (实测 PCM 24kHz 单声道 = 48000 字节/秒),播到 15 秒就被 stop() 砍掉 —— + * 用户听到的就是"读一半就不读了"。长回答越长,砍得越早,症状越明显。 + * + * 这里按 frames / 采样率 算出真实时长,再加 5 秒余量应付播放头更新延迟。 */ fun drainAndRelease() { val t = track ?: return // 每帧 2 字节(单声道 16bit) val totalFrames = writtenBytes / 2 - val deadline = System.currentTimeMillis() + 15_000 + if (totalFrames <= 0) { + stop() + return + } + + val expectMs = totalFrames * 1000L / SAMPLE_RATE + 5_000L + val deadline = System.currentTimeMillis() + expectMs while (System.currentTimeMillis() < deadline) { val played = try { t.playbackHeadPosition.toLong() and 0xFFFFFFFFL } catch (_: Exception) { + // 已经被 stop/release 了(用户点了停止)—— 直接收工 break } if (played >= totalFrames) break diff --git a/scripts/mock-llm.mjs b/scripts/mock-llm.mjs index b8c4478..8499f25 100644 --- a/scripts/mock-llm.mjs +++ b/scripts/mock-llm.mjs @@ -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); diff --git a/scripts/probe-long-tts.py b/scripts/probe-long-tts.py new file mode 100644 index 0000000..68b17be --- /dev/null +++ b/scripts/probe-long-tts.py @@ -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}')