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:
2026-10-26 17:25:33 -07:00
parent 1fd7dacf88
commit cd4edcf81f
13 changed files with 785 additions and 70 deletions
+19
View File
@@ -472,6 +472,25 @@ persistence) → GUI (Qt widgets that read the manager and connect to its signal
`split`'s watcher thread at once (it can go seconds without a progress `split`'s watcher thread at once (it can go seconds without a progress
line). The slider then stays enabled, and grabbing it (or loading the line). The slider then stays enabled, and grabbing it (or loading the
song again) splits it over. song again) splits it over.
**Round 114 — Instant vocals** (`dj/instant.py`, opt-in): Spleeter's
2-stem weights as ONNX through **sherpa-onnx** — the real `spleeter`
package needs TensorFlow and Python ≤3.11 — in its own venv
(`$XDG_DATA_HOME/lintunes/spleeter`, ~160 MB, made by Preferences › DJ's
download button, `InstantSetup`; models pinned by SHA-256 — the
release's checksum.txt is stale for them). `_spleeter_run.py` runs in
that venv (`-I`, no LinTunes imports) in 30 s pieces crossfaded over 1 s
(sample-aligned across seams, measured): ~11–17 s for a 4-min song while
Demucs was running beside it. It is the lowest tier (`stems.TIERS`,
`"spleeter"`), so a cached Demucs split still wins. The decks keep **one
`StemSplitter` per engine** (`HQ`/`FAST`; Instant never queues behind
Demucs); `DeckView.engine` is what the deck wants, `using` the tier
playing. `dj_stem_default` (synced) picks the engine a deck starts with,
`stems.default_engine` falls back to whichever is installed *here*. Each
deck's **Instant ⇄ HQ** button (only when both are set up) swaps: to HQ
keeps the instant stems playing until Demucs lands; to Instant kills a
running Demucs. Every split is cached as **Opus 160k** (`to_opus`, ~1/7
of FLAC; decodes back sample-aligned); old FLAC splits still read.
conftest `_no_instant_split` pins `instant.available` off in tests.
**Prepare for DJ** (a playlist's or songs' right-click; `dj/prepare.py`) **Prepare for DJ** (a playlist's or songs' right-click; `dj/prepare.py`)
does the BPM + split ahead of time with the status-bar progress/cancel. does the BPM + split ahead of time with the status-bar progress/cancel.
Each deck also has a volume fader (outer edge, perceptual), a ½×/1×/2× Each deck also has a volume fader (outer edge, perceptual), a ½×/1×/2×
+1 -1
View File
@@ -1,3 +1,3 @@
"""LinTunes — iTunes-style music library manager and player for Linux.""" """LinTunes — iTunes-style music library manager and player for Linux."""
__version__ = "0.81.0" __version__ = "0.82.0"
+81
View File
@@ -0,0 +1,81 @@
"""Split one song with Spleeter (sherpa-onnx), run inside the instant venv.
<venv>/bin/python -I _spleeter_run.py <model dir> <threads> <song> <out dir>
It can't import LinTunes (``-I``, and the venv doesn't have it), so it is
self-contained: ffmpeg decodes the song to 44.1 kHz float stereo, Spleeter
splits it in pieces (each overlapping the next by a second, crossfaded, so
the seams don't click), and ffmpeg encodes each stem to Opus. Progress goes
to stderr as ``LTPCT <n>`` lines.
"""
import subprocess
import sys
import numpy as np
import sherpa_onnx
RATE = 44100
PIECE = 30 * RATE # Spleeter's own window is ~12 s; 30 s pieces keep progress moving
OVERLAP = RATE # crossfaded between pieces
STEMS = ("vocals", "no_vocals") # sherpa-onnx returns vocals, then accompaniment
def decode(path: str) -> np.ndarray:
done = subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-i", path, "-map", "0:a:0",
"-vn", "-ac", "2", "-ar", str(RATE), "-f", "f32le", "-"],
capture_output=True)
if done.returncode != 0 or not done.stdout:
lines = done.stderr.decode(errors="replace").strip().splitlines()
raise SystemExit(lines[-1] if lines else "no audio")
data = np.frombuffer(done.stdout, np.float32)
return data[:len(data) - len(data) % 2].reshape(-1, 2).T.copy()
def encode(samples: np.ndarray, path: str):
pcm = np.ascontiguousarray(samples.T, dtype=np.float32).tobytes()
done = subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-y", "-f", "f32le",
"-ar", str(RATE), "-ac", "2", "-i", "-", "-c:a", "libopus",
"-b:a", "160k", path], input=pcm, capture_output=True)
if done.returncode != 0:
lines = done.stderr.decode(errors="replace").strip().splitlines()
raise SystemExit(lines[-1] if lines else "ffmpeg couldn't write the stem")
def main():
model_dir, threads, song, out_dir = sys.argv[1:5]
config = sherpa_onnx.OfflineSourceSeparationConfig(
model=sherpa_onnx.OfflineSourceSeparationModelConfig(
spleeter=sherpa_onnx.OfflineSourceSeparationSpleeterModelConfig(
vocals=f"{model_dir}/vocals.fp16.onnx",
accompaniment=f"{model_dir}/accompaniment.fp16.onnx"),
num_threads=int(threads)))
if not config.validate():
raise SystemExit("the Spleeter models are missing")
separator = sherpa_onnx.OfflineSourceSeparation(config)
audio = decode(song)
total = audio.shape[1]
out = [np.zeros_like(audio), np.zeros_like(audio)]
fade_in = np.linspace(0.0, 1.0, OVERLAP, dtype=np.float32)
start = 0
while start < total:
end = min(total, start + PIECE + OVERLAP)
parts = separator.process(sample_rate=RATE, samples=np.ascontiguousarray(audio[:, start:end])).stems
for stem, part in zip(out, parts):
data = np.asarray(part.data, np.float32)[:, :end - start]
if start > 0:
n = min(OVERLAP, data.shape[1])
stem[:, start:start + n] *= 1.0 - fade_in[:n]
data = data.copy()
data[:, :n] *= fade_in[:n]
stem[:, start:start + data.shape[1]] += data
print(f"LTPCT {min(100, round(100 * end / total))}", file=sys.stderr, flush=True)
if end >= total:
break
start = end - OVERLAP
for name, stem in zip(STEMS, out):
encode(stem, f"{out_dir}/{name}.opus")
if __name__ == "__main__":
main()
+171
View File
@@ -0,0 +1,171 @@
"""Instant vocals: Spleeter's 2-stem model, ~20× faster than Demucs here.
Demucs takes minutes a song on this laptop's CPU (Round 94); Spleeter took
11 s for a 4-minute song while Demucs was running beside it. It sounds
rougher (watery vocals, some bleed), so it's opt-in: Preferences › DJ has a
download button, and a deck can swap between the two splits of a song.
The original ``spleeter`` package needs TensorFlow and Python ≤3.11, so this
runs Spleeter's own weights as ONNX through **sherpa-onnx** (onnxruntime, a
~125 MB venv at ``$XDG_DATA_HOME/lintunes/spleeter``, never a pip dependency
of LinTunes). The fp16 models (35 MB) come from sherpa-onnx's GitHub release
and are refused unless their SHA-256 is the one pinned here, the
``airplay/cli.py`` rule. The release's own checksum.txt is stale for this
file (it names a different hash), so this one was measured on download.
"""
import hashlib
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile
import threading
import urllib.request
from pathlib import Path
from PyQt6.QtCore import QObject, pyqtSignal
SHERPA_VERSION = "2.0.1"
MODEL_URL = ("https://github.com/k2-fsa/sherpa-onnx/releases/download/"
"source-separation-models/sherpa-onnx-spleeter-2stems-fp16.tar.bz2")
MODEL_SHA256 = "d54561979bd2e08a51e7dbd99ac36bb47564e089eefd403636dbca93e811bba2"
MODEL_FILES = ("vocals.fp16.onnx", "accompaniment.fp16.onnx")
DOWNLOAD_MB = 160 # the wheels (~125 MB installed) and the models
RUNNER = Path(__file__).with_name("_spleeter_run.py")
class Cancelled(Exception):
pass
def venv_dir() -> Path:
base = os.environ.get("XDG_DATA_HOME") or os.path.expanduser("~/.local/share")
return Path(base) / "lintunes" / "spleeter"
def python() -> Path:
return venv_dir() / "bin" / "python"
def model_dir() -> Path:
return venv_dir() / "models"
def available() -> bool:
return python().is_file() and all((model_dir() / f).is_file() for f in MODEL_FILES)
def threads() -> int:
return os.cpu_count() or 4
def remove():
shutil.rmtree(venv_dir(), ignore_errors=True)
def _fetch(url: str) -> bytes:
with urllib.request.urlopen(url, timeout=120) as response:
return response.read()
def install_models(fetch=None):
"""Download, check and unpack the models. Raises OSError."""
data = (fetch or _fetch)(MODEL_URL)
if hashlib.sha256(data).hexdigest() != MODEL_SHA256:
raise OSError("the downloaded Spleeter models didn't match their checksum")
target = model_dir()
target.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix="lintunes-spleeter-") as tmp:
archive = Path(tmp) / "models.tar.bz2"
archive.write_bytes(data)
with tarfile.open(archive) as tar:
for member in tar.getmembers():
name = Path(member.name).name
if name in MODEL_FILES and member.isfile():
source = tar.extractfile(member)
(target / name).write_bytes(source.read())
missing = [f for f in MODEL_FILES if not (target / f).is_file()]
if missing:
raise OSError(f"the Spleeter download had no {missing[0]}")
class InstantSetup(QObject):
"""Preferences' download button: makes the venv, installs sherpa-onnx,
fetches the models, on a daemon thread."""
progress = pyqtSignal(str)
finished = pyqtSignal()
failed = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self._thread = None
def busy(self) -> bool:
return self._thread is not None and self._thread.is_alive()
def start(self):
self._thread = threading.Thread(target=self._run, daemon=True,
name="lintunes-spleeter-setup")
self._thread.start()
def _run(self):
try:
venv = venv_dir()
if not python().is_file():
self.progress.emit("Making its own Python environment…")
venv.parent.mkdir(parents=True, exist_ok=True)
self._call([sys.executable, "-m", "venv", str(venv)])
self.progress.emit("Downloading sherpa-onnx…")
self._call([str(python()), "-m", "pip", "install", "--quiet",
f"sherpa-onnx=={SHERPA_VERSION}", "numpy"])
self.progress.emit("Downloading the Spleeter models…")
install_models()
except (OSError, subprocess.SubprocessError) as e:
self.failed.emit(str(e) or type(e).__name__)
return
self.finished.emit()
@staticmethod
def _call(cmd):
done = subprocess.run(cmd, capture_output=True, text=True, timeout=1800)
if done.returncode != 0:
lines = (done.stderr or done.stdout).strip().splitlines()
raise OSError(lines[-1] if lines else f"{cmd[2]} failed")
def split(path: str, out_dir: Path, progress=None,
cancel: threading.Event | None = None):
"""Write ``vocals.opus`` and ``no_vocals.opus`` for ``path`` into
``out_dir`` (blocking). Raises OSError, or Cancelled."""
if not available():
raise OSError("instant vocal splitting isn't set up on this computer")
process = subprocess.Popen(
[str(python()), "-I", str(RUNNER), str(model_dir()), str(threads()), path, str(out_dir)],
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True,
preexec_fn=lambda: os.nice(5))
if cancel is not None:
def watch():
while process.poll() is None:
if cancel.wait(0.2):
process.kill()
return
threading.Thread(target=watch, daemon=True, name="lintunes-spleeter-watch").start()
tail = []
for line in process.stderr:
if line.startswith("LTPCT "):
if progress is not None:
try:
progress(int(line.split()[1]))
except ValueError:
pass
else:
tail = (tail + [line.strip()])[-20:]
code = process.wait()
if cancel is not None and cancel.is_set():
raise Cancelled()
if code != 0:
lines = [t for t in tail if t]
raise OSError(lines[-1] if lines else "Spleeter failed")
+4 -2
View File
@@ -2,7 +2,8 @@
For each song: its BPM, worked out and saved to the file when it has none For each song: its BPM, worked out and saved to the file when it has none
(the GUI applies it, as Calculate BPM does), and its vocals/instrumental (the GUI applies it, as Calculate BPM does), and its vocals/instrumental
split with the vocals model set in Preferences › DJ — so a whole playlist split with the engine and model set in Preferences › DJ (Instant, or
Demucs's model) — so a whole playlist
can be split with the slow, best model overnight and every deck load after can be split with the slow, best model overnight and every deck load after
that is instant. One song at a time on a daemon thread, ExportWorker's that is instant. One song at a time on a daemon thread, ExportWorker's
shape: ``progress``, ``bpm_found``, ``finished``; ``cancel`` stops between shape: ``progress``, ``bpm_found``, ``finished``; ``cancel`` stops between
@@ -56,7 +57,8 @@ class DjPrepWorker(QObject):
if bpm is not None: if bpm is not None:
self.bpm_found.emit(track_id, bpm) self.bpm_found.emit(track_id, bpm)
summary["bpm"] += 1 summary["bpm"] += 1
if stems.available() and stems.cached(path, self._model) is None: if (stems.engine_ready(stems.engine_of(self._model))
and stems.cached(path, self._model) is None):
def tick(percent, i=i, label=label): def tick(percent, i=i, label=label):
self.progress.emit(i, total, f"Vocals {percent}% · {label}") self.progress.emit(i, total, f"Vocals {percent}% · {label}")
self.progress.emit(i, total, f"Vocals · {label}") self.progress.emit(i, total, f"Vocals · {label}")
+119 -25
View File
@@ -28,6 +28,13 @@ runs niced, and "gentle" (Preferences › DJ, the default) caps it at a
quarter of the cores: measured only ~20 % slower, far cooler. Which model is quarter of the cores: measured only ~20 % slower, far cooler. Which model is
a Preferences choice too (``MODELS``). Progress is read off its tqdm bar. a Preferences choice too (``MODELS``). Progress is read off its tqdm bar.
One split at a time: two would only halve each other's speed. One split at a time: two would only halve each other's speed.
Round 113: an **Instant** tier too (``instant.py``, Spleeter as ONNX), opt-in
from Preferences › DJ. It's the lowest tier, so a deck still takes a Demucs
split when one is cached, and it runs on its own ``StemSplitter`` so an
impatient swap never waits behind Demucs. Every split is cached as **Opus**
(160 kbps, ~1/7 of the FLAC Demucs writes; ffmpeg decodes it back
sample-aligned with the original); older FLAC splits still read.
""" """
import hashlib import hashlib
@@ -43,6 +50,7 @@ from pathlib import Path
from PyQt6.QtCore import QObject, pyqtSignal from PyQt6.QtCore import QObject, pyqtSignal
from lintunes.config import load_config from lintunes.config import load_config
from lintunes.dj import instant
# id → (label, what it costs). Best first: a deck uses the best split it has. # id → (label, what it costs). Best first: a deck uses the best split it has.
# Measured on this laptop (Round 94, 40 s of audio, 2 threads): htdemucs # Measured on this laptop (Round 94, 40 s of audio, 2 threads): htdemucs
@@ -61,6 +69,9 @@ _ARGS = {
"htdemucs_fast": ["-n", "htdemucs", "--overlap", "0.1"], "htdemucs_fast": ["-n", "htdemucs", "--overlap", "0.1"],
} }
DEFAULT_MODEL = "htdemucs" DEFAULT_MODEL = "htdemucs"
INSTANT = "spleeter" # instant.py's tier, below every Demucs model
TIERS = list(MODELS) + [INSTANT] # best first
HQ, FAST = "hq", "instant" # the two engines, as StemSplitter and the decks name them
LEGACY_MODEL = "htdemucs" # Round 93's splits, made before models had folders LEGACY_MODEL = "htdemucs" # Round 93's splits, made before models had folders
STEMS = ("vocals", "no_vocals") STEMS = ("vocals", "no_vocals")
_PERCENT = re.compile(r"(\d{1,3})%\|") _PERCENT = re.compile(r"(\d{1,3})%\|")
@@ -90,6 +101,36 @@ def setup_command() -> str:
f"https://download.pytorch.org/whl/cpu && {v}/bin/pip install numpy demucs") f"https://download.pytorch.org/whl/cpu && {v}/bin/pip install numpy demucs")
def instant_available() -> bool:
return instant.available()
def label(model: str) -> str:
return "Instant" if model == INSTANT else MODELS.get(model, MODELS[DEFAULT_MODEL])[0]
def engine_of(model: str) -> str:
return FAST if model == INSTANT else HQ
def engine_models(engine: str) -> list[str]:
return [INSTANT] if engine == FAST else list(MODELS)
def default_engine(prefs) -> str:
"""Which engine a deck starts with: Preferences › DJ's choice, unless
only the other one is installed here (the pref syncs, installs don't)."""
value = prefs.get("dj_stem_default", HQ) if prefs is not None else HQ
other = HQ if value == FAST else FAST
if not engine_ready(value) and engine_ready(other):
return other
return FAST if value == FAST else HQ
def engine_ready(engine: str) -> bool:
return instant.available() if engine == FAST else available()
def model_choice(prefs) -> str: def model_choice(prefs) -> str:
value = prefs.get("dj_stem_model", DEFAULT_MODEL) if prefs is not None else DEFAULT_MODEL value = prefs.get("dj_stem_model", DEFAULT_MODEL) if prefs is not None else DEFAULT_MODEL
return value if value in MODELS else DEFAULT_MODEL return value if value in MODELS else DEFAULT_MODEL
@@ -196,8 +237,30 @@ def cache_key(path: str) -> str | None:
def _files(folder: Path) -> dict[str, Path] | None: def _files(folder: Path) -> dict[str, Path] | None:
files = {name: folder / f"{name}.flac" for name in STEMS} for suffix in (".opus", ".flac"): # FLAC: splits made before Round 113
return files if all(f.is_file() for f in files.values()) else None files = {name: folder / f"{name}{suffix}" for name in STEMS}
if all(f.is_file() for f in files.values()):
return files
return None
def model_of(files: dict[str, Path]) -> str:
"""Which tier a ``cached``/``split`` result came from."""
name = files["vocals"].parent.name
return name if name in TIERS else LEGACY_MODEL
def to_opus(source: Path, target: Path):
"""Encode one stem. Raises OSError."""
try:
done = subprocess.run(["ffmpeg", "-nostdin", "-v", "error", "-y", "-i", str(source),
"-c:a", "libopus", "-b:a", "160k", str(target)],
capture_output=True, text=True, timeout=300)
except (OSError, subprocess.SubprocessError) as e:
raise OSError(f"ffmpeg could not run: {e}") from e
if done.returncode != 0:
lines = done.stderr.strip().splitlines()
raise OSError(lines[-1] if lines else "ffmpeg couldn't write the stem")
def _touch(folder: Path): def _touch(folder: Path):
@@ -207,9 +270,11 @@ def _touch(folder: Path):
pass pass
def cached(path: str, model: str | None = None) -> dict[str, Path] | None: def cached(path: str, model: str | None = None,
models: list[str] | None = None) -> dict[str, Path] | None:
"""The stems of ``path``: ``model``'s when given, else the best there """The stems of ``path``: ``model``'s when given, else the best there
are (``MODELS`` order). None when it hasn't been split.""" are among ``models`` (default every tier, ``TIERS`` order). None when it
hasn't been split."""
key = cache_key(path) key = cache_key(path)
if key is None: if key is None:
return None return None
@@ -220,7 +285,7 @@ def cached(path: str, model: str | None = None) -> dict[str, Path] | None:
(cache_root() / legacy).rename(root) (cache_root() / legacy).rename(root)
except OSError: except OSError:
root = cache_root() / legacy root = cache_root() / legacy
order = [model] if model else list(MODELS) order = [model] if model else (models or TIERS)
for name in order: for name in order:
hit = _files(root / name) hit = _files(root / name)
if hit is None and name == LEGACY_MODEL: if hit is None and name == LEGACY_MODEL:
@@ -280,6 +345,8 @@ def split(path: str, model: str, gentle: bool, progress=None,
cancel: threading.Event | None = None) -> dict[str, Path]: cancel: threading.Event | None = None) -> dict[str, Path]:
"""Run Demucs on ``path`` (blocking) and cache the two stems. Raises """Run Demucs on ``path`` (blocking) and cache the two stems. Raises
OSError on failure and Cancelled when ``cancel`` is set.""" OSError on failure and Cancelled when ``cancel`` is set."""
if model == INSTANT:
return _split_instant(path, progress, cancel)
if not available(): if not available():
raise OSError("Demucs isn't set up on this computer") raise OSError("Demucs isn't set up on this computer")
key = cache_key(path) key = cache_key(path)
@@ -325,32 +392,56 @@ def split(path: str, model: str, gentle: bool, progress=None,
made = list(work.rglob("vocals.flac")) made = list(work.rglob("vocals.flac"))
if not made: if not made:
raise OSError("Demucs wrote nothing") raise OSError("Demucs wrote nothing")
folder = cache_root() / key / model
folder.mkdir(parents=True, exist_ok=True)
out = {}
for name in STEMS: for name in STEMS:
target = folder / f"{name}.flac" to_opus(made[0].parent / f"{name}.flac", work / f"{name}.opus")
shutil.move(str(made[0].parent / f"{name}.flac"), target) return _keep(key, model, work)
out[name] = target
_touch(cache_root() / key)
prune(keep=key)
return out
finally: finally:
shutil.rmtree(work, ignore_errors=True) shutil.rmtree(work, ignore_errors=True)
def _split_instant(path: str, progress, cancel) -> dict[str, Path]:
key = cache_key(path)
if key is None:
raise OSError("the file is missing")
work = Path(tempfile.mkdtemp(prefix="lintunes-stems-"))
try:
try:
instant.split(path, work, progress, cancel)
except instant.Cancelled:
raise Cancelled() from None
return _keep(key, INSTANT, work)
finally:
shutil.rmtree(work, ignore_errors=True)
def _keep(key: str, model: str, work: Path) -> dict[str, Path]:
"""Move the two ``.opus`` stems in ``work`` into the cache."""
folder = cache_root() / key / model
folder.mkdir(parents=True, exist_ok=True)
out = {}
for name in STEMS:
target = folder / f"{name}.opus"
shutil.move(str(work / f"{name}.opus"), target)
out[name] = target
_touch(cache_root() / key)
prune(keep=key)
return out
class StemSplitter(QObject): class StemSplitter(QObject):
"""A queue of songs to split for the decks, one Demucs run at a time. """A queue of songs to split for the decks, one run at a time, with one
``settings`` returns (model, gentle) when each split starts, so a change engine: ``HQ`` (Demucs; ``settings`` returns (model, gentle) when each
in Preferences applies to the next song.""" split starts, so a change in Preferences applies to the next song) or
``FAST`` (Instant). The decks keep one of each."""
progress = pyqtSignal(str, int) # path, percent progress = pyqtSignal(str, int) # path, percent
finished = pyqtSignal(str, object) # path, {"vocals": Path, …} finished = pyqtSignal(str, object) # path, {"vocals": Path, …}
failed = pyqtSignal(str, str) # path, why failed = pyqtSignal(str, str) # path, why
cancelled = pyqtSignal(str) # path cancelled = pyqtSignal(str) # path
def __init__(self, settings=None, parent=None): def __init__(self, settings=None, parent=None, engine: str = HQ):
super().__init__(parent) super().__init__(parent)
self.engine = engine
self._settings = settings or (lambda: (DEFAULT_MODEL, True)) self._settings = settings or (lambda: (DEFAULT_MODEL, True))
self._queue: list[str] = [] self._queue: list[str] = []
self._busy = False self._busy = False
@@ -359,12 +450,14 @@ class StemSplitter(QObject):
self._stop = threading.Event() self._stop = threading.Event()
self._called_off: set[str] = set() self._called_off: set[str] = set()
def request(self, path: str): def request(self, path: str, best: bool = False):
"""Split ``path`` unless any split of it is cached (then report it). """Split ``path`` unless this engine has a split of it cached (then
report it). ``best`` (Instant only): a cached Demucs split counts
too, and wins — what a deck wants when a song loads.
The lookup may hash the audio (~0.2 s), so it runs off this thread.""" The lookup may hash the audio (~0.2 s), so it runs off this thread."""
with self._lock: with self._lock:
self._called_off.discard(path) self._called_off.discard(path)
threading.Thread(target=self._look_up, args=(path,), daemon=True, threading.Thread(target=self._look_up, args=(path, best), daemon=True,
name="lintunes-stems-lookup").start() name="lintunes-stems-lookup").start()
def cancel(self, path: str): def cancel(self, path: str):
@@ -381,8 +474,9 @@ class StemSplitter(QObject):
return # _run reports it once Demucs is gone return # _run reports it once Demucs is gone
self.cancelled.emit(path) self.cancelled.emit(path)
def _look_up(self, path: str): def _look_up(self, path: str, best: bool = False):
hit = cached(path) models = TIERS if best and self.engine == FAST else engine_models(self.engine)
hit = cached(path, models=models)
if hit is not None: if hit is not None:
self.finished.emit(path, hit) self.finished.emit(path, hit)
return return
@@ -397,7 +491,7 @@ class StemSplitter(QObject):
self._busy = True self._busy = True
if start: if start:
threading.Thread(target=self._run, daemon=True, threading.Thread(target=self._run, daemon=True,
name="lintunes-demucs").start() name=f"lintunes-stems-{self.engine}").start()
def _run(self): def _run(self):
while True: while True:
@@ -408,7 +502,7 @@ class StemSplitter(QObject):
path = self._queue.pop(0) path = self._queue.pop(0)
self._running = path self._running = path
self._stop.clear() self._stop.clear()
model, gentle = self._settings() model, gentle = self._settings() if self.engine == HQ else (INSTANT, False)
try: try:
result = split(path, model, gentle, result = split(path, model, gentle,
lambda percent, path=path: self.progress.emit(path, percent), lambda percent, path=path: self.progress.emit(path, percent),
+124 -32
View File
@@ -69,8 +69,11 @@ class DeckView:
self.bpm: float | None = None self.bpm: float | None = None
self.offset = 0.0 self.offset = 0.0
self.stem_note = "" self.stem_note = ""
self.splitting = False # waiting for / running Demucs (the ✕ shows) self.splitting = False # waiting for / running a split (the ✕ shows)
self.split_called_off = False # cancelled: grabbing the slider retries self.split_called_off = False # cancelled: grabbing the slider retries
self.engine = stems.HQ # which engine this deck's split is (or is to be)
self.using = None # the tier of the stems playing (stems.TIERS), or None
self.percent = None # the running split's progress
self.bpm_saved = False self.bpm_saved = False
self.counted = False self.counted = False
self.mult = 1.0 # the ½× / 1× / 2× switch self.mult = 1.0 # the ½× / 1× / 2× switch
@@ -1034,7 +1037,7 @@ class DjShelf(QWidget):
resize_requested = pyqtSignal(int) # dy from the grip resize_requested = pyqtSignal(int) # dy from the grip
resize_finished = pyqtSignal() resize_finished = pyqtSignal()
resize_reset = pyqtSignal() resize_reset = pyqtSignal()
_stems_ready = pyqtSignal(str, object) # path, (vocals, instrumental) _stems_ready = pyqtSignal(str, object) # path, (vocals, instrumental, tier)
_stems_error = pyqtSignal(str, str) _stems_error = pyqtSignal(str, str)
def __init__(self, manager, prefs=None, parent=None): def __init__(self, manager, prefs=None, parent=None):
@@ -1052,13 +1055,19 @@ class DjShelf(QWidget):
loader.loaded.connect(self._on_loader_result) loader.loaded.connect(self._on_loader_result)
loader.failed.connect(self._on_loader_failed) loader.failed.connect(self._on_loader_failed)
self._stems_ready.connect(self._apply_stems) self._stems_ready.connect(self._apply_stems)
self._stems_error.connect(self._on_stem_failed) self._stems_error.connect(lambda path, why: [self._on_stem_failed(path, why, e)
for e in (stems.HQ, stems.FAST)])
# One splitter per engine, so a swap to Instant never waits behind
# Demucs. ``_splitter`` is Demucs's.
self._splitter = stems.StemSplitter( self._splitter = stems.StemSplitter(
lambda: (stems.model_choice(self._prefs), stems.gentle_choice(self._prefs)), self) lambda: (stems.model_choice(self._prefs), stems.gentle_choice(self._prefs)), self)
self._splitter.progress.connect(self._on_stem_progress) self._instant = stems.StemSplitter(parent=self, engine=stems.FAST)
self._splitter.finished.connect(self._on_stems) for engine, splitter in ((stems.HQ, self._splitter), (stems.FAST, self._instant)):
self._splitter.failed.connect(self._on_stem_failed) splitter.progress.connect(
self._splitter.cancelled.connect(self._on_stem_cancelled) lambda path, pct, e=engine: self._on_stem_progress(path, pct, e))
splitter.finished.connect(self._on_stems)
splitter.failed.connect(lambda path, why, e=engine: self._on_stem_failed(path, why, e))
splitter.cancelled.connect(lambda path, e=engine: self._on_stem_cancelled(path, e))
self.setAutoFillBackground(False) self.setAutoFillBackground(False)
self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, False) self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, False)
self._build() self._build()
@@ -1098,6 +1107,7 @@ class DjShelf(QWidget):
self.vocal_sliders = {} self.vocal_sliders = {}
self.stem_labels = {} self.stem_labels = {}
self.stem_cancel = {} self.stem_cancel = {}
self.stem_swap = {}
self.titles = {} self.titles = {}
self.states = {} self.states = {}
self.bpm_labels = {} self.bpm_labels = {}
@@ -1290,12 +1300,19 @@ class DjShelf(QWidget):
"QToolButton:pressed { background: #2a2b2d; }") "QToolButton:pressed { background: #2a2b2d; }")
stop.clicked.connect(lambda _=False, v=v: self.cancel_split(v)) stop.clicked.connect(lambda _=False, v=v: self.cancel_split(v))
stop.hide() stop.hide()
# Instant ⇄ HQ for this deck's song, once both engines are set up.
swap = QToolButton()
swap.setFocusPolicy(Qt.FocusPolicy.NoFocus)
swap.setStyleSheet(stop.styleSheet())
swap.clicked.connect(lambda _=False, v=v: self.swap_engine(v))
swap.hide()
note_line = QHBoxLayout() note_line = QHBoxLayout()
note_line.setSpacing(2) note_line.setSpacing(2)
if right: if right:
note_line.addStretch(1) note_line.addStretch(1)
note_line.addWidget(stem) note_line.addWidget(stem)
note_line.addWidget(stop) note_line.addWidget(stop)
note_line.addWidget(swap)
if not right: if not right:
note_line.addStretch(1) note_line.addStretch(1)
col.addLayout(note_line) col.addLayout(note_line)
@@ -1312,6 +1329,7 @@ class DjShelf(QWidget):
self.vocal_sliders[v.name] = slider self.vocal_sliders[v.name] = slider
self.stem_labels[v.name] = stem self.stem_labels[v.name] = stem
self.stem_cancel[v.name] = stop self.stem_cancel[v.name] = stop
self.stem_swap[v.name] = swap
holder.setFixedWidth(CONTROLS_WIDTH) holder.setFixedWidth(CONTROLS_WIDTH)
self._compact_hidden.append(holder) self._compact_hidden.append(holder)
return holder return holder
@@ -1657,6 +1675,7 @@ class DjShelf(QWidget):
view.bpm_saved = False view.bpm_saved = False
view.stem_note = "Loading…" view.stem_note = "Loading…"
view.splitting = view.split_called_off = False view.splitting = view.split_called_off = False
view.using = view.percent = None
self.speed[view.name].setValue(0) self.speed[view.name].setValue(0)
self.vocal_sliders[view.name].setValue(0) self.vocal_sliders[view.name].setValue(0)
self.set_mult(view, 1.0) self.set_mult(view, 1.0)
@@ -1710,8 +1729,9 @@ class DjShelf(QWidget):
# "auto-calculate bpm and save it to the file if it wasn't there" # "auto-calculate bpm and save it to the file if it wasn't there"
self._manager.set_tracks_bpm({view.track.track_id: bpm_detect.fold_bpm(result.bpm)}) self._manager.set_tracks_bpm({view.track.track_id: bpm_detect.fold_bpm(result.bpm)})
view.bpm_saved = True view.bpm_saved = True
if stems.available(): if stems.available() or stems.instant_available():
self._request_split(view) view.engine = stems.default_engine(self._prefs)
self._request_split(view, best=True)
else: else:
view.stem_note = "Vocals slider needs Demucs" view.stem_note = "Vocals slider needs Demucs"
self.vocal_sliders[view.name].setToolTip( self.vocal_sliders[view.name].setToolTip(
@@ -1736,44 +1756,99 @@ class DjShelf(QWidget):
def _views_for(self, path: str): def _views_for(self, path: str):
return [v for v in self.views if v.track is not None and v.track.location == path] return [v for v in self.views if v.track is not None and v.track.location == path]
def _request_split(self, view: DeckView): def _splitter_for(self, engine: str) -> stems.StemSplitter:
view.stem_note = "Vocals: waiting to separate…" return self._instant if engine == stems.FAST else self._splitter
def _views_on(self, path: str, engine: str):
return [v for v in self._views_for(path) if v.engine == engine]
def _split_note(self, view: DeckView, doing: str) -> str:
"""What a deck's note says while it waits or splits — short, the
note has ~30 characters beside the ✕ and the swap button. With
stems already playing, those come first: "Instant vocals · HQ
split 42%"."""
split = "Instant split" if view.engine == stems.FAST else "HQ split"
if view.using is not None:
return f"{self._using_note(view)} · {split} {doing}"
if view.engine == stems.HQ:
split = "Separating vocals"
if doing == "cancelled":
return "Vocals split cancelled · grab the slider to retry"
return f"{split}: {doing}" if not doing[0].isdigit() else f"{split}… {doing}"
def _using_note(self, view: DeckView) -> str:
return "Instant vocals" if view.using == stems.INSTANT else "HQ vocals"
def _request_split(self, view: DeckView, best: bool = False):
view.percent = None
view.stem_note = self._split_note(view, "waiting…")
view.splitting, view.split_called_off = True, False view.splitting, view.split_called_off = True, False
self._splitter.request(view.track.location) if view.engine == stems.FAST:
self._instant.request(view.track.location, best=best)
else:
self._splitter.request(view.track.location)
def cancel_split(self, view: DeckView): def cancel_split(self, view: DeckView):
"""The ✕: stop Demucs on this song. Grabbing the vocals slider, or """The ✕: stop this deck's split. Grabbing the vocals slider, or
loading the song again, starts it over.""" loading the song again, starts it over."""
if view.track is not None and view.splitting: if view.track is not None and view.splitting:
self._splitter.cancel(view.track.location) self._splitter_for(view.engine).cancel(view.track.location)
def swap_engine(self, view: DeckView):
"""Instant ⇄ HQ for this deck's song. The stems playing keep playing
until the other split lands; a split still running on the old
engine is stopped (trav: "if you're impatient there's a button to
switch to spleeter")."""
if view.track is None or view.deck.source is None:
return
target = stems.HQ if view.engine == stems.FAST else stems.FAST
if not stems.engine_ready(target):
return
if view.splitting:
self._splitter_for(view.engine).cancel(view.track.location)
view.engine = target
if view.using is not None and stems.engine_of(view.using) == target:
view.splitting = False # already playing that engine's stems
view.stem_note = self._using_note(view)
else:
self._request_split(view)
self._refresh_note(view)
def _retry_split(self, view: DeckView): def _retry_split(self, view: DeckView):
if view.split_called_off and view.track is not None and stems.available(): if (view.split_called_off and view.track is not None
and stems.engine_ready(view.engine)):
self._request_split(view) self._request_split(view)
self._refresh_note(view) self._refresh_note(view)
def _on_stem_cancelled(self, path: str): def _on_stem_cancelled(self, path: str, engine: str = stems.HQ):
for v in self._views_for(path): for v in self._views_on(path, engine):
if not v.splitting: if not v.splitting:
continue continue
v.splitting, v.split_called_off = False, True v.splitting, v.split_called_off = False, True
v.stem_note = "Vocals split cancelled · grab the slider to retry" v.stem_note = self._split_note(v, "cancelled")
self._refresh_note(v) self._refresh_note(v)
def _on_stem_progress(self, path: str, percent: int): def _on_stem_progress(self, path: str, percent: int, engine: str = stems.HQ):
for v in self._views_for(path): for v in self._views_on(path, engine):
if v.split_called_off: if v.split_called_off or not v.splitting:
continue continue
v.stem_note = f"Separating vocals… {percent}%" v.percent = percent
v.stem_note = self._split_note(v, f"{percent}%")
self._refresh_note(v) self._refresh_note(v)
def _on_stems(self, path: str, files: dict): def _on_stems(self, path: str, files: dict):
views = self._views_for(path) tier = stems.model_of(files)
engine = stems.engine_of(tier)
# An Instant lookup at load may answer with a cached Demucs split:
# then this deck is on HQ.
views = [v for v in self._views_for(path)
if v.engine == engine or (v.engine == stems.FAST and v.using is None)]
if not views: if not views:
return return
for v in views: for v in views:
v.engine = engine
v.splitting = v.split_called_off = False v.splitting = v.split_called_off = False
v.stem_note = "Vocals: reading…" v.stem_note = self._split_note(v, "reading…") if v.using else "Vocals: reading…"
self._refresh_note(v) self._refresh_note(v)
def work(): def work():
@@ -1783,22 +1858,28 @@ class DjShelf(QWidget):
except OSError as e: except OSError as e:
self._stems_error.emit(path, str(e)) self._stems_error.emit(path, str(e))
return return
self._stems_ready.emit(path, (vocals, instrumental)) self._stems_ready.emit(path, (vocals, instrumental, tier))
threading.Thread(target=work, daemon=True, name="lintunes-dj-stems").start() threading.Thread(target=work, daemon=True, name="lintunes-dj-stems").start()
def _apply_stems(self, path: str, pair): def _apply_stems(self, path: str, stem_set):
vocals, instrumental, tier = stem_set
for v in self._views_for(path): for v in self._views_for(path):
src = v.deck.source src = v.deck.source
if src is not None: if src is None or v.engine != stems.engine_of(tier):
src.vocals, src.instrumental = pair continue # swapped away while it decoded
v.stem_note = "Vocals separated" src.vocals, src.instrumental = vocals, instrumental
v.using = tier
v.splitting = False
v.stem_note = self._using_note(v)
self._refresh_note(v) self._refresh_note(v)
def _on_stem_failed(self, path: str, why: str): def _on_stem_failed(self, path: str, why: str, engine: str = stems.HQ):
for v in self._views_for(path): for v in self._views_on(path, engine):
v.splitting = False v.splitting = False
v.stem_note = f"Couldn't separate vocals: {why}" note = f"couldn't separate vocals: {why}"
v.stem_note = (f"{self._using_note(v)} · {note}" if v.using
else note[0].upper() + note[1:])
self._refresh_note(v) self._refresh_note(v)
def sync(self, view: DeckView): def sync(self, view: DeckView):
@@ -1885,6 +1966,17 @@ class DjShelf(QWidget):
def _refresh_note(self, view: DeckView): def _refresh_note(self, view: DeckView):
self.stem_labels[view.name].setText(view.stem_note) self.stem_labels[view.name].setText(view.stem_note)
self.stem_cancel[view.name].setVisible(view.splitting and view.track is not None) self.stem_cancel[view.name].setVisible(view.splitting and view.track is not None)
swap = self.stem_swap[view.name]
both = stems.available() and stems.instant_available()
swap.setVisible(both and view.track is not None and view.deck.source is not None)
if view.engine == stems.FAST:
swap.setText("HQ")
swap.setToolTip("Split this song with Demucs: much slower, cleaner vocals. "
"The instant split keeps playing until it's done.")
else:
swap.setText("Instant")
swap.setToolTip("Split this song with Spleeter instead: done in seconds, "
"rougher vocals. Stops the Demucs split.")
has = view.deck.source is not None and view.deck.source.has_stems() has = view.deck.source is not None and view.deck.source.has_stems()
# After a cancel the slider stays live: grabbing it splits again. # After a cancel the slider stays live: grabbing it splits again.
self.vocal_sliders[view.name].setEnabled(has or view.split_called_off) self.vocal_sliders[view.name].setEnabled(has or view.split_called_off)
+8 -5
View File
@@ -2511,11 +2511,13 @@ class MainWindow(QMainWindow):
self.statusBar().showMessage( self.statusBar().showMessage(
"Wait for the current transfer to finish first", 6000) "Wait for the current transfer to finish first", 6000)
return return
if not stems.available(): if not stems.available() and not stems.instant_available():
QMessageBox.information( QMessageBox.information(
self, "Prepare for DJ", self, "Prepare for DJ",
"Separating vocals needs Demucs, which isn't set up on this " "Separating vocals needs Demucs or instant vocal splitting, "
"computer. It's a one-time ~1 GB download:\n\n" + stems.setup_command()) "and neither is set up on this computer. Instant is a button "
"in Preferences › DJ; Demucs is a one-time ~1 GB download:\n\n"
+ stems.setup_command())
return return
items, seen = [], set() items, seen = [], set()
for tid in track_ids: for tid in track_ids:
@@ -2528,14 +2530,15 @@ class MainWindow(QMainWindow):
items.append((tid, track.location, label, bool(track.bpm))) items.append((tid, track.location, label, bool(track.bpm)))
if not items: if not items:
return return
model = stems.model_choice(self._prefs) model = (stems.INSTANT if stems.default_engine(self._prefs) == stems.FAST
else stems.model_choice(self._prefs))
worker = DjPrepWorker(items, model, stems.gentle_choice(self._prefs), self) worker = DjPrepWorker(items, model, stems.gentle_choice(self._prefs), self)
self._dj_prep_bpm = {} self._dj_prep_bpm = {}
worker.bpm_found.connect(self._dj_prep_bpm.__setitem__) worker.bpm_found.connect(self._dj_prep_bpm.__setitem__)
worker.progress.connect(self._on_dj_prep_progress) worker.progress.connect(self._on_dj_prep_progress)
worker.finished.connect(self._on_dj_prep_finished) worker.finished.connect(self._on_dj_prep_finished)
self._dj_prep_worker = worker self._dj_prep_worker = worker
self._sync_label.setText(f"Preparing for DJ ({stems.MODELS[model][0]})…") self._sync_label.setText(f"Preparing for DJ ({stems.label(model)})…")
self._sync_progress.setRange(0, len(items)) self._sync_progress.setRange(0, len(items))
self._sync_progress.setValue(0) self._sync_progress.setValue(0)
self._sync_cancel.show() self._sync_cancel.show()
+70 -2
View File
@@ -143,7 +143,7 @@ class PreferencesDialog(QDialog):
("Appearance", [self._build_general_group(), ("Appearance", [self._build_general_group(),
self._build_colors_group()]), self._build_colors_group()]),
("Visualizer", [self._build_visualizer_group()]), ("Visualizer", [self._build_visualizer_group()]),
("DJ", [self._build_dj_group()]), ("DJ", [self._build_dj_engine_group(), self._build_dj_group()]),
("Integrations", [self._build_lastfm_group(), ("Integrations", [self._build_lastfm_group(),
self._build_acoustid_group(), self._build_acoustid_group(),
self._build_web_server_group()])): self._build_web_server_group()])):
@@ -327,11 +327,79 @@ class PreferencesDialog(QDialog):
# ---- DJ ---- # ---- DJ ----
def _build_dj_engine_group(self) -> QGroupBox:
"""Instant (Spleeter) or High quality (Demucs): which a deck starts
splitting with, and the download that unlocks Instant on this
computer. Each deck can swap its song to the other."""
from lintunes.dj import instant, stems
group = QGroupBox("Vocal splitting")
column = QVBoxLayout(group)
column.addWidget(QLabel("When a song loads onto a deck, start splitting with:"))
self._dj_engine_group = QButtonGroup(self)
self._dj_engine_radios = {}
for engine, text in ((stems.FAST, "Instant (Spleeter): seconds a song, rougher vocals"),
(stems.HQ, "High quality (Demucs, the model below): "
"minutes a song, clean vocals")):
radio = QRadioButton(text)
self._dj_engine_group.addButton(radio)
self._dj_engine_radios[engine] = radio
column.addWidget(radio)
chosen = self._prefs.get("dj_stem_default", stems.HQ)
self._dj_engine_radios[stems.FAST if chosen == stems.FAST else stems.HQ].setChecked(True)
for engine, radio in self._dj_engine_radios.items():
radio.toggled.connect(
lambda on, e=engine: on and self._prefs.set("dj_stem_default", e))
self._dj_instant_note = self._note("")
column.addWidget(self._dj_instant_note)
row = QHBoxLayout()
self._dj_instant_button = QPushButton()
self._dj_instant_button.clicked.connect(self._on_instant_button)
row.addWidget(self._dj_instant_button)
row.addStretch(1)
column.addLayout(row)
self._instant_setup = None
self._refresh_instant()
return group
def _refresh_instant(self, note: str | None = None):
from lintunes.dj import instant
ready = instant.available()
busy = self._instant_setup is not None and self._instant_setup.busy()
self._dj_engine_radios["instant"].setEnabled(ready)
button = self._dj_instant_button
button.setEnabled(not busy)
if ready:
button.setText("Remove Instant Vocal Splitting")
text = ("Instant vocal splitting is set up on this computer. Each deck "
"has a button to swap its song between Instant and HQ.")
else:
button.setText(f"Download Instant Vocal Splitting (Spleeter, "
f"~{instant.DOWNLOAD_MB} MB)")
text = ("Instant isn't set up on this computer yet. Until it is, decks "
"split with Demucs.")
self._dj_instant_note.setText(note or text)
def _on_instant_button(self):
from lintunes.dj import instant
if instant.available():
instant.remove()
if self._prefs.get("dj_stem_default") == "instant":
self._dj_engine_radios["hq"].setChecked(True)
self._refresh_instant()
return
setup = instant.InstantSetup(self)
setup.progress.connect(lambda text: self._refresh_instant(text))
setup.finished.connect(lambda: self._refresh_instant())
setup.failed.connect(lambda why: self._refresh_instant(f"Couldn't set it up: {why}"))
self._instant_setup = setup
setup.start()
self._refresh_instant("Starting…")
def _build_dj_group(self) -> QGroupBox: def _build_dj_group(self) -> QGroupBox:
"""Vocals separation (Demucs): which model, and how hard it may run """Vocals separation (Demucs): which model, and how hard it may run
the CPU. Both apply to the next song split.""" the CPU. Both apply to the next song split."""
from lintunes.dj import stems from lintunes.dj import stems
group = QGroupBox("Separating vocals") group = QGroupBox("High quality (Demucs)")
form = QFormLayout(group) form = QFormLayout(group)
model = QComboBox() model = QComboBox()
for key, (label, cost) in stems.MODELS.items(): for key, (label, cost) in stems.MODELS.items():
+9
View File
@@ -44,6 +44,15 @@ def _no_dj_sound(monkeypatch):
monkeypatch.setattr(output.DjOutput, "shutdown", lambda self: None) monkeypatch.setattr(output.DjOutput, "shutdown", lambda self: None)
@pytest.fixture(autouse=True)
def _no_instant_split(monkeypatch):
"""No test finds this machine's Instant vocal splitting (Round 114): a
deck loaded in a test would run Spleeter on it for real. Tests about it
patch ``instant.available`` back on."""
from lintunes.dj import instant
monkeypatch.setattr(instant, "available", lambda: False)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def _no_real_sound(monkeypatch): def _no_real_sound(monkeypatch):
"""No test may play the finish "tink" out loud: every test that runs an """No test may play the finish "tink" out loud: every test that runs an
+2 -2
View File
@@ -27,7 +27,7 @@ def test_cancelling_the_running_split_stops_demucs(qapp, monkeypatch):
pass pass
raise stems.Cancelled() raise stems.Cancelled()
monkeypatch.setattr(stems, "cached", lambda path, model=None: None) monkeypatch.setattr(stems, "cached", lambda path, model=None, **kw: None)
monkeypatch.setattr(stems, "split", fake_split) monkeypatch.setattr(stems, "split", fake_split)
splitter = stems.StemSplitter() splitter = stems.StemSplitter()
called_off, done = [], [] called_off, done = [], []
@@ -49,7 +49,7 @@ def test_cancelling_a_queued_song_takes_it_out(qapp, monkeypatch):
release.wait(2) release.wait(2)
return {} return {}
monkeypatch.setattr(stems, "cached", lambda path, model=None: None) monkeypatch.setattr(stems, "cached", lambda path, model=None, **kw: None)
monkeypatch.setattr(stems, "split", fake_split) monkeypatch.setattr(stems, "split", fake_split)
splitter = stems.StemSplitter() splitter = stems.StemSplitter()
called_off = [] called_off = []
+176
View File
@@ -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
+1 -1
View File
@@ -169,7 +169,7 @@ def test_prepare_for_dj(monkeypatch, qapp):
# thread — a segfault later. Collect them here, on this one, first. # thread — a segfault later. Collect them here, on this one, first.
gc.collect() gc.collect()
monkeypatch.setattr(prepare.stems, "available", lambda: True) monkeypatch.setattr(prepare.stems, "available", lambda: True)
monkeypatch.setattr(prepare.stems, "cached", lambda path, model=None: None) monkeypatch.setattr(prepare.stems, "cached", lambda path, model=None, **kw: None)
split = [] split = []
monkeypatch.setattr(prepare.stems, "split", monkeypatch.setattr(prepare.stems, "split",
lambda path, model, gentle, tick, cancel: split.append((path, model, gentle)) or tick(50)) lambda path, model, gentle, tick, cancel: split.append((path, model, gentle)) or tick(50))