fix: 修复实时语音 asr_start 直接失败,并补齐全链路诊断与 EBML 头裁剪
- websocket.js 补上缺失的 logger 导入:asr_start 里 logger.info 抛 ReferenceError 被 catch 兜住, 客户端只会收到「语音识别服务暂不可用」,实时回显整条链路直接失效 - asr_start 失败时中止本次会话,避免遗留半截 asrSession 让后续分片误判 - onError / 降级 / 空音频收尾统一走 logger,记录 format/bytes/partialSeen,便于区分 「没发分片」「解码失败」「供应商失败」三种情况 - 首帧嗅探失败时记录前 48 字节 hex;新增 findEbmlHeader,在头 4KB 内定位 EBML 魔数, 命中后裁掉前导字节按 WebM 继续流式解码(开发者工具分片头不一定在偏移 0) - WebM 首帧只有容器头、解不出 PCM 时不提前建供应商会话
This commit is contained in:
@@ -13,6 +13,26 @@ import decodeWebm, { decoder as createWebmDecoder } from '@audio/decode-webm';
|
||||
/** WebM / Matroska 的 EBML 魔数:1A 45 DF A3 */
|
||||
const EBML_MAGIC = 0x1a45dfa3;
|
||||
|
||||
/**
|
||||
* 在头部窗口内定位 EBML 头
|
||||
*
|
||||
* 有些平台的 onFrameRecorded 首片前面会带少量前导字节(半帧 / padding),
|
||||
* 容器头不在偏移 0,直接嗅探会判成 unknown;EBML 魔数是 4 字节常量,
|
||||
* 在小窗口内扫描不会误判,找到后裁掉前导字节即可继续按 WebM 流式解码。
|
||||
*
|
||||
* @param {Buffer|Uint8Array} audio - 已累积的帧头字节
|
||||
* @param {number} limit - 最多扫描多少字节
|
||||
* @returns {number} 魔数偏移,找不到返回 -1
|
||||
*/
|
||||
export function findEbmlHeader(audio, limit = 4096) {
|
||||
const buf = Buffer.isBuffer(audio) ? audio : Buffer.from(audio);
|
||||
const last = Math.min(buf.length - 4, limit);
|
||||
for (let offset = 0; offset <= last; offset += 1) {
|
||||
if (buf.readUInt32BE(offset) === EBML_MAGIC) return offset;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按文件头判断音频容器
|
||||
* @param {Buffer|Uint8Array} audio - 原始音频字节
|
||||
|
||||
Reference in New Issue
Block a user