mirror of
https://github.com/MrPatben8/konduktor.git
synced 2026-11-27 10:22:51 +01:00
Implemented looping
This commit is contained in:
1 parent
f88f1932dc
commit
9dc020140a
12 files changed
+847
-143
No files matched your search
@@ -30,6 +30,8 @@ from .schemas import (
|
||||
RenamePlaylist,
|
||||
SaveResult,
|
||||
SetEntries,
|
||||
SetHotcue,
|
||||
SetHotcueType,
|
||||
Stats,
|
||||
Track,
|
||||
TrackCues,
|
||||
@@ -330,12 +332,7 @@ def track_audio(track_id: str) -> FileResponse:
|
||||
return FileResponse(path, media_type=mime)
|
||||
|
||||
|
||||
@app.get("/api/tracks/cues", response_model=TrackCues)
|
||||
def track_cues(track_id: str) -> TrackCues:
|
||||
"""Beatgrid + cue/loop markers for a track (read-only, from the NML)."""
|
||||
entry = require_store().model_entry(track_id)
|
||||
if entry is None:
|
||||
raise HTTPException(404, "Track not found")
|
||||
def _build_track_cues(entry) -> TrackCues:
|
||||
grid_anchor: float | None = None
|
||||
grid_bpm: float | None = None
|
||||
cues: list[CuePoint] = []
|
||||
@@ -361,6 +358,58 @@ def track_cues(track_id: str) -> TrackCues:
|
||||
return TrackCues(bpm=bpm, grid_anchor=grid_anchor, cues=cues)
|
||||
|
||||
|
||||
def _cues_for(track_id: str) -> TrackCues:
|
||||
entry = require_store().model_entry(track_id)
|
||||
if entry is None:
|
||||
raise HTTPException(404, "Track not found")
|
||||
return _build_track_cues(entry)
|
||||
|
||||
|
||||
@app.get("/api/tracks/cues", response_model=TrackCues)
|
||||
def track_cues(track_id: str) -> TrackCues:
|
||||
"""Beatgrid + cue/loop markers for a track (read-only, from the NML)."""
|
||||
return _cues_for(track_id)
|
||||
|
||||
|
||||
def _sync_cue_edit(track_id: str) -> TrackCues:
|
||||
"""After a hotcue edit, refresh the read projection and return fresh cues."""
|
||||
store = require_store()
|
||||
entry = store.model_entry(track_id)
|
||||
if entry is not None:
|
||||
require_service().replace_track(track_id, entry)
|
||||
return _build_track_cues(entry) if entry is not None else TrackCues()
|
||||
|
||||
|
||||
@app.post("/api/tracks/hotcue", response_model=TrackCues)
|
||||
def create_hotcue(body: SetHotcue) -> TrackCues:
|
||||
"""Create (or reposition + retype) the hotcue in a slot at a position."""
|
||||
try:
|
||||
require_store().set_hotcue(body.track_id, body.slot, body.start, body.type)
|
||||
except PlaylistError as ex:
|
||||
raise HTTPException(400, str(ex))
|
||||
return _sync_cue_edit(body.track_id)
|
||||
|
||||
|
||||
@app.patch("/api/tracks/hotcue", response_model=TrackCues)
|
||||
def edit_hotcue_type(body: SetHotcueType) -> TrackCues:
|
||||
"""Change the type of an existing hotcue (keeps its position)."""
|
||||
try:
|
||||
require_store().set_hotcue_type(body.track_id, body.slot, body.type)
|
||||
except PlaylistError as ex:
|
||||
raise HTTPException(400, str(ex))
|
||||
return _sync_cue_edit(body.track_id)
|
||||
|
||||
|
||||
@app.delete("/api/tracks/hotcue", response_model=TrackCues)
|
||||
def delete_hotcue(track_id: str, slot: int) -> TrackCues:
|
||||
"""Remove the hotcue in a slot."""
|
||||
try:
|
||||
require_store().delete_hotcue(track_id, slot)
|
||||
except PlaylistError as ex:
|
||||
raise HTTPException(400, str(ex))
|
||||
return _sync_cue_edit(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)."""
|
||||
|
||||
@@ -31,6 +31,7 @@ from traktor_nml_utils import (
|
||||
)
|
||||
from traktor_nml_utils.models.collection import (
|
||||
Albumtype,
|
||||
CueV2Type,
|
||||
Entrytype,
|
||||
Infotype,
|
||||
Nodetype,
|
||||
@@ -265,6 +266,69 @@ class PlaylistStore:
|
||||
with self._lock:
|
||||
return self._entry_by_key.get(track_id)
|
||||
|
||||
# ---- hotcues (beatgrid-independent point markers) -----------------
|
||||
# Only self-contained point types are editable here: 0 cue, 1 fade-in,
|
||||
# 2 fade-out, 3 load. Loops (need a length) and grid markers (redefine the
|
||||
# beatgrid) are deliberately excluded. Cues are NML-only (not synced to files).
|
||||
POINT_CUE_TYPES = {0, 1, 2, 3}
|
||||
|
||||
def _entry_or_raise(self, track_id: str) -> Entrytype:
|
||||
entry = self._entry_by_key.get(track_id)
|
||||
if entry is None:
|
||||
raise PlaylistError(f"Track not found: {track_id}")
|
||||
return entry
|
||||
|
||||
def set_hotcue(self, track_id: str, slot: int, start_sec: float, cue_type: int) -> None:
|
||||
"""Create (or reposition + retype) the hotcue in `slot` at `start_sec`."""
|
||||
if not 0 <= slot <= 7:
|
||||
raise PlaylistError(f"Invalid hotcue slot: {slot}")
|
||||
if cue_type not in self.POINT_CUE_TYPES:
|
||||
raise PlaylistError(f"Unsupported cue type: {cue_type}")
|
||||
with self._lock:
|
||||
entry = self._entry_or_raise(track_id)
|
||||
start_ms = max(0.0, start_sec * 1000.0) # Traktor stores START in ms
|
||||
existing = next(
|
||||
(c for c in (entry.cue_v2 or []) if c.hotcue == slot), None
|
||||
)
|
||||
if existing is not None:
|
||||
existing.start = start_ms
|
||||
existing.type = cue_type
|
||||
else:
|
||||
if entry.cue_v2 is None:
|
||||
entry.cue_v2 = []
|
||||
entry.cue_v2.append(
|
||||
CueV2Type(
|
||||
name="n.n.", # Traktor's default name for a manual cue
|
||||
displ_order=0,
|
||||
type=cue_type,
|
||||
start=start_ms,
|
||||
len=0.0,
|
||||
repeats=-1,
|
||||
hotcue=slot,
|
||||
color=None,
|
||||
grid=None,
|
||||
)
|
||||
)
|
||||
self.dirty = True
|
||||
|
||||
def set_hotcue_type(self, track_id: str, slot: int, cue_type: int) -> None:
|
||||
"""Change the type of the existing hotcue in `slot` (keeps its position)."""
|
||||
if cue_type not in self.POINT_CUE_TYPES:
|
||||
raise PlaylistError(f"Unsupported cue type: {cue_type}")
|
||||
with self._lock:
|
||||
entry = self._entry_or_raise(track_id)
|
||||
cue = next((c for c in (entry.cue_v2 or []) if c.hotcue == slot), None)
|
||||
if cue is None:
|
||||
raise PlaylistError(f"Hotcue {slot} is not set")
|
||||
cue.type = cue_type
|
||||
self.dirty = True
|
||||
|
||||
def delete_hotcue(self, track_id: str, slot: int) -> None:
|
||||
with self._lock:
|
||||
entry = self._entry_or_raise(track_id)
|
||||
entry.cue_v2 = [c for c in (entry.cue_v2 or []) if c.hotcue != slot]
|
||||
self.dirty = True
|
||||
|
||||
# ---- cover art -----------------------------------------------------
|
||||
def set_track_art(self, track_id: str, data: bytes, mime: str) -> None:
|
||||
with self._lock:
|
||||
|
||||
@@ -133,6 +133,19 @@ class TrackCues(BaseModel):
|
||||
cues: list[CuePoint] = [] # cue/loop markers (grid markers excluded)
|
||||
|
||||
|
||||
class SetHotcue(BaseModel):
|
||||
track_id: str
|
||||
slot: int # 0–7
|
||||
start: float # seconds
|
||||
type: int # 0 cue, 1 fade-in, 2 fade-out, 3 load
|
||||
|
||||
|
||||
class SetHotcueType(BaseModel):
|
||||
track_id: str
|
||||
slot: int
|
||||
type: int
|
||||
|
||||
|
||||
class EditState(BaseModel):
|
||||
dirty: bool # unsaved in-memory playlist changes exist
|
||||
nml_path: str
|
||||
|
||||
@@ -175,5 +175,56 @@ with tempfile.TemporaryDirectory() as d:
|
||||
check("re-parses + genre persisted", e0.info.genre == "KONDUKTOR-FIDELITY-TEST")
|
||||
check("rating persisted (4 stars => RANKING 204)", e0.info.ranking == 204)
|
||||
|
||||
# ---- Invariant D: a hotcue edit is fully localized ---------------------
|
||||
print("== D. hotcue create 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)
|
||||
entry = store._nml.collection.entry[0]
|
||||
loc = entry.location
|
||||
track_id = f"{loc.volume or ''}{loc.dir or ''}{loc.file or ''}"
|
||||
|
||||
# Use a free slot so we exercise the create path (not overwrite an existing).
|
||||
used = {c.hotcue for c in (entry.cue_v2 or []) if c.hotcue is not None}
|
||||
slot = next(s for s in range(8) if s not in used)
|
||||
store.set_hotcue(track_id, slot, 42.5, 0)
|
||||
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:],
|
||||
)
|
||||
n = diff_count(original[os_:oe], edited[es_:ee])
|
||||
check("edited ENTRY diff is tiny (<= 3 tag-lines)", n <= 3, f"got {n}")
|
||||
|
||||
from traktor_nml_utils import TraktorCollection
|
||||
|
||||
reparsed = TraktorCollection(path=work)
|
||||
e0 = reparsed.nml.collection.entry[0]
|
||||
new_cue = next((c for c in (e0.cue_v2 or []) if c.hotcue == slot), None)
|
||||
check("re-parses + hotcue persisted", new_cue is not None)
|
||||
check("hotcue START stored in ms", new_cue is not None and abs(new_cue.start - 42500.0) < 1)
|
||||
|
||||
# Create then delete must round-trip back to a byte-identical file.
|
||||
store2 = PlaylistStore(work) # reload the edited file
|
||||
store2.delete_hotcue(track_id, slot)
|
||||
store2.save()
|
||||
check("create+delete round-trips to the 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