Conversion scripts
Browse files- convert/frontend.py +51 -0
convert/frontend.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""What the app does around the Core ML model: front end id `scnet-stft-4096-v1`.
|
| 2 |
+
|
| 3 |
+
All numpy, no torch, so it's an exact spec for the Swift port (vDSP).
|
| 4 |
+
- STFT: rectangular window (none), n_fft 4096, hop 1024, centre=True with reflect padding of 2048,
|
| 5 |
+
one-sided (2049 bins), scaled by 1/sqrt(4096) ("normalized").
|
| 6 |
+
- Model input `spec` [1, 4, 2049, T]: channels L.re, L.im, R.re, R.im.
|
| 7 |
+
- Model output `stems` [1, 16, 2049, T]: channel (source*2 + lr)*2 + (0 re | 1 im), sources drums, bass, other, vocals.
|
| 8 |
+
- iSTFT: irfft each frame (x sqrt(4096)), overlap-add at hop 1024, divide by the window envelope
|
| 9 |
+
(how many frames cover each sample), drop 2048 samples at the start.
|
| 10 |
+
- Before the STFT the segment is padded at the end so T is even (see pad_amount).
|
| 11 |
+
"""
|
| 12 |
+
import numpy as np
|
| 13 |
+
|
| 14 |
+
NFFT, HOP = 4096, 1024
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def pad_amount(n: int) -> int:
|
| 18 |
+
pad = HOP - n % HOP
|
| 19 |
+
if (n + pad) // HOP % 2 == 0:
|
| 20 |
+
pad += HOP
|
| 21 |
+
return pad
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def frames_for(n: int) -> int:
|
| 25 |
+
return (n + pad_amount(n)) // HOP + 1
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def stft(x: np.ndarray) -> np.ndarray:
|
| 29 |
+
"""x [2, n] float32 (n already padded) -> spec [1, 4, 2049, T] float32."""
|
| 30 |
+
xp = np.pad(x, ((0, 0), (NFFT // 2, NFFT // 2)), mode="reflect")
|
| 31 |
+
t = 1 + (xp.shape[1] - NFFT) // HOP
|
| 32 |
+
idx = np.arange(NFFT)[None, :] + HOP * np.arange(t)[:, None]
|
| 33 |
+
spec = np.fft.rfft(xp[:, idx], axis=-1) / np.sqrt(NFFT) # [2, T, 2049]
|
| 34 |
+
out = np.stack([spec.real, spec.imag], axis=1) # [2, 2, T, 2049]
|
| 35 |
+
return out.transpose(0, 1, 3, 2).reshape(1, 4, NFFT // 2 + 1, t).astype(np.float32)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def istft(stems: np.ndarray, n: int) -> np.ndarray:
|
| 39 |
+
"""stems [1, 16, 2049, T] -> [4, 2, n] (n = padded length given to stft)."""
|
| 40 |
+
t = stems.shape[-1]
|
| 41 |
+
s = stems.reshape(4, 2, 2, NFFT // 2 + 1, t)
|
| 42 |
+
spec = (s[:, :, 0] + 1j * s[:, :, 1]).transpose(0, 1, 3, 2) # [4, 2, T, 2049]
|
| 43 |
+
frames = np.fft.irfft(spec * np.sqrt(NFFT), n=NFFT, axis=-1) # [4, 2, T, 4096]
|
| 44 |
+
total = NFFT + HOP * (t - 1)
|
| 45 |
+
y = np.zeros((4, 2, total))
|
| 46 |
+
env = np.zeros(total)
|
| 47 |
+
for i in range(t):
|
| 48 |
+
y[..., i * HOP:i * HOP + NFFT] += frames[:, :, i]
|
| 49 |
+
env[i * HOP:i * HOP + NFFT] += 1.0
|
| 50 |
+
y = y[..., NFFT // 2: NFFT // 2 + n]
|
| 51 |
+
return (y / env[NFFT // 2: NFFT // 2 + n]).astype(np.float32)
|