/** * 在实体机上实测:流式朗读到底通没通。 * * 这是用户报"语音还是没有实时读"之后加的针对性验收。 * 之前的教训: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);