Files
konduktor/backend/konduktor/adapters/rekordbox/beatgrid.py
T
2026-10-01 10:54:27 +02:00

162 lines
6.9 KiB
Python

"""Rekordbox's beatgrid <-> the generic marker list.
**Rekordbox does not store markers.** There is no beatgrid table in
``master.db`` at all; the grid lives in the track's ANLZ analysis file as a
``PQTZ`` tag holding EVERY BEAT explicitly — beat-number-in-bar, BPM and time,
one entry per beat (a 4-minute track is ~500 entries).
Konduktor's generic model is a marker list, so this module is the projection
between the two:
* read — collapse runs of equal BPM into one marker each, so a constant-tempo
track becomes a list of length one and a variable-tempo track becomes one
marker per tempo change. This is exactly the shape ``core.model.GridMarker``
describes.
* write — expand a marker list back into a full beat list (milestone 2; the
read direction is what a read-only adapter needs).
BPMs are compared with a tolerance because Rekordbox stores them as hundredths
and floating-point division reintroduces noise; two beats within `BPM_EPSILON`
are the same tempo and must not produce a spurious marker.
"""
from __future__ import annotations
from ...core.model import GridMarker
BPM_EPSILON = 0.005 # half of the 0.01 BPM Rekordbox can actually represent
def markers_from_beats(
times: list[float], bpms: list[float], beats: list[int] | None = None
) -> list[GridMarker]:
"""Collapse a per-beat grid into the generic marker list.
`times` are seconds, `bpms` the tempo in force at each beat, `beats` the
beat-in-bar (1-4) of each. A marker is emitted for the start of the grid and
wherever the tempo changes.
**The first marker goes on the first DOWNBEAT**, not the first beat. In the
generic model the first marker is bar 1 (Traktor's convention), but a
Pioneer grid often starts mid-bar: Motorola's opens on beat 3 at 0.026 s,
with its first downbeat at 0.986 s. Put the marker on the first beat and
every bar lands two beats early — in Traktor after an import, and on the
CDJ after an export. The beats before it are not lost: a grid extends back
to the start of the track from its first marker, and `beats_from_markers`
writes them back numbered as they were.
"""
if not times or not bpms:
return []
out: list[GridMarker] = []
last_bpm: float | None = None
for i, (t, bpm) in enumerate(zip(times, bpms)):
if last_bpm is None or abs(bpm - last_bpm) > BPM_EPSILON:
out.append(GridMarker(start=float(t), bpm=float(bpm), name=None, companion=None))
last_bpm = float(bpm)
if beats and out:
# The first tempo run ends where the second marker starts (or never).
run_end = out[1].start if len(out) > 1 else float("inf")
downbeat = next((t for t, b in zip(times, beats) if int(b) == 1 and t < run_end), None)
if downbeat is not None:
out[0] = out[0].model_copy(update={"start": float(downbeat)})
return out
def beats_from_markers(
markers: list[GridMarker], duration_sec: float
) -> tuple[list[int], list[float], list[float]]:
"""Expand a marker list into the per-beat arrays Rekordbox stores.
Each marker governs until the next one; the last runs to `duration_sec`.
Returns ``(beat_in_bar, bpms, times)`` with beat numbers cycling 1-4 from
each marker, which is how Rekordbox numbers bars.
The first marker's tempo also runs BACKWARDS to the start of the track: a
grid covers the whole track in Traktor and in rekordbox alike, and a marker
placed on the first downbeat (see `markers_from_beats`) has beats before it
— numbered 4, 3, … counting back, which reproduces rekordbox's own grid.
"""
beat_nums: list[int] = []
bpms: list[float] = []
times: list[float] = []
if not markers:
return beat_nums, bpms, times
ordered = sorted(markers, key=lambda m: m.start)
first = next((m for m in ordered if m.bpm > 0), None)
if first is not None:
step = 60.0 / first.bpm
lead = int((first.start + 1e-9) // step) # whole beats that fit before it
for k in range(lead, 0, -1):
beat_nums.append((-k) % 4 + 1)
bpms.append(first.bpm)
times.append(first.start - k * step)
for i, m in enumerate(ordered):
if m.bpm <= 0:
continue
end = ordered[i + 1].start if i + 1 < len(ordered) else duration_sec
step = 60.0 / m.bpm
t = m.start
n = 1
# Guard against a zero/negative span producing an endless loop.
while t < end - 1e-9 and step > 0:
# 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
# ---- writing the analysis file ----------------------------------------------
#
# The grid lives in the ANLZ files, and only `.DAT`'s `PQTZ` tag is written here.
# `.EXT` carries a second, "extended" grid (`PQT2`) whose per-beat byte and `u3`
# field are NOT decoded — `pyrekordbox` itself calls the field `unkown` — so
# rewriting it would mean guessing at bytes in real user data that has no version
# history. Leaving it alone is the only safe option until it is decoded.
#
# `.DAT`/`.EXT` round-trip byte-identically through parse -> build (verified
# across a whole library), which is what makes this checkable at all: a rebuild
# must differ ONLY inside the tag we touched.
def write_pqtz(path, beat_nums, bpms, times) -> bool:
"""Replace the beat grid in an ANLZ `.DAT`, in place. True if written.
`pyrekordbox`'s own `PQTZAnlzTag.set*()` refuse to change the NUMBER of
beats ("only values of existing beats can be set"), and every real grid edit
changes it — so the entry list is rebuilt directly and the tag's own
`update_len()` recomputes `len_tag`. `check_parse()` then asserts the
declared count and the list agree before anything is written.
"""
import construct
from pyrekordbox.anlz import AnlzFile
anlz = AnlzFile.parse_file(str(path))
tag = next((t for t in anlz.tags if t.type == "PQTZ"), None)
if tag is None:
return False
entries = construct.ListContainer()
for beat, bpm, t in zip(beat_nums, bpms, times):
entries.append(
construct.Container(
beat=int(beat),
tempo=int(round(bpm * 100)), # BPM is stored x100
time=int(round(t * 1000)), # seconds -> ms
)
)
tag.struct.content.entries = entries
tag.struct.content.entry_count = len(entries)
tag.update_len()
tag.check_parse()
data = anlz.build()
with open(path, "wb") as fh:
fh.write(data)
return True