Files
lintunes/lintunes/tap_tempo.py
T
trav b7f8761bd1 Import existing LinTunes project
Snapshot of the existing codebase before working through the TASKS.md
backlog. Real library data (data/) and the iTunes import fixture
(itunes-test-library/) are gitignored.
2026-06-26 21:12:01 -04:00

35 lines
998 B
Python

"""Tap-tempo math, kept Qt-free so it's unit-testable."""
class TapTempo:
RESET_GAP_SECONDS = 2.5
MAX_TAPS = 8
def __init__(self):
self._taps: list[float] = []
def tap(self, now: float) -> int | None:
"""Record a tap at monotonic time `now`; returns the current bpm
estimate (None until there are two taps in the series)."""
if self._taps and now - self._taps[-1] > self.RESET_GAP_SECONDS:
self._taps = []
self._taps.append(now)
self._taps = self._taps[-self.MAX_TAPS:]
return self.bpm()
def bpm(self) -> int | None:
if len(self._taps) < 2:
return None
intervals = [b - a for a, b in zip(self._taps, self._taps[1:])]
average = sum(intervals) / len(intervals)
if average <= 0:
return None
return round(60.0 / average)
@property
def tap_count(self) -> int:
return len(self._taps)
def reset(self):
self._taps = []