File size: 15,200 Bytes
c600f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cdb2ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c600f78
 
2cdb2ce
 
c600f78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""DAW-DSP: destruktive Effekte (Audacity-Basis), VST3-Plugins (LSP, Dragonfly), Prozedurale SFX."""

from __future__ import annotations

import math
import os
import threading
from functools import lru_cache
from pathlib import Path
from typing import Any

import numpy as np
import pedalboard as pb
from pedalboard.io import AudioFile

SR = 48000
PLUGIN_DIR = Path(__file__).resolve().parents[2] / "plugins" / "vst3"
_LOCK = threading.Lock()


# ------------------------------------------------------------------ I/O
def load(path: str | Path) -> tuple[np.ndarray, int]:
    """Liest WAV/MP3/FLAC/OGG → float32 (channels, samples), max. Stereo."""
    with AudioFile(str(path)) as f:
        data = f.read(f.frames)
        sr = int(f.samplerate)
    if data.shape[0] == 1:
        data = np.vstack([data, data])
    return np.ascontiguousarray(data[:2], dtype=np.float32), sr


def save(path: str | Path, data: np.ndarray, sr: int, *, quality: Any = None) -> Path:
    path = Path(path)
    data = np.nan_to_num(np.asarray(data, dtype=np.float32))
    kwargs = {"quality": quality} if quality is not None and path.suffix == ".mp3" else {}
    with AudioFile(str(path), "w", samplerate=sr, num_channels=data.shape[0], **kwargs) as f:
        f.write(data)
    return path


def db(x: float) -> float:
    return 20 * math.log10(max(float(x), 1e-9))


def peak_db(data: np.ndarray) -> float:
    return db(np.max(np.abs(data))) if data.size else -120.0


def lufs(data: np.ndarray, sr: int) -> float:
    import pyloudnorm as pyln

    if data.shape[1] < int(0.45 * sr):
        return float("-inf")
    return float(pyln.Meter(sr).integrated_loudness(data.T.astype(np.float64)))


def true_peak_db(data: np.ndarray) -> float:
    """True-Peak nach ITU-R BS.1770-4 (4× Oversampling, polyphaser FIR) in dBTP."""
    if not data.size:
        return -120.0
    from scipy.signal import resample_poly

    x = np.asarray(data, dtype=np.float64)
    if x.ndim == 1:
        x = x[None, :]
    peak = 0.0
    step = 1 << 20  # blockweise → konstanter Speicher
    for s in range(0, x.shape[1], step):
        blk = x[:, max(0, s - 64):s + step + 64]
        peak = max(peak, float(np.max(np.abs(resample_poly(blk, 4, 1, axis=1, window=("kaiser", 8.0))))))
    return db(max(peak, float(np.max(np.abs(x)))))


def stats(data: np.ndarray, sr: int) -> dict[str, Any]:
    lv = lufs(data, sr)
    return {"peak_db": round(peak_db(data), 2), "true_peak_db": round(true_peak_db(data), 2),
            "lufs": None if not math.isfinite(lv) else round(lv, 2),
            "duration_s": round(data.shape[1] / sr, 3), "sample_rate": sr}


# ------------------------------------------------------------------ VST3
@lru_cache(maxsize=8)
def _plugin(file: str, name: str | None = None):
    path = PLUGIN_DIR / file
    if not path.exists() or os.environ.get("DAW_DISABLE_VST") == "1":
        return None
    try:
        return pb.load_plugin(str(path), plugin_name=name) if name else pb.load_plugin(str(path))
    except Exception:  # noqa: BLE001 – fehlende Systembibliotheken → Built-in-Fallback
        return None


def plugin_status() -> dict[str, bool]:
    return {
        "LSP Compressor": _plugin("lsp-plugins.vst3", "Compressor Stereo") is not None,
        "LSP Limiter": _plugin("lsp-plugins.vst3", "Limiter Stereo") is not None,
        "Dragonfly Hall": _plugin("DragonflyHallReverb.vst3") is not None,
        "Dragonfly Plate": _plugin("DragonflyPlateReverb.vst3") is not None,
    }


def _set(plugin: Any, **values: Any) -> None:
    for key, value in values.items():
        try:
            setattr(plugin, key, value)
        except Exception:  # noqa: BLE001
            pass


def _run(plugin: Any, data: np.ndarray, sr: int, flush_s: float = 1.0) -> np.ndarray:
    """VST3 aus Worker-Threads: kein reset() (nur Main-Thread) → Stille vorweg zum Leeren,
    Latenz (Lookahead) über reported_latency_samples kompensieren."""
    with _LOCK:  # Plugin-Instanzen sind nicht threadsicher
        n, flush = data.shape[1], int(flush_s * sr)
        lat = int(getattr(plugin, "reported_latency_samples", 0) or 0)
        padded = np.concatenate([np.zeros((data.shape[0], flush), np.float32), data,
                                 np.zeros((data.shape[0], lat + 64), np.float32)], axis=1)
        out = plugin(padded, sr, reset=False)
        return np.ascontiguousarray(out[:, flush + lat:flush + lat + n])


def preload() -> None:
    """Muss im Main-Thread laufen (VST3-Laden)."""
    for args in (("lsp-plugins.vst3", "Compressor Stereo"), ("lsp-plugins.vst3", "Limiter Stereo"),
                 ("DragonflyHallReverb.vst3",), ("DragonflyPlateReverb.vst3",)):
        _plugin(*args)


# ------------------------------------------------------------------ Effekte
def fx_normalize(data, sr, peak=-1.0, **_):
    p = np.max(np.abs(data))
    return data if p <= 0 else data * (10 ** (float(peak) / 20) / p), "Built-in"


def fx_limiter(data, sr, ceiling=-1.0, release=80.0, **_):
    plug = _plugin("lsp-plugins.vst3", "Limiter Stereo")
    if plug is not None:
        _set(plug, threshold_db=float(ceiling), release_time_ms=float(release), lookahead_ms=5.0)
        out = np.clip(_run(plug, data, sr), -1, 1)
        want = min(peak_db(data), float(ceiling))  # Pegel halten, nur Spitzen begrenzen
        out = out * 10 ** ((want - peak_db(out)) / 20)
        return out, "VST3 · LSP Limiter"
    out = pb.Pedalboard([pb.Limiter(threshold_db=float(ceiling), release_ms=float(release))])(data, sr)
    return out, "Built-in Limiter"


def fx_lufs(data, sr, target=-14.0, ceiling=-1.0, **_):
    import pyloudnorm as pyln

    current = lufs(data, sr)
    if not math.isfinite(current):
        return data, "Built-in (zu kurz für LUFS)"
    out = pyln.normalize.loudness(data.T.astype(np.float64), current, float(target)).T.astype(np.float32)
    if peak_db(out) > float(ceiling):
        out, eng = fx_limiter(out, sr, ceiling=ceiling)
        return out, f"pyloudnorm + {eng}"
    return out, "pyloudnorm (EBU R128)"


def fx_compressor(data, sr, threshold=-18.0, ratio=4.0, attack=10.0, release=120.0, makeup=0.0, **_):
    plug = _plugin("lsp-plugins.vst3", "Compressor Stereo")
    if plug is not None:
        _set(plug, attack_threshold_db=float(threshold), ratio=float(ratio), attack_time_ms=float(attack),
             release_time_ms=float(release), makeup_gain_db=float(makeup), knee_db=-6.0)
        return _run(plug, data, sr), "VST3 · LSP Compressor"
    board = pb.Pedalboard([pb.Compressor(threshold_db=float(threshold), ratio=float(ratio), attack_ms=float(attack),
                                         release_ms=float(release)), pb.Gain(gain_db=float(makeup))])
    return board(data, sr), "Built-in Compressor"


def fx_dyncomp(data, sr, target=-18.0, amount=0.6, window_ms=60.0, lookahead_ms=30.0, **_):
    """Dynamic Compressor (Audacity-artig): RMS-Leveler mit Lookahead, hebt Leises, senkt Lautes."""
    mono = np.mean(np.abs(data), axis=0)
    win = max(1, int(sr * float(window_ms) / 1000))
    kernel = np.ones(win) / win
    rms = np.sqrt(np.convolve(mono**2, kernel, mode="same") + 1e-12)
    env_db = 20 * np.log10(rms)
    gain_db = np.clip((float(target) - env_db) * float(amount), -24, 12)
    gain_db[env_db < -60] = 0.0  # Stille nicht hochziehen
    look = int(sr * float(lookahead_ms) / 1000)
    gain_db = np.roll(gain_db, -look)
    smooth = max(1, int(sr * 0.08))
    gain_db = np.convolve(gain_db, np.ones(smooth) / smooth, mode="same")
    out = data * (10 ** (gain_db / 20))[None, :]
    out, _eng = fx_limiter(out.astype(np.float32), sr, ceiling=-1.0)
    return out, "Dynamic Compressor (Leveler) + Limiter"


def fx_eq(data, sr, low=0.0, mid=0.0, mid_freq=1000.0, high=0.0, hp=0.0, **_):
    chain = []
    if float(hp) > 10:
        chain.append(pb.HighpassFilter(cutoff_frequency_hz=float(hp)))
    chain += [pb.LowShelfFilter(cutoff_frequency_hz=120, gain_db=float(low), q=0.7),
              pb.PeakFilter(cutoff_frequency_hz=float(mid_freq), gain_db=float(mid), q=0.9),
              pb.HighShelfFilter(cutoff_frequency_hz=8000, gain_db=float(high), q=0.7)]
    return pb.Pedalboard(chain)(data, sr), "Built-in EQ (Biquad)"


def fx_denoise(data, sr, strength=0.6, **_):
    """Spektrales Gate: Rauschprofil aus den leisesten 10 % Frames, weiche Subtraktion."""
    n, hop = 2048, 512
    win = np.hanning(n).astype(np.float32)
    out = np.zeros_like(data)
    for ch in range(data.shape[0]):
        x = np.pad(data[ch], (n, n))
        frames = np.lib.stride_tricks.sliding_window_view(x, n)[::hop] * win
        spec = np.fft.rfft(frames, axis=1)
        mag = np.abs(spec)
        energy = mag.mean(axis=1)
        quiet = mag[energy <= np.quantile(energy, 0.1)]
        noise = quiet.mean(axis=0) if len(quiet) else mag.min(axis=0)
        gain = np.clip(1 - float(strength) * 1.5 * noise[None, :] / (mag + 1e-9), 0.05, 1.0)
        gain = np.apply_along_axis(lambda g: np.convolve(g, np.ones(5) / 5, "same"), 0, gain)
        y = np.fft.irfft(spec * gain, n=n, axis=1) * win
        acc = np.zeros(len(x) + n, dtype=np.float32)
        norm = np.zeros_like(acc)
        for i, frame in enumerate(y):
            acc[i * hop:i * hop + n] += frame
            norm[i * hop:i * hop + n] += win**2
        res = acc / np.maximum(norm, 1e-6)
        out[ch] = res[n:n + data.shape[1]]
    return out, "Spektrale Rauschminderung"


def fx_reverb(data, sr, mix=0.25, size=0.6, plate=False, **_):
    plug = _plugin("DragonflyPlateReverb.vst3" if plate else "DragonflyHallReverb.vst3")
    tail = np.zeros((data.shape[0], int(sr * 2.5)), dtype=np.float32)
    padded = np.concatenate([data, tail], axis=1)
    if plug is not None:
        _set(plug, dry_level=0.0, decay_s=0.6 + 4 * float(size), size_m=12 + 40 * float(size))
        if not plate:
            _set(plug, late_level=100.0, early_level=30.0)
        wet = _run(plug, padded, sr, flush_s=5.0)
        eng = "VST3 · Dragonfly " + ("Plate" if plate else "Hall")
    else:
        wet = pb.Reverb(room_size=float(size), wet_level=1.0, dry_level=0.0)(padded, sr)
        eng = "Built-in Reverb"
    out = padded * (1 - float(mix) * 0.5) + wet * float(mix)
    # Hall-Ausklang abschneiden, wenn praktisch still
    keep = data.shape[1] + int(np.max(np.nonzero(np.max(np.abs(out[:, data.shape[1]:]), axis=0) > 1e-4)[0], initial=0))
    return out[:, :keep], eng


def fx_gain(data, sr, gain=0.0, **_):
    return data * 10 ** (float(gain) / 20), "Built-in"


def fx_reverse(data, sr, **_):
    return data[:, ::-1].copy(), "Built-in"


def fx_fade(data, sr, fade_in=0.0, fade_out=0.0, **_):
    out = data.copy()
    n_in, n_out = int(float(fade_in) * sr), int(float(fade_out) * sr)
    if n_in > 0:
        out[:, :n_in] *= np.linspace(0, 1, min(n_in, out.shape[1]))[None, :]
    if n_out > 0:
        out[:, -n_out:] *= np.linspace(1, 0, min(n_out, out.shape[1]))[None, :]
    return out, "Built-in"


EFFECTS = {
    "normalize": fx_normalize, "lufs": fx_lufs, "compressor": fx_compressor, "dyncomp": fx_dyncomp,
    "limiter": fx_limiter, "eq": fx_eq, "denoise": fx_denoise, "reverb": fx_reverb, "gain": fx_gain,
    "reverse": fx_reverse, "fade": fx_fade,
}


def apply_effect(data: np.ndarray, sr: int, effect: str, params: dict[str, Any]) -> tuple[np.ndarray, str]:
    if effect not in EFFECTS:
        raise ValueError(f"Unbekannter Effekt: {effect}")
    out, engine = EFFECTS[effect](data.astype(np.float32), sr, **params)
    return np.nan_to_num(np.asarray(out, dtype=np.float32)), engine


# ------------------------------------------------------------------ Fit & Prozedural
def fit(data: np.ndarray, sr: int, seconds: float | None, *, loop: bool = False, fade: float = 0.02) -> np.ndarray:
    if not seconds or seconds <= 0:
        return data
    n = int(seconds * sr)
    if data.shape[1] >= n:
        out = data[:, :n].copy()
    elif loop and data.shape[1] > sr // 2:
        reps = int(math.ceil(n / data.shape[1]))
        out = np.tile(data, (1, reps))[:, :n].copy()
    else:
        out = np.concatenate([data, np.zeros((data.shape[0], n - data.shape[1]), np.float32)], axis=1)
    f = min(int(fade * sr), out.shape[1] // 4)
    if f > 0:
        out[:, :f] *= np.linspace(0, 1, f)[None, :]
        out[:, -f:] *= np.linspace(1, 0, f)[None, :]
    return out


def stretch_to_bpm(data: np.ndarray, sr: int, src_bpm: float | None, dst_bpm: float | None) -> np.ndarray:
    if not src_bpm or not dst_bpm:
        return data
    ratio = float(dst_bpm) / float(src_bpm)
    for cand in (ratio, ratio * 2, ratio / 2):
        if 0.85 <= cand <= 1.15:
            if abs(cand - 1) < 0.01:
                return data
            return pb.time_stretch(data, sr, stretch_factor=cand).astype(np.float32)
    return data


def procedural(kind: str, seconds: float, sr: int = SR, seed: int = 0) -> np.ndarray:
    """Lizenzfreie, synthetisierte Übergänge/SFX: riser, downlifter, impact, whoosh, noise."""
    rng = np.random.default_rng(seed or None)
    n = max(1, int(seconds * sr))
    t = np.linspace(0, 1, n, dtype=np.float32)
    noise = rng.standard_normal((2, n)).astype(np.float32)
    if kind == "impact":
        env = np.exp(-t * 9)
        sub = np.sin(2 * np.pi * np.cumsum(np.linspace(90, 35, n)) / sr)
        y = (0.9 * sub[None, :] + 0.35 * noise * np.exp(-t * 30)[None, :]) * env[None, :]
        y = pb.Pedalboard([pb.Reverb(room_size=0.8, wet_level=0.35)])(y.astype(np.float32), sr)
    else:
        up = kind != "downlifter"
        curve = t if up else 1 - t
        cutoff_env = 200 + 9000 * curve**2
        board_out = np.zeros_like(noise)
        blocks = 32
        for b in range(blocks):
            s, e = b * n // blocks, (b + 1) * n // blocks
            fc = float(cutoff_env[(s + e) // 2])
            board_out[:, s:e] = pb.Pedalboard([pb.LowpassFilter(cutoff_frequency_hz=fc)])(noise[:, s:e], sr)
        tone = np.sin(2 * np.pi * np.cumsum(220 * 2 ** (2 * (curve if up else curve))) / sr) * 0.25
        amp = (curve**1.5) if up else (1 - t) ** 1.5
        if kind == "whoosh":
            amp = np.sin(np.pi * t) ** 2
            tone *= 0
        y = (board_out * 0.6 + tone[None, :]) * amp[None, :]
        y = pb.Pedalboard([pb.Reverb(room_size=0.7, wet_level=0.3), pb.Delay(delay_seconds=0.18, feedback=0.25, mix=0.2)])(y.astype(np.float32), sr)[:, :n]
    y = y / max(1e-6, np.max(np.abs(y))) * 0.8
    return y.astype(np.float32)


def procedural_kind(prompt: str) -> str:
    p = (prompt or "").lower()
    for kind, words in (("impact", ("impact", "hit", "boom", "gunshot", "schuss", "explosion", "drop")),
                        ("downlifter", ("down", "fall", "abwärts")), ("whoosh", ("whoosh", "swoosh", "wind", "train", "zug"))):
        if any(w in p for w in words):
            return kind
    return "riser"


def mix_crossfade(a: np.ndarray, b: np.ndarray, sr: int, seconds: float) -> np.ndarray:
    n = min(int(seconds * sr), a.shape[1], b.shape[1])
    if n <= 0:
        return np.concatenate([a, b], axis=1)
    t = np.linspace(0, np.pi / 2, n)
    xf = a[:, -n:] * np.cos(t)[None, :] + b[:, :n] * np.sin(t)[None, :]
    return np.concatenate([a[:, :-n], xf, b[:, n:]], axis=1)