75 lines
3.0 KiB
Python
75 lines
3.0 KiB
Python
"""Prepare for DJ: the slow part of a deck load, done ahead for many songs.
|
||
|
||
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
|
||
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
|
||
that is instant. One song at a time on a daemon thread, ExportWorker's
|
||
shape: ``progress``, ``bpm_found``, ``finished``; ``cancel`` stops between
|
||
songs and kills a split in progress.
|
||
"""
|
||
|
||
import threading
|
||
|
||
from PyQt6.QtCore import QObject, pyqtSignal
|
||
|
||
from lintunes import bpm_detect
|
||
from lintunes.dj import stems
|
||
|
||
|
||
class DjPrepWorker(QObject):
|
||
progress = pyqtSignal(int, int, str) # done, total, what's happening
|
||
bpm_found = pyqtSignal(int, int) # track id, bpm
|
||
finished = pyqtSignal(dict) # {"done", "split", "bpm", "failed", "cancelled"}
|
||
|
||
def __init__(self, items, model: str, gentle: bool, parent=None):
|
||
"""``items``: (track id, path, label, has bpm)."""
|
||
super().__init__(parent)
|
||
self._items = list(items)
|
||
self._model = model
|
||
self._gentle = gentle
|
||
self._cancel = threading.Event()
|
||
self._thread = None
|
||
|
||
def busy(self) -> bool:
|
||
return self._thread is not None and self._thread.is_alive()
|
||
|
||
def cancel(self):
|
||
self._cancel.set()
|
||
|
||
def start(self):
|
||
self._thread = threading.Thread(target=self._run, daemon=True,
|
||
name="lintunes-dj-prepare")
|
||
self._thread.start()
|
||
|
||
def _run(self):
|
||
summary = {"done": 0, "split": 0, "bpm": 0, "failed": 0, "cancelled": False}
|
||
total = len(self._items)
|
||
for i, (track_id, path, label, has_bpm) in enumerate(self._items):
|
||
if self._cancel.is_set():
|
||
summary["cancelled"] = True
|
||
break
|
||
try:
|
||
if not has_bpm:
|
||
self.progress.emit(i, total, f"BPM · {label}")
|
||
bpm = bpm_detect.detect_file(path)
|
||
if bpm is not None:
|
||
self.bpm_found.emit(track_id, bpm)
|
||
summary["bpm"] += 1
|
||
if (stems.engine_ready(stems.engine_of(self._model))
|
||
and stems.cached(path, self._model) is None):
|
||
def tick(percent, i=i, label=label):
|
||
self.progress.emit(i, total, f"Vocals {percent}% · {label}")
|
||
self.progress.emit(i, total, f"Vocals · {label}")
|
||
stems.split(path, self._model, self._gentle, tick, self._cancel)
|
||
summary["split"] += 1
|
||
summary["done"] += 1
|
||
except stems.Cancelled:
|
||
summary["cancelled"] = True
|
||
break
|
||
except Exception: # noqa: BLE001 — counted, the batch goes on
|
||
summary["failed"] += 1
|
||
self.progress.emit(total, total, "")
|
||
self.finished.emit(summary)
|