File size: 3,412 Bytes
29c3be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy as np
import pytest
import soundfile as sf
from app.audio import (
    AudioBuffer, AudioError, analyze, analyze_file, hpss_separate,
    hpss_separate_file, load_audio, mixture_report, save_stems,
)


def synth(n=12000, sr=12000):
    t = np.arange(n) / sr
    signal = 0.15 * np.sin(2 * np.pi * 330 * t)
    for p in (1500, 4500, 7800):
        pulse = np.exp(-np.arange(70)/15)
        signal[p:p+len(pulse)] += 0.5 * pulse
    return AudioBuffer(np.stack((signal, signal * 0.8), axis=1), sr)


def test_real_hpss_reconstructs_stereo(tmp_path):
    audio = synth()
    stems = hpss_separate(audio)
    assert list(stems) == ["harmonic", "percussive", "residual"]
    assert all(v.shape == audio.samples.shape for v in stems.values())
    report = mixture_report(audio.samples, stems)
    assert report["consistent"]
    assert report["max_abs_reconstruction_error"] < 1e-12
    assert np.max(np.abs(stems["harmonic"])) > 1e-4
    assert np.max(np.abs(stems["percussive"])) > 1e-4
    saved = save_stems(tmp_path, audio, stems)
    assert set(saved) == set(stems)
    for stem in saved.values():
        x, rate = sf.read(tmp_path / stem["master"], always_2d=True)
        assert x.shape == audio.samples.shape and rate == audio.samplerate
        assert (tmp_path / stem["preview"]).exists()


def test_silence_supported():
    audio = AudioBuffer(np.zeros((1024, 1)), 8000)
    stems = hpss_separate(audio)
    assert mixture_report(audio.samples, stems)["consistent"]
    assert analyze(audio)["peak_dbfs"] == -240.0


def test_corrupted_input_rejected(tmp_path):
    source = tmp_path / "fake.wav"
    source.write_text("not audio")
    with pytest.raises(AudioError):
        load_audio(source)


def test_audio_over_180_seconds_has_no_duration_ceiling(tmp_path):
    source = tmp_path / "long_recording.wav"
    with sf.SoundFile(source, "w", samplerate=8000, channels=1) as target:
        target.seek(8000 * 181 - 1)
        target.write(np.zeros((1,), dtype=np.float32))
    stats = analyze_file(source, tmp_path / "original_preview.wav")
    assert stats["duration_seconds"] == 181
    assert stats["frames"] == 8000 * 181
    assert (tmp_path / "original_preview.wav").exists()
    assert load_audio(source).samples.shape == (8000 * 181, 1)
    stems, report = hpss_separate_file(source, tmp_path / "stems")
    assert report["consistent"], report
    assert all(sf.info(tmp_path / "stems" / info["master"]).frames == 8000 * 181
               for info in stems.values())




def test_streamed_hpss_handles_non_silent_stereo_chunk_boundaries(tmp_path):
    original = synth(n=30000, sr=12000)
    source = tmp_path / "song.wav"
    sf.write(source, original.samples, original.samplerate, subtype="FLOAT")
    stem_dir = tmp_path / "stems"
    fractions = []
    entries, report = hpss_separate_file(
        source, stem_dir, progress=fractions.append,
        chunk_frames=8192, margin_frames=4096,
    )
    assert len(fractions) >= 3 and fractions[-1] == 1
    assert report["consistent"]
    exports = []
    for item in entries.values():
        loaded, sr = sf.read(stem_dir / item["master"], always_2d=True)
        assert sr == original.samplerate and loaded.shape == original.samples.shape
        exports.append(loaded)
    assert np.max(np.abs(sum(exports) - original.samples)) < 1e-6
    assert np.max(np.abs(exports[0])) > 0.01
    assert np.max(np.abs(exports[1])) > 0.01