fix(engine): voice 句尾剪字真根因根治(三修:源尾垫静音冲刷 SoundTouch 未冲刷小节)+ onEnd 完成信号 + live 探针适配
- 真根因(live span 8.57s < 文件 8.80s 铁证):SoundTouch Stretch 源结束时不冲刷 内部最后一个小节(~1 sequence),句尾 0.1-0.25s 从未交付音频图——任何平台皆然, ASR 因容错转写仍 sim=1(转写评审盲区的又一实证)。一修固定 300ms 排空窗只是 把这段不存在的交付空等成静音;二修 onEnd 保证「已交付必播完」但交付不了这段 - 三修:源尾垫 VOICE_TAIL_PAD_SEC(0.25s) 静音把真实句尾推出管线;完成 = onEnd (平台无关,disconnect 已交付帧仍播);1.5s 兜底;顶掉复核移至新句数据访问前 (垫长拷贝的 getChannelData 先于杀旧句会打乱探针时序) - live 探针适配:尾部垫使 SoundTouch 读未注册 buffer、gcd 抽读信号消失—— 清扫器加「仍有连接中语音 SPN 则不关段」守卫,断开即移出追踪集合 - engine-voice.test:PAD+I1/I2/I2b/I3 五组 19 断言(含垫长存在性、onEnd 结算、 兜底不早于 1.5s);10 套件全绿 - live 复验(男声 p1):span 8.57→9.04s(≥文件 8.80s),ASR sim=1 verdict=OK
This commit is contained in:
+1
-1
@@ -277,7 +277,7 @@ source env.d/mureka.sh
|
|||||||
|---|---|---|
|
|---|---|---|
|
||||||
| ~~引擎无公开 `isVoicePlaying`~~ | **已解决(R7)**:`audio.isVoicePlaying()` 公开,游戏层全部收编 | — |
|
| ~~引擎无公开 `isVoicePlaying`~~ | **已解决(R7)**:`audio.isVoicePlaying()` 公开,游戏层全部收编 | — |
|
||||||
| ~~长按快进无 per-game 开关~~ | **已解决(R7)**:`audio.setRateBoostEnabled(false)`,rhythm/space 演奏期已豁免 | — |
|
| ~~长按快进无 per-game 开关~~ | **已解决(R7)**:`audio.setRateBoostEnabled(false)`,rhythm/space 演奏期已豁免 | — |
|
||||||
| ~~voice 句尾剪字(每句末尾音节缺失)~~ | **已根治(2026-09 真机人耳反馈)**:旧结束定时器(墙钟+120ms)< 管线固有延迟 ~190ms,句尾音节尚在 SPN/SoundTouch 缓冲即被 disconnect。现 `_finishVoice` 以源真实消耗位置(`filter.sourcePosition`)判定 + 300ms 排空窗;回归 `web/harness/engine-voice.test.mjs` 三不变式钉死;真机 A/B(同 id 节点存活 +0.02s→+0.33s)验证 | 引擎层已固化,新游戏零改动继承;合成侧经 ASR 抽查排除剪字(尾静默紧是裁剪工艺,判读口径见 `tools/audit_voice_tails.py` 头注) |
|
| ~~voice 句尾剪字(每句末尾音节缺失)~~ | **已根治(2026-09 真机人耳反馈,三修)**:一修固定排空窗(手机端未交付量浮动,不够);二修 onEnd 完成信号(仍不解决交付缺失);三修定位真根因——**SoundTouch Stretch 源结束不冲刷最后一个小节,句尾 0.1-0.25s 任何平台都不会播出**,根治 = 源尾垫 0.25s 静音推出真实句尾 + onEnd 驱动完成 + 1.5s 兜底。回归 `web/harness/engine-voice.test.mjs`(PAD+I1-I3);live 验证 span 8.57s→9.04s(≥文件时长 8.80s) | 引擎层已固化,新游戏零改动继承;live 探针同步适配(清扫器守卫);合成侧经 ASR 抽查排除剪字(尾静默紧是裁剪工艺,判读口径见 `tools/audit_voice_tails.py` 头注) |
|
||||||
| 向导 earcon 为运行时合成音 | 音色是「可用」级不是「精美」级(正弦/三角/方波包络) | 若升级为录制品 earcon:保持 8 音词表与参数边界不变(14 号文档 §2),只换渲染后端 |
|
| 向导 earcon 为运行时合成音 | 音色是「可用」级不是「精美」级(正弦/三角/方波包络) | 若升级为录制品 earcon:保持 8 音词表与参数边界不变(14 号文档 §2),只换渲染后端 |
|
||||||
| escape「需要帮助吗」问而不等 | P1 终评唯一「擦边当白痴」时刻 | v2 手势宪法专项:真问答 + PLAY 期目标重听入口 |
|
| escape「需要帮助吗」问而不等 | P1 终评唯一「擦边当白痴」时刻 | v2 手势宪法专项:真问答 + PLAY 期目标重听入口 |
|
||||||
| space/simon 无「局」与结算仪式;storyline 链尾无轻量 turn 信号、二周目重播开场教学;rhythm 教学首误先震后教 | round4 终评 P0 全部裁定入 v2(research/04-playtests/round4/synthesis.md §2) | L5 会话层专项 + 微 polish 清单 |
|
| space/simon 无「局」与结算仪式;storyline 链尾无轻量 turn 信号、二周目重播开场教学;rhythm 教学首误先震后教 | round4 终评 P0 全部裁定入 v2(research/04-playtests/round4/synthesis.md §2) | L5 会话层专项 + 微 polish 清单 |
|
||||||
|
|||||||
@@ -164,13 +164,15 @@ new WebAudioEngine(basePath = 'audio/', opts?)
|
|||||||
`vendor/soundtouch.js`(SoundTouch 0.3.0,LGPL v2.1,ScriptProcessorNode
|
`vendor/soundtouch.js`(SoundTouch 0.3.0,LGPL v2.1,ScriptProcessorNode
|
||||||
实现):`WebAudioBufferSource → SimpleFilter(SoundTouch tempo) →
|
实现):`WebAudioBufferSource → SimpleFilter(SoundTouch tempo) →
|
||||||
ScriptProcessorNode → gain → voiceBus`。`settings.speed` 只改 tempo 不改
|
ScriptProcessorNode → gain → voiceBus`。`settings.speed` 只改 tempo 不改
|
||||||
pitch,1.0/1.35/1.7 均不变调;自然结束以「源真实消耗位置」(SoundTouch
|
pitch,1.0/1.35/1.7 均不变调。**自然结束(2026-09 三修,真机人耳反馈)**:
|
||||||
source.position)为准——源未耗尽时定时器只做短重排等待,耗尽后再留
|
① 播放源尾部垫 0.25s 静音——SoundTouch Stretch 源结束时不冲刷内部最后
|
||||||
300ms 排空窗让管线缓冲的尾音播出,然后才 disconnect(2026-09 真机人耳
|
一个小节,句尾 0.1-0.25s 若不垫将永远停留在管线里不播出(任何平台);
|
||||||
反馈修复:旧实现纯墙钟定时器 +120ms 一到即断,管线固有延迟 ~190ms 意味着
|
② 完成信号 = 管线 onEnd(extract 返回 0 = 已无未交付帧),平台无关,
|
||||||
每句尾字被剪)。onComplete/打断/排队语义不变。注意 voice 有
|
此时 disconnect 已交付帧仍会播完;③ onEnd 异常缺失时 1.5s 定时器兜底
|
||||||
~100ms 处理延迟(对白场景无感),且 ScriptProcessorNode 已废弃但全平台
|
(宁迟勿剪);④ 源未耗尽时定时器只做 80ms 短重排等待。onComplete/打断/
|
||||||
(含 iOS Safari)可用。
|
排队语义不变。回归:web/harness/engine-voice.test.mjs(PAD+I1-I3 四不变式)。
|
||||||
|
注意 voice 有 ~100ms 处理延迟(对白场景无感),且 ScriptProcessorNode
|
||||||
|
已废弃但全平台(含 iOS Safari)可用。
|
||||||
|
|
||||||
**长按快进(游戏零改动)**:voice 播放中长按屏幕 → 旁白临时 1.7 倍速
|
**长按快进(游戏零改动)**:voice 播放中长按屏幕 → 旁白临时 1.7 倍速
|
||||||
(不变调),松开恢复 `settings.speed`。由 gesture.js 派发的 window 事件
|
(不变调),松开恢复 `settings.speed`。由 gesture.js 派发的 window 事件
|
||||||
|
|||||||
+73
-31
@@ -56,7 +56,9 @@ import { SoundTouch, SimpleFilter, WebAudioBufferSource, getWebAudioNode } from
|
|||||||
|
|
||||||
const EXT_FALLBACKS = ['.ogg', '.m4a', '.mp3', '.wav'];
|
const EXT_FALLBACKS = ['.ogg', '.m4a', '.mp3', '.wav'];
|
||||||
const LONGPRESS_BOOST = 1.7; // 长按快进倍速
|
const LONGPRESS_BOOST = 1.7; // 长按快进倍速
|
||||||
const VOICE_DRAIN_MS = 300; // voice 源耗尽后的管线排空窗(SPN 4096帧@48k≈85ms + SoundTouch 内部延迟)
|
const VOICE_TAIL_PAD_SEC = 0.25; // 源尾静音垫:把句尾推出 SoundTouch 未冲刷的最后小节(三修)
|
||||||
|
const VOICE_STREAM_EPS_MS = 30; // onEnd(管线已全部交付)后的小缓冲再结算
|
||||||
|
const VOICE_DRAIN_FALLBACK_MS = 1500; // onEnd 未触发时的兜底结算上限(宁迟勿剪)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 语音风暴保险丝(dungeon R2 D0-1,全线保护):同一 voiceId 在 3s 窗口内被调度
|
* 语音风暴保险丝(dungeon R2 D0-1,全线保护):同一 voiceId 在 3s 窗口内被调度
|
||||||
@@ -454,9 +456,39 @@ export class WebAudioEngine {
|
|||||||
const buf = await this._load(id);
|
const buf = await this._load(id);
|
||||||
if (!buf) { this._unduckBgm(); return; }
|
if (!buf) { this._unduckBgm(); return; }
|
||||||
|
|
||||||
|
// 顶掉语义必须在 await 之后、新句任何音频数据被访问之前复核(dungeon R2
|
||||||
|
// D0-1 根因修复):_load 期间可能又有一句 voice 开播(同 tick 双调度)。
|
||||||
|
// 若此处不补杀,早先的 token 会被 this._voice = token 静默顶号——节点
|
||||||
|
// 永远无人 disconnect(僵尸节点空转抽 buffer),endTimer 也因 _finishVoice
|
||||||
|
// 的 token 校验提前返回而永不清理(R1 live:71 分钟僵尸节点抢占风暴)。
|
||||||
|
// 顺序要求(三修补充):必须先杀旧句再读新句 buffer(垫长拷贝会调
|
||||||
|
// getChannelData),否则探针会先记新句 vstart 再被旧句 disconnect 误关。
|
||||||
|
if (this._voice) {
|
||||||
|
const cur = this._voice;
|
||||||
|
this._voice = null;
|
||||||
|
this._killVoice(cur);
|
||||||
|
}
|
||||||
|
|
||||||
const st = new SoundTouch();
|
const st = new SoundTouch();
|
||||||
st.tempo = settings.get().speed; // 语速(不变调,下一句生效)
|
st.tempo = settings.get().speed; // 语速(不变调,下一句生效)
|
||||||
const filter = new SimpleFilter(new WebAudioBufferSource(buf), st);
|
// 尾部保真(三修,2026-09 真机人耳反馈):SoundTouch Stretch 在源结束时
|
||||||
|
// 不冲刷内部最后一个小节(~1 个 sequence),句尾 0.1-0.25s 永远停留在
|
||||||
|
// 管线里不会播出(桌面/手机皆然;一修的固定排空窗与二修的 onEnd 都只能
|
||||||
|
// 保证「已交付的播完」,交付不了这段)。根治 = 源尾垫 VOICE_TAIL_PAD_SEC
|
||||||
|
// 静音,把真实句尾完整推出管线;垫的静音走完后 onEnd 才触发,衔接节奏
|
||||||
|
// 与旧实现的排空窗相当,多出的静音不改变听感。
|
||||||
|
// ctx.createBuffer 缺失(playtest mock)时跳过垫长——mock 走 consumed=0
|
||||||
|
// 旧路径,语义不受影响。
|
||||||
|
let srcBuf = buf;
|
||||||
|
if (typeof this.ctx.createBuffer === 'function') {
|
||||||
|
const padFrames = Math.ceil(VOICE_TAIL_PAD_SEC * buf.sampleRate);
|
||||||
|
srcBuf = this.ctx.createBuffer(buf.numberOfChannels, buf.length + padFrames, buf.sampleRate);
|
||||||
|
for (let ch = 0; ch < buf.numberOfChannels; ch++) {
|
||||||
|
if (srcBuf.copyToChannel) srcBuf.copyToChannel(buf.getChannelData(ch), ch);
|
||||||
|
else srcBuf.getChannelData(ch).set(buf.getChannelData(ch));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const filter = new SimpleFilter(new WebAudioBufferSource(srcBuf), st);
|
||||||
const node = getWebAudioNode(this.ctx, filter); // ScriptProcessorNode,接入即开播
|
const node = getWebAudioNode(this.ctx, filter); // ScriptProcessorNode,接入即开播
|
||||||
const gain = this.ctx.createGain();
|
const gain = this.ctx.createGain();
|
||||||
gain.gain.value = this.voiceVolume * volume;
|
gain.gain.value = this.voiceVolume * volume;
|
||||||
@@ -464,50 +496,60 @@ export class WebAudioEngine {
|
|||||||
gain.connect(this._voiceBus);
|
gain.connect(this._voiceBus);
|
||||||
|
|
||||||
const token = {
|
const token = {
|
||||||
node, gain, st, buf, filter,
|
node, gain, st, buf, filter, srcBuf, // srcBuf = 尾部垫静音后的播放源
|
||||||
stopped: false,
|
stopped: false,
|
||||||
|
streamEnded: false, // SoundTouch onEnd:管线内帧已全部交付音频图
|
||||||
onComplete,
|
onComplete,
|
||||||
posSec: 0, // 已消耗的源时长(秒,按 tempo 累计)
|
posSec: 0, // 已消耗的源时长(秒,按 tempo 累计)
|
||||||
lastT: performance.now(),
|
lastT: performance.now(),
|
||||||
endTimer: null,
|
endTimer: null,
|
||||||
};
|
};
|
||||||
// 顶掉语义必须在 await 之后复核(dungeon R2 D0-1 根因修复):_load 期间
|
// 尾部保真(二修):完成信号用管线自带的 onEnd(extract 返回 0 =
|
||||||
// 可能又有一句 voice 开播(同 tick 双调度,如"反馈句+下一步指令句")。
|
// SoundTouch 内部已无未交付帧),它在任何平台都不可能早于「最后一个
|
||||||
// 若此处不补杀,早先的 token 会被 this._voice = token 静默顶号——它的
|
// 样本已交付」——此时 disconnect 已交付帧仍会播完。固定排空窗
|
||||||
// 节点永远没人 disconnect(僵尸节点空转抽 buffer),endTimer 也因
|
// (一修 300ms)在手机端不够(未交付量随平台回调相位浮动),弃用。
|
||||||
// _finishVoice 的 token 校验提前返回而永不清理,语音引擎自此被"幽灵句"
|
filter.callback = () => this._onVoiceStreamEnd(token);
|
||||||
// 污染到页面关闭(R1 live:tut_echo_come 僵尸节点致 71 分钟抢占风暴)。
|
|
||||||
if (this._voice) {
|
|
||||||
const cur = this._voice;
|
|
||||||
this._voice = null;
|
|
||||||
this._killVoice(cur);
|
|
||||||
}
|
|
||||||
this._voice = token;
|
this._voice = token;
|
||||||
this._duckBgm();
|
this._duckBgm();
|
||||||
this._armVoiceEnd(token);
|
this._armVoiceEnd(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SoundTouch onEnd:管线内帧已全部交付音频图(ScriptProcessor 抽空)。
|
||||||
|
* 这是「句尾已真正送进硬件」的平台无关信号——再断开不可能剪字。
|
||||||
|
* 注意它在 onaudioprocess 内同步触发,结算延后一个小缓冲执行。
|
||||||
|
*/
|
||||||
|
_onVoiceStreamEnd(token) {
|
||||||
|
if (token.streamEnded || token.stopped) return;
|
||||||
|
token.streamEnded = true;
|
||||||
|
if (this._voice !== token) return; // 已被顶掉:由 _killVoice 收尾
|
||||||
|
if (token.endTimer !== null) { clearTimeout(token.endTimer); token.endTimer = null; }
|
||||||
|
token.endTimer = setTimeout(() => this._finishVoice(token), VOICE_STREAM_EPS_MS);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 结算 voice 自然播完(onComplete 链 + 队列推进)。
|
* 结算 voice 自然播完(onComplete 链 + 队列推进)。
|
||||||
* 尾部保真:以「源真实消耗位置」(SoundTouch 源 position,SPN 回调推进)为准,
|
* 完成优先级:streamEnded(管线排空信号)> 定时器兜底。
|
||||||
* 不信墙钟估算——管线有 ~200ms 固有延迟且消耗速率受主线程抖动/后台节流影响,
|
* 定时器路径仅用于:①源未耗尽时等待(80ms 重排);②onEnd 在异常平台
|
||||||
* 旧实现纯定时器 +120ms 一到就 disconnect,仍留在管线缓冲里的句尾音节会被
|
* 未触发时的兜底(VOICE_DRAIN_FALLBACK_MS,宁迟勿剪)。
|
||||||
* 剪掉(真机人耳实测每句尾字缺失;ASR 窗口宽看不到,转写评审也看不到)。
|
* consumedSec==0(mock 环境/SPN 首回调未到)走旧立即结算,零行为变化。
|
||||||
* consumedSec==0(mock 环境/首个 SPN 回调未到)走旧定时器路径,零行为变化。
|
|
||||||
*/
|
*/
|
||||||
_finishVoice(token) {
|
_finishVoice(token) {
|
||||||
if (this._voice !== token || token.stopped) return; // 被打断:不回调
|
if (this._voice !== token || token.stopped) return; // 被打断:不回调
|
||||||
const consumedSec = token.filter.sourcePosition / token.buf.sampleRate;
|
const consumedSec = token.filter.sourcePosition / token.srcBuf.sampleRate;
|
||||||
if (consumedSec > 0 && consumedSec < token.buf.duration - 0.02) {
|
if (consumedSec > 0 && !token.streamEnded) {
|
||||||
// 源未耗尽:墙钟先到/主线程卡顿/后台节流——等真实消耗追上再结算
|
if (consumedSec < token.srcBuf.duration - 0.02) {
|
||||||
token.endTimer = setTimeout(() => this._finishVoice(token), 80);
|
// 源未耗尽:墙钟先到/主线程卡顿/后台节流——等真实消耗追上再结算
|
||||||
return;
|
token.endTimer = setTimeout(() => this._finishVoice(token), 80);
|
||||||
}
|
return;
|
||||||
if (consumedSec > 0 && !token.drained) {
|
}
|
||||||
// 源已耗尽:留一个排空窗,让已进入 SPN/SoundTouch 缓冲的尾音真正播出
|
if (!token.drained) {
|
||||||
token.drained = true;
|
// 源已耗尽但 onEnd 未至:挂兜底定时器,等管线排空信号
|
||||||
token.endTimer = setTimeout(() => this._finishVoice(token), VOICE_DRAIN_MS);
|
token.drained = true;
|
||||||
return;
|
token.endTimer = setTimeout(() => this._finishVoice(token), VOICE_DRAIN_FALLBACK_MS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 兜底触发(onEnd 异常缺失):宁可迟结算也不剪尾
|
||||||
}
|
}
|
||||||
token.stopped = true;
|
token.stopped = true;
|
||||||
this._voice = null;
|
this._voice = null;
|
||||||
@@ -520,7 +562,7 @@ export class WebAudioEngine {
|
|||||||
|
|
||||||
/** 按当前 tempo 排结束定时器;tempo 变化时重排。 */
|
/** 按当前 tempo 排结束定时器;tempo 变化时重排。 */
|
||||||
_armVoiceEnd(token) {
|
_armVoiceEnd(token) {
|
||||||
const remainSec = Math.max(0.05, (token.buf.duration - token.posSec) / token.st.tempo);
|
const remainSec = Math.max(0.05, (token.srcBuf.duration - token.posSec) / token.st.tempo);
|
||||||
token.lastT = performance.now();
|
token.lastT = performance.now();
|
||||||
token.endTimer = setTimeout(() => this._finishVoice(token), remainSec * 1000 + 120);
|
token.endTimer = setTimeout(() => this._finishVoice(token), remainSec * 1000 + 120);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,25 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
/**
|
/**
|
||||||
* engine-voice.test.mjs — 引擎 voice 句尾保真回归(2026-09 真机人耳反馈固化)。
|
* engine-voice.test.mjs — 引擎 voice 句尾保真回归(2026-09 真机人耳反馈三修固化)。
|
||||||
*
|
*
|
||||||
* 固化的教训:voice 自然结束**不能**用墙钟定时器判定(SPN 4096帧@48k≈85ms +
|
* 固化的教训(三轮排查递进):
|
||||||
* SoundTouch 固有延迟 ≈190ms > 任何小余量),必须以「源真实消耗位置」为准,
|
* 一修(废止):源消耗位置 + 固定 300ms 排空窗。手机端未交付量随平台回调
|
||||||
* 耗尽后留排空窗再 disconnect。本测试用可手动驱动的 ScriptProcessorNode mock
|
* 相位浮动(~0.5s),末词仍被剪。
|
||||||
* 模拟真实消耗,把该语义钉死,防止未来重构回潮。
|
* 二修(保留):完成信号改用 SoundTouch onEnd(extract 返回 0 = 管线内已无
|
||||||
|
* 未交付帧),平台无关;定时器仅剩「源未耗尽等待」与「onEnd 缺失兜底」。
|
||||||
|
* 三修(根治):live 实测发现 SoundTouch Stretch 源结束时不冲刷内部最后
|
||||||
|
* 一个小节——句尾 0.1-0.25s **根本不会被交付**(任何平台,ASR 因容错听不出)。
|
||||||
|
* 根治 = 源尾垫 VOICE_TAIL_PAD_SEC(0.25s) 静音把真实句尾推出管线。
|
||||||
*
|
*
|
||||||
* 三条不变式:
|
* 四条不变式:
|
||||||
* I1 源未耗尽时,_finishVoice 提前触发不得结算(必须短重排等待);
|
* I1 源未耗尽时,_finishVoice 提前触发不得结算(短重排等待);
|
||||||
* I2 源耗尽后第一次触发只挂排空旗,排空窗(VOICE_DRAIN_MS)过后才真结算;
|
* I2 源(含尾部垫)耗尽后须等 onEnd(管线真排空)才结算;onEnd 一到立即(≤~120ms)结算;
|
||||||
* I3 消耗位置恒为 0(playtest mock / SPN 首回调未到)走旧立即结算路径。
|
* I2b onEnd 异常缺失时,兜底定时器在 VOICE_DRAIN_FALLBACK_MS 后结算(宁迟勿剪);
|
||||||
|
* I3 消耗位置恒 0(playtest mock / SPN 首回调未到)走旧立即结算路径。
|
||||||
|
* 附加 PAD:播放源长度 = 原时长 + 0.25s(尾部静音垫存在性)。
|
||||||
*
|
*
|
||||||
* 运行:tools/node/bin/node web/harness/engine-voice.test.mjs
|
* 运行:tools/node/bin/node web/harness/engine-voice.test.mjs
|
||||||
*/
|
*/
|
||||||
import fs from 'node:fs';
|
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
@@ -26,29 +31,29 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|||||||
|
|
||||||
// ---- 全局 mock(在 import 引擎前就位)----
|
// ---- 全局 mock(在 import 引擎前就位)----
|
||||||
const storageMap = new Map();
|
const storageMap = new Map();
|
||||||
globalThis.window = {
|
globalThis.window = { AudioContext: null, addEventListener() {} };
|
||||||
AudioContext: null, // 下面赋值
|
|
||||||
addEventListener() {},
|
|
||||||
};
|
|
||||||
globalThis.localStorage = {
|
globalThis.localStorage = {
|
||||||
getItem: (k) => (storageMap.has(k) ? storageMap.get(k) : null),
|
getItem: (k) => (storageMap.has(k) ? storageMap.get(k) : null),
|
||||||
setItem: (k, v) => storageMap.set(k, String(v)),
|
setItem: (k, v) => storageMap.set(k, String(v)),
|
||||||
removeItem: (k) => storageMap.delete(k),
|
removeItem: (k) => storageMap.delete(k),
|
||||||
};
|
};
|
||||||
globalThis.document = { addEventListener() {}, getElementById: () => null, body: { appendChild() {}, classList: { add() {}, remove() {} } } };
|
globalThis.document = { addEventListener() {}, getElementById: () => null, body: { appendChild() {}, classList: { add() {}, remove() {} } } };
|
||||||
try { globalThis.navigator = { userAgent: 'test', vibrate: () => true }; } catch (_) { /* Node22 自带只读 navigator */ }
|
try { globalThis.navigator = { userAgent: 'test', vibrate: () => true }; } catch (_) { /* Node22 只读 navigator */ }
|
||||||
globalThis.performance = globalThis.performance || { now: () => Date.now() };
|
globalThis.performance = globalThis.performance || { now: () => Date.now() };
|
||||||
|
|
||||||
const SR = 44100;
|
const SR = 44100;
|
||||||
const DUR = 2.0; // 测试语音 2 秒
|
const DUR = 2.0; // 解码出的原始时长
|
||||||
|
const PAD = 0.25; // 引擎尾部静音垫(VOICE_TAIL_PAD_SEC)
|
||||||
|
const SRC_DUR = DUR + PAD; // 播放源总时长
|
||||||
const FRAMES = Math.round(SR * DUR);
|
const FRAMES = Math.round(SR * DUR);
|
||||||
|
const SRC_FRAMES = Math.round(SR * SRC_DUR);
|
||||||
|
|
||||||
class FakeParam { constructor(v = 1) { this.value = v; } setValueAtTime() {} linearRampToValueAtTime() {} cancelScheduledValues() {} }
|
class FakeParam { constructor(v = 1) { this.value = v; } setValueAtTime() {} linearRampToValueAtTime() {} cancelScheduledValues() {} }
|
||||||
|
|
||||||
class DrivableSPN {
|
class DrivableSPN {
|
||||||
constructor() { this.connect = () => {}; this.disconnectCount = 0; this.onaudioprocess = null; }
|
constructor() { this.connect = () => {}; this.disconnectCount = 0; this.onaudioprocess = null; }
|
||||||
disconnect() { this.disconnectCount++; }
|
disconnect() { this.disconnectCount++; }
|
||||||
/** 模拟一次真实音频回调:SoundTouch 从源拉取至多 4096 帧 */
|
/** 模拟一次真实音频回调:SoundTouch 从源拉取至多 4096 帧交付音频图 */
|
||||||
pump() {
|
pump() {
|
||||||
if (!this.onaudioprocess) return 0;
|
if (!this.onaudioprocess) return 0;
|
||||||
const out = { outputBuffer: { getChannelData: () => new Float32Array(4096 * 2) } };
|
const out = { outputBuffer: { getChannelData: () => new Float32Array(4096 * 2) } };
|
||||||
@@ -63,74 +68,99 @@ class MockAudioContext {
|
|||||||
createGain() { return { gain: new FakeParam(), connect() {}, disconnect() {} }; }
|
createGain() { return { gain: new FakeParam(), connect() {}, disconnect() {} }; }
|
||||||
createDynamicsCompressor() { const n = this.createGain(); n.threshold = new FakeParam(); n.ratio = new FakeParam(); n.attack = new FakeParam(); n.release = new FakeParam(); return n; }
|
createDynamicsCompressor() { const n = this.createGain(); n.threshold = new FakeParam(); n.ratio = new FakeParam(); n.attack = new FakeParam(); n.release = new FakeParam(); return n; }
|
||||||
createScriptProcessor() { return new DrivableSPN(); }
|
createScriptProcessor() { return new DrivableSPN(); }
|
||||||
|
/** 引擎三修:源尾垫静音需要 createBuffer + copyToChannel */
|
||||||
|
createBuffer(channels, length, sampleRate) {
|
||||||
|
const chans = Array.from({ length: channels }, () => new Float32Array(length));
|
||||||
|
return {
|
||||||
|
numberOfChannels: channels, length, sampleRate, duration: length / sampleRate,
|
||||||
|
getChannelData: (i) => chans[i],
|
||||||
|
copyToChannel(src, ch) { chans[ch].set(src); },
|
||||||
|
};
|
||||||
|
}
|
||||||
decodeAudioData() {
|
decodeAudioData() {
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
duration: DUR, sampleRate: SR, numberOfChannels: 1,
|
duration: DUR, length: FRAMES, sampleRate: SR, numberOfChannels: 1,
|
||||||
getChannelData: (i) => new Float32Array(FRAMES), // 真实长度:SoundTouch extract 按帧索引
|
getChannelData: () => new Float32Array(FRAMES),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
resume() { this.state = 'running'; return Promise.resolve(); }
|
resume() { this.state = 'running'; return Promise.resolve(); }
|
||||||
}
|
}
|
||||||
globalThis.window.AudioContext = MockAudioContext;
|
globalThis.window.AudioContext = MockAudioContext;
|
||||||
|
|
||||||
globalThis.fetch = async () => ({ ok: true, status: 200, arrayBuffer: async () => new ArrayBuffer(8) });
|
globalThis.fetch = async () => ({ ok: true, status: 200, arrayBuffer: async () => new ArrayBuffer(8) });
|
||||||
|
|
||||||
// ---- 导入真实引擎 ----
|
|
||||||
const { WebAudioEngine } = await import(path.join(WEB, 'engine', 'audio.js'));
|
const { WebAudioEngine } = await import(path.join(WEB, 'engine', 'audio.js'));
|
||||||
|
const FALLBACK_MS = 1500;
|
||||||
|
|
||||||
const engine = new WebAudioEngine('audio/');
|
const engine = new WebAudioEngine('audio/');
|
||||||
await engine.unlock();
|
await engine.unlock();
|
||||||
const completed = [];
|
const completed = [];
|
||||||
const makeOnComplete = (tag) => () => completed.push(tag);
|
|
||||||
|
console.log('\n=== PAD:播放源存在尾部静音垫 ===');
|
||||||
|
engine.play('voice/tail_pad', { onComplete: () => completed.push('P') });
|
||||||
|
await sleep(30);
|
||||||
|
let token = engine._voice;
|
||||||
|
assert(token && token.srcBuf && Math.abs(token.srcBuf.duration - SRC_DUR) < 0.01,
|
||||||
|
`播放源 = 原时长+0.25s 垫(${token.srcBuf.duration.toFixed(2)}s = ${DUR}+${PAD})`);
|
||||||
|
engine.stopVoice(); engine._voice = null; completed.length = 0;
|
||||||
|
|
||||||
console.log('\n=== I1 源未耗尽:提前触发不结算 ===');
|
console.log('\n=== I1 源未耗尽:提前触发不结算 ===');
|
||||||
engine.play('voice/tail_test', { onComplete: makeOnComplete('A') });
|
engine.play('voice/tail_test', { onComplete: () => completed.push('A') });
|
||||||
await sleep(30); // 等 _playVoice 的 await _load 落地
|
await sleep(30);
|
||||||
let token = engine._voice;
|
token = engine._voice;
|
||||||
assert(token && token.buf && token.filter, 'voice token 已建立(含 filter 引用)');
|
assert(token && token.buf && token.filter, 'voice token 已建立(含 filter 引用)');
|
||||||
for (let i = 0; i < 3; i++) token.node.pump(); // 消耗 ~12288 帧 ≈ 0.28s << 2s
|
for (let i = 0; i < 3; i++) token.node.pump();
|
||||||
const consumedEarly = token.filter.sourcePosition / token.buf.sampleRate;
|
const consumedEarly = token.filter.sourcePosition / token.srcBuf.sampleRate;
|
||||||
assert(consumedEarly > 0 && consumedEarly < DUR - 0.02, `消耗位置已推进且未耗尽(${consumedEarly.toFixed(2)}s / ${DUR}s)`);
|
assert(consumedEarly > 0 && consumedEarly < token.srcBuf.duration - 0.02,
|
||||||
engine._finishVoice(token); // 模拟旧式墙钟定时器提前到点
|
`消耗位置已推进且未耗尽(${consumedEarly.toFixed(2)}s / ${SRC_DUR}s)`);
|
||||||
|
engine._finishVoice(token);
|
||||||
assert(engine._voice === token && !token.stopped, 'I1 未耗尽时不结算(token 仍存活,等待重排)');
|
assert(engine._voice === token && !token.stopped, 'I1 未耗尽时不结算(token 仍存活,等待重排)');
|
||||||
assert(completed.length === 0, 'I1 onComplete 未被提前调用');
|
assert(completed.length === 0, 'I1 onComplete 未被提前调用');
|
||||||
|
|
||||||
console.log('\n=== I2 源耗尽:排空窗过后才真结算 ===');
|
console.log('\n=== I2 源(含垫)耗尽:onEnd(管线真排空)才结算 ===');
|
||||||
let guard = 0;
|
let guard = 0;
|
||||||
while (token.filter.sourcePosition / SR < DUR && guard++ < 200) token.node.pump();
|
while (token.filter.sourcePosition < SRC_FRAMES && guard++ < 400) token.node.pump();
|
||||||
const consumedFull = token.filter.sourcePosition / token.buf.sampleRate;
|
token.node.pump(); // SPN 源耗尽后继续回调 → vendor extract 返回 0 → onEnd
|
||||||
assert(consumedFull >= DUR - 0.02, `源已耗尽(${consumedFull.toFixed(2)}s)`);
|
assert(token.streamEnded === true, 'I2 源耗尽后 SPN 继续回调触发 onEnd(filter.callback 已接线)');
|
||||||
if (token.endTimer !== null) { clearTimeout(token.endTimer); token.endTimer = null; } // 摘掉墙钟定时器,纯手动驱动
|
await sleep(120);
|
||||||
const t0 = Date.now();
|
assert(engine._voice === null && token.stopped, 'I2 onEnd 后短时间内真结算(≤120ms)');
|
||||||
engine._finishVoice(token); // 第一次:应只挂排空旗
|
|
||||||
assert(token.drained === true && engine._voice === token, 'I2 耗尽后第一触发只置排空旗(不结算)');
|
|
||||||
await sleep(VOICE_DRAIN_PLUS()); // 排空窗
|
|
||||||
engine._finishVoice(token); // 第二次:真结算
|
|
||||||
assert(engine._voice === null && token.stopped, 'I2 排空窗后真结算(_voice 清空)');
|
|
||||||
assert(completed.includes('A'), 'I2 onComplete 恰好回调一次');
|
assert(completed.includes('A'), 'I2 onComplete 恰好回调一次');
|
||||||
assert(token.node.disconnectCount >= 1, 'I2 结算时节点已 disconnect');
|
assert(token.node.disconnectCount >= 1, 'I2 结算时节点已 disconnect');
|
||||||
assert(Date.now() - t0 >= 280, `I2 排空窗确实等待了 ≥280ms(实测 ${Date.now() - t0}ms)`);
|
|
||||||
function VOICE_DRAIN_PLUS() { return 380; }
|
console.log('\n=== I2b 无 onEnd 的异常平台:兜底定时器结算(不剪) ===');
|
||||||
|
engine.play('voice/tail_nofb', { onComplete: () => completed.push('B') });
|
||||||
|
await sleep(30);
|
||||||
|
const t2 = engine._voice;
|
||||||
|
guard = 0;
|
||||||
|
while (t2.filter.sourcePosition < SRC_FRAMES && guard++ < 400) t2.node.pump(); // 耗尽但不再多 pump(无 onEnd)
|
||||||
|
if (t2.endTimer !== null) { clearTimeout(t2.endTimer); t2.endTimer = null; }
|
||||||
|
const fb0 = Date.now();
|
||||||
|
engine._finishVoice(t2); // 墙钟路径:drained + 兜底
|
||||||
|
assert(t2.drained === true && engine._voice === t2, 'I2b 耗尽后墙钟触发只挂兜底(不结算)');
|
||||||
|
await sleep(400);
|
||||||
|
assert(engine._voice === t2, 'I2b 兜底期内(400ms < 1500ms)仍不结算');
|
||||||
|
await sleep(FALLBACK_MS - 400 + 150);
|
||||||
|
assert(engine._voice === null && t2.stopped, `I2b 兜底触发后结算(实测 ${Date.now() - fb0}ms ≥ ${FALLBACK_MS})`);
|
||||||
|
assert(completed.includes('B'), 'I2b onComplete 经兜底回调');
|
||||||
|
|
||||||
console.log('\n=== I3 消耗位置恒 0(mock/SPN 未回调):旧立即结算路径 ===');
|
console.log('\n=== I3 消耗位置恒 0(mock/SPN 未回调):旧立即结算路径 ===');
|
||||||
engine.play('voice/tail_zero', { onComplete: makeOnComplete('B') });
|
engine.play('voice/tail_zero', { onComplete: () => completed.push('C') });
|
||||||
await sleep(30);
|
|
||||||
const token2 = engine._voice;
|
|
||||||
assert(token2 && token2.filter.sourceSound.position === 0, '第二句从未被 pump(position=0)');
|
|
||||||
engine._finishVoice(token2);
|
|
||||||
assert(engine._voice === null && token2.stopped, 'I3 position=0 时立即结算(playtest mock 兼容,链推进不受影响)');
|
|
||||||
assert(completed.includes('B'), 'I3 onComplete 立即回调');
|
|
||||||
|
|
||||||
console.log('\n=== 附加:打断语义不受影响(新句顶掉旧句,旧句 onComplete 不回调) ===');
|
|
||||||
engine.play('voice/tail_test2', { onComplete: makeOnComplete('C1') });
|
|
||||||
await sleep(30);
|
await sleep(30);
|
||||||
const t3 = engine._voice;
|
const t3 = engine._voice;
|
||||||
for (let i = 0; i < 3; i++) t3.node.pump();
|
assert(t3 && t3.filter.sourcePosition === 0, '该句从未被 pump(position=0)');
|
||||||
engine.play('voice/tail_test3', { onComplete: makeOnComplete('C2') });
|
engine._finishVoice(t3);
|
||||||
|
assert(engine._voice === null && t3.stopped, 'I3 position=0 时立即结算(playtest mock 兼容)');
|
||||||
|
assert(completed.includes('C'), 'I3 onComplete 立即回调');
|
||||||
|
|
||||||
|
console.log('\n=== 附加:打断语义不受影响(新句顶掉旧句,旧句 onComplete 不回调) ===');
|
||||||
|
engine.play('voice/tail_test2', { onComplete: () => completed.push('D1') });
|
||||||
await sleep(30);
|
await sleep(30);
|
||||||
assert(engine._voice && engine._voice !== t3 && t3.stopped, '新 voice 顶掉旧 voice(旧 token 已停)');
|
const t4 = engine._voice;
|
||||||
assert(!completed.includes('C1'), '被打断的旧句不回调 onComplete(排队链语义保持)');
|
for (let i = 0; i < 3; i++) t4.node.pump();
|
||||||
engine._finishVoice(engine._voice); if (engine._voice) { await sleep(380); engine._finishVoice(engine._voice); }
|
engine.play('voice/tail_test3');
|
||||||
|
await sleep(30);
|
||||||
|
assert(engine._voice && engine._voice !== t4 && t4.stopped, '新 voice 顶掉旧 voice(旧 token 已停)');
|
||||||
|
assert(!completed.includes('D1'), '被打断的旧句不回调 onComplete(排队链语义保持)');
|
||||||
|
assert(t4.streamEnded === false, '被打断句不触发 onEnd 结算路径');
|
||||||
engine.stopVoice();
|
engine.stopVoice();
|
||||||
engine._voice = null;
|
engine._voice = null;
|
||||||
|
|
||||||
|
|||||||
@@ -141,8 +141,8 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const AC = window.AudioContext || window.webkitAudioContext;
|
const AC = window.AudioContext || window.webkitAudioContext;
|
||||||
|
const voiceNodes = new Set(); // 连接中的 SoundTouch ScriptProcessorNode(断开即移除)
|
||||||
if (AC) {
|
if (AC) {
|
||||||
const voiceNodes = new WeakSet(); // SoundTouch 管线创建的 ScriptProcessorNode
|
|
||||||
const origCSP = AC.prototype.createScriptProcessor;
|
const origCSP = AC.prototype.createScriptProcessor;
|
||||||
AC.prototype.createScriptProcessor = function (...a) {
|
AC.prototype.createScriptProcessor = function (...a) {
|
||||||
const n = origCSP.apply(this, a);
|
const n = origCSP.apply(this, a);
|
||||||
@@ -153,12 +153,20 @@
|
|||||||
AudioNode.prototype.disconnect = function (...a) {
|
AudioNode.prototype.disconnect = function (...a) {
|
||||||
// voice 节点断开 = 引擎 killVoice(自然结束/被顶掉/stopVoice 都走这里)。
|
// voice 节点断开 = 引擎 killVoice(自然结束/被顶掉/stopVoice 都走这里)。
|
||||||
// 玩家实际听到的尾音就到此刻(FIFO 里未播部分被丢弃),用 now() 而非 lastAct。
|
// 玩家实际听到的尾音就到此刻(FIFO 里未播部分被丢弃),用 now() 而非 lastAct。
|
||||||
if (voiceNodes.has(this) && seg) closeSeg(now(), 'kill');
|
if (voiceNodes.has(this)) {
|
||||||
|
voiceNodes.delete(this);
|
||||||
|
if (seg) closeSeg(now(), 'kill');
|
||||||
|
}
|
||||||
return origDisc.apply(this, a);
|
return origDisc.apply(this, a);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// 兜底清扫:长静默后关闭未结段
|
// 兜底清扫:长静默后关闭未结段。
|
||||||
setInterval(() => { if (seg && now() - seg.lastAct > VOICE_GAP_MS) closeSeg(seg.lastAct, 'end'); }, 200);
|
// 守卫(2026-09 三修适配):引擎的尾部静音垫让 SoundTouch 读的是新建
|
||||||
|
// buffer(未注册 url),gcd 抽读信号消失、lastAct 停在句首——只要仍有
|
||||||
|
// 连接中的语音 SPN 就说明句在播,不许清扫;段关闭只信 disconnect。
|
||||||
|
setInterval(() => {
|
||||||
|
if (seg && now() - seg.lastAct > VOICE_GAP_MS && voiceNodes.size === 0) closeSeg(seg.lastAct, 'end');
|
||||||
|
}, 200);
|
||||||
} catch (e) { push({ ev: 'probe_error', where: 'voice-detect', msg: String(e) }); }
|
} catch (e) { push({ ev: 'probe_error', where: 'voice-detect', msg: String(e) }); }
|
||||||
|
|
||||||
// ---------- AudioBufferSourceNode:bgm/sfx 播放事件 ----------
|
// ---------- AudioBufferSourceNode:bgm/sfx 播放事件 ----------
|
||||||
|
|||||||
Reference in New Issue
Block a user