Refined Rekordbox timing fix

This commit is contained in:
patben8 committed 2026-10-01 10:54:27 +02:00
1 parent 34b18893ef
commit b5b0fa91fe
6 files changed
+267 -39

No files matched your search

@@ -99,9 +99,15 @@ def beats_from_markers(
n = 1
# Guard against a zero/negative span producing an endless loop.
while t < end - 1e-9 and step > 0:
beat_nums.append(n)
bpms.append(m.bpm)
times.append(t)
# A beat before the audio starts is skipped but still COUNTED, so the
# next one keeps its place in the bar. The generic model can hold one
# (a Traktor grid anchored inside an MP3's header frame reads
# negative); rekordbox stores no negative times, and clamping it to 0
# would invent a beat there.
if t >= 0.0:
beat_nums.append(n)
bpms.append(m.bpm)
times.append(t)
n = n % 4 + 1
t += step
return beat_nums, bpms, times
@@ -324,11 +324,11 @@ class RekordboxStore:
def time_offset(self, track_id: str) -> float:
"""Seconds rekordbox's clock runs behind the decoded audio for this
track (~25 ms on MP3/AAC, 0 lossless). Added on every position written,
subtracted on every position read — see `timebase`. Decided by the
stored FolderPath's format, so it holds even when the file is offline."""
folder = getattr(self.content(track_id), "FolderPath", None)
return timebase.offset(str(folder) if folder else None)
track — per FILE (an MP3's header, an M4A's edit list; 0 lossless).
Added on every position written, subtracted on every position read —
see `timebase`. Read from the file as it resolves here (path mapping
applied); a file that is offline falls back to its format's default."""
return timebase.offset(self.audio_path(track_id))
def cover_art(self, track_id: str) -> tuple[bytes, str] | None:
"""The art embedded in the audio file, else Rekordbox's own copy.
@@ -1,36 +1,53 @@
"""Pioneer's clock vs the decoded audio's — one rule, used at every boundary.
**On MP3 and AAC, rekordbox's time 0 is NOT the decoded audio's time 0.** It does
not trim the codec delay a decoder removes, so every position it stores — beats,
cues, loops — sits a constant 1105 samples (~25 ms at 44.1 kHz) LATER than the
same moment in the decoded audio. Lossless files have no such delay.
**rekordbox's time 0 is not always the decoded audio's time 0**, and by how much
depends on the FILE, not just its format. rekordbox does not trim the start
padding a gapless decoder removes, so every position it stores — beats, cues,
loops — sits LATER than the same moment in the decoded audio by exactly that
padding:
Measured, not assumed: Konduktor's grid detector (which works in the decoded
time base) against rekordbox's own grids for the local library gave a median of
+25.0 ms on 16 MP3s, +24.5 ms on 4 M4As and 0.0 ms on 7 WAVs. 1105 samples is the
classic MP3 codec delay (576 encoder + 529 decoder).
* **MP3 with a Xing/Info header**: the header's encoder delay + 529 decoder
delay — 1105 samples (~25 ms at 44.1 kHz) for LAME's usual 576. rekordbox
SKIPS the header frame (Traktor does not — see `adapters/traktor/timebase.py`),
it just does not trim.
* **MP3 without a header**: 0. No decoder knows the delay, so none trims it.
* **AAC / M4A** (incl. a Traktor `.stem.m4a`): the edit list's priming, which
rekordbox ignores — 1024 samples from FFmpeg's encoder, 2112 from Apple's.
* **Lossless**: 0.
The generic model is in the DECODED time base — it is what Konduktor's deck plays
and what the grid detector measures. (Traktor's clock differs from it too, by a
different rule: `adapters/traktor/timebase.py`.) So a
Pioneer adapter ADDS the offset on every write and SUBTRACTS it on every read.
Get it wrong and nothing fails: every cue and beat is simply 25 ms early in
rekordbox (as the first OneLibrary exports were), or late in Konduktor.
Measured 2026-09-30 in rekordbox 7 with kit 4 (see the stem-conversion discussion
log): blinded hotcues Konduktor wrote on a synthetic kick landed on it only at
these offsets, hot cues placed by hand read back at them to rekordbox's 1 ms
resolution, and rekordbox's own grids agree. It replaced a fixed 1105 samples
for every lossy file, fitted to a library of MP3s that all had headers and
FFmpeg-encoded M4As — right for those, 25 ms wrong on a header-less MP3 and
22 ms wrong on Apple-encoded AAC.
Suspected, not yet measured: on an MP3 WITHOUT a Xing/Info header the decoder
trims nothing either, so the offset there should be 0, not 1105 — the three
header-less MP3s imported from a stick are exactly the ones whose grids read
~25 ms off (`core/mp3_gapless.py` can tell them apart).
Derived, not measured: an MP3 header whose delay field a decoder does not trust
(no LAME/Lavf/Lavc tag, or VBRI) — nothing is trimmed, so 0.
A file that cannot be READ (offline, or a path that does not resolve) falls back
to the old 1105 samples for a lossy suffix: the overwhelmingly common case, and
what every position written before this rule assumed.
The generic model is in the DECODED time base — what Konduktor's deck plays and
the grid detector measures. So a Pioneer adapter ADDS the offset on every write
and SUBTRACTS it on every read. Get it wrong and nothing fails: every cue and
beat is simply early in rekordbox (as the first OneLibrary exports were), or
late in Konduktor.
"""
from __future__ import annotations
from functools import lru_cache
from pathlib import Path
#: The codec delay rekordbox does not trim.
from ...core import mp3_gapless, mp4_edit
#: The fallback for a lossy file that cannot be read: MP3's classic codec delay.
CODEC_DELAY_SAMPLES = 1105
#: Formats that carry one. `.stem.m4a` (a Traktor STEM) is AAC.
#: Formats that carry a start padding. `.stem.m4a` (a Traktor STEM) is AAC.
LOSSY = frozenset({".mp3", ".m4a", ".mp4", ".aac"})
_MP4 = frozenset({".m4a", ".mp4"})
_DEFAULT_RATE = 44100
@@ -45,23 +62,43 @@ def _sample_rate(path: str) -> int:
return _DEFAULT_RATE
def _measured(p: Path) -> float | None:
"""The per-file offset, or None when the file cannot be read."""
suffix = p.suffix.lower()
if suffix == ".mp3":
start = mp3_gapless.read(p)
if start is None:
return None
return start.trimmed / start.sample_rate if start.header_frame else 0.0
if suffix in _MP4:
return mp4_edit.priming_seconds(p)
return None # raw ADTS .aac: nothing to read, keep the fallback
def offset(path: Path | str | None) -> float:
"""Seconds to ADD to a decoded-audio time to get rekordbox's; 0 if lossless.
A missing path (a track whose audio cannot be located) gets 0: there is no
file to say which format it is, and guessing lossy would move a lossless
track's cues.
track's cues. A path to a lossy file that cannot be read gets the fallback.
"""
if not path:
return 0.0
p = Path(path)
if p.suffix.lower() not in LOSSY:
return 0.0
measured = _measured(p)
if measured is not None:
return measured
return CODEC_DELAY_SAMPLES / _sample_rate(str(p))
def to_pioneer(seconds: float, off: float) -> float:
return seconds + off
"""A decoded-audio time -> rekordbox's, never before its own time 0: the
generic model can hold a position before the decoded audio starts (a
Traktor grid anchored inside an MP3's header frame), which no Pioneer field
can store."""
return max(0.0, seconds + off)
def from_pioneer(seconds: float, off: float) -> float: