fix: auto-compute length_bars for legacy items on BPM change

- Items without length_bars: compute bars from duration/secondsPerBar
- Round to nearest quarter bar for musical accuracy
- Recalculate duration = bars * new secondsPerBar for all items
- Maintains correct bar count for both legacy and new items
This commit is contained in:
2026-07-27 08:10:05 +07:00
parent 5915049260
commit ea5043c981
2 changed files with 19 additions and 3 deletions
+17 -2
View File
@@ -6422,8 +6422,23 @@ const App = () => {
const secondsPerBar = (60.0 / bpmVal) * 4;
updateActiveTracks(prev => prev.map(t => ({
...t,
midiItems: (t.midiItems || []).map(m => m.length_bars ? { ...m, duration: m.length_bars * secondsPerBar } : m),
sections: (t.sections || []).map(s => s.length_bars ? { ...s, duration: s.length_bars * secondsPerBar } : s)
midiItems: (t.midiItems || []).map(m => {
if (m.length_bars) return { ...m, duration: m.length_bars * secondsPerBar };
if (m.duration) {
// Legacy item without length_bars: compute bars from current duration/BPM
const bars = Math.max(0.25, Math.round(m.duration / secondsPerBar * 4) / 4);
return { ...m, length_bars: bars, duration: bars * secondsPerBar };
}
return m;
}),
sections: (t.sections || []).map(s => {
if (s.length_bars) return { ...s, duration: s.length_bars * secondsPerBar };
if (s.duration) {
const bars = Math.max(0.25, Math.round(s.duration / secondsPerBar * 4) / 4);
return { ...s, length_bars: bars, duration: bars * secondsPerBar };
}
return s;
})
})));
}, [bpm]);
const openPanel = id => {