diff --git a/CLAUDE.md b/CLAUDE.md index 58d7b78..8e9e724 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -336,6 +336,10 @@ Two independent apps that talk over HTTP: big collection unchanged, though every one of those is NI's default layout. Without it the entry read as plain audio (Type column, playlist `TYPE="TRACK"`) while the deck, which reads the file, played stems. + Rekordbox and OneLibrary have no such element, so their projections take + `media_kind` from the FILE (`stem_file.is_stem_file`, only `.m4a`/`.mp4` + opened, ~0.7 ms each, cached per path in `RekordboxStore.is_stem`) — the + same mismatch otherwise: Type said audio while the deck played stems. - `importer.py` also serves the folder add: `reference=True` adds each file where it is (nothing to copy or roll back), and every planned file the destination already points at (matched on `audio_path`, never the display @@ -854,7 +858,9 @@ serialization path.** It enforces: out-of-range / non-stem requests 404; the cache reuses, follows an edited file, evicts LRU but never what it just wrote; the folder origin serves too and lists the file as a stem track; a stem file ADDED from a folder gets - `` and a `TYPE="STEM"` playlist key, the MP3 beside it neither. + `` and a `TYPE="STEM"` playlist key, the MP3 beside it neither. On a + Rekordbox copy, an added stem file projects as a stem track (also after a + reopen) and the deck is offered its stems. - `test_traktor_adapter.py` — the **generic layer**: one parse per open, the projection refreshing after every command family, cue-type translation, capabilities, and `set_analysed_grid` vs `replace_grid`. `test_save_fidelity` diff --git a/backend/konduktor/adapters/onelibrary/adapter.py b/backend/konduktor/adapters/onelibrary/adapter.py index 6f7aa5d..fb6e202 100644 --- a/backend/konduktor/adapters/onelibrary/adapter.py +++ b/backend/konduktor/adapters/onelibrary/adapter.py @@ -50,12 +50,14 @@ class OneLibraryAdapter: # ---- projection maintenance ----------------------------------------- def _rebuild(self) -> None: - self._index.rebuild( - [ - projection.to_track(row, self._drive_path(row)) - for row in self._store.iter_content() - ] - ) + from ...core.stem_file import is_stem_file + + tracks = [] + for row in self._store.iter_content(): + path = self._drive_path(row) + tracks.append(projection.to_track( + row, path, stem=bool(path) and is_stem_file(path))) + self._index.rebuild(tracks) def _drive_path(self, row) -> str | None: resolved = self._store.layout.resolve(getattr(row, "path", None)) diff --git a/backend/konduktor/adapters/onelibrary/capabilities.py b/backend/konduktor/adapters/onelibrary/capabilities.py index 5b79ee9..06ca604 100644 --- a/backend/konduktor/adapters/onelibrary/capabilities.py +++ b/backend/konduktor/adapters/onelibrary/capabilities.py @@ -69,7 +69,7 @@ def capabilities_for(device_name: str | None = None, version: str | None = None) tracks=TrackCapabilities( rating_max=5, # stored 0-5 directly, as in master.db editable_fields=[], - media_kinds=["audio"], + media_kinds=["audio", "stem"], # `artwork` gates EDITING, and a drive is read-only here. Reading # works: `store.cover_art` follows `content.image_id` -> `image.path`. artwork=False, diff --git a/backend/konduktor/adapters/onelibrary/projection.py b/backend/konduktor/adapters/onelibrary/projection.py index 3de141d..e1b9919 100644 --- a/backend/konduktor/adapters/onelibrary/projection.py +++ b/backend/konduktor/adapters/onelibrary/projection.py @@ -54,7 +54,7 @@ def _bpm(row) -> float | None: return round(int(raw) / 100.0, 2) -def to_track(row, drive_path: str | None = None) -> Track: +def to_track(row, drive_path: str | None = None, *, stem: bool = False) -> Track: """Project one ``Content`` row. **The three count fields are approximations here**, and deliberately so. Cues @@ -104,7 +104,8 @@ def to_track(row, drive_path: str | None = None) -> Track: hotcue_count=0, grid_marker_count=1 if (bpm and has_analysis) else 0, grid_locked=False, # OneLibrary has no grid lock - media_kind="audio", + # What the FILE is, as the deck plays it — see the Rekordbox projection. + media_kind="stem" if stem else "audio", ) diff --git a/backend/konduktor/adapters/rekordbox/adapter.py b/backend/konduktor/adapters/rekordbox/adapter.py index fa39f2b..f9abf01 100644 --- a/backend/konduktor/adapters/rekordbox/adapter.py +++ b/backend/konduktor/adapters/rekordbox/adapter.py @@ -46,7 +46,8 @@ class RekordboxAdapter: kinds = self._store.cue_kinds() self._index.rebuild( [ - projection.to_track(row, kinds.get(str(row.ID), ())) + projection.to_track(row, kinds.get(str(row.ID), ()), + stem=self._store.is_stem(str(row.ID))) for row in self._store.iter_content() ] ) @@ -68,7 +69,8 @@ class RekordboxAdapter: """ kinds = self._store.cue_kinds() row = self._store.content(track_id) - self._index.replace(projection.to_track(row, kinds.get(str(row.ID), ()))) + self._index.replace(projection.to_track(row, kinds.get(str(row.ID), ()), + stem=self._store.is_stem(str(row.ID)))) def close(self) -> None: """Release the database connection. @@ -202,6 +204,8 @@ class RekordboxAdapter: def set_path_mapping(self, mapping: PathMapping) -> None: self._store.set_path_mapping(mapping) + # The FILE decides a track's media kind, and the mapping moves files. + self._rebuild() def unresolved_path_groups(self) -> list: """Not checked yet: the open-time missing-files check is Traktor-only @@ -465,7 +469,8 @@ class RekordboxAdapter: for item, audio, measured, markers, art in prepared: track_id = self._store.add_track( audio, item.track, measured=measured, with_grid=bool(markers), art=art) - self._index.add(projection.to_track(self._store.content(track_id), ())) + self._index.add(projection.to_track(self._store.content(track_id), (), + stem=self._store.is_stem(track_id))) if markers: self._store.replace_grid(track_id, markers) if item.cues is not None: diff --git a/backend/konduktor/adapters/rekordbox/capabilities.py b/backend/konduktor/adapters/rekordbox/capabilities.py index 36bbb5c..affda98 100644 --- a/backend/konduktor/adapters/rekordbox/capabilities.py +++ b/backend/konduktor/adapters/rekordbox/capabilities.py @@ -86,7 +86,9 @@ def capabilities_for( # column (its Composer is a different field), so they are absent # rather than mapped onto something approximate. editable_fields=sorted(editable_fields or []), - media_kinds=["audio"], + # A stem file is held (and played by Rekordbox as its mix); + # Konduktor's deck plays its stems. + media_kinds=["audio", "stem"], removable=True, addable=True, artwork=False, diff --git a/backend/konduktor/adapters/rekordbox/projection.py b/backend/konduktor/adapters/rekordbox/projection.py index 227875e..f53db7a 100644 --- a/backend/konduktor/adapters/rekordbox/projection.py +++ b/backend/konduktor/adapters/rekordbox/projection.py @@ -97,7 +97,7 @@ def _name_of(related) -> str | None: return None -def to_track(row, cue_kinds=()) -> Track: +def to_track(row, cue_kinds=(), *, stem: bool = False) -> Track: """Project one ``DjmdContent`` row. `cue_kinds` is this track's ``(Kind, OutMsec, ColorTableIndex)`` from the store's single @@ -156,7 +156,10 @@ def to_track(row, cue_kinds=()) -> Track: hotcues=chips, grid_marker_count=1 if (_bpm(row) and getattr(row, "AnalysisDataPath", None)) else 0, grid_locked=False, # Rekordbox has no per-track grid lock - media_kind="audio", + # What the FILE is (`store.is_stem`): Rekordbox itself plays a stem + # file's mix, but Konduktor's deck plays its stems, and the Type column + # must not disagree with the deck. + media_kind="stem" if stem else "audio", ) diff --git a/backend/konduktor/adapters/rekordbox/store.py b/backend/konduktor/adapters/rekordbox/store.py index e882b45..6dae850 100644 --- a/backend/konduktor/adapters/rekordbox/store.py +++ b/backend/konduktor/adapters/rekordbox/store.py @@ -140,6 +140,7 @@ class RekordboxStore: except Exception as ex: # noqa: BLE001 — surfaced as a clean 4xx raise LibraryNotSupported(f"Could not open Rekordbox library: {ex}") from ex self._grid_cache.clear() + self._stem_cache: dict[str, bool] = {} self._content_cache: list | None = None self._by_id: dict[str, object] | None = None @@ -373,6 +374,20 @@ class RekordboxStore: def set_path_mapping(self, mapping: PathMapping) -> None: self._mapping = mapping or PathMapping() + def is_stem(self, track_id: str) -> bool: + """Whether the track's FILE is a native-instruments stem file — read + from its contents (only `.m4a`/`.mp4` are opened, ~0.7 ms each), and + cached per path, since the projection asks for every track at open.""" + from ...core.stem_file import is_stem_file + + path = self.audio_path(track_id) + if path is None: + return False + key = str(path) + if key not in self._stem_cache: + self._stem_cache[key] = is_stem_file(path) + return self._stem_cache[key] + @property def path_mapping(self) -> PathMapping: return self._mapping diff --git a/backend/test_stem_playback.py b/backend/test_stem_playback.py index 8f31bd1..3fdf825 100644 --- a/backend/test_stem_playback.py +++ b/backend/test_stem_playback.py @@ -187,5 +187,55 @@ with TestClient(main.app) as c: re.search(r': the FILE decides, so the Type column agrees + # with the deck, which plays a stem file's stems on every platform. + print("== adding a stem file to a Rekordbox library ==") + from konduktor.adapters.rekordbox import discovery as rb_discovery + + found = rb_discovery.detect_libraries() + if not found: + print(" (skipped: no Rekordbox library on this machine)") + else: + rb_src = Path(found[0]["path"]).parent + rb_dir = ROOT / "rekordbox" + rb_dir.mkdir() + for name in ("master.db", "masterPlaylists6.xml"): + if (rb_src / name).exists(): + shutil.copy2(rb_src / name, rb_dir / name) + if (rb_src / "share").is_dir(): + shutil.copytree(rb_src / "share", rb_dir / "share") + rb_stem_dir = ROOT / "ForRekordbox" + rb_stem_dir.mkdir() + rb_stem = rb_stem_dir / "Rb.stem.m4a" + shutil.copy2(stem, rb_stem) + rb_plain = make_mp3(rb_stem_dir / "Rb.mp3", seed=11) + r = c.post("/api/library/open", json={"path": str(rb_dir / "master.db")}) + check("(setup) a Rekordbox copy opens", r.status_code == 200, r.text[:300]) + assert c.get("/api/folder/tracks", params={"path": str(rb_stem_dir)}).status_code == 200 + job = c.post("/api/folder/add", json={"track_ids": [str(rb_stem), str(rb_plain)], + "mode": "reference"}).json() + while job["state"] == "running": + time.sleep(0.05) + job = c.get(f"/api/jobs/{job['id']}").json() + check("(setup) the add finishes", job["state"] == "done", str(job)) + rb_new_stem, rb_new_plain = (job.get("result") or {}).get("track_ids") or [None, None] + + def rb_kinds(): + items = c.get("/api/tracks", params={"limit": 20000}).json()["items"] + return {t["id"]: t["media_kind"] for t in items} + + kinds = rb_kinds() + check("the added stem file is a stem track, the MP3 beside it audio", + kinds.get(rb_new_stem) == "stem" and kinds.get(rb_new_plain) == "audio", + f"{kinds.get(rb_new_stem)} {kinds.get(rb_new_plain)}") + r = c.post("/api/library/open", json={"path": str(rb_dir / "master.db")}) + kinds = rb_kinds() + check("…and still after a reopen", + kinds.get(rb_new_stem) == "stem" and kinds.get(rb_new_plain) == "audio", + f"{kinds.get(rb_new_stem)} {kinds.get(rb_new_plain)}") + r = c.get("/api/tracks/stems", params={"track_id": rb_new_stem}) + check("and the deck is offered its stems", + r.status_code == 200 and len(r.json()["stems"]) == 4, r.text[:200]) + print("\nRESULT:", "FAILED" if failed else "ALL PASSED") sys.exit(1 if failed else 0)