Skip to content

fix: keep the beat in step with the music, and make tapping it reliable - #77

Merged
madeofpendletonwool merged 2 commits into
mainfrom
fix/beat-timing
Nov 10, 2026
Merged

madeofpendletonwool merged 2 commits into
mainfrom
fix/beat-timing

Conversation

@madeofpendletonwool

Copy link
Copy Markdown
Owner

Why

The beat looked off even when the BPM was right, and tapping the beat in the beat lab would reset part way through.

I checked Razorblades and Runways (The Revivalists) against librosa. The server's beat map is right: 101.6 BPM, beats on the onsets in every band, and the downbeat on the kick (kick on 1 and 3, snare on 2 and 4). The error is on the playback side, in where each screen thinks the song is.

Playback timing

  • Speaker reports are back-dated to when they were read. The server used to stamp each report's position with the time it arrived, so every screen lagged by the report's trip, which varied report to report. PlayerReport.at (server clock, ms) now says when the position was read, and the engine moves the position on by that age, capped at 2 s (maxReportAge) and never backwards.
  • Measured clock sync. New public GET /time. On each room-socket hello, the client times 5 round trips and keeps the offset from the quickest (measureServerClock). The hello's rough offset, which treated anything under 250 ms as zero, is now only a stopgap until the measurement lands.

Tap tempo (web/src/lib/tap-tempo.ts)

  • Taps are timed on performance.now() and converted to song position once, so server position corrections no longer reset or skew them. The delay calibration tap gets the same fix.
  • A skipped beat is counted and a double tap is dropped. The fix applies from the 4th tap, as the UI says.
  • When the taps match the analysed tempo (or half of it), they shift the map's own beats (offset plus which beat is the one) instead of replacing them with a fixed grid, so a live band's drift is still followed. ±20 ms and Move 1 shift the map the same way. A different tempo still makes a grid.

Testing

  • Web: typecheck, lint, all 325 tests pass (new tap-tempo.test.ts, plus a clock offset test).
  • Server: playback tests pass, including new report-age cases.
  • Six httpapi auth/invite tests (TestLogout, TestLoginAndRateLimit, …) fail identically on main without this change, so they're unrelated.
  • Not yet tried by playing music on a deployed build.

The beat maps were right (checked on a real song against librosa: same
tempo, beats on the onsets, the one on the kick), but every screen placed
them late, by a varying amount:

- The speaker's position reports were stamped when they reached the
  server, so everyone lagged by the report's trip, more when it queued
  behind the last one. Reports now say when they were read (`at`), and
  the server moves the position on by up to 2 s.
- Clock sync was one sample from the socket's hello, with anything under
  250 ms taken as zero. A new GET /time lets each client time a few round
  trips and keep the quickest.

Tap tempo in the beat lab:

- Taps are timed on performance.now(). The room's position jumps as the
  server corrects it, which reset taps part way or skewed them.
- A skipped beat is counted and a double tap dropped, instead of either
  wrecking the fit. Four taps before it applies, as the lab says.
- At the analysed tempo, taps (and the ±20 ms / Move 1 buttons) shift the
  song's own beats into place rather than replacing them with a fixed
  grid, so a live band's drift is still followed.
It was fixed at 2026-10-05. Sessions last 30 days by that clock, but the
test client's cookie jar runs on the real one, so from 2026-11-04 it
dropped every session cookie and the signed-in tests failed.
@madeofpendletonwool
madeofpendletonwool merged commit 9b489cf into main Nov 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant