mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented beatgrid controls
This commit is contained in:
1 parent
14f5bf3774
commit
815b700075
8 files changed
+457
-23
No files matched your search
@@ -25,6 +25,7 @@ from .schemas import (
|
||||
FileTagOutcome,
|
||||
FsEntry,
|
||||
FsListing,
|
||||
GridEdit,
|
||||
OpenCollection,
|
||||
PlaylistNode,
|
||||
RenamePlaylist,
|
||||
@@ -32,6 +33,7 @@ from .schemas import (
|
||||
SetEntries,
|
||||
SetHotcue,
|
||||
SetHotcueType,
|
||||
SetLock,
|
||||
Stats,
|
||||
Track,
|
||||
TrackCues,
|
||||
@@ -355,7 +357,9 @@ def _build_track_cues(entry) -> TrackCues:
|
||||
)
|
||||
)
|
||||
bpm = grid_bpm if grid_bpm is not None else (entry.tempo.bpm if entry.tempo else None)
|
||||
return TrackCues(bpm=bpm, grid_anchor=grid_anchor, cues=cues)
|
||||
return TrackCues(
|
||||
bpm=bpm, grid_anchor=grid_anchor, locked=bool(entry.lock), cues=cues
|
||||
)
|
||||
|
||||
|
||||
def _cues_for(track_id: str) -> TrackCues:
|
||||
@@ -412,6 +416,36 @@ def delete_hotcue(track_id: str, slot: int) -> TrackCues:
|
||||
return _sync_cue_edit(track_id)
|
||||
|
||||
|
||||
@app.patch("/api/tracks/grid", response_model=TrackCues)
|
||||
def edit_grid(body: GridEdit) -> TrackCues:
|
||||
"""Set the beatgrid BPM and/or move the grid marker (beat 1)."""
|
||||
try:
|
||||
require_store().set_grid(body.track_id, body.bpm, body.anchor)
|
||||
except PlaylistError as ex:
|
||||
raise HTTPException(400, str(ex))
|
||||
return _sync_cue_edit(body.track_id)
|
||||
|
||||
|
||||
@app.delete("/api/tracks/grid", response_model=TrackCues)
|
||||
def remove_grid(track_id: str) -> TrackCues:
|
||||
"""Delete the track's grid marker(s)."""
|
||||
try:
|
||||
require_store().delete_grid(track_id)
|
||||
except PlaylistError as ex:
|
||||
raise HTTPException(400, str(ex))
|
||||
return _sync_cue_edit(track_id)
|
||||
|
||||
|
||||
@app.patch("/api/tracks/lock", response_model=TrackCues)
|
||||
def edit_lock(body: SetLock) -> TrackCues:
|
||||
"""Toggle Traktor's LOCK flag on a track."""
|
||||
try:
|
||||
require_store().set_lock(body.track_id, body.locked)
|
||||
except PlaylistError as ex:
|
||||
raise HTTPException(400, str(ex))
|
||||
return _sync_cue_edit(body.track_id)
|
||||
|
||||
|
||||
@app.put("/api/tracks/art")
|
||||
async def set_track_art(track_id: str = Form(...), file: UploadFile = File(...)) -> dict:
|
||||
"""Stage replacement cover art for a track (written to the file on save)."""
|
||||
|
||||
@@ -33,11 +33,13 @@ from traktor_nml_utils.models.collection import (
|
||||
Albumtype,
|
||||
CueV2Type,
|
||||
Entrytype,
|
||||
GridType,
|
||||
Infotype,
|
||||
Nodetype,
|
||||
Playlisttype,
|
||||
Primarykeytype,
|
||||
Subnodestype,
|
||||
Tempotype,
|
||||
)
|
||||
from xsdata.formats.dataclass.serializers import XmlSerializer
|
||||
|
||||
@@ -336,6 +338,69 @@ class PlaylistStore:
|
||||
entry.cue_v2 = [c for c in (entry.cue_v2 or []) if c.hotcue != slot]
|
||||
self.dirty = True
|
||||
|
||||
# ---- beatgrid -----------------------------------------------------
|
||||
@staticmethod
|
||||
def _grid_marker(entry: Entrytype) -> CueV2Type | None:
|
||||
return next(
|
||||
(c for c in (entry.cue_v2 or []) if getattr(c, "grid", None) is not None), None
|
||||
)
|
||||
|
||||
def set_grid(
|
||||
self, track_id: str, bpm: float | None = None, anchor_sec: float | None = None
|
||||
) -> None:
|
||||
"""Set the beatgrid tempo (TEMPO + grid marker BPM) and/or move the grid
|
||||
marker (beat 1). Creates a grid marker if the track has none."""
|
||||
with self._lock:
|
||||
entry = self._entry_or_raise(track_id)
|
||||
marker = self._grid_marker(entry)
|
||||
if bpm is not None:
|
||||
if bpm <= 0:
|
||||
raise PlaylistError(f"BPM must be positive: {bpm}")
|
||||
if entry.tempo is None:
|
||||
entry.tempo = Tempotype(bpm=bpm, bpm_quality=100.0)
|
||||
else:
|
||||
entry.tempo.bpm = bpm
|
||||
if marker is not None and marker.grid is not None:
|
||||
marker.grid.bpm = bpm
|
||||
if anchor_sec is not None:
|
||||
start_ms = max(0.0, anchor_sec * 1000.0)
|
||||
if marker is not None:
|
||||
marker.start = start_ms
|
||||
else:
|
||||
grid_bpm = bpm if bpm is not None else (entry.tempo.bpm if entry.tempo else None)
|
||||
if grid_bpm is None:
|
||||
raise PlaylistError("No BPM available to create a beatgrid")
|
||||
if entry.cue_v2 is None:
|
||||
entry.cue_v2 = []
|
||||
entry.cue_v2.append(
|
||||
CueV2Type(
|
||||
name="AutoGrid",
|
||||
displ_order=0,
|
||||
type=4,
|
||||
start=start_ms,
|
||||
len=0.0,
|
||||
repeats=-1,
|
||||
hotcue=-1,
|
||||
color=None,
|
||||
grid=GridType(bpm=grid_bpm),
|
||||
)
|
||||
)
|
||||
self.dirty = True
|
||||
|
||||
def delete_grid(self, track_id: str) -> None:
|
||||
with self._lock:
|
||||
entry = self._entry_or_raise(track_id)
|
||||
entry.cue_v2 = [
|
||||
c for c in (entry.cue_v2 or []) if getattr(c, "grid", None) is None
|
||||
]
|
||||
self.dirty = True
|
||||
|
||||
def set_lock(self, track_id: str, locked: bool) -> None:
|
||||
with self._lock:
|
||||
entry = self._entry_or_raise(track_id)
|
||||
entry.lock = 1 if locked else None
|
||||
self.dirty = True
|
||||
|
||||
# ---- cover art -----------------------------------------------------
|
||||
def set_track_art(self, track_id: str, data: bytes, mime: str) -> None:
|
||||
with self._lock:
|
||||
|
||||
@@ -130,9 +130,21 @@ class CuePoint(BaseModel):
|
||||
class TrackCues(BaseModel):
|
||||
bpm: float | None = None # beatgrid BPM (from the grid marker; falls back to tempo)
|
||||
grid_anchor: float | None = None # seconds — first grid marker (beat 1 of the grid)
|
||||
locked: bool = False # Traktor LOCK flag
|
||||
cues: list[CuePoint] = [] # cue/loop markers (grid markers excluded)
|
||||
|
||||
|
||||
class GridEdit(BaseModel):
|
||||
track_id: str
|
||||
bpm: float | None = None # set the beatgrid tempo
|
||||
anchor: float | None = None # set the grid marker (beat 1) position, in seconds
|
||||
|
||||
|
||||
class SetLock(BaseModel):
|
||||
track_id: str
|
||||
locked: bool
|
||||
|
||||
|
||||
class SetHotcue(BaseModel):
|
||||
track_id: str
|
||||
slot: int # 0–7
|
||||
|
||||
@@ -226,5 +226,71 @@ with tempfile.TemporaryDirectory() as d:
|
||||
store2.save()
|
||||
check("create+delete round-trips to the original bytes", work.read_bytes() == original)
|
||||
|
||||
# ---- Invariant E: a beatgrid edit is fully localized -------------------
|
||||
print("== E. grid/bpm edit touches only that track's ENTRY ==")
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
work = Path(d) / "collection.nml"
|
||||
shutil.copy2(REAL, work)
|
||||
original = work.read_bytes()
|
||||
|
||||
store = PlaylistStore(work)
|
||||
# first entry that has both a tempo and a grid marker
|
||||
entry = next(
|
||||
e
|
||||
for e in store._nml.collection.entry
|
||||
if e.tempo and store._grid_marker(e) is not None
|
||||
)
|
||||
loc = entry.location
|
||||
track_id = f"{loc.volume or ''}{loc.dir or ''}{loc.file or ''}"
|
||||
orig_bpm = entry.tempo.bpm
|
||||
|
||||
store.set_grid(track_id, bpm=orig_bpm / 2, anchor_sec=1.5)
|
||||
store.save()
|
||||
edited = work.read_bytes()
|
||||
|
||||
check("file actually changed", original != edited)
|
||||
|
||||
def entry_span(data: bytes, file_name: str):
|
||||
marker = f'FILE="{file_name}"'.encode()
|
||||
i = data.find(marker)
|
||||
start = data.rfind(b"<ENTRY ", 0, i)
|
||||
end = data.find(b"</ENTRY>", i) + len(b"</ENTRY>")
|
||||
return start, end
|
||||
|
||||
os_, oe = entry_span(original, loc.file)
|
||||
es_, ee = entry_span(edited, loc.file)
|
||||
check(
|
||||
"everything outside the edited track's ENTRY is byte-identical",
|
||||
original[:os_] + b"@@" + original[oe:] == edited[:es_] + b"@@" + edited[ee:],
|
||||
)
|
||||
|
||||
from traktor_nml_utils import TraktorCollection
|
||||
|
||||
reparsed = TraktorCollection(path=work)
|
||||
e0 = next(
|
||||
e
|
||||
for e in reparsed.nml.collection.entry
|
||||
if e.location
|
||||
and f"{e.location.volume or ''}{e.location.dir or ''}{e.location.file or ''}" == track_id
|
||||
)
|
||||
m = store._grid_marker(e0)
|
||||
check("tempo BPM halved + persisted", abs(e0.tempo.bpm - orig_bpm / 2) < 1e-3)
|
||||
check("grid marker BPM matches tempo", m is not None and abs(m.grid.bpm - orig_bpm / 2) < 1e-3)
|
||||
check("grid anchor moved to 1.5s (1500 ms)", m is not None and abs(m.start - 1500.0) < 1)
|
||||
|
||||
# Reverting BPM + anchor to the original values round-trips to the file.
|
||||
orig_anchor_ms = store._grid_marker(
|
||||
next(
|
||||
e
|
||||
for e in TraktorCollection(path=REAL).nml.collection.entry
|
||||
if e.location
|
||||
and f"{e.location.volume or ''}{e.location.dir or ''}{e.location.file or ''}" == track_id
|
||||
)
|
||||
).start
|
||||
store2 = PlaylistStore(work)
|
||||
store2.set_grid(track_id, bpm=orig_bpm, anchor_sec=orig_anchor_ms / 1000.0)
|
||||
store2.save()
|
||||
check("revert BPM + anchor round-trips to original bytes", work.read_bytes() == original)
|
||||
|
||||
print("\nRESULT:", "FAILED" if failed else "ALL PASSED")
|
||||
sys.exit(1 if failed else 0)
|
||||
Reference in new issue
Block a user