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>
72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
"""Tests for waveform peak extraction (no Essentia required)."""
|
|
|
|
import json
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from worker.pipeline.waveform import extract_peaks, generate_waveform_file, peaks_to_json
|
|
|
|
|
|
def test_extract_peaks_returns_correct_length(sine_440hz):
|
|
audio, sr = sine_440hz
|
|
peaks = extract_peaks(audio, num_points=500)
|
|
assert len(peaks) == 500
|
|
|
|
|
|
def test_extract_peaks_mini_returns_correct_length(sine_440hz):
|
|
audio, sr = sine_440hz
|
|
peaks = extract_peaks(audio, num_points=100)
|
|
assert len(peaks) == 100
|
|
|
|
|
|
def test_extract_peaks_normalized_between_0_and_1(sine_440hz):
|
|
audio, sr = sine_440hz
|
|
peaks = extract_peaks(audio, num_points=200)
|
|
assert all(0.0 <= p <= 1.0 for p in peaks)
|
|
assert max(peaks) == pytest.approx(1.0, abs=0.01)
|
|
|
|
|
|
def test_extract_peaks_mini_normalized_between_0_and_1(sine_440hz):
|
|
audio, sr = sine_440hz
|
|
peaks = extract_peaks(audio, num_points=100)
|
|
assert all(0.0 <= p <= 1.0 for p in peaks)
|
|
assert max(peaks) == pytest.approx(1.0, abs=0.01)
|
|
|
|
|
|
def test_extract_peaks_empty_audio():
|
|
audio = np.array([], dtype=np.float32)
|
|
peaks = extract_peaks(audio, num_points=100)
|
|
assert len(peaks) == 100
|
|
assert all(p == 0.0 for p in peaks)
|
|
|
|
|
|
def test_extract_peaks_custom_num_points(sine_440hz):
|
|
audio, _ = sine_440hz
|
|
for n in [50, 100, 250, 500]:
|
|
peaks = extract_peaks(audio, num_points=n)
|
|
assert len(peaks) == n, f"Expected {n} peaks, got {len(peaks)}"
|
|
assert all(0.0 <= p <= 1.0 for p in peaks)
|
|
|
|
|
|
def test_peaks_to_json_valid_structure(sine_440hz):
|
|
audio, _ = sine_440hz
|
|
peaks = extract_peaks(audio)
|
|
json_str = peaks_to_json(peaks)
|
|
data = json.loads(json_str)
|
|
assert data["version"] == 2
|
|
assert data["channels"] == 1
|
|
assert len(data["data"]) == len(peaks)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_generate_waveform_file_writes_json(tmp_path, sine_440hz):
|
|
audio, _ = sine_440hz
|
|
output = str(tmp_path / "waveform.json")
|
|
await generate_waveform_file(audio, output)
|
|
with open(output) as f:
|
|
data = json.load(f)
|
|
assert data["version"] == 2
|
|
# generate_waveform_file uses the default num_points=500
|
|
assert len(data["data"]) == 500
|