feat(waveform): precompute and store peaks in DB for instant rendering

Store waveform peaks inline in audio_versions (JSONB columns) so WaveSurfer
can render the waveform immediately on page load without waiting for audio
decode. Adds a 100-point mini-waveform for version selector thumbnails.

Backend:
- Migration 0006: adds waveform_peaks and waveform_peaks_mini JSONB columns
- Worker generates both resolutions (500-pt full, 100-pt mini) during transcode
  and stores them directly in DB — replaces file-based waveform_url approach
- AudioVersionRead schema exposes both fields inline (no extra HTTP round-trip)
- GET /versions/{id}/waveform reads from DB; adds ?resolution=mini support

Frontend:
- audioService.initialize() accepts peaks and calls ws.load(url, Float32Array)
  so waveform renders instantly without audio decode
- useWaveform hook threads peaks option through to audioService
- PlayerPanel passes waveform_peaks from the active version to the hook
- New MiniWaveform SVG component (no WaveSurfer) renders mini peaks in the
  version selector buttons

Fix: docker-compose.dev.yml now runs alembic upgrade head before starting
the API server, so a fresh volume gets the full schema automatically.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mistral Vibe
2026-04-10 09:16:00 +02:00
parent 6876bc1390
commit 037881a821
21 changed files with 919 additions and 49 deletions

View File

@@ -0,0 +1,71 @@
"""Unit tests for handle_transcode waveform peaks storage."""
from unittest.mock import AsyncMock, MagicMock, patch, call
import uuid
import numpy as np
import pytest
@pytest.fixture
def mock_audio(sine_440hz):
audio, sr = sine_440hz
return audio, sr
@pytest.mark.asyncio
async def test_handle_transcode_stores_both_peak_resolutions(mock_audio):
"""After handle_transcode, waveform_peaks (500) and waveform_peaks_mini (100) are stored in DB."""
audio, sr = mock_audio
version_id = uuid.uuid4()
# Capture the statement passed to session.execute
executed_stmts = []
async def capture_execute(stmt):
executed_stmts.append(stmt)
return MagicMock()
mock_session = AsyncMock()
mock_session.execute = capture_execute
mock_session.commit = AsyncMock()
mock_settings = MagicMock()
mock_settings.nextcloud_url = "http://nc.test"
mock_settings.nextcloud_user = "user"
mock_settings.nextcloud_pass = "pass"
mock_settings.target_sample_rate = 44100
mock_settings.audio_tmp_dir = "/tmp"
payload = {
"version_id": str(version_id),
"nc_file_path": "/bands/test/songs/test/v1.wav",
}
with (
patch("worker.main.load_audio", return_value=(audio, sr, "/tmp/v1.wav")),
patch("worker.main.get_duration_ms", return_value=5000),
patch("worker.main.transcode_to_hls", new_callable=AsyncMock),
patch("worker.main.run_full_analysis", new_callable=AsyncMock),
):
from worker.main import handle_transcode
await handle_transcode(payload, mock_session, mock_settings)
assert len(executed_stmts) == 1, "Expected exactly one UPDATE statement"
stmt = executed_stmts[0]
# Extract the values dict from the SQLAlchemy Update statement
values = stmt._values
value_keys = {col.key for col, _ in values.items()}
assert "waveform_peaks" in value_keys, f"waveform_peaks not in UPDATE values: {value_keys}"
assert "waveform_peaks_mini" in value_keys, f"waveform_peaks_mini not in UPDATE values: {value_keys}"
# Resolve the actual peak lists from the BindParameter objects
peaks_500 = next(val.value for col, val in values.items() if col.key == "waveform_peaks")
peaks_100 = next(val.value for col, val in values.items() if col.key == "waveform_peaks_mini")
assert len(peaks_500) == 500, f"Expected 500 peaks, got {len(peaks_500)}"
assert len(peaks_100) == 100, f"Expected 100 mini peaks, got {len(peaks_100)}"
assert all(0.0 <= p <= 1.0 for p in peaks_500), "Full peaks out of [0, 1] range"
assert all(0.0 <= p <= 1.0 for p in peaks_100), "Mini peaks out of [0, 1] range"