Fixed Rekordbox export preview waveform not matching playhead position

This commit is contained in:
patben8 committed 2026-09-28 21:15:11 +02:00
1 parent bb9f6e2bd3
commit 86b26e3229
4 files changed
+50 -5

No files matched your search

@@ -52,6 +52,8 @@ from pathlib import Path
from construct import Container, ListContainer
from pyrekordbox.anlz import structs
from ...core.waveform import even_slices
log = logging.getLogger(__name__)
NO_LOOP = 0xFFFFFFFF
@@ -237,7 +239,7 @@ def _pwv4(bands) -> bytes:
full = np.sqrt((bands ** 2).sum(axis=1))
x = np.column_stack([bands, full]) # low, mid, high, full
cols = [idx for idx in np.array_split(np.arange(len(x)), _OVERVIEW_COLUMNS)]
cols = [np.arange(len(x))[s] for s in even_slices(len(x), _OVERVIEW_COLUMNS)]
mean = np.array([x[i].mean(axis=0) if len(i) else np.zeros(4) for i in cols])
instant = np.array([x[i[len(i) // 2]] if len(i) else np.zeros(4) for i in cols])
@@ -285,8 +287,8 @@ def waveform_tags(bands) -> tuple[list[bytes], list[bytes]]:
for r, g, b, h in zip(*rgb, height))
pwv7 = np.stack([_norm(bands[:, i], top, g, 127) for i, (top, g) in enumerate(_PWV7)], axis=1)
cols = np.array([bands[idx].mean(axis=0) if len(idx) else np.zeros(3)
for idx in np.array_split(np.arange(n), _OVERVIEW_COLUMNS)])
cols = np.array([bands[s].mean(axis=0) if n else np.zeros(3)
for s in even_slices(n, _OVERVIEW_COLUMNS)])
pwv6 = np.stack([_norm(cols[:, i], top, g, 127) for i, (top, g) in enumerate(_PWV6)], axis=1)
gains = [max(_PWVC_FLOOR, int(round(c / (float(np.percentile(bands[:, i], 99.5)) or 1.0))))
for i, c in enumerate(_PWVC_C)]
+16 -2
View File
@@ -106,6 +106,20 @@ def _decode_pyav(path: Path) -> np.ndarray:
return np.concatenate(chunks) if chunks else np.zeros(0, dtype=np.float32)
def even_slices(n: int, k: int) -> list[slice]:
"""`k` slices of `range(n)`, each starting where its share of the whole
begins — so column `i` holds exactly the audio at `i/k` of the track.
NOT `np.array_split`: that gives the FIRST `n % k` chunks one extra element,
which pushes every early column's content later than its position — up to
~1.8 s by column 420 of a 1200-column overview (24,420 frames), shrinking
back to 0 at the end. Rekordbox drew such an overview a bar ahead of the
playhead mid-track, and closer to right near the end.
"""
edges = np.round(np.linspace(0, n, k + 1)).astype(int)
return [slice(int(a), int(max(b, a + 1)) if n else 0) for a, b in zip(edges[:-1], edges[1:])]
def _decode(path: Path) -> np.ndarray | None:
try:
return _decode_pyav(path)
@@ -124,12 +138,12 @@ def _decode(path: Path) -> np.ndarray | None:
def _columns(y: np.ndarray, n: int) -> Columns:
import librosa
rms = np.array([float(np.sqrt(np.mean(c * c))) for c in np.array_split(y, n)])
rms = np.array([float(np.sqrt(np.mean(y[s] * y[s]))) for s in even_slices(len(y), n)])
spec = np.abs(librosa.stft(y, n_fft=N_FFT, hop_length=N_FFT)) ** 2
freqs = librosa.fft_frequencies(sr=SR, n_fft=N_FFT)
share = spec[freqs > _BRIGHT_HZ].sum(0) / (spec.sum(0) + 1e-12)
brightness = np.array([float(c.mean()) if len(c) else 0.0
for c in np.array_split(share, n)])
for c in (share[s] for s in even_slices(len(share), n))])
return Columns(rms=rms, brightness=brightness, duration=len(y) / SR)
+23
View File
@@ -590,5 +590,28 @@ check("and reads the memory cue back, at its place",
len(memory_back) == 1 and abs(memory_back[0].start - 4.0) < 0.002, memory_back)
rb_back.close()
print("\n== overview columns sit where their audio is (no drift) ==")
# np.array_split gives the FIRST n % k chunks an extra frame, so every early
# overview column held later audio than its position: ~1 bar early mid-track in
# rekordbox, right again near the end. A burst at a known time, with a frame
# count whose remainder is large (24,420 % 1200 = 420), must land in its column.
from konduktor.core.waveform import even_slices # noqa: E402
n_frames = 24420
check("even_slices covers every frame once, in order",
[x.start for x in even_slices(n_frames, 1200)][1:] == [x.stop for x in even_slices(n_frames, 1200)][:-1]
and even_slices(n_frames, 1200)[-1].stop == n_frames)
for when in (0.35, 0.5, 0.9):
burst = np.full((n_frames, 3), 1.0)
at = int(when * n_frames)
burst[at:at + 40] = 100.0
ext_t, two_t = W.waveform_tags(burst)
tags_t = {x[:4]: x for x in ext_t + two_t}
col6 = int(np.argmax(np.frombuffer(tags_t[b"PWV6"][20:], np.uint8).reshape(-1, 3)[:, 0]))
col4 = int(np.argmax(np.frombuffer(tags_t[b"PWV4"][24:], np.uint8).reshape(-1, 6)[:, 3]))
want = at * 1200 / n_frames
check(f"a burst {int(when * 100)}% in lands in its overview column (PWV6 {col6}, PWV4 {col4}, want ~{want:.0f})",
abs(col6 - want) <= 2 and abs(col4 - want) <= 2)
print("\n" + ("❌ FAILED" if failed else "✅ PASSED"))
raise SystemExit(1 if failed else 0)