Begun implementing playback

This commit is contained in:
Ben committed 2026-07-04 16:15:50 +02:00
1 parent 279e5ac34f
commit fa7375d0e9
9 files changed
+712 -49

No files matched your search

+29
View File
@@ -11,6 +11,7 @@ from pathlib import Path
from fastapi import FastAPI, File, Form, HTTPException, Query, Response, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from .collection_service import CollectionService
from .playlist_store import PlaylistError, PlaylistStore
@@ -299,6 +300,34 @@ def track_art(track_id: str) -> Response:
return Response(content=data, media_type=mime, headers={"Cache-Control": "no-store"})
_AUDIO_MIME = {
".mp3": "audio/mpeg",
".m4a": "audio/mp4",
".mp4": "audio/mp4",
".aac": "audio/aac",
".flac": "audio/flac",
".wav": "audio/wav",
".aif": "audio/aiff",
".aiff": "audio/aiff",
".ogg": "audio/ogg",
}
@app.get("/api/tracks/audio")
def track_audio(track_id: str) -> FileResponse:
"""Stream a track's audio file for playback (supports HTTP Range/seeking)."""
path = require_store().audio_path(track_id)
if path is None:
raise HTTPException(404, "Track not found")
if not path.exists():
raise HTTPException(404, f"Audio file not found: {path}")
# .stem.m4a and other MP4s serve as audio/mp4; browsers play the first track.
mime = _AUDIO_MIME.get(path.suffix.lower(), "application/octet-stream")
# Cacheable so the waveform's decode-fetch and the <audio> element can share
# the download (FileResponse adds ETag/Last-Modified for revalidation).
return FileResponse(path, media_type=mime)
@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)."""
+11
View File
@@ -286,6 +286,17 @@ class PlaylistStore:
loc = entry.location
return file_tags.read_cover(file_tags.resolve_path(loc.volume, loc.dir, loc.file))
def audio_path(self, track_id: str) -> "Path | None":
"""Resolve a track's audio file to an OS path (for playback streaming)."""
from . import file_tags
with self._lock:
entry = self._entry_by_key.get(track_id)
if entry is None or entry.location is None:
return None
loc = entry.location
return file_tags.resolve_path(loc.volume, loc.dir, loc.file)
def _entry_field_value(self, entry: Entrytype, field: str):
"""Read a single editable field's current value from the model, in the
shape file_tags expects (rating as 0–5 stars)."""