Conversion scripts
Browse files- convert/common.py +86 -0
convert/common.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared bits for the SCNet-Large → Core ML conversion (Wobble Split, #212)."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import sys
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import soundfile as sf
|
| 9 |
+
import torch
|
| 10 |
+
import yaml
|
| 11 |
+
|
| 12 |
+
W = Path(__file__).resolve().parent.parent
|
| 13 |
+
sys.path.insert(0, str(W / "src" / "SCNet"))
|
| 14 |
+
from scnet.SCNet import SCNet # noqa: E402
|
| 15 |
+
|
| 16 |
+
CKPT = W / "weights" / "scnet-large.ckpt"
|
| 17 |
+
CONFIG = W / "weights" / "config-large.yaml"
|
| 18 |
+
OUT = W / "out"
|
| 19 |
+
SR = 44100
|
| 20 |
+
SOURCES = ["drums", "bass", "other", "vocals"]
|
| 21 |
+
KIT = Path("/Volumes/Media 2TB/Samples/Bingoshakerz - Organic Jazzy Downtempo/Organic Jazzy Downtempo [WAV]/"
|
| 22 |
+
"Construction Kit 08 Gm 78")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def load_model() -> SCNet:
|
| 26 |
+
cfg = yaml.safe_load(CONFIG.read_text())
|
| 27 |
+
model = SCNet(**cfg["model"])
|
| 28 |
+
state = torch.load(CKPT, map_location="cpu", weights_only=True)["best_state"]
|
| 29 |
+
model.load_state_dict({k.removeprefix("module."): v for k, v in state.items()})
|
| 30 |
+
return model.eval()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def read(name: str, length: int) -> np.ndarray:
|
| 34 |
+
x, sr = sf.read(KIT / f"OJD Construction Kit 08 {name}.wav", dtype="float32", always_2d=True)
|
| 35 |
+
assert sr == SR
|
| 36 |
+
x = x.T
|
| 37 |
+
reps = int(np.ceil(length / x.shape[1]))
|
| 38 |
+
return np.tile(x, reps)[:, :length]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_mix() -> tuple[np.ndarray, dict[str, np.ndarray]]:
|
| 42 |
+
"""Kit 08 rebuilt from its parts, so every stem has a known right answer. No vocals in it."""
|
| 43 |
+
n = int(24.62 * SR)
|
| 44 |
+
truth = {
|
| 45 |
+
"drums": sum(read(p, n) for p in ["Kick Loop 78", "Snare Loop 78", "Hat Loop 78", "Drum Top Loop 78"]),
|
| 46 |
+
"bass": read("Bass Loop Gm 78", n),
|
| 47 |
+
"other": sum(read(p, n) for p in ["Piano Loop Gm 78", "Guitar Loop Gm 78", "Synth Loop Gm 78",
|
| 48 |
+
"Sax Loop Gm 78", "FX Loop 78"]),
|
| 49 |
+
}
|
| 50 |
+
mix = sum(truth.values())
|
| 51 |
+
peak = np.abs(mix).max()
|
| 52 |
+
if peak > 0.99:
|
| 53 |
+
mix, truth = mix / peak * 0.99, {k: v / peak * 0.99 for k, v in truth.items()}
|
| 54 |
+
return mix.astype(np.float32), truth
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def sdr(ref: np.ndarray, est: np.ndarray) -> float:
|
| 58 |
+
return float(10 * np.log10((ref ** 2).sum() / max(((ref - est) ** 2).sum(), 1e-12)))
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def separate(run_segment, mix: np.ndarray, segment: int, overlap: float = 0.5) -> np.ndarray:
|
| 62 |
+
"""Whole-file separation as scnet.apply does it: normalise by the mono mean/std, cut into
|
| 63 |
+
`segment`-sample windows (centre-padded with zeros), cross-fade with a triangle, undo the
|
| 64 |
+
normalisation. `run_segment(x[2, segment]) -> [4, 2, segment]`. This is what the app will do."""
|
| 65 |
+
mono = mix.mean(0)
|
| 66 |
+
mean, std = mono.mean(), mono.std(ddof=1)
|
| 67 |
+
x = (mix - mean) / std
|
| 68 |
+
length = x.shape[1]
|
| 69 |
+
stride = int((1 - overlap) * segment)
|
| 70 |
+
weight = np.concatenate([np.arange(1, segment // 2 + 1), np.arange(segment - segment // 2, 0, -1)]).astype(np.float32)
|
| 71 |
+
weight /= weight.max()
|
| 72 |
+
out = np.zeros((4, 2, length), np.float32)
|
| 73 |
+
total = np.zeros(length, np.float32)
|
| 74 |
+
for offset in range(0, length, stride):
|
| 75 |
+
chunk_len = min(segment, length - offset)
|
| 76 |
+
delta = segment - chunk_len
|
| 77 |
+
start = offset - delta // 2
|
| 78 |
+
lo, hi = max(0, start), min(length, start + segment)
|
| 79 |
+
chunk = np.zeros((2, segment), np.float32)
|
| 80 |
+
chunk[:, lo - start:hi - start] = x[:, lo:hi]
|
| 81 |
+
y = run_segment(chunk)
|
| 82 |
+
# center_trim back to the chunk's own length
|
| 83 |
+
y = y[..., delta // 2: delta // 2 + chunk_len]
|
| 84 |
+
out[..., offset:offset + chunk_len] += weight[:chunk_len] * y
|
| 85 |
+
total[offset:offset + chunk_len] += weight[:chunk_len]
|
| 86 |
+
return out / total * std + mean
|