feat(stems): implement stage 2 Demucs stem separation with low-VRAM chunking
This commit is contained in:
78
tests/test_s2_stems.py
Normal file
78
tests/test_s2_stems.py
Normal file
@ -0,0 +1,78 @@
|
||||
"""Unit and integration tests for Stage 2 (Stem separation and SNR verification)."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
from songsplit.pipeline.runner import PipelineContext
|
||||
from songsplit.pipeline.s2_stems import StemsStage
|
||||
|
||||
|
||||
def calculate_snr(original: np.ndarray, reconstructed: np.ndarray) -> float:
|
||||
"""Calculate Signal-to-Noise Ratio (SNR) in decibels (dB).
|
||||
|
||||
SNR = 10 * log10( P_signal / P_noise )
|
||||
"""
|
||||
signal_power = np.mean(original**2)
|
||||
noise = original - reconstructed
|
||||
noise_power = np.mean(noise**2)
|
||||
|
||||
if noise_power < 1e-12:
|
||||
return 100.0 # Essentially zero noise
|
||||
if signal_power < 1e-12:
|
||||
return 0.0
|
||||
|
||||
return float(10.0 * np.log10(signal_power / noise_power))
|
||||
|
||||
|
||||
def test_calculate_snr_exact_match():
|
||||
signal = np.sin(np.linspace(0, 100, 1000))
|
||||
snr = calculate_snr(signal, signal)
|
||||
assert snr >= 90.0
|
||||
|
||||
|
||||
def test_calculate_snr_noisy_signal():
|
||||
signal = np.sin(np.linspace(0, 100, 1000))
|
||||
noisy = signal + 0.1 * np.random.RandomState(42).randn(1000)
|
||||
snr = calculate_snr(signal, noisy)
|
||||
# 0.1 noise on amplitude 1.0 signal is approx 15-20 dB SNR
|
||||
assert 15.0 <= snr <= 25.0
|
||||
|
||||
|
||||
def test_stems_stage_declared_outputs(tmp_path: Path):
|
||||
source = tmp_path / "song.wav"
|
||||
out_dir = tmp_path / "pkg"
|
||||
ctx4 = PipelineContext(source_file=source, output_dir=out_dir, stems=4)
|
||||
stage = StemsStage()
|
||||
|
||||
outputs4 = stage.get_outputs(ctx4)
|
||||
stem_names4 = [p.name for p in outputs4]
|
||||
assert "vocals.wav" in stem_names4
|
||||
assert "instrumental.wav" in stem_names4
|
||||
assert "drums.wav" in stem_names4
|
||||
assert "bass.wav" in stem_names4
|
||||
assert "other.wav" in stem_names4
|
||||
assert len(outputs4) == 5
|
||||
|
||||
ctx6 = PipelineContext(source_file=source, output_dir=out_dir, stems=6)
|
||||
outputs6 = stage.get_outputs(ctx6)
|
||||
stem_names6 = [p.name for p in outputs6]
|
||||
assert "guitar.wav" in stem_names6
|
||||
assert "piano.wav" in stem_names6
|
||||
assert len(outputs6) == 7
|
||||
|
||||
|
||||
def test_stems_stage_chunking_params(tmp_path: Path):
|
||||
source = tmp_path / "song.wav"
|
||||
out_dir = tmp_path / "pkg"
|
||||
stage = StemsStage()
|
||||
|
||||
ctx_fast = PipelineContext(source_file=source, output_dir=out_dir, quality="fast")
|
||||
params_fast = stage.get_params(ctx_fast)
|
||||
assert params_fast["segment"] == 7
|
||||
assert params_fast["shifts"] == 0
|
||||
|
||||
ctx_best = PipelineContext(source_file=source, output_dir=out_dir, quality="best")
|
||||
params_best = stage.get_params(ctx_best)
|
||||
assert params_best["segment"] == 10
|
||||
assert params_best["shifts"] == 1
|
||||
Reference in New Issue
Block a user