FIX: sửa lỗi đồng bộ playhead vị trí play giữa MAIN SESSION/SECTION-TAB/PIANO ROLL TAB

This commit is contained in:
2026-08-07 10:37:34 +07:00
parent 4981785913
commit 6c16bf8410
4 changed files with 116 additions and 40 deletions
+79 -25
View File
@@ -14750,6 +14750,31 @@ const App = () => {
const sessionTabsRef = useRef(sessionTabs);
sessionTabsRef.current = sessionTabs;
// V trí MIDI item (trong context ca piano roll) dùng đ đng b playhead
// 2 chiu SECTION-TAB PIANO ROLL: item.startTime (local section / main) +
// st.currentTime (v trí TRONG item). Dùng REFS (activeTracksRef/
// sessionTabsRef) an toàn vi closure cũ (updatePlayhead/rAF loop).
const getPianoRollItemInfo = st => {
try {
if (!st) return null;
let list = null;
if (st.parent_tab_id && st.parent_tab_id.startsWith('session_')) {
const _pst = sessionTabsRef.current.find(s => s.id === st.parent_tab_id);
list = _pst ? _pst.tracks : null;
} else {
list = activeTracksRef.current;
}
if (!list) return null;
const _trk = list.find(t => t.id === st.trackId);
if (!_trk) return null;
const _item = (_trk.midiItems || []).find(m => m.id === st.target_id);
if (!_item) return null;
return { startTime: _item.startTime || 0, duration: _item.duration || 4 };
} catch (e) {
return null;
}
};
// Track mute/solo/volume signature: only re-apply gains when something that
// affects audibility actually changed (loads, undo, AI ops, section tabs).
const trackMuteSoloSigRef = useRef({});
@@ -14851,28 +14876,41 @@ const App = () => {
// trưc đó m tab lúc main đang play (mainResumeRef đã set).
const _resume = mainResumeRef.current;
mainResumeRef.current = null;
if (_resume && prevSub.type === 'PIANO_ROLL' && (activeTab === 'main' || (activeTab && activeTab.startsWith('session_')))) {
try {
const _elapsed = getAudioContext().currentTime - _resume.audioTime;
const _resumeAt = _resume.offset + _elapsed;
console.log('[Resume] main play từ', _resumeAt.toFixed(2), 's (offset', _resume.offset.toFixed(2), '+ elapsed', _elapsed.toFixed(2), ')');
stopAllPlayback();
startOffsetTimeRef.current = _resumeAt;
startAudioTimeRef.current = getAudioContext().currentTime;
startTrackPlayback(_resumeAt);
if (prevSub.type === 'PIANO_ROLL' && (activeTab === 'main' || (activeTab && activeTab.startsWith('session_')))) {
// Resume section/main play ti v trí TƯƠNG NG vi piano roll:
// resumeAt = item.startTime + st.currentTime (v trí TRONG item
// yêu cu: play piano roll v trí nào v SECTION play đúng v trí
// đó). Ưu tiên item info; fallback mainResumeRef (offset+elapsed);
// fallback cui: setIsPlaying(true) playhead chy theo âm còn kêu.
let _resumeAt = null;
const _itemInfo = getPianoRollItemInfo(prevSub);
if (_itemInfo) {
_resumeAt = _itemInfo.startTime + (prevSub.currentTime || 0);
// V MAIN mà item thuc SECTION: v trí main = secStart + local
if (activeTab === 'main' && prevSub.parent_tab_id && prevSub.parent_tab_id.startsWith('session_')) {
const _pst = sessionTabsRef.current.find(s => s.id === prevSub.parent_tab_id);
if (_pst && _pst.sectionId) {
(activeTracksRef.current || []).forEach(_t => (_t.sections || []).forEach(_s => {
if (_s.sectionId === _pst.sectionId || _s.id === _pst.sectionId) _resumeAt += (_s.start || 0);
}));
}
}
} else if (_resume) {
_resumeAt = _resume.offset + (getAudioContext().currentTime - _resume.audioTime);
}
if (_resumeAt !== null) {
try {
console.log('[Resume] play tại', _resumeAt.toFixed(2), 's (item.startTime=' + (_itemInfo ? _itemInfo.startTime.toFixed(2) : '?') + ' + st.currentTime=' + (prevSub.currentTime || 0).toFixed(2) + ')');
stopAllPlayback();
startOffsetTimeRef.current = _resumeAt;
startAudioTimeRef.current = getAudioContext().currentTime;
startTrackPlayback(_resumeAt);
setIsPlaying(true);
} catch (e) { console.warn('[Resume] error:', e); }
} else {
console.log('[Playhead] tiếp tục theo piano roll (không stop âm) — activeTab=' + activeTab);
setIsPlaying(true);
} catch (e) { console.warn('[Resume] error:', e); }
} else if (prevSub.type === 'PIANO_ROLL' && (activeTab === 'main' || (activeTab && activeTab.startsWith('session_')))) {
// Play THUN piano roll (main chưa tng play mainResumeRef null):
// ri tab KHÔNG stop âm (0415) NHƯNG isPlaying=false nhánh
// main updatePlayhead `if (!isPlaying) return` playhead NGNG dù
// âm piano roll còn kêu (user: playhead không đng b/ngng khi
// chuyn tab). setIsPlaying(true): playhead main/section chy theo
// elapsed (startOffsetTimeRef/startAudioTimeRef đang là ca piano
// roll play đng b vi âm đang phát). Hết bài (projectEnd)
// nhánh main t stop.
console.log('[Playhead] tiếp tục theo piano roll (không stop âm) — activeTab=' + activeTab);
setIsPlaying(true);
}
}
} else if (prevSub && prevSub.type === 'PIANO_ROLL') {
// Ri PIANO ROLL tab KHÔNG play: main/section play ngm vn chy (âm
@@ -18495,13 +18533,29 @@ const App = () => {
if (!st || !st.buffer) return;
if (!st.isPlaying) {
// PIANO ROLL / sub-tab KHÔNG play: playhead theo MAIN/SECTION nếu đang
// play (đng b v trí âm THT user: di chuyn tab qua li quay
// li piano roll tab playhead phi chy theo âm đang phát); không
// có play nào đng yên (chế đ edit). Loop luôn tiếp tc (rAF).
// play ĐNG B 2 CHIU: v trí play (section/main) v trí TRONG
// item (st.currentTime = playPos - item.startTime); playhead vưt ra
// ngoài duration item bt đu t đu (0). Không có play nào đng
// yên (chế đ edit). Loop luôn tiếp tc (rAF).
if (isPlaying) {
const context = getAudioContext();
const _ph = startOffsetTimeRef.current + (context.currentTime - startAudioTimeRef.current);
setSubTabs(prev => prev.map(s => s.id === st.id ? { ...s, currentTime: _ph } : s));
let _local = _ph;
const _itemInfo = getPianoRollItemInfo(st);
if (_itemInfo) {
if (_ph < _itemInfo.startTime) _local = 0;
else if (_ph >= _itemInfo.startTime + _itemInfo.duration) _local = 0; // vưt duration item đu
else _local = _ph - _itemInfo.startTime;
}
// Log chn đoán (rate-limited ~2 ln/giây): xác nhn đng b MAIN/
// SECTION PIANO ROLL ph (v trí play) local (trong item).
try {
const _sf = (window.__syncFrame = (window.__syncFrame || 0) + 1);
if (_sf % 45 === 0) {
console.log('[Sync] PR', st.id, 'ph=', _ph.toFixed(2), 'item.start=', _itemInfo ? _itemInfo.startTime.toFixed(2) : '-', 'dur=', _itemInfo ? _itemInfo.duration.toFixed(2) : '-', '→ local=', _local.toFixed(2), 'tab=', activeTabRef.current);
}
} catch (e) { }
setSubTabs(prev => prev.map(s => s.id === st.id ? { ...s, currentTime: _local } : s));
}
animationFrameIdRef.current = requestAnimationFrame(updatePlayhead);
return;