diff --git a/CLAUDE.md b/CLAUDE.md index ff517e2..9c96ff4 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1037,6 +1037,12 @@ that number and nothing else — everything derives from it: generated from one decode (`core/waveform.py`: 150 frames/s, bands split at 200 Hz / 2.5 kHz) with constants fitted to rekordbox's own files for 12 local tracks and checked on 5 held-out ones (per-band correlation 0.77-0.96). + **Every column split goes through `waveform.even_slices`, never + `np.array_split`**: the latter gives the first `n % k` chunks an extra frame, + so overview column k held LATER audio than its position — up to ~1.8 s by + column 420 of 1200, back to 0 at the end. Rekordbox drew such an overview a + bar ahead of the playhead mid-track (`test_export_pioneer.py` pins a burst + landing in its own column). **rekordbox 7's SONG LIST draws `PWV6` — and only when `content.contentLink` is set.** Both measured by bisecting a rekordbox-written stick: removing a track's `PWV6` emptied its row (removing `PWV4` changed nothing), and blanking diff --git a/backend/konduktor/adapters/rekordbox/anlz_writer.py b/backend/konduktor/adapters/rekordbox/anlz_writer.py index 06d07d3..a0661e9 100644 --- a/backend/konduktor/adapters/rekordbox/anlz_writer.py +++ b/backend/konduktor/adapters/rekordbox/anlz_writer.py @@ -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)] diff --git a/backend/konduktor/core/waveform.py b/backend/konduktor/core/waveform.py index b2e3930..59a430f 100644 --- a/backend/konduktor/core/waveform.py +++ b/backend/konduktor/core/waveform.py @@ -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) diff --git a/backend/test_export_pioneer.py b/backend/test_export_pioneer.py index b8f1892..36a93d0 100644 --- a/backend/test_export_pioneer.py +++ b/backend/test_export_pioneer.py @@ -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)