mirror of
https://github.com/kennethreitz/pytheory.git
synced 2026-06-05 06:46:14 +00:00
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:
+9
-10
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user