123 lines
4.6 KiB
Python
123 lines
4.6 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import torch
|
|
|
|
|
|
class EnsembleSeparatorTests(unittest.TestCase):
|
|
def test_incremental_weighted_accumulation(self):
|
|
from backend.services import ensemble_separator as ens
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
input_audio = Path(tmp) / "input.wav"
|
|
input_audio.write_bytes(b"x")
|
|
out_dir = Path(tmp) / "out"
|
|
|
|
ens.ENSEMBLE_CONFIGS["_test_ensemble"] = {
|
|
"description": "test",
|
|
"models": [
|
|
{"type": "m1", "weight": 0.25},
|
|
{"type": "m2", "weight": 0.75},
|
|
],
|
|
"stems": ["vocals"],
|
|
}
|
|
|
|
sep = ens.EnsembleSeparator(ensemble_id="_test_ensemble", output_format="wav")
|
|
model_outputs = [
|
|
{"vocals": str(Path(tmp) / "vocals_a.wav")},
|
|
{"vocals": str(Path(tmp) / "vocals_b.wav")},
|
|
]
|
|
|
|
wave_a = torch.tensor([[1.0, 2.0, 3.0, 4.0]], dtype=torch.float32)
|
|
wave_b = torch.tensor([[5.0, 6.0, 7.0, 8.0]], dtype=torch.float32)
|
|
|
|
saved = {}
|
|
|
|
def fake_run_model_component(*_args, **_kwargs):
|
|
return model_outputs.pop(0)
|
|
|
|
def fake_load_audio(path: str):
|
|
if path.endswith("vocals_a.wav"):
|
|
return wave_a.clone(), 44100
|
|
return wave_b.clone(), 44100
|
|
|
|
def fake_save_audio(waveform, sr, path):
|
|
saved["waveform"] = waveform.clone()
|
|
saved["sr"] = sr
|
|
saved["path"] = path
|
|
|
|
with patch.object(sep, "_run_model_component", side_effect=fake_run_model_component), \
|
|
patch.object(sep, "_load_audio", side_effect=fake_load_audio), \
|
|
patch.object(sep, "_save_audio", side_effect=fake_save_audio):
|
|
result = sep.separate_into_parts(str(input_audio), str(out_dir))
|
|
|
|
expected = (wave_a * 0.25 + wave_b * 0.75) / (0.25 + 0.75)
|
|
self.assertIn("vocals", result)
|
|
self.assertTrue(torch.allclose(saved["waveform"], expected, atol=1e-6))
|
|
self.assertEqual(saved["sr"], 44100)
|
|
|
|
del ens.ENSEMBLE_CONFIGS["_test_ensemble"]
|
|
|
|
def test_resample_and_length_alignment(self):
|
|
from backend.services import ensemble_separator as ens
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
input_audio = Path(tmp) / "input.wav"
|
|
input_audio.write_bytes(b"x")
|
|
out_dir = Path(tmp) / "out"
|
|
|
|
ens.ENSEMBLE_CONFIGS["_test_ensemble_resample"] = {
|
|
"description": "test",
|
|
"models": [
|
|
{"type": "m1", "weight": 1.0},
|
|
{"type": "m2", "weight": 1.0},
|
|
],
|
|
"stems": ["vocals"],
|
|
}
|
|
|
|
sep = ens.EnsembleSeparator(ensemble_id="_test_ensemble_resample", output_format="wav")
|
|
model_outputs = [
|
|
{"vocals": str(Path(tmp) / "vocals_a.wav")},
|
|
{"vocals": str(Path(tmp) / "vocals_b.wav")},
|
|
]
|
|
|
|
wave_a = torch.tensor([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=torch.float32)
|
|
wave_b = torch.tensor([[10.0, 20.0, 30.0]], dtype=torch.float32)
|
|
|
|
saved = {}
|
|
|
|
def fake_run_model_component(*_args, **_kwargs):
|
|
return model_outputs.pop(0)
|
|
|
|
def fake_load_audio(path: str):
|
|
if path.endswith("vocals_a.wav"):
|
|
return wave_a.clone(), 44100
|
|
return wave_b.clone(), 48000
|
|
|
|
def fake_resample(waveform, _src, _dst):
|
|
return waveform
|
|
|
|
def fake_save_audio(waveform, sr, path):
|
|
saved["waveform"] = waveform.clone()
|
|
saved["sr"] = sr
|
|
saved["path"] = path
|
|
|
|
with patch.object(sep, "_run_model_component", side_effect=fake_run_model_component), \
|
|
patch.object(sep, "_load_audio", side_effect=fake_load_audio), \
|
|
patch("backend.services.ensemble_separator.torchaudio.functional.resample", side_effect=fake_resample) as resample_mock, \
|
|
patch.object(sep, "_save_audio", side_effect=fake_save_audio):
|
|
result = sep.separate_into_parts(str(input_audio), str(out_dir))
|
|
|
|
self.assertIn("vocals", result)
|
|
self.assertTrue(resample_mock.called)
|
|
self.assertEqual(saved["waveform"].shape[-1], 3)
|
|
self.assertEqual(saved["sr"], 44100)
|
|
|
|
del ens.ENSEMBLE_CONFIGS["_test_ensemble_resample"]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|