Fix BPM: average up to 240 ticks, round to integer, update every beat

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-29 19:36:37 -04:00
parent 20fc5e40b8
commit 12f15d5138
+9 -10
View File
@@ -352,17 +352,16 @@ class LiveEngine:
now = _time.perf_counter()
self._clock_times.append(now)
if len(self._clock_times) > 96:
self._clock_times = self._clock_times[-96:]
if len(self._clock_times) >= 96:
# Average over 4 quarter notes (96 ticks) for stability
total_time = self._clock_times[-1] - self._clock_times[-96]
if len(self._clock_times) > 240:
self._clock_times = self._clock_times[-240:]
# Only update BPM every 24 ticks to avoid jitter
if self._clock_count % 24 == 0 and len(self._clock_times) >= 48:
# Use as many ticks as we have for best accuracy
n = min(len(self._clock_times), 240)
total_time = self._clock_times[-1] - self._clock_times[-n]
if total_time > 0:
self._bpm = 60.0 * 4.0 / total_time
elif len(self._clock_times) >= 24:
quarter_note_time = self._clock_times[-1] - self._clock_times[-24]
if quarter_note_time > 0:
self._bpm = 60.0 / quarter_note_time
ticks = n - 1
self._bpm = round(60.0 * ticks / (24.0 * total_time))
# Trigger drum hits at the right time
if self._drum_pattern and self._drum_channel: