Implemented basic track analysis

This commit is contained in:
Ben committed 2026-07-08 13:48:21 +02:00
1 parent 986a2cee54
commit c7169c8bbe
6 files changed
+83 -1

No files matched your search

+24
View File
@@ -19,6 +19,7 @@ from .collection_service import CollectionService
from .discovery import describe, detect_collections
from .playlist_store import PlaylistError, PlaylistStore
from .schemas import (
AutoGridRequest,
AutoHotcue,
AutoHotcuesRequest,
CollectionCandidate,
@@ -480,6 +481,29 @@ def auto_hotcues(body: AutoHotcuesRequest) -> TrackCues:
return _sync_cue_edit(body.track_id)
@app.post("/api/tracks/auto-grid", response_model=TrackCues)
def auto_grid(body: AutoGridRequest) -> TrackCues:
"""Detect tempo + first beat and build a beatgrid: sets BPM, sets hotcue 1
(slot 0) to the first beat, and anchors the grid to that position. Octave
(half/double) errors are left for the user to fix with the ×2/÷2 controls."""
store = require_store()
if store.model_entry(body.track_id) is None:
raise HTTPException(404, "Track not found")
path = store.audio_path(body.track_id)
if path is None or not path.exists():
raise HTTPException(400, "Audio file not found (is the drive mounted?)")
try:
bpm, first_beat = ah.detect_grid(str(path))
except Exception as ex: # analysis is best-effort; never 500 the UI
raise HTTPException(400, f"Analysis failed: {ex}")
try:
store.set_grid(body.track_id, bpm=bpm, anchor_sec=first_beat)
store.set_hotcue(body.track_id, 0, first_beat, 0) # hotcue 1 = slot 0
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)."""