Files
doubao-clone/scripts/verify-tts-device-stream.mjs
l 7a39d73295 修:语音"没有实时读"(流式下发成了 mp3)+ 附件上限提到 100MB
【一、流式朗读为什么没生效】
用户报"语音还是没有实时读"。根因是我自己埋的:

  fmt = (speech.get('volcEncoding') or 'pcm').strip() or 'pcm'

volcEncoding 的**默认值是 mp3**,所以 `or 'pcm'` 永远不触发 —— 内核一直
下发 mp3,而播放端是把字节直接喂 AudioTrack 的、没有解码器,
一看不是 PCM 就放弃流式、退回"整段收完再播"。整条链路白做。

更糟的是我在 AudioIo.kt 里写了句"内核那边 volcEncoding 默认给 pcm",
跟事实相反,等于给自己留了个假线索。

修:流式路径**强制 PCM**。这不是用户偏好而是硬要求 ——
AudioTrack 没有解码器。volcEncoding 仍然管非流式那条路(/api/tts),
那条客户端用 MediaPlayer,mp3 没问题。

★ 测试为什么没抓到:verify-tts-route.mjs 里写死了 volcEncoding: 'pcm',
一直是绿的;而真机上是默认的 mp3。**测试跑的不是用户会遇到的那个配置**。
现在改成故意不写 volcEncoding,跑默认配置,并加了一条 Content-Type 必须是
PCM 的断言。新增 verify-tts-device-stream.mjs 直接在实体机上验。

实体机实测(配置里编码还是 mp3):Content-Type=audio/L16、45 块、
首块 1178ms 末块 2002ms —— 确实边生成边下发。

【二、附件上限 25MB → 100MB】
联动改了五处,少改一处就会先被别的限制挡下来:
  uploads.py / uploads.mjs  COPY_LIMIT        25MB → 100MB
  app.py  /api/upload 请求体                   40MB → 150MB
  app.py  _read_body_raw(socket 层)          40MB → 150MB
  index.mjs /api/upload 请求体                 40MB → 150MB
  ChatScreen.kt MAX_UPLOAD_BYTES              25MB → 100MB

150MB 是这么算的:100MB 的文件 base64 后约 133MB,再套一层 JSON。
注释里写明了这几个值是联动的,改一个要改另一个。

⚠ 内存:一次上传峰值约 文件大小 × 2.4(原始 JSON 串 + 解码结果),
100MB 要吃 240MB 左右,低端机可能被系统杀掉。真要传超大文件,
正确做法是分片上传而不是继续抬这个数 —— 这条也写进注释了。
2026-10-10 02:59:16 +08:00

85 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 在实体机上实测:流式朗读到底通没通。
*
* 这是用户报"语音还是没有实时读"之后加的针对性验收。
* 之前的教训:verify-tts-route.mjs 里写死了 volcEncoding=pcm,所以一直是绿的,
* 而真机上是默认的 mp3 —— 内核跟着配置走就永远发 mp3,播放端拿不到 PCM
* 只能退回整段播放。**测试必须跑设备上的真实配置**。
*
* 跑法: node scripts/verify-tts-device-stream.mjs 3B1F4FE65NHWCLXP
*/
import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const SERIAL = process.argv[2] || '3B1F4FE65NHWCLXP';
const ADB = process.env.ADB || 'C:\\Android\\Sdk\\platform-tools\\adb.exe';
const sh = (cmd, timeout = 180000) =>
spawnSync(ADB, ['-s', SERIAL, 'shell', cmd], { encoding: 'utf8', timeout, maxBuffer: 16 * 1024 * 1024 });
// ① 看设备上真实生效的语音配置
const st = JSON.parse(sh('curl -s --max-time 15 http://127.0.0.1:5199/api/speech').stdout || '{}');
console.log(` ① 设备当前配置:引擎=${st.speech?.ttsEngine} 编码=${st.speech?.volcEncoding ?? '(默认)'} 有Key=${st.speech?.hasVolcApiKey}`);
// ② 在设备进程里打流式接口,既量时序也验 Content-Type
const probe = [
'import time, json, urllib.request',
'body = json.dumps({"text": "人工智能正在改变我们生活的方方面面,从早上叫醒我们的智能音箱,到通勤路上的导航软件。"}).encode("utf-8")',
'req = urllib.request.Request("http://127.0.0.1:5199/api/tts/stream", data=body, method="POST")',
'req.add_header("Content-Type", "application/json")',
't0 = time.time()',
'resp = urllib.request.urlopen(req, timeout=180)',
'ctype = resp.headers.get("Content-Type") or ""',
'first = None; last = None; total = 0; chunks = 0',
'while True:',
' buf = resp.read(8192)',
' if not buf: break',
' chunks += 1; total += len(buf)',
' now = time.time() - t0',
' if first is None: first = now',
' last = now',
"print('CTYPE', ctype)",
"print('CHUNKS', chunks, 'BYTES', total)",
"print('FIRST_MS', round((first or 0) * 1000))",
"print('LAST_MS', round((last or 0) * 1000))",
].join('\n');
const probeFile = path.join(os.tmpdir(), `doubao-stream-probe-${Date.now()}.json`);
fs.writeFileSync(probeFile, JSON.stringify({ code: probe }), 'utf8');
const tool = spawnSync(
process.execPath,
[path.join(ROOT, 'scripts', 'device-tool.mjs'), SERIAL, 'python_run', `@${probeFile}`],
{ encoding: 'utf8', timeout: 240000, maxBuffer: 16 * 1024 * 1024 },
);
try { fs.rmSync(probeFile, { force: true }); } catch { /* 忽略 */ }
const out = `${tool.stdout || ''}${tool.stderr || ''}`;
const num = (k) => Number((out.match(new RegExp(`${k}\\s+([0-9]+)`)) || [])[1] || 0);
const ctype = (out.match(/CTYPE\s+([^\s\\]+)/) || [])[1] || '';
const first = num('FIRST_MS');
const last = num('LAST_MS');
console.log(' ② 设备端流式实测:');
console.log(` Content-Type = ${ctype || '(没拿到)'}`);
console.log(` ${num('CHUNKS')} 块 / ${num('BYTES')} 字节`);
console.log(` 首块 ${first}ms 末块 ${last}ms`);
let failed = 0;
if (!/L16|pcm/i.test(ctype)) {
console.log(' [FAIL] 不是 PCM —— 播放端没有解码器,只会退回整段播放(用户感觉就是没实时读)');
failed += 1;
}
if (!first || first >= last * 0.6) {
console.log(' [FAIL] 首块没有明显早于末块,不像流式');
failed += 1;
}
if (!failed) console.log(' [OK] 安卓端确实在边生成边下发 PCM');
if (!out.includes('FIRST_MS')) console.log(` 原始输出: ${out.replace(/\s+/g, ' ').slice(0, 400)}`);
console.log(failed ? '\n 失败' : '\n 通过');
process.exit(failed ? 1 : 0);