feat: BPM and musical key detection for all clips

Backend:
- New analysis.py module using librosa for BPM + key detection
  - detect_bpm(): librosa.beat.beat_track() returns tempo
  - detect_key(): Krumhansl-Schmuckler key profiles on chroma features
  - analyze_clip(): combined call returning {bpm, key}
- New dependency: librosa==0.10.2 + soundfile==0.12.1
- Database migrations: bpm (REAL) and key (TEXT) columns added
- New set_analysis() function in database.py
- New endpoint: GET /api/clips/{id}/analysis
  - Returns cached analysis if available
  - Computes on-demand if missing, caches result
  - 400 if clip not ready, 404 if no audio file

Frontend:
- Clip interface extended with bpm?: number | null, key?: string | null
- Gallery: BPM and key badges shown in ribbon and tile views
  - BPM badge: lyria-accent background
  - Key badge: white/5 background
- DetailPanel: BPM and key shown in metadata grid when available
- App.tsx: handleSelectClip() fetches analysis on-demand when opening
  detail panel if not already cached

Docs:
- Architecture KB updated with Audio Analysis section
- Tasks KB: T-028 marked In Progress → will mark Done after commit
This commit is contained in:
rin.kagamine committed 2026-05-29 21:09:04 -04:00
1 parent 2d47f77820
commit 1e21b7dcc8
7 files changed
+187 -3

No files matched your search

+28 -1
View File
@@ -10,9 +10,10 @@ from dotenv import load_dotenv
load_dotenv(Path(__file__).parent.parent / ".env")
from models import GenerateRequest, GenerateResponse, ClipList, RenameRequest, LYRIA_MODELS, DEFAULT_MODEL
from database import init_db, create_clip, get_clips, get_clip, delete_clip, update_clip_status, rename_clip, set_artwork, remove_artwork, delete_all_clips
from database import init_db, create_clip, get_clips, get_clip, delete_clip, update_clip_status, rename_clip, set_artwork, remove_artwork, delete_all_clips, set_analysis
from lyria_client import generate_clip, save_clip_file
from waveform import generate_waveform_svg, get_waveform_path
from analysis import analyze_clip
app = FastAPI(title="Lyria Studio", version="0.1.0")
@@ -228,6 +229,32 @@ async def rename_clip_endpoint(clip_id: str, request: RenameRequest):
# ─── Admin Endpoints ──────────────────────────────────────────────
@app.get("/api/clips/{clip_id}/analysis")
async def get_clip_analysis(clip_id: str):
"""Get BPM and key analysis for a clip. Computes on-demand if not cached."""
clip = await get_clip(clip_id)
if not clip:
raise HTTPException(status_code=404, detail="Clip not found")
if clip.get("status") != "ready":
raise HTTPException(status_code=400, detail="Clip is not ready for analysis")
# Return cached analysis if available
if clip.get("bpm") and clip.get("key"):
return {"bpm": clip["bpm"], "key": clip["key"]}
# Compute analysis
mp3_path = CLIPS_DIR / clip["filename"]
if not mp3_path.exists():
raise HTTPException(status_code=404, detail="Audio file not found")
try:
result = analyze_clip(mp3_path)
# Cache in DB
await set_analysis(clip_id, result["bpm"], result["key"])
return result
except Exception as e:
raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")
@app.post("/api/admin/clear-waveforms")
async def clear_waveforms():
"""Delete all cached waveform SVGs. They regenerate on next view."""