"""The "Rekordbox Export" target: a stick's legacy Device Library (`export.pdb`). What Rekordbox lists as a stick's Device Library and pre-OneLibrary players read. There is no public spec, so this pins the writer to bytes REKORDBOX wrote: rows and a whole page taken from a real rekordbox 7 device export (`fixtures/rekordbox/device/reference_rows.json`) must be reproduced exactly. Then the structure a player walks (page chains, allocation, row indexes), and an export end to end — including that alongside the OneLibrary target the stick carries ONE set of analysis files and artwork, as a rekordbox-made stick does. """ import json import os import struct import sys import tempfile from pathlib import Path os.environ.setdefault("KONDUKTOR_DATA_DIR", tempfile.mkdtemp(prefix="konduktor-test-")) sys.path.insert(0, str(Path(__file__).resolve().parent)) import numpy as np # noqa: E402 import soundfile as sf # noqa: E402 from PIL import Image # noqa: E402 import konduktor.adapters # noqa: E402,F401 — registers every target from konduktor.adapters.rekordbox import pdb as P # noqa: E402 from konduktor.core import export as core_export # noqa: E402 from konduktor.core.export import ExportPayload, ExportPlaylist, ExportTrack # noqa: E402 from konduktor.core.model import CuePoint, GridMarker, Track, TrackCues # noqa: E402 failed = False def check(label, cond, detail=""): global failed print(f" [{'PASS' if cond else 'FAIL'}] {label}" + (f" — {detail}" if detail and not cond else "")) if not cond: failed = True REF = json.loads((P.TEMPLATE_DIR / "reference_rows.json").read_text()) print("== rows are rekordbox's bytes ==") fields = REF["track_row_demo_track_2_fields"] ours = P.track_row({k: v for k, v in fields.items() if k not in P.TRACK_STRINGS}, {k: fields[k] for k in P.TRACK_STRINGS}) check("a track row is byte-identical to rekordbox's", ours.hex() == REF["track_row_demo_track_2"]) # rekordbox leaves variable slack after a row on its page (6-9 bytes, sometimes # stale data); readers find rows through the page's index, never by size. So a # row is compared over its own bytes. artist = P.artist_row(1, "Loopmasters").rstrip(b"\0") check("an artist row is byte-identical", REF["artist_row_loopmasters"].startswith(artist.hex())) plist = P.playlist_tree_row(1, 0, 1, "demos", folder=False).rstrip(b"\0") check("a playlist row is byte-identical", REF["playlist_row_demos"].startswith(plist.hex())) print("== a page is rekordbox's bytes ==") page = bytes.fromhex(REF["keys_page"]) n = (page[0x19] >> 5) | (page[0x1A] << 3) used = struct.unpack_from(" 1, len(tpages)) check("the first data page is the table's reserved candidate", tpages[0]["index"] == candidate) check("further pages come from the next-unused counter", all(p["index"] >= start_unused for p in tpages[1:])) check("the chain ends in a fresh reserved candidate beyond every data page", tpages[-1]["next"] >= max(p["index"] for p in tpages)) check("the header page still starts the chain", lib.pages[P.TRACKS][0]["index"] == first) check("every page's free space follows rekordbox's arithmetic", all(p["free"] == P.PAGE - 0x28 - p["used"] - (2 * p["rows"] + 4 * ((p["rows"] + 15) // 16)) for p in tpages)) check("tracks pages carry flags 0x34, others 0x24", all(p["flags"] == 0x34 for p in tpages) and all(p["flags"] == 0x24 for p in lib.pages[P.ARTISTS] if not p["flags"] & 0x40)) fixed_pages = (13, 14, 33, 34, 35, 36, 37, 38) check("the fixed tables (colours, columns, menus) are untouched", all(out[p * P.PAGE:(p + 1) * P.PAGE] == tmpl[p * P.PAGE:(p + 1) * P.PAGE] for p in fixed_pages)) print("== header pages index their data (else rekordbox: 'Device library is corrupted') ==") # A header page holds an index: its own number, the FIRST DATA PAGE, ... a # count and entries `page << 3 | flag`. Left pointing nowhere, rekordbox 7 called # a Konduktor-written library corrupted. Rules read off a real export. w.stamp_track_count(40) out = w.render() def index(buf, table): _t, _e, first, _l = next(struct.unpack_from("<4I", buf, 28 + 16 * i) for i in range(20) if struct.unpack_from("> 3, e & 7) for e in struct.unpack_from(f"<{n}I", buf, o + 0x3C)], struct.unpack_from("<2H", buf, o + 0x20), struct.unpack_from(" House > Peak, plus Other for loose tracks", tree["GIG"]["folder"] and tree["House"]["parent"] == tree["GIG"]["id"] and tree["Peak"]["parent"] == tree["House"]["id"] and tree["Other"]["parent"] == tree["GIG"]["id"]) peak = [e for e in lib.entries if e[2] == tree["Peak"]["id"]] check("playlist entries in order, 1-based", peak == [(1, 1, tree["Peak"]["id"]), (2, 2, tree["Peak"]["id"])], peak) check("every track has an analysis path, and the files exist", all((root / t["analyze_path"].lstrip("/")).is_file() for t in lib.tracks)) check("artwork rows name the a.jpg files, which exist", all((root / p.lstrip("/")).is_file() for p in lib.artwork.values()) and len(lib.artwork) == 3) check("every file written is reported, so a re-export can clear it", all(p.exists() for p in written.all_paths)) print("== alongside OneLibrary: one set of analysis files and artwork ==") both = Path(tempfile.mkdtemp()) for t in tracks: dst = both / t.destination.relative_to(root) dst.parent.mkdir(parents=True, exist_ok=True) dst.write_bytes(t.destination.read_bytes()) import dataclasses # noqa: E402 moved = [dataclasses.replace(t, destination=both / t.destination.relative_to(root)) for t in tracks] both_payload = ExportPayload(name="GIG", tracks=moved, playlists=payload.playlists) ol_written = core_export.for_platform("onelibrary").write(both_payload, both) stamp = {p: p.stat().st_mtime_ns for p in ol_written.extra if p.suffix in (".DAT", ".EXT", ".2EX")} calls = [] both_payload.checkpoint = lambda m, **_progress: calls.append(m) dev_written = dev.write(both_payload, both) dat_files = sorted(both.rglob("*.DAT")) check("still one .DAT per track", len(dat_files) == 3, len(dat_files)) check("the device library reuses the analysis OneLibrary wrote (not rewritten)", all(p.stat().st_mtime_ns == m for p, m in stamp.items())) lib2 = P.read(dev_written.library.read_bytes()) from konduktor.adapters.onelibrary.driver import OneLibraryDriver # noqa: E402 ol = OneLibraryDriver().open(both) check("both libraries point at the same analysis files", sorted(str(r.analysisDataFilePath) for r in ol._store.iter_content()) == sorted(t["analyze_path"] for t in lib2.tracks)) check("the a.jpg files both write are the same files", {p.name for p in (both / "PIONEER/Artwork/00001").iterdir()} >= {Path(v).name for v in lib2.artwork.values()}) check("progress is reported per track", len(calls) == 3, calls) print("== the export dialog lists the targets in a fixed order ==") from fastapi.testclient import TestClient # noqa: E402 from konduktor.main import app # noqa: E402 names = [o["name"] for o in TestClient(app).get("/api/export-targets").json()] check("OneLibrary, Traktor, Rekordbox Export, Rekordbox Library", names[:4] == ["OneLibrary", "Traktor", "Rekordbox Export", "Rekordbox Library"], names) flags = {o["name"]: o for o in TestClient(app).get("/api/export-targets").json()} check("Rekordbox Library says it is computer-only (the dialog warns beside a stick format)", flags["Rekordbox Library"]["computer_only"] and not flags["Rekordbox Export"]["computer_only"] and flags["Rekordbox Export"]["drive_root"]) print("\n" + ("❌ FAILED" if failed else "✅ PASSED")) raise SystemExit(1 if failed else 0)