fix(bounce): offline render qua native bridge — bỏ gap realtime, thêm headroom, strip DIAG

This commit is contained in:
2026-08-25 09:11:29 +07:00
parent 4037dbf40d
commit dd17f87e4e
7 changed files with 66 additions and 107 deletions
@@ -24,7 +24,6 @@
// Idempotent: updateSfRouting/ensureMasteringRouting chay tren MOI keydown/
// note-on — neu da noi dung dest thi KHONG disconnect (disconnect flush ring
// -> am phim dang vang bi cat). Chi doi khi dest/track thuc su khac.
if (window.__sfDiag) window.__sfDiag("ROUTE to=" + (trackId || "masterBus") + " prev=" + (this._trackId || "masterBus") + " bridgeReady=" + bridgeNode.isReady() + " conn=" + this._connected);
if (this._connected && this._dest === dest && this._trackId === (trackId || null)) return true;
if (!bridgeNode.isReady()) bridgeNode.init();
bridgeNode.connect(dest);
-14
View File
@@ -15,9 +15,6 @@
var _workletNode = null;
var _ctx = null;
var _initialized = false;
var _frames = 0;
var _peak = 0;
var _lastDiag = 0;
var _useWorklet = false;
var _dest = null; // destination gain node da noi (chong connect trung)
@@ -103,18 +100,7 @@
function onAudio(l, r) {
if (!_initialized) init();
if (!window.__baeAt || Date.now() - window.__baeAt >= 1000) { window.__baeAt = Date.now(); if (window.__sfDiag) window.__sfDiag('BRIDGE_AUDIO_ENTER len=' + (l ? l.length : -1)); }
var blk = { l: new Float32Array(l), r: new Float32Array(r) };
_frames = (_frames || 0) + blk.l.length;
for (var _i = 0; _i < blk.l.length; _i++) {
var _a = Math.abs(blk.l[_i]); if (_a > (_peak || 0)) _peak = _a;
var _b = Math.abs(blk.r[_i]); if (_b > (_peak || 0)) _peak = _b;
}
if (!_lastDiag || Date.now() - _lastDiag >= 1000) {
_lastDiag = Date.now();
if (window.__sfDiag) window.__sfDiag("BRIDGE_AUDIO frames=" + _frames + " peak=" + (_peak ? (20 * Math.log10(_peak)).toFixed(1) : "-inf") + "dB path=" + (_useWorklet ? "worklet" : (_preBuffer.length ? "preBuffer" : "chunks")));
_peak = 0;
}
if (_useWorklet && _workletNode) {
_workletNode.port.postMessage({ type: 'audio', l: blk.l, r: blk.r });
return;
+1 -12
View File
@@ -54,7 +54,6 @@
var self = this;
try {
window.__TAURI__.event.listen('bridge-audio', function (e) {
if (!self._evtDiagAt || Date.now() - self._evtDiagAt >= 1000) { self._evtDiagAt = Date.now(); if (window.__sfDiag) window.__sfDiag('BRIDGE_EVT_RX cb=' + (self._audioCb ? 'yes' : 'no') + ' len=' + (e.payload && e.payload.l ? e.payload.l.length : 0)); }
// Bridge recovered after bridge-down (or never bootstrapped):
// audio frames flow again -> re-establish routing (Fix C).
if (!self.isBridgeConnected) self._reconnectIfNeeded();
@@ -72,7 +71,6 @@
if (window.AudioRoutingEngine) window.AudioRoutingEngine.disconnect();
console.warn('[BridgeService] bridge-down event received.');
});
if (window.__sfDiag) window.__sfDiag('EVT_REG ok');
} catch (e) {
console.warn('[BridgeService] event listen failed:', e);
}
@@ -126,8 +124,6 @@
* data2/data3 (A12): CC value / program / PB LSB|MSB.
*/
dispatchMidiEvent: function (cmd, channel, pitch, velocity, sampleOffset, data2, data3, live) {
this._pushEnter = (this._pushEnter || 0) + 1;
if (this._pushEnter % 100 === 0 && window.__sfDiag) window.__sfDiag('PUSH_ENTER cmd=' + cmd + ' ch=' + channel + ' tauri=' + !!this._tauri());
if (!this._tauri()) return false;
var safeVelocity = Math.floor(Math.min(1.0, Math.max(0.0, velocity)) * 127);
var byteCmd;
@@ -137,8 +133,6 @@
case 'PITCH_BEND': byteCmd = 0xE; break;
default: byteCmd = cmd === 'NOTE_ON' ? 0x9 : 0x8; break;
}
this._pushSent = (this._pushSent || 0) + 1;
if (this._pushSent % 100 === 0 && window.__sfDiag) window.__sfDiag('PUSH_SENT ' + this._pushSent);
// DIAG: Promise.race 800ms — invoke gửi đi nhưng không settle -> timeout
// (phan biet main-thread block vs Rust hang).
Promise.race([
@@ -153,12 +147,7 @@
live: !!live
}),
new Promise(function (_, rej) { setTimeout(function () { rej(new Error('timeout-800ms')); }, 800); })
]).then(function () {
this._pushOk = (this._pushOk || 0) + 1;
if ((_pushOk + (_pushFail || 0)) % 100 === 0 && window.__sfDiag) window.__sfDiag('PUSH_MIDI ok=' + _pushOk + ' fail=' + (_pushFail || 0));
}).catch(function (e) {
this._pushFail = (this._pushFail || 0) + 1;
if (_lastPushFail !== String(e)) { _lastPushFail = String(e); if (window.__sfDiag) window.__sfDiag('PUSH_MIDI_FAIL ' + e); }
]).catch(function (e) {
console.warn('[BridgeService] push_midi_event:', e);
});
return true;
@@ -56,13 +56,10 @@
var sampleOffset = opts.sampleOffset || 0;
if (typeof ch === 'string') ch = this.allocateChannel(ch, opts.percussion);
if (ch === undefined || ch === null) ch = 0;
this._pushEvt = (this._pushEvt || 0) + 1;
if (this._pushEvt % 100 === 0 && window.__sfDiag) window.__sfDiag('PUSH_ROUTE cmd=' + cmd + ' ch=' + ch + ' brAct=' + this.isBridgeActive() + ' nbs=' + !!window.NativeBridgeService + ' tauri=' + !!(window.__TAURI__));
if (this.isBridgeActive()) {
window.NativeBridgeService.dispatchMidiEvent(cmd, ch, pitch, vel, sampleOffset, opts.data2, opts.data3, opts.live);
return;
}
if (this._pushEvt % 100 === 0 && window.__sfDiag) window.__sfDiag('PUSH_FALLBACK cmd=' + cmd + ' ch=' + ch);
if (this.onFallback) this.onFallback(cmd, ch, pitch, vel);
},