mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented export track grid locking
This commit is contained in:
1 parent
2821ba124d
commit
9329236955
16 files changed
+196
-19
No files matched your search
@@ -109,6 +109,8 @@ class RekordboxExporter:
|
||||
computer_only = True
|
||||
library_filename = "master.db"
|
||||
drive_root = False
|
||||
#: `djmdContent.Analysed` bit 0x80 — from `TrackCues.grid_locked`.
|
||||
locks_grids = True
|
||||
|
||||
def capabilities(self):
|
||||
"""What a Rekordbox library can hold, with no library to read.
|
||||
@@ -332,7 +334,9 @@ class RekordboxExporter:
|
||||
"""
|
||||
rel = new_content.anlz_rel(str(content.UUID))
|
||||
content.AnalysisDataPath = rel
|
||||
content.Analysed = 105 # what a rekordbox-analysed track carries
|
||||
content.Analysed = new_content.ANALYSED
|
||||
if item.cues is not None and item.cues.grid_locked:
|
||||
content.Analysed |= new_content.ANALYSED_LOCKED
|
||||
content.AnalysisUpdated = 1
|
||||
db.flush()
|
||||
|
||||
|
||||
@@ -34,6 +34,12 @@ from . import artwork
|
||||
#: only what is written: 0x2C060E.
|
||||
CONTENT_LINK = 2885134
|
||||
|
||||
#: `djmdContent.Analysed` as a rekordbox-analysed track carries it, and the bit
|
||||
#: rekordbox 7 sets beside it when the grid is LOCKED (the padlock in Grid Edit):
|
||||
#: measured 2026-10-19 by locking two tracks — 105 -> 233, nothing else changed.
|
||||
ANALYSED = 105
|
||||
ANALYSED_LOCKED = 0x80
|
||||
|
||||
|
||||
def anlz_rel(track_uuid: str) -> str:
|
||||
"""`djmdContent.AnalysisDataPath` for a track. Rekordbox derives it from
|
||||
|
||||
@@ -788,7 +788,7 @@ class RekordboxStore(ReplaceGridCommands):
|
||||
track_uuid = str(content.UUID)
|
||||
rel = new_content.anlz_rel(track_uuid)
|
||||
content.AnalysisDataPath = rel
|
||||
content.Analysed = 105 # what a rekordbox-analysed track carries
|
||||
content.Analysed = new_content.ANALYSED
|
||||
content.AnalysisUpdated = 1
|
||||
files = new_content.anlz_files(audio, measured, [] if with_grid else None)
|
||||
for suffix, data in files.items():
|
||||
|
||||
@@ -242,6 +242,10 @@ class TraktorAdapter:
|
||||
# invent one outside Auto Grid), and it occupies pad A — which
|
||||
# silently displaced the imported track's own pad A cue.
|
||||
self.replace_grid(track_id, markers)
|
||||
if cues.grid_locked:
|
||||
# The source's lock, or an export's "Lock beatgrids". Unlocked, an
|
||||
# added entry (no AUDIO_ID) is re-analysed on its first load.
|
||||
self._store.set_lock(track_id, True)
|
||||
|
||||
slots = self.capabilities().cues.hotcue_slots
|
||||
taken = {c.slot for c in self.track_cues(track_id).cues if c.slot is not None}
|
||||
|
||||
@@ -66,6 +66,8 @@ class TraktorExporter:
|
||||
menu_order = 20
|
||||
library_filename = "collection.nml"
|
||||
drive_root = False
|
||||
#: `LOCK` — honoured from `TrackCues.grid_locked` by `add_tracks`.
|
||||
locks_grids = True
|
||||
|
||||
def capabilities(self):
|
||||
"""What a Traktor library can hold — answered with no library to read.
|
||||
|
||||
@@ -22,6 +22,7 @@ import copy
|
||||
import math
|
||||
import threading
|
||||
import uuid as uuidlib
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from traktor_nml_utils import (
|
||||
@@ -1210,9 +1211,19 @@ class TraktorStore:
|
||||
self.dirty = True
|
||||
|
||||
def set_lock(self, track_id: str, locked: bool) -> None:
|
||||
"""Lock or unlock a track's grid.
|
||||
|
||||
Locking writes what Traktor 4.5 writes (measured 2026-10-19):
|
||||
``LOCK="1"`` plus ``LOCK_MODIFICATION_TIME``, an ISO time in UTC to the
|
||||
second. What Traktor leaves on UNLOCK is unmeasured, so unlocking only
|
||||
removes ``LOCK``, as it always has.
|
||||
"""
|
||||
with self._lock:
|
||||
entry = self._entry_or_raise(track_id)
|
||||
entry.lock = 1 if locked else None
|
||||
if locked:
|
||||
entry.lock_modification_time = (
|
||||
datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S"))
|
||||
self._note("lock", "on" if locked else "off", track_id)
|
||||
self.dirty = True
|
||||
|
||||
|
||||
@@ -234,6 +234,9 @@ class PlatformOption(BaseModel):
|
||||
#: As an export TARGET: its library is only ever read on a computer, never
|
||||
#: from a drive — so it does nothing beside a stick format.
|
||||
computer_only: bool = False
|
||||
#: As an export TARGET: its library can store a grid lock, so the export's
|
||||
#: "Lock beatgrids" option means something there.
|
||||
locks_grids: bool = False
|
||||
|
||||
|
||||
class RemapSample(BaseModel):
|
||||
|
||||
@@ -433,12 +433,30 @@ def _folder_paths(adapter) -> dict[str, list[str]]:
|
||||
# ---- running -----------------------------------------------------------------
|
||||
|
||||
|
||||
def _locked(cues, lock_grids: bool):
|
||||
"""The cues as exported: with `lock_grids`, a grid is LOCKED.
|
||||
|
||||
The lock rides on the generic `TrackCues.grid_locked`, the same flag a
|
||||
source's own lock arrives in, so a writer reads one signal and never learns
|
||||
which of the two set it. Unlocked Traktor re-analyses an exported track on
|
||||
its first load (it has no AUDIO_ID, which cannot be computed), re-measuring
|
||||
the BPM — a 174 track can come back as 87 — and drifting the grid; a
|
||||
locked grid is left alone. A track with no grid is never locked: that
|
||||
would stop Traktor ever giving it one.
|
||||
"""
|
||||
if lock_grids and cues is not None and cues.grid_markers and not cues.grid_locked:
|
||||
return cues.model_copy(update={"grid_locked": True})
|
||||
return cues
|
||||
|
||||
|
||||
def run(adapter, export_set, built: ExportPlan, handle: JobHandle, *,
|
||||
root_folder: bool = False) -> dict:
|
||||
root_folder: bool = False, lock_grids: bool = False) -> dict:
|
||||
"""Copy what changed, then write every target's library over the audio.
|
||||
|
||||
`root_folder` nests the playlists in a folder named after the export (the
|
||||
"Put exported playlists in a folder" setting); otherwise they sit at the top.
|
||||
`lock_grids` locks every exported track that HAS a grid (`_locked`); off,
|
||||
each track keeps the source's own lock.
|
||||
|
||||
Rolls back EVERYTHING this run wrote on cancel or on any failure —
|
||||
including the libraries of targets that had already been written when a
|
||||
@@ -530,7 +548,7 @@ def run(adapter, export_set, built: ExportPlan, handle: JobHandle, *,
|
||||
ExportTrack(
|
||||
track=adapter.track(planned.track_id),
|
||||
destination=target,
|
||||
cues=adapter.track_cues(planned.track_id),
|
||||
cues=_locked(adapter.track_cues(planned.track_id), lock_grids),
|
||||
art=_cover(adapter, planned.track_id),
|
||||
fingerprint=_fingerprint(planned),
|
||||
analysis_cache=cache,
|
||||
|
||||
@@ -62,6 +62,10 @@ class ExportSet:
|
||||
destination: str = "" # OS path of the folder the export is written into
|
||||
playlist_ids: list[str] = field(default_factory=list)
|
||||
track_ids: list[str] = field(default_factory=list) # loose tracks only
|
||||
#: Lock every exported grid, so Traktor does not re-analyse a track on its
|
||||
#: first load (it re-measures the BPM — 174 can come back as 87 — and the
|
||||
#: grid drifts). Remembered per set: it is a decision about this stick.
|
||||
lock_grids: bool = True
|
||||
created: float = 0.0
|
||||
modified: float = 0.0
|
||||
|
||||
@@ -170,6 +174,7 @@ def _load(library_id: str) -> dict[str, ExportSet]:
|
||||
destination=values.get("destination") or "",
|
||||
playlist_ids=list(values.get("playlist_ids") or []),
|
||||
track_ids=list(values.get("track_ids") or []),
|
||||
lock_grids=bool(values.get("lock_grids", True)),
|
||||
created=float(values.get("created") or 0.0),
|
||||
modified=float(values.get("modified") or 0.0),
|
||||
)
|
||||
@@ -251,7 +256,8 @@ def create(library_id: str, *, name: str, targets: list[str], destination: str)
|
||||
|
||||
|
||||
def update(library_id: str, set_id: str, **fields) -> ExportSet | None:
|
||||
"""Rename, retarget or relocate a set. Unknown fields are ignored."""
|
||||
"""Rename, retarget or relocate a set, or change its options. Unknown fields
|
||||
are ignored."""
|
||||
sets = _load(library_id)
|
||||
found = sets.get(set_id)
|
||||
if found is None:
|
||||
@@ -261,6 +267,8 @@ def update(library_id: str, set_id: str, **fields) -> ExportSet | None:
|
||||
setattr(found, key, fields[key])
|
||||
if fields.get("targets"):
|
||||
found.targets = list(dict.fromkeys(fields["targets"]))
|
||||
if fields.get("lock_grids") is not None:
|
||||
found.lock_grids = bool(fields["lock_grids"])
|
||||
found.modified = time.time()
|
||||
_save(library_id, sets)
|
||||
return found
|
||||
|
||||
@@ -78,6 +78,7 @@ from .schemas import (
|
||||
FsListing,
|
||||
FsPlace,
|
||||
UpdateExportSet,
|
||||
RunExport,
|
||||
AddGridMarker,
|
||||
GridMarkerEdit,
|
||||
ReplaceGrid,
|
||||
@@ -655,6 +656,7 @@ def update_export(set_id: str, body: UpdateExportSet) -> ExportSetOut:
|
||||
name=body.name,
|
||||
targets=body.targets,
|
||||
destination=body.destination,
|
||||
lock_grids=body.lock_grids,
|
||||
)
|
||||
return ExportSetOut(**updated.as_dict())
|
||||
|
||||
@@ -764,14 +766,19 @@ def preview_export(set_id: str) -> ExportPreview:
|
||||
|
||||
|
||||
@app.post("/api/exports/{set_id}/run", response_model=JobStatus)
|
||||
def run_export(set_id: str) -> JobStatus:
|
||||
def run_export(set_id: str, body: RunExport | None = None) -> JobStatus:
|
||||
"""Start the export on a background thread; poll `/api/jobs/{id}`.
|
||||
|
||||
Planned again HERE rather than trusting whatever the preview showed: a
|
||||
playlist can be edited, a file deleted or a drive unplugged between looking
|
||||
and clicking, and the plan carries the destination-safety check.
|
||||
|
||||
The dialog's options arrive with the click and are remembered on the set,
|
||||
so the next export of the same stick starts from the same answer.
|
||||
"""
|
||||
found = _require_set(set_id)
|
||||
if body is not None and body.lock_grids is not None and body.lock_grids != found.lock_grids:
|
||||
found = exports.update(_require_library_id(), set_id, lock_grids=body.lock_grids)
|
||||
adapter = require_adapter()
|
||||
built = exporter.plan(adapter, found)
|
||||
if built.blocked:
|
||||
@@ -779,7 +786,7 @@ def run_export(set_id: str) -> JobStatus:
|
||||
# A Konduktor setting, read when the export starts.
|
||||
root_folder = bool(prefs.load_prefs().get("exportRootFolder", False))
|
||||
job = JOBS.submit("export", lambda handle: exporter.run(
|
||||
adapter, found, built, handle, root_folder=root_folder))
|
||||
adapter, found, built, handle, root_folder=root_folder, lock_grids=found.lock_grids))
|
||||
return JobStatus(**job.as_dict())
|
||||
|
||||
|
||||
@@ -829,6 +836,7 @@ def export_targets() -> list[PlatformOption]:
|
||||
removable=bool(getattr(d, "removable", False)),
|
||||
drive_root=bool(getattr(exporter, "drive_root", False)),
|
||||
computer_only=bool(getattr(exporter, "computer_only", False)),
|
||||
locks_grids=bool(getattr(exporter, "locks_grids", False)),
|
||||
))
|
||||
# Targets with no reader of their own (the stick's legacy Device Library):
|
||||
# listed too, or registering one would not make it selectable.
|
||||
@@ -847,6 +855,7 @@ def export_targets() -> list[PlatformOption]:
|
||||
removable=bool(getattr(exporter, "drive_root", False)),
|
||||
drive_root=bool(getattr(exporter, "drive_root", False)),
|
||||
computer_only=bool(getattr(exporter, "computer_only", False)),
|
||||
locks_grids=bool(getattr(exporter, "locks_grids", False)),
|
||||
))
|
||||
# Each exporter states its own place; a platform with no exporter (not yet a
|
||||
# target, shown disabled) goes last. Stable, so equal places keep their order.
|
||||
|
||||
@@ -347,6 +347,7 @@ class ExportSetOut(BaseModel):
|
||||
destination: str
|
||||
playlist_ids: list[str] = []
|
||||
track_ids: list[str] = [] # loose tracks only
|
||||
lock_grids: bool = True
|
||||
created: float = 0.0
|
||||
modified: float = 0.0
|
||||
|
||||
@@ -361,6 +362,13 @@ class UpdateExportSet(BaseModel):
|
||||
name: str | None = None
|
||||
targets: list[str] | None = None
|
||||
destination: str | None = None
|
||||
lock_grids: bool | None = None
|
||||
|
||||
|
||||
class RunExport(BaseModel):
|
||||
#: The run dialog's "Lock beatgrids" — remembered on the set. None keeps the
|
||||
#: set's own answer.
|
||||
lock_grids: bool | None = None
|
||||
|
||||
|
||||
class ExportSetMembers(BaseModel):
|
||||
|
||||
+44
-4
@@ -174,10 +174,14 @@ check("with its tempo changes",
|
||||
check("and its marker positions",
|
||||
[round(m.start, 3) for m in out_grid.grid_markers]
|
||||
== [round(m.start, 3) for m in src_grid.grid_markers])
|
||||
# Ben's decision: exported grids are NOT locked, so they stay editable like any
|
||||
# other track. If Traktor turns out to re-analyse and flatten them, this is what
|
||||
# makes that visible rather than hidden behind a lock.
|
||||
check("grids are not locked", not by_title[flexible.title].grid_locked)
|
||||
# A grid crosses with the lock it had: unlocked at the source, unlocked here.
|
||||
# Locking every grid is the run's "Lock beatgrids" option (below), because
|
||||
# Traktor DID turn out to re-analyse an unlocked exported track on first load.
|
||||
check("each track keeps its source's lock",
|
||||
[by_title[t.title].grid_locked for t in chosen] == [t.grid_locked for t in chosen],
|
||||
str([(t.title, t.grid_locked, by_title[t.title].grid_locked) for t in chosen]))
|
||||
check("and only a locked one has LOCK written",
|
||||
text.count(' LOCK="1"') == sum(1 for t in chosen if t.grid_locked))
|
||||
|
||||
src_cues = src.track_cues(cued.id)
|
||||
out_cues = back.track_cues(by_title[cued.title].id)
|
||||
@@ -278,6 +282,42 @@ check("the audio is really there",
|
||||
check("the result re-opens as a library",
|
||||
len(TraktorDriver().open(dest / "collection.nml").tracks) == len(built.exportable))
|
||||
|
||||
print("== Lock beatgrids ==")
|
||||
check("a new export set locks by default", exports.get(LIB, eset.id).lock_grids is True)
|
||||
check("and remembers being told not to",
|
||||
exports.update(LIB, eset.id, lock_grids=False).lock_grids is False
|
||||
and exports.get(LIB, eset.id).lock_grids is False)
|
||||
exports.update(LIB, eset.id, lock_grids=True)
|
||||
gridded = src.track_cues(flexible.id).model_copy(update={"grid_locked": False})
|
||||
bare = gridded.model_copy(update={"grid_markers": []})
|
||||
check("on: a grid is locked", exporter._locked(gridded, True).grid_locked)
|
||||
check("on: a track with no grid is not (Traktor would never give it one)",
|
||||
not exporter._locked(bare, True).grid_locked)
|
||||
check("off: the source's own lock is kept",
|
||||
exporter._locked(gridded.model_copy(update={"grid_locked": True}), False).grid_locked
|
||||
and not exporter._locked(gridded, False).grid_locked)
|
||||
lock_dest = Path(tempfile.mkdtemp()) / "LOCKED"
|
||||
lset = exports.create(LIB, name="LOCKED", targets=["traktor"], destination=str(lock_dest))
|
||||
exports.add(LIB, lset.id, playlist_ids=[source_pl.id], track_ids=[ad.tracks[3].id])
|
||||
lset = exports.get(LIB, lset.id)
|
||||
ljob = JOBS.submit("export", lambda h: exporter.run(
|
||||
ad, lset, exporter.plan(ad, lset), h, lock_grids=True))
|
||||
while not ljob.finished:
|
||||
time.sleep(0.02)
|
||||
check("a locked export finishes", ljob.state == "done", f"{ljob.state}: {ljob.error}")
|
||||
ltext = (lock_dest / "collection.nml").read_text(encoding="utf-8")
|
||||
lback = TraktorDriver().open(lock_dest / "collection.nml")
|
||||
with_grid = [t for t in lback.tracks if t.grid_marker_count]
|
||||
check("every exported track with a grid is locked",
|
||||
with_grid and all(t.grid_locked for t in with_grid),
|
||||
str([(t.title, t.grid_locked) for t in lback.tracks]))
|
||||
check("none without one is",
|
||||
not any(t.grid_locked for t in lback.tracks if not t.grid_marker_count))
|
||||
# Measured in Traktor 4.5: LOCK="1" then LOCK_MODIFICATION_TIME, ISO, UTC.
|
||||
check("written as Traktor writes it: LOCK then an ISO LOCK_MODIFICATION_TIME",
|
||||
len(re.findall(r' LOCK="1" LOCK_MODIFICATION_TIME="\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d"',
|
||||
ltext)) == len(with_grid))
|
||||
|
||||
print("== Konduktor only ever deletes what Konduktor wrote ==")
|
||||
mine = dest / "MY OWN NOTES.txt"
|
||||
mine.write_text("do not delete")
|
||||
|
||||
@@ -146,6 +146,16 @@ for platform, reopen in (
|
||||
[by_title[t.title].color for t in chosen] == COLOURS[:len(chosen)],
|
||||
str([by_title[t.title].color for t in chosen]))
|
||||
|
||||
if platform == "rekordbox":
|
||||
# Measured 2026-10-19: rekordbox 7's grid padlock sets bit 0x80 of
|
||||
# `djmdContent.Analysed` (105 -> 233) and nothing else.
|
||||
analysed = {c.Title: c.Analysed for c in back._store._db.get_content()}
|
||||
check("a locked grid is Analysed 233, an unlocked one 105",
|
||||
[analysed[t.title] for t in chosen]
|
||||
== [233 if src.track_cues(t.id).grid_locked else 105 for t in chosen]
|
||||
and any(src.track_cues(t.id).grid_locked for t in chosen),
|
||||
str([(t.title, analysed[t.title]) for t in chosen]))
|
||||
|
||||
print(f"-- {platform}: the key was CONVERTED, not copied")
|
||||
for track in chosen:
|
||||
if not track.key:
|
||||
|
||||
Reference in new issue
Block a user