M25: fix master FX - worker cross-origin (Tauri origin tauri://) -> Blob URL + bump stamp precompiled
This commit is contained in:
+20
-4
@@ -2033,7 +2033,8 @@ function fxRtWsOnMessage(ev) {
|
||||
// onclose/onerror) nhung WS + binary deinterleave chay tren Web Worker; output
|
||||
// di THANG worker->worklet qua MessageChannel (directPort). ~20 call-site
|
||||
// fxRt.ws giu nguyen - khong sua. Main chi nhan text (latency/flush_done).
|
||||
function fxRtWsConnect(wsUrl, directPort) {
|
||||
let fxRtWorkerUrl = null; // M25.1: blob URL cua worker (cache 1 lan/session)
|
||||
async function fxRtWsConnect(wsUrl, directPort) {
|
||||
const _fireClose = (sh) => {
|
||||
if (sh._closed) return;
|
||||
sh._closed = true;
|
||||
@@ -2059,7 +2060,19 @@ function fxRtWsConnect(wsUrl, directPort) {
|
||||
},
|
||||
close() { _fireClose(this); }
|
||||
};
|
||||
try { shim._worker = new Worker(`${API_BASE_URL}/static/js/fx-rt-worker.js?v=202608291749`); } catch (e) { throw e; } // giong ws constructor that -> caller catch cu giu nguyen
|
||||
// M25.1 fix: UI Tauri chay origin tauri://localhost (engine HTML nap qua
|
||||
// document.write) — new Worker(http://127.0.0.1:8000/...) CROSS-ORIGIN ->
|
||||
// SecurityError -> fxRtStart chet -> master FX khong hoat dong (M24 dung
|
||||
// new WebSocket, khong bi same-origin). Fetch text qua engine (CORS * da bat)
|
||||
// -> Blob URL same-origin -> worker chay duoc ca Tauri lan http dev.
|
||||
try {
|
||||
if (!fxRtWorkerUrl) {
|
||||
const _r = await fetch(`${API_BASE_URL}/static/js/fx-rt-worker.js?v=202608291749`, { cache: 'no-store' });
|
||||
if (!_r.ok) throw new Error('fx-rt-worker load ' + _r.status);
|
||||
fxRtWorkerUrl = URL.createObjectURL(new Blob([await _r.text()], { type: 'text/javascript' }));
|
||||
}
|
||||
shim._worker = new Worker(fxRtWorkerUrl);
|
||||
} catch (e) { throw e; } // giong ws constructor that -> caller catch cu giu nguyen
|
||||
shim._worker.onmessage = (ev) => {
|
||||
const d = ev.data;
|
||||
if (!d || shim._closed) return;
|
||||
@@ -2225,7 +2238,10 @@ async function fxRtStart() {
|
||||
// Text (latency/flush_done) van forward main nhu cu.
|
||||
const _chan = new MessageChannel();
|
||||
try { fxRt.node.port.postMessage({ type: '__direct', port: _chan.port2 }, [_chan.port2]); } catch (e) {}
|
||||
const ws = fxRtWsConnect(wsUrl, _chan.port1);
|
||||
const ws = await fxRtWsConnect(wsUrl, _chan.port1);
|
||||
// M25.1: await fetch worker giua start — stop/restart co the xay ra; ws cu
|
||||
// phai dong (worker terminate), khong de ro.
|
||||
if (token !== fxRt.startToken) { try { ws.close(); } catch (e) {} fxRt.starting = false; return; }
|
||||
fxRt.ws = ws;
|
||||
ws.onopen = () => {
|
||||
if (token !== fxRt.startToken) { try { ws.close(); } catch (e) {} fxRt.starting = false; return; }
|
||||
@@ -2519,7 +2535,7 @@ async function fxRtWarmEnsure(chain) {
|
||||
// worker van giu WS, binary drop trong worker nhu cu main drop (!fxRt.node).
|
||||
let ws;
|
||||
try {
|
||||
ws = fxRtWsConnect(wsUrl, null);
|
||||
ws = await fxRtWsConnect(wsUrl, null);
|
||||
} catch (e) {
|
||||
try { window.SonicAPI.fxRealtimeStop({ session_id: st.session_id }).catch(() => {}); } catch (e2) {}
|
||||
fxRt.sessionId = null;
|
||||
|
||||
@@ -382,9 +382,15 @@ const f=new Float32Array(ev.data);const half=f.length/2;let l=fxRt.outL,r=fxRt.o
|
||||
// onclose/onerror) nhung WS + binary deinterleave chay tren Web Worker; output
|
||||
// di THANG worker->worklet qua MessageChannel (directPort). ~20 call-site
|
||||
// fxRt.ws giu nguyen - khong sua. Main chi nhan text (latency/flush_done).
|
||||
function fxRtWsConnect(wsUrl,directPort){const _fireClose=sh=>{if(sh._closed)return;sh._closed=true;sh.readyState=3;// CLOSED
|
||||
let fxRtWorkerUrl=null;// M25.1: blob URL cua worker (cache 1 lan/session)
|
||||
async function fxRtWsConnect(wsUrl,directPort){const _fireClose=sh=>{if(sh._closed)return;sh._closed=true;sh.readyState=3;// CLOSED
|
||||
try{if(sh._worker)sh._worker.terminate();}catch(e){}if(sh.onclose){try{sh.onclose();}catch(e){}}};const shim={readyState:0,onopen:null,onmessage:null,onclose:null,onerror:null,_worker:null,_closed:false,send(data){if(this._closed||!this._worker||this.readyState!==1)return;try{if(typeof data==='string')this._worker.postMessage({kind:'send',text:data});// KHONG transfer list - fxRtChunks c.buffer reused (ws.send cu khong neuter)
|
||||
else if(data instanceof ArrayBuffer)this._worker.postMessage({kind:'send',buf:data});}catch(e){}},close(){_fireClose(this);}};try{shim._worker=new Worker(`${API_BASE_URL}/static/js/fx-rt-worker.js?v=202608291749`);}catch(e){throw e;}// giong ws constructor that -> caller catch cu giu nguyen
|
||||
else if(data instanceof ArrayBuffer)this._worker.postMessage({kind:'send',buf:data});}catch(e){}},close(){_fireClose(this);}};// M25.1 fix: UI Tauri chay origin tauri://localhost (engine HTML nap qua
|
||||
// document.write) — new Worker(http://127.0.0.1:8000/...) CROSS-ORIGIN ->
|
||||
// SecurityError -> fxRtStart chet -> master FX khong hoat dong (M24 dung
|
||||
// new WebSocket, khong bi same-origin). Fetch text qua engine (CORS * da bat)
|
||||
// -> Blob URL same-origin -> worker chay duoc ca Tauri lan http dev.
|
||||
try{if(!fxRtWorkerUrl){const _r=await fetch(`${API_BASE_URL}/static/js/fx-rt-worker.js?v=202609071534`,{cache:'no-store'});if(!_r.ok)throw new Error('fx-rt-worker load '+_r.status);fxRtWorkerUrl=URL.createObjectURL(new Blob([await _r.text()],{type:'text/javascript'}));}shim._worker=new Worker(fxRtWorkerUrl);}catch(e){throw e;}// giong ws constructor that -> caller catch cu giu nguyen
|
||||
shim._worker.onmessage=ev=>{const d=ev.data;if(!d||shim._closed)return;if(d.kind==='open'){shim.readyState=1;if(shim.onopen){try{shim.onopen();}catch(e){}}return;}if(d.kind==='close'){_fireClose(shim);return;}if(d.kind==='text'){if(shim.onmessage){try{shim.onmessage({data:d.text});}catch(e){}}}};shim._worker.onerror=()=>_fireClose(shim);try{shim._worker.postMessage({kind:'init',wsUrl,port:directPort||undefined},directPort?[directPort]:[]);}catch(e){}return shim;}async function fxRtStart(){// V21: mọi play/chain-change gọi đây — hủy idle timer của lần stop trước
|
||||
// (không để session warm bị teardown giữa lúc đang chơi).
|
||||
if(fxRtWarmIdleTimer){clearTimeout(fxRtWarmIdleTimer);fxRtWarmIdleTimer=null;}const chain=fxRtChain();const ck=fxRtChainKey(chain);const ckStruct=fxRtChainStructKey(chain);if(fxRt.active){// masterBus bị rebuild (recovery/export) → node thuộc graph cũ → restart.
|
||||
@@ -432,14 +438,16 @@ if(fxRt.warm)fxRtStop(true);// Token chống race bypass: user bấm PWR/Bypass
|
||||
// node → bypass vô hiệu. Mỗi fxRtStop() bump startToken → mọi checkpoint
|
||||
// dưới đây thấy mismatch → hủy session vừa tạo, không nối node.
|
||||
const token=++fxRt.startToken;fxRt.starting=true;const ctx=getAudioContext();if(!ctx||!ctx.audioWorklet||!masterBus||!masterBus.output||!masterBus.analyser||!masterBus.masterFxGain||!masterBus.masterFxDryGain)return;const chainPayload=chain.map(s=>s.type==='vst3'?{type:'vst3',path:s.path,name:s.name||s.path,preset_b64:s.preset_b64||'',bypass:!!s.bypass||!!(window.currentMasteringSettings||{}).vstBypass}:{type:'builtin',id:s.type,params:s.params&&Object.keys(s.params).length?s.params:{},bypass:!!s.bypass});let st;try{st=await window.SonicAPI.fxRealtimeStart({fx_chain:chainPayload,sample_rate:ctx.sampleRate||44100});}catch(e){console.warn('[FX RT] start fail:',e);showToast('Không khởi động VST FX realtime: '+(e.message||e),'warning');fxRt.starting=false;return;}if(token!==fxRt.startToken){// Bypass/stop giữa lúc engine đang load chain — hủy session vừa tạo.
|
||||
console.warn('[FX RT] start cancelled mid-flight — session stopped');try{window.SonicAPI.fxRealtimeStop({session_id:st.session_id}).catch(()=>{});}catch(e2){}fxRt.starting=false;return;}fxRt.sessionId=st.session_id;fxRtGuiPushShm();try{await ctx.audioWorklet.addModule(`${API_BASE_URL}/static/js/sf-fx-realtime.js?v=202608291749`);if(token!==fxRt.startToken){fxRt.starting=false;return;}fxRt.node=new AudioWorkletNode(ctx,'sf-fx-realtime',{numberOfInputs:1,numberOfOutputs:1,outputChannelCount:[2]});const u=new URL(API_BASE_URL);const wsProto=u.protocol==='https:'?'wss://':'ws://';// WS to 'localhost' bi tre ~300ms: Chromium thu ::1 truoc (Windows khong
|
||||
console.warn('[FX RT] start cancelled mid-flight — session stopped');try{window.SonicAPI.fxRealtimeStop({session_id:st.session_id}).catch(()=>{});}catch(e2){}fxRt.starting=false;return;}fxRt.sessionId=st.session_id;fxRtGuiPushShm();try{await ctx.audioWorklet.addModule(`${API_BASE_URL}/static/js/sf-fx-realtime.js?v=202609071534`);if(token!==fxRt.startToken){fxRt.starting=false;return;}fxRt.node=new AudioWorkletNode(ctx,'sf-fx-realtime',{numberOfInputs:1,numberOfOutputs:1,outputChannelCount:[2]});const u=new URL(API_BASE_URL);const wsProto=u.protocol==='https:'?'wss://':'ws://';// WS to 'localhost' bi tre ~300ms: Chromium thu ::1 truoc (Windows khong
|
||||
// co listener IPv6 -> SYN hang ~2s) roi moi fallback 127.0.0.1. Dung
|
||||
// thang 127.0.0.1 (loopback) -> WS open ~5ms. Server chap nhan loopback.
|
||||
const wsHost=u.hostname==='localhost'||u.hostname==='::1'?'127.0.0.1'+(u.port?':'+u.port:''):u.host;const tok=localStorage.getItem('sonic_token')||'';const wsUrl=`${wsProto}${wsHost}${st.ws_url}${tok?'?token='+encodeURIComponent(tok):''}`;// M25: WS chay tren Web Worker - binary output/input di THANG
|
||||
// worker<->worklet qua MessageChannel (directPort). Main-thread stall (rAF/
|
||||
// React/render luc cold start) khong con tre frame -> het underrun u1/u10.
|
||||
// Text (latency/flush_done) van forward main nhu cu.
|
||||
const _chan=new MessageChannel();try{fxRt.node.port.postMessage({type:'__direct',port:_chan.port2},[_chan.port2]);}catch(e){}const ws=fxRtWsConnect(wsUrl,_chan.port1);fxRt.ws=ws;ws.onopen=()=>{if(token!==fxRt.startToken){try{ws.close();}catch(e){}fxRt.starting=false;return;}fxRt.active=true;fxRt.flushing=false;fxRt.starting=false;fxRt.inQueue=[];// bỏ audio stale sản xuất trước khi WS open (~4s load VST) — hết lag khởi đầu
|
||||
const _chan=new MessageChannel();try{fxRt.node.port.postMessage({type:'__direct',port:_chan.port2},[_chan.port2]);}catch(e){}const ws=await fxRtWsConnect(wsUrl,_chan.port1);// M25.1: await fetch worker giua start — stop/restart co the xay ra; ws cu
|
||||
// phai dong (worker terminate), khong de ro.
|
||||
if(token!==fxRt.startToken){try{ws.close();}catch(e){}fxRt.starting=false;return;}fxRt.ws=ws;ws.onopen=()=>{if(token!==fxRt.startToken){try{ws.close();}catch(e){}fxRt.starting=false;return;}fxRt.active=true;fxRt.flushing=false;fxRt.starting=false;fxRt.inQueue=[];// bỏ audio stale sản xuất trước khi WS open (~4s load VST) — hết lag khởi đầu
|
||||
try{fxRtPushAllParams();}catch(e){}};ws.onmessage=fxRtWsOnMessage;ws.onclose=()=>fxRtStop(true);ws.onerror=()=>{try{ws.close();}catch(e){}};// Worklet gom 4 block (1024 mẫu) vào 1 frame → tách 4 WS frame 512 floats
|
||||
// ở đây. Bridge VST3 nhận input dồn → take=4 → batch 1 call process 1024
|
||||
// mẫu (~3.5-4.75ms/block) → đủ 187.5/s (take=1: 5.5ms > budget 5.33ms →
|
||||
@@ -518,7 +526,7 @@ try{window.SonicAPI.fxRealtimeStop({session_id:st.session_id}).catch(()=>{});}ca
|
||||
}fxRt.sessionId=st.session_id;fxRtGuiPushShm();const u=new URL(API_BASE_URL);const wsProto=u.protocol==='https:'?'wss://':'ws://';// WS to 'localhost' bi tre ~300ms (xem fxRtStart) — dùng 127.0.0.1 loopback.
|
||||
const wsHost=u.hostname==='localhost'||u.hostname==='::1'?'127.0.0.1'+(u.port?':'+u.port:''):u.host;const tok=localStorage.getItem('sonic_token')||'';const wsUrl=`${wsProto}${wsHost}${st.ws_url}${tok?'?token='+encodeURIComponent(tok):''}`;// M25: WS chay tren Web Worker - warm chua co node (khong directPort);
|
||||
// worker van giu WS, binary drop trong worker nhu cu main drop (!fxRt.node).
|
||||
let ws;try{ws=fxRtWsConnect(wsUrl,null);}catch(e){try{window.SonicAPI.fxRealtimeStop({session_id:st.session_id}).catch(()=>{});}catch(e2){}fxRt.sessionId=null;fxRtGuiPushShm();fxRt.starting=false;return null;}fxRt.ws=ws;ws.onopen=()=>{if(token!==fxRt.startToken){// Bị thay bằng session mới hơn — dọn session mình vừa tạo (nếu vẫn là
|
||||
let ws;try{ws=await fxRtWsConnect(wsUrl,null);}catch(e){try{window.SonicAPI.fxRealtimeStop({session_id:st.session_id}).catch(()=>{});}catch(e2){}fxRt.sessionId=null;fxRtGuiPushShm();fxRt.starting=false;return null;}fxRt.ws=ws;ws.onopen=()=>{if(token!==fxRt.startToken){// Bị thay bằng session mới hơn — dọn session mình vừa tạo (nếu vẫn là
|
||||
// session hiện tại) + đóng ws.
|
||||
if(fxRt.sessionId===st.session_id){fxRt.sessionId=null;try{window.SonicAPI.fxRealtimeStop({session_id:st.session_id}).catch(()=>{});}catch(e){}fxRtGuiPushShm();}try{ws.close();}catch(e){}return;}fxRt.starting=false;fxRt.warm=true;fxRt.chainKey=fxRtChainKey(chain);fxRt.chainStructKey=fxRtChainStructKey(chain);};ws.onmessage=fxRtWsOnMessage;ws.onclose=()=>{// Chỉ teardown khi ws này vẫn là session hiện tại (guard session mới hơn).
|
||||
if(token===fxRt.startToken&&fxRt.sessionId===st.session_id)fxRtStop(true);};ws.onerror=()=>{try{ws.close();}catch(e){}};return st.session_id;}// C: bảo đảm session realtime sống cho GUI master native của path: PWR ON đã
|
||||
@@ -2237,7 +2245,7 @@ fxRtTrackReconcile(trackId,node,track);}catch(e){console.warn('rebuildTrackFxGra
|
||||
// Cần activeTrackNodesRef/isPlayingRef. Session giống master (fxRtStart):
|
||||
// fetch start → AudioWorkletNode 'sf-fx-realtime' → WS; WS open → splice
|
||||
// worklet vào chain hiện tại (đọc mods MỚI NHẤT — an toàn với rebuild).
|
||||
const fxRtTrackStart=async(trackId,track,path)=>{const key=trackId+':'+path;const chain=fxRtTrackChain(track);if(!chain.length)return;if(fxRtTracks[key]){if(!fxRtTracks[key].node||fxRtTracks[key].node.context!==getAudioContext())fxRtTrackStop(trackId,path,true);else return;}const ctx=getAudioContext();if(!ctx||!ctx.audioWorklet)return;const chainPayload=chain.map(s=>s.type==='vst3'?{type:'vst3',path:s.path,name:s.name||s.path,preset_b64:s.preset_b64||'',bypass:!!s.bypass||!!(track&&track.vstFxBypass)}:{type:'builtin',id:s.type,params:s.params&&Object.keys(s.params).length?s.params:{},bypass:!!s.bypass});let st;try{st=await window.SonicAPI.fxRealtimeStart({fx_chain:chainPayload,sample_rate:ctx.sampleRate||44100});}catch(e){console.warn('[FX RT track] start fail',trackId,path,e);return;}const entry={sessionId:st.session_id,ws:null,node:null,ready:false,path,spliced:null,chainKey:fxRtTrackChainKey(chain)+'|vb:'+(track&&track.vstFxBypass?'1':'0'),paramsBySlot:chain.map(s=>fxRtEntryParams(s,null)),inQueue:[],pumpTimer:null,lastInSent:0,flushing:false,flushTimer:null};fxRtTracks[key]=entry;fxRtGuiPushShm();try{await ctx.audioWorklet.addModule(`${API_BASE_URL}/static/js/sf-fx-realtime.js?v=202608291749`);const node=new AudioWorkletNode(ctx,'sf-fx-realtime',{numberOfInputs:1,numberOfOutputs:1,outputChannelCount:[2]});entry.node=node;const u=new URL(API_BASE_URL);const wsProto=u.protocol==='https:'?'wss://':'ws://';// WS to 'localhost' bi tre ~300ms (Chromium thu ::1 truoc roi fallback) - dung 127.0.0.1
|
||||
const fxRtTrackStart=async(trackId,track,path)=>{const key=trackId+':'+path;const chain=fxRtTrackChain(track);if(!chain.length)return;if(fxRtTracks[key]){if(!fxRtTracks[key].node||fxRtTracks[key].node.context!==getAudioContext())fxRtTrackStop(trackId,path,true);else return;}const ctx=getAudioContext();if(!ctx||!ctx.audioWorklet)return;const chainPayload=chain.map(s=>s.type==='vst3'?{type:'vst3',path:s.path,name:s.name||s.path,preset_b64:s.preset_b64||'',bypass:!!s.bypass||!!(track&&track.vstFxBypass)}:{type:'builtin',id:s.type,params:s.params&&Object.keys(s.params).length?s.params:{},bypass:!!s.bypass});let st;try{st=await window.SonicAPI.fxRealtimeStart({fx_chain:chainPayload,sample_rate:ctx.sampleRate||44100});}catch(e){console.warn('[FX RT track] start fail',trackId,path,e);return;}const entry={sessionId:st.session_id,ws:null,node:null,ready:false,path,spliced:null,chainKey:fxRtTrackChainKey(chain)+'|vb:'+(track&&track.vstFxBypass?'1':'0'),paramsBySlot:chain.map(s=>fxRtEntryParams(s,null)),inQueue:[],pumpTimer:null,lastInSent:0,flushing:false,flushTimer:null};fxRtTracks[key]=entry;fxRtGuiPushShm();try{await ctx.audioWorklet.addModule(`${API_BASE_URL}/static/js/sf-fx-realtime.js?v=202609071534`);const node=new AudioWorkletNode(ctx,'sf-fx-realtime',{numberOfInputs:1,numberOfOutputs:1,outputChannelCount:[2]});entry.node=node;const u=new URL(API_BASE_URL);const wsProto=u.protocol==='https:'?'wss://':'ws://';// WS to 'localhost' bi tre ~300ms (Chromium thu ::1 truoc roi fallback) - dung 127.0.0.1
|
||||
const wsHost=u.hostname==='localhost'||u.hostname==='::1'?'127.0.0.1'+(u.port?':'+u.port:''):u.host;const tok=localStorage.getItem('sonic_token')||'';const ws=new WebSocket(`${wsProto}${wsHost}${st.ws_url}${tok?'?token='+encodeURIComponent(tok):''}`);entry.ws=ws;ws.binaryType='arraybuffer';ws.onopen=()=>{entry.ready=true;entry.inQueue=[];// bỏ audio stale trước khi WS open — hết lag khởi đầu
|
||||
fxRtTrackSplice(trackId,path);try{(entry.paramsBySlot||[]).forEach((pl,slot)=>pl.forEach(p=>ws.send(JSON.stringify({cmd:'set_param',slot,key:p.key,value:p.value}))));}catch(e){}};ws.onmessage=ev=>{if(typeof ev.data==='string'){try{const d=JSON.parse(ev.data);if(d&&d.cmd==='flush_done'){entry.flushing=false;if(entry.flushTimer){clearTimeout(entry.flushTimer);entry.flushTimer=null;}if(entry.node)entry.node.port.postMessage({type:'flush_done'});}}catch(e){}return;}if(!(ev.data instanceof ArrayBuffer)||!entry.node)return;if(entry.flushing)return;// V24: output stale trong luc flush — drop
|
||||
const f=new Float32Array(ev.data);const half=f.length/2;const l=new Float32Array(half);const r=new Float32Array(half);for(let i=0;i<half;i++){l[i]=f[i*2];r[i]=f[i*2+1];}entry.node.port.postMessage({type:'out',l,r});};ws.onclose=()=>fxRtTrackStop(trackId,path,true);ws.onerror=()=>{try{ws.close();}catch(e){}};entry.node.port.onmessage=ev=>{const d=ev.data;if(!d||!entry.ws)return;if(d.type==='flush'){// V24: nhu master — worklet underrun-resume → flush engine ring
|
||||
|
||||
Reference in New Issue
Block a user