v0.82.0: Instant vocal splitting (Spleeter via sherpa-onnx, opt-in), a per-deck Instant/HQ swap, and stems cached as Opus
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
"""Round 114: Instant vocal splitting (Spleeter), a per-deck Instant ⇄ HQ
|
||||
swap, and the stem cache in Opus."""
|
||||
import shutil
|
||||
import subprocess
|
||||
import threading
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from lintunes.dj import instant, stems
|
||||
from tests.test_round93 import RATE, _clicks, _loaded, shelf # noqa: F401 — fixture
|
||||
from tests.test_round102 import _wait
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cache(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache"))
|
||||
monkeypatch.setattr(stems, "cache_key", lambda path: "k1")
|
||||
return tmp_path / "cache" / "lintunes" / "stems" / "k1"
|
||||
|
||||
|
||||
def _stems_in(folder, suffix=".opus"):
|
||||
folder.mkdir(parents=True)
|
||||
for name in stems.STEMS:
|
||||
(folder / f"{name}{suffix}").write_bytes(b"x")
|
||||
|
||||
|
||||
def test_a_cached_demucs_split_beats_an_instant_one(cache):
|
||||
_stems_in(cache / stems.INSTANT)
|
||||
assert stems.model_of(stems.cached("/s.mp3")) == stems.INSTANT
|
||||
_stems_in(cache / "htdemucs")
|
||||
assert stems.model_of(stems.cached("/s.mp3")) == "htdemucs"
|
||||
# Demucs's splitter looks at Demucs's tiers only
|
||||
shutil.rmtree(cache / "htdemucs")
|
||||
assert stems.cached("/s.mp3", models=stems.engine_models(stems.HQ)) is None
|
||||
|
||||
|
||||
def test_old_flac_splits_still_read(cache):
|
||||
_stems_in(cache / "htdemucs_ft", ".flac")
|
||||
hit = stems.cached("/s.mp3")
|
||||
assert hit["vocals"].suffix == ".flac" and stems.model_of(hit) == "htdemucs_ft"
|
||||
|
||||
|
||||
def test_default_engine_falls_back_to_what_is_installed(monkeypatch):
|
||||
prefs = {"dj_stem_default": "instant"}
|
||||
monkeypatch.setattr(stems, "available", lambda: True)
|
||||
assert stems.default_engine(prefs) == stems.HQ # Instant not here
|
||||
monkeypatch.setattr(instant, "available", lambda: True)
|
||||
assert stems.default_engine(prefs) == stems.FAST
|
||||
monkeypatch.setattr(stems, "available", lambda: False)
|
||||
assert stems.default_engine({}) == stems.FAST # only Instant here
|
||||
|
||||
|
||||
def test_models_with_the_wrong_checksum_are_refused(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path))
|
||||
with pytest.raises(OSError, match="checksum"):
|
||||
instant.install_models(fetch=lambda url: b"not the models")
|
||||
assert not instant.model_dir().exists()
|
||||
|
||||
|
||||
def test_the_instant_splitter_runs_spleeter_and_prefers_a_cached_demucs(qapp, monkeypatch):
|
||||
ran, looked = [], []
|
||||
monkeypatch.setattr(stems, "split", lambda path, model, gentle, progress=None,
|
||||
cancel=None: ran.append(model) or {})
|
||||
|
||||
def lookup(path, model=None, models=None):
|
||||
looked.append(models)
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(stems, "cached", lookup)
|
||||
splitter = stems.StemSplitter(engine=stems.FAST)
|
||||
done = []
|
||||
splitter.finished.connect(lambda p, f: done.append(p))
|
||||
splitter.request("/a.mp3", best=True)
|
||||
assert _wait(lambda: done == ["/a.mp3"])
|
||||
assert ran == [stems.INSTANT] and looked == [stems.TIERS]
|
||||
|
||||
|
||||
def _instant_shelf(shelf, monkeypatch):
|
||||
asked = {stems.HQ: [], stems.FAST: []}
|
||||
cancelled = {stems.HQ: [], stems.FAST: []}
|
||||
monkeypatch.setattr(stems, "available", lambda: True)
|
||||
monkeypatch.setattr(instant, "available", lambda: True)
|
||||
for engine, splitter in ((stems.HQ, shelf._splitter), (stems.FAST, shelf._instant)):
|
||||
monkeypatch.setattr(splitter, "request",
|
||||
lambda path, best=False, e=engine: asked[e].append((path, best)))
|
||||
monkeypatch.setattr(splitter, "cancel", cancelled[engine].append)
|
||||
shelf._prefs = {"dj_stem_default": "instant"}
|
||||
return asked, cancelled
|
||||
|
||||
|
||||
def _stem_pair(view):
|
||||
n = view.deck.source.frames
|
||||
return np.zeros((n, 2), np.int16), np.zeros((n, 2), np.int16)
|
||||
|
||||
|
||||
def test_a_deck_swaps_between_instant_and_hq(shelf, monkeypatch):
|
||||
asked, cancelled = _instant_shelf(shelf, monkeypatch)
|
||||
a, _ = shelf.views
|
||||
_loaded(shelf, a, shelf.tracks[0], _clicks(124, 0.1, 5), 124.0, 0.1)
|
||||
swap = shelf.stem_swap["A"]
|
||||
assert asked[stems.FAST] == [("/x.mp3", True)] and asked[stems.HQ] == []
|
||||
assert not swap.isHidden() and swap.text() == "HQ"
|
||||
|
||||
# the instant split lands
|
||||
shelf._apply_stems("/x.mp3", (*_stem_pair(a), stems.INSTANT))
|
||||
assert a.using == stems.INSTANT and a.deck.source.has_stems()
|
||||
assert shelf.stem_labels["A"].text() == "Instant vocals"
|
||||
|
||||
# → HQ: Demucs is asked, and the instant stems keep playing meanwhile
|
||||
swap.click()
|
||||
assert asked[stems.HQ] == [("/x.mp3", False)] and swap.text() == "Instant"
|
||||
shelf._on_stem_progress("/x.mp3", 42, stems.HQ)
|
||||
assert shelf.stem_labels["A"].text() == "Instant vocals · HQ split 42%"
|
||||
assert a.deck.source.has_stems()
|
||||
|
||||
# impatient → Instant again: Demucs is stopped, the instant stems stay
|
||||
swap.click()
|
||||
assert cancelled[stems.HQ] == ["/x.mp3"]
|
||||
assert asked[stems.FAST] == [("/x.mp3", True)] # nothing new to split
|
||||
assert shelf.stem_labels["A"].text() == "Instant vocals"
|
||||
assert shelf.stem_cancel["A"].isHidden()
|
||||
shelf._on_stem_cancelled("/x.mp3", stems.HQ) # Demucs's answer changes nothing
|
||||
assert shelf.stem_labels["A"].text() == "Instant vocals"
|
||||
|
||||
|
||||
def test_hq_stems_replace_instant_ones_when_they_land(shelf, monkeypatch):
|
||||
asked, _ = _instant_shelf(shelf, monkeypatch)
|
||||
a, _ = shelf.views
|
||||
_loaded(shelf, a, shelf.tracks[0], _clicks(124, 0.1, 5), 124.0, 0.1)
|
||||
shelf._apply_stems("/x.mp3", (*_stem_pair(a), stems.INSTANT))
|
||||
shelf.stem_swap["A"].click()
|
||||
vocals, instrumental = _stem_pair(a)
|
||||
shelf._apply_stems("/x.mp3", (vocals, instrumental, "htdemucs"))
|
||||
assert a.deck.source.vocals is vocals and a.using == "htdemucs"
|
||||
assert shelf.stem_labels["A"].text() == "HQ vocals"
|
||||
# an instant result arriving late doesn't push the HQ ones out
|
||||
shelf._apply_stems("/x.mp3", (*_stem_pair(a), stems.INSTANT))
|
||||
assert a.deck.source.vocals is vocals
|
||||
|
||||
|
||||
def test_a_cached_demucs_split_puts_an_instant_deck_on_hq(shelf, monkeypatch, tmp_path):
|
||||
_instant_shelf(shelf, monkeypatch)
|
||||
a, _ = shelf.views
|
||||
_loaded(shelf, a, shelf.tracks[0], _clicks(124, 0.1, 5), 124.0, 0.1)
|
||||
folder = tmp_path / "htdemucs_ft"
|
||||
_stems_in(folder)
|
||||
monkeypatch.setattr(threading.Thread, "start", lambda self: None) # no decoding
|
||||
shelf._on_stems("/x.mp3", {n: folder / f"{n}.opus" for n in stems.STEMS})
|
||||
assert a.engine == stems.HQ and shelf.stem_swap["A"].text() == "Instant"
|
||||
|
||||
|
||||
def test_no_swap_button_with_only_one_engine(shelf, monkeypatch):
|
||||
monkeypatch.setattr(stems, "available", lambda: True)
|
||||
monkeypatch.setattr(shelf._splitter, "request", lambda path, best=False: None)
|
||||
a, _ = shelf.views
|
||||
_loaded(shelf, a, shelf.tracks[0], _clicks(124, 0.1, 5), 124.0, 0.1)
|
||||
assert shelf.stem_swap["A"].isHidden()
|
||||
|
||||
|
||||
@pytest.mark.skipif(not shutil.which("ffmpeg"), reason="needs ffmpeg")
|
||||
def test_an_opus_stem_stays_sample_aligned(tmp_path):
|
||||
from lintunes.dj.loader import decode
|
||||
samples = _clicks(124, 0.1, 6)
|
||||
wav = tmp_path / "s.wav"
|
||||
subprocess.run(["ffmpeg", "-v", "error", "-f", "s16le", "-ar", str(RATE), "-ac", "2",
|
||||
"-i", "-", str(wav)], input=samples.tobytes(), check=True)
|
||||
stems.to_opus(wav, tmp_path / "s.opus")
|
||||
back = decode(str(tmp_path / "s.opus")).astype(np.float64)[:, 0]
|
||||
orig = samples.astype(np.float64)[:, 0]
|
||||
assert abs(len(back) - len(orig)) <= 1
|
||||
a = RATE * 2
|
||||
seg = orig[a:a + 4410]
|
||||
lags = range(-50, 51)
|
||||
best = max(lags, key=lambda k: np.dot(seg, back[a + k:a + k + 4410]))
|
||||
assert best == 0
|
||||
Reference in New Issue
Block a user