diff --git a/app/static/js/app.jsx b/app/static/js/app.jsx index 23450ac..f101889 100644 --- a/app/static/js/app.jsx +++ b/app/static/js/app.jsx @@ -309,6 +309,8 @@ let _dbgFirstAudiblePending = false; let _fxRtPlayheadSnapPending = false; let _fxRtFreezeTimer = null; let _fxRtFreezeTries = 0; +let _fxRtSeekFreeze = false; +let _fxRtSeekFreezeTimer = null; const _nativeTrackUrls = {}; const _nativeTrackPending = {}; // ── MIDI overlap cleanup + polyphony gain (spec midi_polyphony_loudness) ─ @@ -21004,6 +21006,17 @@ const App = () => { if (fxRt && fxRt.active) { _fxRtPlayheadSnapPending = false; fxRtAnchorProcessed(); + // V45.16: seek/restart FX active — giữ playhead tại startOffset cho tới khi + // worklet phát block đầu (fx_play_first). Trước đây playhead chạy theo + // processedSamples trong lúc re-buffer FILL 24 (~128ms) → playhead trước + // âm (Bug 1b). Fail-safe 1500ms: engine không phát (session fail) → thả. + _fxRtSeekFreeze = true; + if (_fxRtSeekFreezeTimer) { clearTimeout(_fxRtSeekFreezeTimer); } + _fxRtSeekFreezeTimer = setTimeout(function () { + _fxRtSeekFreeze = false; + _fxRtSeekFreezeTimer = null; + fxRtAnchorProcessed(); + }, 1500); return; } // V41: gate theo CHAIN (không chỉ active) — session FX warm (stop giữ warm, @@ -21034,6 +21047,13 @@ const App = () => { }, _estMs); }; window.__fxRtSnapPlayhead = function () { + // V45.16: hết seek-freeze — playhead bắt đầu từ startOffset tại mẫu nghe được. + if (_fxRtSeekFreeze) { + _fxRtSeekFreeze = false; + if (_fxRtSeekFreezeTimer) { clearTimeout(_fxRtSeekFreezeTimer); _fxRtSeekFreezeTimer = null; } + fxRtAnchorProcessed(); + return; + } if (fxRt && fxRt.active && _fxRtPlayheadSnapPending) { _fxRtPlayheadSnapPending = false; startAudioTimeRef.current = getAudioContext().currentTime; @@ -24573,9 +24593,11 @@ const App = () => { const updatedTime = startOffsetTimeRef.current + elapsed; // G3: playhead display engine-derived khi FX active (wrap/loop trigger giu // wall-clock — transport truth). - const _playheadSec = (fxRt && fxRt.active && typeof fxRt.processedSamples === 'number' && fxRt.processedAnchor != null) - ? Math.max(startOffsetTimeRef.current, startOffsetTimeRef.current + (fxRt.processedSamples - fxRt.processedAnchor - (fxRt.latencyTotal || 0)) / (context.sampleRate || 44100)) - : updatedTime; + const _playheadSec = (_fxRtSeekFreeze && fxRt && fxRt.active) + ? startOffsetTimeRef.current + : ((fxRt && fxRt.active && typeof fxRt.processedSamples === 'number' && fxRt.processedAnchor != null) + ? Math.max(startOffsetTimeRef.current, startOffsetTimeRef.current + (fxRt.processedSamples - fxRt.processedAnchor - (fxRt.latencyTotal || 0)) / (context.sampleRate || 44100)) + : updatedTime); // Selection Loop - LOOP_MAKER.md + LOOP_EDITOR_2.md §4.2 // If selection cleared by user, play linearly (don't loop) diff --git a/app/static/js/app.precompiled.js b/app/static/js/app.precompiled.js index 62d5fc2..edcdc53 100644 --- a/app/static/js/app.precompiled.js +++ b/app/static/js/app.precompiled.js @@ -70,7 +70,7 @@ window.__restorePendingFadeSession=restorePendingFadeSession;// V31: live note ( window.__clearMasterFxQueue=function(){_dbgLastClearMs=performance.now();try{if(fxRt&&fxRt.node&&fxRt.node.port)fxRt.node.port.postMessage({type:'clear_queue',mode:'live'});}catch(e){}};// V32: queueLen master FX worklet hien tai (midi_push log — ARM latency). window.__fxRtQueueLen=function(){try{return fxRt&&fxRt.stats?fxRt.stats.queueLen:'n/a';}catch(e){return'n/a';}};// V28: do jank — play_start timestamp; play_rAF / play_first_audio log delta. let _dbgPlayStartMs=0;let _dbgLastClearMs=0;let _dbgFirstAudioPending=false;let _dbgFirstAudiblePending=false;// V40: real PDC — freeze playhead theo latency đo được (fx_play_first self-calib). -let _fxRtPlayheadSnapPending=false;let _fxRtFreezeTimer=null;let _fxRtFreezeTries=0;const _nativeTrackUrls={};const _nativeTrackPending={};// ── MIDI overlap cleanup + polyphony gain (spec midi_polyphony_loudness) ─ +let _fxRtPlayheadSnapPending=false;let _fxRtFreezeTimer=null;let _fxRtFreezeTries=0;let _fxRtSeekFreeze=false;let _fxRtSeekFreezeTimer=null;const _nativeTrackUrls={};const _nativeTrackPending={};// ── MIDI overlap cleanup + polyphony gain (spec midi_polyphony_loudness) ─ // Dedup: note cùng pitch + cùng start_beat → gộp 1, giữ velocity cao hơn // (tránh synth instantiate nhiều voice cùng pitch — spec §II.5). // Legato cleanup: note trước cùng pitch kéo dài qua start_beat note sau → cắt @@ -339,7 +339,7 @@ else if(fxRt.chainKey!==ck){fxRtSetChain(chain);fxRt.chainKey=ck;return;}else re // 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=202608311122`);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=202608311143`);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):''}`;const ws=new WebSocket(wsUrl);fxRt.ws=ws;ws.binaryType='arraybuffer';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 @@ -1566,14 +1566,19 @@ const startOffsetTimeRef=useRef(0);const startBufferOffsetRef=useRef(0);const st // session không active trong 1500ms → hủy freeze (đừng đóng băng playhead vô hạn). window.__fxRtPlayheadFreeze=function(allowCold){try{if(_fxRtFreezeTimer){clearTimeout(_fxRtFreezeTimer);_fxRtFreezeTimer=null;}}catch(e3){}// G3: FX active -> playhead engine-derived (processed) — KHONG freeze/snap. // Anchor processed tai day (moi play/resume path deu goi ham nay). -if(fxRt&&fxRt.active){_fxRtPlayheadSnapPending=false;fxRtAnchorProcessed();return;}// V41: gate theo CHAIN (không chỉ active) — session FX warm (stop giữ warm, +if(fxRt&&fxRt.active){_fxRtPlayheadSnapPending=false;fxRtAnchorProcessed();// V45.16: seek/restart FX active — giữ playhead tại startOffset cho tới khi +// worklet phát block đầu (fx_play_first). Trước đây playhead chạy theo +// processedSamples trong lúc re-buffer FILL 24 (~128ms) → playhead trước +// âm (Bug 1b). Fail-safe 1500ms: engine không phát (session fail) → thả. +_fxRtSeekFreeze=true;if(_fxRtSeekFreezeTimer){clearTimeout(_fxRtSeekFreezeTimer);}_fxRtSeekFreezeTimer=setTimeout(function(){_fxRtSeekFreeze=false;_fxRtSeekFreezeTimer=null;fxRtAnchorProcessed();},1500);return;}// V41: gate theo CHAIN (không chỉ active) — session FX warm (stop giữ warm, // bypass toggle gọi rebuildMasteringGraph KHÔNG gọi fxRtStop) → fxRt.active vẫn // true khi bypass → trước đây freeze nhầm → playhead chạy trước → wrap sớm → vấp. const _fxWillRun=!!(fxRt&&fxRtChain().length>0&&(fxRt.active||allowCold));if(!_fxWillRun)return;const _estMs=fxRt.pdcCalibMs||FX_RT_START_EST_MS;const _wasActiveAtFreeze=!!(fxRt&&fxRt.active);startAudioTimeRef.current=getAudioContext().currentTime+_estMs/1000;_fxRtPlayheadSnapPending=true;_fxRtFreezeTries=0;// V41: snap xác định bằng timer tại est — loop wrap warm không post fx_play_first // cho pass mới (old-tail tiêu thụ dbgPlayFirst) nên không có event để snap. Cold // start (fxRt.active=false) KHÔNG snap sớm: chờ fx_play_first thật (VST load 4s). _fxRtFreezeTimer=setTimeout(function freezeSnapRetry(){_fxRtFreezeTimer=null;if(_wasActiveAtFreeze&&_fxRtPlayheadSnapPending){_fxRtPlayheadSnapPending=false;startAudioTimeRef.current=getAudioContext().currentTime;return;}if(fxRt&&fxRt.starting){if(_fxRtFreezeTries<20){_fxRtFreezeTries++;_fxRtFreezeTimer=setTimeout(freezeSnapRetry,500);}return;}if(!fxRt||!fxRt.active){startAudioTimeRef.current=getAudioContext().currentTime;}// session fail → bỏ freeze -},_estMs);};window.__fxRtSnapPlayhead=function(){if(fxRt&&fxRt.active&&_fxRtPlayheadSnapPending){_fxRtPlayheadSnapPending=false;startAudioTimeRef.current=getAudioContext().currentTime;}};const animationFrameIdRef=useRef(null);const toastTimeoutRef=useRef(null);const rulerDragStartRef=useRef(null);const rulerAnchorRef=useRef(null);const isDraggingRulerRef=useRef(false);const subTabDragStartRef=useRef(null);const isDraggingSubTabRef=useRef(false);const handlePlayPauseRef=useRef(null);const currentTimeRef=useRef(currentTime);// D9: playhead sample counter cho native bridge (A11/A13 dùng để đồng bộ +},_estMs);};window.__fxRtSnapPlayhead=function(){// V45.16: hết seek-freeze — playhead bắt đầu từ startOffset tại mẫu nghe được. +if(_fxRtSeekFreeze){_fxRtSeekFreeze=false;if(_fxRtSeekFreezeTimer){clearTimeout(_fxRtSeekFreezeTimer);_fxRtSeekFreezeTimer=null;}fxRtAnchorProcessed();return;}if(fxRt&&fxRt.active&&_fxRtPlayheadSnapPending){_fxRtPlayheadSnapPending=false;startAudioTimeRef.current=getAudioContext().currentTime;}};const animationFrameIdRef=useRef(null);const toastTimeoutRef=useRef(null);const rulerDragStartRef=useRef(null);const rulerAnchorRef=useRef(null);const isDraggingRulerRef=useRef(false);const subTabDragStartRef=useRef(null);const isDraggingSubTabRef=useRef(false);const handlePlayPauseRef=useRef(null);const currentTimeRef=useRef(currentTime);// D9: playhead sample counter cho native bridge (A11/A13 dùng để đồng bộ // timeline sample-accurate); cập nhật trong updatePlayhead khi bridge active. const bridgePlayheadSampleRef=useRef(0);// V10: timer của transport STOP#2 (150ms sau STOP#1, V8 bug 3 guard) — loop // restart gọi stopAllPlayback() + startTrackPlayback() cùng lúc, STOP#2 trễ @@ -1895,7 +1900,7 @@ stopAllPlayback();if(st.isLooping){setSubTabs(prev=>prev.map(s=>s.id===activeTab // nếu chỉ chạy silent buffer). if(st.type==='PIANO_ROLL')schedulePianoRollMidi(st,0);startSubTabPlayback(st,0);animationFrameIdRef.current=requestAnimationFrame(updatePlayhead);}else{setSubTabs(prev=>prev.map(s=>s.id===activeTabRef.current?{...s,currentTime:0,isPlaying:false}:s));}return;}setSubTabs(prev=>prev.map(s=>s.id===activeTabRef.current?{...s,currentTime:wallTime}:s));animationFrameIdRef.current=requestAnimationFrame(updatePlayhead);return;}if(!isPlaying)return;const context=getAudioContext();const elapsed=context.currentTime-startAudioTimeRef.current;const updatedTime=startOffsetTimeRef.current+elapsed;// G3: playhead display engine-derived khi FX active (wrap/loop trigger giu // wall-clock — transport truth). -const _playheadSec=fxRt&&fxRt.active&&typeof fxRt.processedSamples==='number'&&fxRt.processedAnchor!=null?Math.max(startOffsetTimeRef.current,startOffsetTimeRef.current+(fxRt.processedSamples-fxRt.processedAnchor-(fxRt.latencyTotal||0))/(context.sampleRate||44100)):updatedTime;// Selection Loop - LOOP_MAKER.md + LOOP_EDITOR_2.md §4.2 +const _playheadSec=_fxRtSeekFreeze&&fxRt&&fxRt.active?startOffsetTimeRef.current:fxRt&&fxRt.active&&typeof fxRt.processedSamples==='number'&&fxRt.processedAnchor!=null?Math.max(startOffsetTimeRef.current,startOffsetTimeRef.current+(fxRt.processedSamples-fxRt.processedAnchor-(fxRt.latencyTotal||0))/(context.sampleRate||44100)):updatedTime;// Selection Loop - LOOP_MAKER.md + LOOP_EDITOR_2.md §4.2 // If selection cleared by user, play linearly (don't loop) if(!selectionCleared&&isLoopingSelection&&selLeft!==null&&selRight!==null){if(selRight>selLeft&&updatedTime>=selRight){if(hasAnySolo||selectionMode==='local'){fxRtWrapRestart=true;// V38: loop wrap restart try{_dbgLog('wrap_info','solo='+(hasAnySolo?1:0)+' local='+(selectionMode==='local'?1:0)+' tracks='+(tracks?tracks.length:-1)+' nl='+nativeLoopSrcsRef.current.size);}catch(e){}fxRtKeepForRestart=true;// restart/resume — giữ session FX realtime @@ -1953,7 +1958,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=202608311122`);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=202608311143`);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;ifxRtTrackStop(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 diff --git a/app/templates/index.html b/app/templates/index.html index 4e7ae26..72ac1fc 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -51,7 +51,7 @@ - +