micforge 1.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. micforge/__init__.py +3 -0
  2. micforge/__main__.py +4 -0
  3. micforge/analysis/__init__.py +0 -0
  4. micforge/analysis/stats.py +133 -0
  5. micforge/audio/__init__.py +0 -0
  6. micforge/audio/backend.py +32 -0
  7. micforge/audio/cues.py +110 -0
  8. micforge/audio/device_watch.py +237 -0
  9. micforge/audio/devices.py +160 -0
  10. micforge/audio/engine.py +746 -0
  11. micforge/audio/fileio.py +45 -0
  12. micforge/audio/latency.py +169 -0
  13. micforge/audio/loopback.py +113 -0
  14. micforge/audio/metrics.py +134 -0
  15. micforge/audio/noise_profile.py +250 -0
  16. micforge/audio/recorder.py +100 -0
  17. micforge/audio/ring.py +89 -0
  18. micforge/cli.py +293 -0
  19. micforge/commands/__init__.py +0 -0
  20. micforge/commands/calibrate.py +47 -0
  21. micforge/commands/common.py +69 -0
  22. micforge/commands/devices_cmd.py +86 -0
  23. micforge/commands/diagnostics_cmd.py +68 -0
  24. micforge/commands/doctor.py +127 -0
  25. micforge/commands/files.py +48 -0
  26. micforge/commands/help_cmd.py +449 -0
  27. micforge/commands/latency_test.py +65 -0
  28. micforge/commands/live.py +321 -0
  29. micforge/commands/preset_cmd.py +142 -0
  30. micforge/commands/test_mic.py +98 -0
  31. micforge/commands/wizard.py +70 -0
  32. micforge/config.py +279 -0
  33. micforge/diagnostics.py +308 -0
  34. micforge/dsp/__init__.py +0 -0
  35. micforge/dsp/aec.py +388 -0
  36. micforge/dsp/base.py +30 -0
  37. micforge/dsp/biquad.py +144 -0
  38. micforge/dsp/chain.py +153 -0
  39. micforge/dsp/cofilite.py +427 -0
  40. micforge/dsp/compressor.py +109 -0
  41. micforge/dsp/deesser.py +64 -0
  42. micforge/dsp/denoise.py +181 -0
  43. micforge/dsp/deverb.py +227 -0
  44. micforge/dsp/export_cofilite.py +111 -0
  45. micforge/dsp/gate.py +76 -0
  46. micforge/dsp/leveler.py +74 -0
  47. micforge/dsp/proximity.py +164 -0
  48. micforge/dsp/util.py +80 -0
  49. micforge/errors.py +25 -0
  50. micforge/logging_setup.py +25 -0
  51. micforge/models/cofilite.onnx +0 -0
  52. micforge/presets.py +284 -0
  53. micforge/profile_store.py +41 -0
  54. micforge/ui/__init__.py +0 -0
  55. micforge/ui/dashboard.py +573 -0
  56. micforge/ui/hotkeys.py +169 -0
  57. micforge/ui/keys.py +64 -0
  58. micforge/ui/report.py +88 -0
  59. micforge/ui/settings_gui.py +899 -0
  60. micforge/ui/theme.py +42 -0
  61. micforge/ui/tray.py +276 -0
  62. micforge/ui/tuning.py +138 -0
  63. micforge/ui/widgets.py +109 -0
  64. micforge/util/autostart.py +56 -0
  65. micforge-1.3.0.dist-info/METADATA +334 -0
  66. micforge-1.3.0.dist-info/RECORD +70 -0
  67. micforge-1.3.0.dist-info/WHEEL +5 -0
  68. micforge-1.3.0.dist-info/entry_points.txt +2 -0
  69. micforge-1.3.0.dist-info/licenses/LICENSE +22 -0
  70. micforge-1.3.0.dist-info/top_level.txt +1 -0
micforge/dsp/chain.py ADDED
@@ -0,0 +1,153 @@
1
+ """Assemble processors into a chain and build one from a ChainConfig."""
2
+ from __future__ import annotations
3
+
4
+ import numpy as np
5
+
6
+ from ..config import ChainConfig
7
+ from .aec import AcousticEchoCanceller
8
+ from .base import Processor
9
+ from .biquad import BiquadFilter
10
+ from .compressor import Compressor, Limiter
11
+ from .deesser import DeEsser
12
+ from .denoise import SpectralDenoiser
13
+ from .deverb import RoomDereverberation
14
+ from .gate import NoiseGate
15
+ from .leveler import Leveler
16
+ from .proximity import VoiceProximity
17
+ from .util import db2lin
18
+
19
+
20
+ class Chain:
21
+ """Ordered list of processors plus input/output trim."""
22
+
23
+ def __init__(self, stages: list[Processor], input_gain_db: float = 0.0, output_gain_db: float = 0.0):
24
+ self.stages = stages
25
+ self.input_gain_db = input_gain_db
26
+ self.output_gain_db = output_gain_db
27
+
28
+ def stage(self, name: str) -> Processor | None:
29
+ return next((s for s in self.stages if s.name == name), None)
30
+
31
+ @property
32
+ def latency_samples(self) -> int:
33
+ return sum(s.latency_samples for s in self.stages if s.enabled)
34
+
35
+ def set_input_gain_db(self, db: float) -> None:
36
+ self.input_gain_db = float(min(40.0, max(-40.0, db)))
37
+
38
+ def process(self, x: np.ndarray) -> np.ndarray:
39
+ y = x * np.float32(db2lin(self.input_gain_db)) if self.input_gain_db else x
40
+ for s in self.stages:
41
+ if s.enabled:
42
+ y = s.process(y)
43
+ if self.output_gain_db:
44
+ y = y * np.float32(db2lin(self.output_gain_db))
45
+ return y
46
+
47
+ def reset(self) -> None:
48
+ for s in self.stages:
49
+ s.reset()
50
+
51
+ def telemetry(self) -> dict[str, dict[str, float]]:
52
+ return {s.name: s.telemetry() for s in self.stages if s.enabled}
53
+
54
+
55
+ def build_chain(
56
+ cfg: ChainConfig,
57
+ sample_rate: int,
58
+ noise_profile: np.ndarray | None = None,
59
+ model_override: str | None = None,
60
+ ) -> Chain:
61
+ cfg.validate()
62
+ hp_enabled = cfg.highpass_hz > 0
63
+ model_path = model_override or cfg.denoise.model_path
64
+
65
+ denoise: Processor
66
+ if cfg.denoise.model_type == "cofilite":
67
+ try:
68
+ from .cofilite import CoFiLiteDenoiser, find_model_file
69
+ if find_model_file(model_path) is not None:
70
+ denoise = CoFiLiteDenoiser(
71
+ sample_rate,
72
+ cfg.denoise.strength,
73
+ cfg.denoise.max_reduction_db,
74
+ model_path=model_path,
75
+ enabled=cfg.denoise.enabled,
76
+ )
77
+ else:
78
+ denoise = SpectralDenoiser(
79
+ sample_rate, cfg.denoise.strength, cfg.denoise.max_reduction_db, enabled=cfg.denoise.enabled
80
+ )
81
+ except Exception:
82
+ denoise = SpectralDenoiser(
83
+ sample_rate, cfg.denoise.strength, cfg.denoise.max_reduction_db, enabled=cfg.denoise.enabled
84
+ )
85
+ else:
86
+ denoise = SpectralDenoiser(
87
+ sample_rate, cfg.denoise.strength, cfg.denoise.max_reduction_db, enabled=cfg.denoise.enabled
88
+ )
89
+
90
+ if noise_profile is not None and hasattr(denoise, "set_profile"):
91
+ denoise.set_profile(noise_profile)
92
+
93
+ aec_cfg = getattr(cfg, "aec", None)
94
+ aec = AcousticEchoCanceller(
95
+ sample_rate,
96
+ aec_cfg.amount if aec_cfg else 1.0,
97
+ aec_cfg.filter_length_ms if aec_cfg else 60.0,
98
+ aec_cfg.delay_ms if aec_cfg else 40.0,
99
+ aec_cfg.suppression_db if aec_cfg else 24.0,
100
+ enabled=aec_cfg.enabled if aec_cfg else False,
101
+ )
102
+
103
+ deverb_cfg = getattr(cfg, "deverb", None)
104
+ deverb = RoomDereverberation(
105
+ sample_rate,
106
+ deverb_cfg.amount if deverb_cfg else 0.5,
107
+ deverb_cfg.room_decay if deverb_cfg else 0.4,
108
+ deverb_cfg.early_retention if deverb_cfg else 0.8,
109
+ enabled=deverb_cfg.enabled if deverb_cfg else True,
110
+ )
111
+
112
+ prox_cfg = getattr(cfg, "proximity", None)
113
+ proximity = VoiceProximity(
114
+ sample_rate,
115
+ prox_cfg.amount if prox_cfg else 0.5,
116
+ prox_cfg.body_hz if prox_cfg else 120.0,
117
+ prox_cfg.warmth if prox_cfg else 0.35,
118
+ prox_cfg.mud_cleanup if prox_cfg else 0.4,
119
+ enabled=prox_cfg.enabled if prox_cfg else True,
120
+ )
121
+
122
+ g, d, lv, c, li = cfg.gate, cfg.deesser, cfg.leveler, cfg.compressor, cfg.limiter
123
+ stages: list[Processor] = [
124
+ BiquadFilter.highpass(sample_rate, cfg.highpass_hz or 80.0, order=4, enabled=hp_enabled),
125
+ aec,
126
+ denoise,
127
+ deverb,
128
+ NoiseGate(
129
+ sample_rate, g.threshold_db, g.range_db, g.attack_ms, g.hold_ms, g.release_ms, g.hysteresis_db,
130
+ enabled=g.enabled,
131
+ ),
132
+ proximity,
133
+ BiquadFilter.from_bands(sample_rate, cfg.eq, name="eq", enabled=cfg.eq_enabled and bool(cfg.eq)),
134
+ DeEsser(sample_rate, d.freq_hz, d.threshold_db, d.ratio, enabled=d.enabled),
135
+ Leveler(sample_rate, lv.target_db, lv.max_gain_db, lv.max_cut_db, lv.gate_db, enabled=lv.enabled),
136
+ Compressor(
137
+ sample_rate, c.threshold_db, c.ratio, c.attack_ms, c.release_ms, c.knee_db, c.makeup_db,
138
+ enabled=c.enabled,
139
+ ),
140
+ Limiter(sample_rate, li.ceiling_db, li.release_ms, enabled=li.enabled),
141
+ ]
142
+ return Chain(stages, cfg.input_gain_db, cfg.output_gain_db)
143
+
144
+
145
+ def process_offline(chain: Chain, x: np.ndarray, block: int = 480) -> np.ndarray:
146
+ """Run a whole array through the chain block-by-block, compensating latency."""
147
+ x = np.asarray(x, dtype=np.float32)
148
+ lat = chain.latency_samples
149
+ padded = np.concatenate((x, np.zeros(lat, dtype=np.float32)))
150
+ out = np.empty_like(padded)
151
+ for i in range(0, padded.shape[0], block):
152
+ out[i : i + block] = chain.process(padded[i : i + block])
153
+ return out[lat:]
@@ -0,0 +1,427 @@
1
+ """CoFi-Lite neural speech enhancement denoiser (ONNX + swappable models).
2
+
3
+ Ultra-lightweight causal speech enhancement (~83k params, 12.87M MACs/s)
4
+ designed for low-end devices and edge deployment.
5
+ Operates on 16 kHz audio with stateful polyphase resampling for 48/44.1 kHz,
6
+ keeping process RAM under 100 MB and DSP load around 10-15%.
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import logging
11
+ import math
12
+ import os
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ import numpy as np
17
+
18
+ from .base import Processor
19
+ from .util import EPS, db2lin, rms_db
20
+
21
+ log = logging.getLogger("micforge.cofilite")
22
+
23
+ _MODEL_NAME = "cofilite.onnx"
24
+ _SESSION_CACHE: dict[str, Any] = {}
25
+
26
+
27
+ def find_model_file(custom_path: str | Path | None = None) -> Path | None:
28
+ """Find a usable CoFi-Lite model file (.onnx or .tar/.pth)."""
29
+ if custom_path is not None:
30
+ p = Path(custom_path).expanduser().resolve()
31
+ if p.is_file():
32
+ return p
33
+ log.warning("Specified model file not found: %s", custom_path)
34
+
35
+ # Check user config directory: %APPDATA%\micforge\models or ~/.config/micforge/models
36
+ try:
37
+ from ..config import config_dir
38
+ user_dir = config_dir() / "models"
39
+ for name in (_MODEL_NAME, "cofilite.tar", "model_on_dns3.tar", "cofilite.pth"):
40
+ cand = user_dir / name
41
+ if cand.is_file():
42
+ return cand
43
+ except Exception:
44
+ pass
45
+
46
+ # Check package bundled models: src/micforge/models/cofilite.onnx
47
+ pkg_model = Path(__file__).parent.parent / "models" / _MODEL_NAME
48
+ if pkg_model.is_file():
49
+ return pkg_model
50
+
51
+ # Check external repo if cloned in workspace
52
+ ext_model = Path(__file__).parent.parent.parent.parent / "external" / "cofilite_core.onnx"
53
+ if ext_model.is_file():
54
+ return ext_model
55
+
56
+ return None
57
+
58
+
59
+ class CoFiLiteDenoiser(Processor):
60
+ name = "denoise"
61
+
62
+ def __init__(
63
+ self,
64
+ sample_rate: int,
65
+ strength: float = 1.0,
66
+ max_reduction_db: float = 24.0,
67
+ model_path: str | Path | None = None,
68
+ enabled: bool = True,
69
+ ) -> None:
70
+ super().__init__(sample_rate, enabled)
71
+ self.model_sr = 16000
72
+ self.n_fft = 512
73
+ self.hop = 256
74
+ self.n_bins = self.n_fft // 2 + 1
75
+ self.max_reduction_db = float(max_reduction_db)
76
+ self.strength = float(min(1.0, max(0.0, strength)))
77
+ self.hist_frames = 12 # ~192ms temporal context; balance of accuracy and CPU
78
+
79
+ # Square-root Hanning analysis and synthesis window
80
+ self.win = np.hanning(self.n_fft).astype(np.float32) ** 0.5
81
+ self._scale = float(self.hop / np.sum(self.win**2))
82
+
83
+ # Design stateful polyphase FIR anti-aliasing filters for 48k <-> 16k resampling
84
+ if self.sample_rate == 48000:
85
+ self._resample_ratio = 3
86
+ # Lowpass FIR filter with cutoff at pi / 3 (pure numpy sinc with hamming window)
87
+ n = np.arange(31) - 15.0
88
+ h = (1.0 / 3.0) * np.sinc((1.0 / 3.0) * n)
89
+ w = 0.54 - 0.46 * np.cos(2.0 * np.pi * np.arange(31) / 30.0)
90
+ hw = h * w
91
+ self._fir_h = (hw / np.sum(hw)).astype(np.float32)
92
+ self._fir_h_up = (3.0 * self._fir_h).astype(np.float32)
93
+ self._ext_down = np.zeros(0, dtype=np.float32)
94
+ self._ext_up = np.zeros(0, dtype=np.float32)
95
+ else:
96
+ self._resample_ratio = 1
97
+ self._fir_h = None
98
+ self._fir_h_up = None
99
+ self._ext_down = np.zeros(0, dtype=np.float32)
100
+ self._ext_up = np.zeros(0, dtype=np.float32)
101
+
102
+ self._model_path: Path | None = None
103
+ self._session: Any = None
104
+ self._profile: np.ndarray | None = None
105
+ self._reduction_db = 0.0
106
+ self._silence_blocks = 0
107
+ self._is_napping = False
108
+ self._noise_floor_est = -65.0
109
+
110
+ # Smart Nap State: saves ~99% CPU during speech pauses / silence
111
+ self.nap_enabled = True
112
+ self.nap_after_sec = 5.0
113
+ self.speech_threshold_db = -45.0
114
+ self._silence_blocks = 0
115
+ self._nap_blocks_limit = max(50, int(self.nap_after_sec * (self.sample_rate / 480)))
116
+ self._is_napping = False
117
+
118
+ resolved = find_model_file(model_path)
119
+ if resolved is not None:
120
+ self.load_model(resolved)
121
+ else:
122
+ log.warning("No CoFi-Lite model file found. Run will operate in pass-through mode.")
123
+
124
+ self.reset()
125
+
126
+ def load_model(self, model_file: str | Path) -> bool:
127
+ """Load an ONNX or PyTorch model file."""
128
+ p = Path(model_file).expanduser().resolve()
129
+ if not p.is_file():
130
+ log.error("Model file does not exist: %s", p)
131
+ return False
132
+
133
+ if p.suffix.lower() == ".onnx":
134
+ cache_key = str(p)
135
+ if cache_key in _SESSION_CACHE:
136
+ self._session = _SESSION_CACHE[cache_key]
137
+ self._model_path = p
138
+ log.debug("Reused cached CoFi-Lite ONNX session: %s", p.name)
139
+ return True
140
+ try:
141
+ import onnxruntime as ort
142
+
143
+ opts = ort.SessionOptions()
144
+ opts.intra_op_num_threads = 1
145
+ opts.inter_op_num_threads = 1
146
+ opts.enable_cpu_mem_arena = False
147
+ opts.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
148
+ opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
149
+ session = ort.InferenceSession(str(p), opts, providers=["CPUExecutionProvider"])
150
+ _SESSION_CACHE[cache_key] = session
151
+ self._session = session
152
+ self._model_path = p
153
+ log.info("Loaded CoFi-Lite ONNX model: %s", p.name)
154
+ return True
155
+ except Exception as exc:
156
+ log.exception("Failed to load ONNX model %s: %s", p, exc)
157
+ return False
158
+ elif p.suffix.lower() in (".tar", ".pth", ".pt"):
159
+ # If given a PyTorch checkpoint, attempt to convert to ONNX and cache it
160
+ try:
161
+ from ..config import config_dir
162
+ cache_dir = config_dir() / "models"
163
+ cache_dir.mkdir(parents=True, exist_ok=True)
164
+ cached_onnx = cache_dir / f"{p.stem}.onnx"
165
+ if not cached_onnx.exists():
166
+ log.info("Converting PyTorch checkpoint %s to ONNX: %s", p.name, cached_onnx.name)
167
+ from .export_cofilite import export_checkpoint_to_onnx
168
+ export_checkpoint_to_onnx(p, cached_onnx)
169
+ return self.load_model(cached_onnx)
170
+ except Exception as exc:
171
+ log.exception("Could not convert/load PyTorch checkpoint %s: %s", p, exc)
172
+ return False
173
+ else:
174
+ log.error("Unsupported model format: %s", p.suffix)
175
+ return False
176
+
177
+ @property
178
+ def model_name(self) -> str:
179
+ return self._model_path.name if self._model_path else "none"
180
+
181
+ @property
182
+ def latency_samples(self) -> int:
183
+ """Algorithmic delay in output sample rate samples.
184
+
185
+ Exact group delay of CoFi-Lite STFT overlap-add plus polyphase FIR resampling:
186
+ 490 samples @ 16 kHz = 30.625 ms (1470 samples @ 48 kHz).
187
+ """
188
+ if self.sample_rate == 48000:
189
+ return 1470
190
+ return int(490 * (self.sample_rate / self.model_sr))
191
+
192
+ def set_strength(self, strength: float) -> None:
193
+ self.strength = float(min(1.0, max(0.0, strength)))
194
+
195
+ def reset(self) -> None:
196
+ """Clear streaming buffers and state."""
197
+ self._in_buf = np.zeros(4096, dtype=np.float32)
198
+ self._in_len = 0
199
+ self._out_buf = np.zeros(4096, dtype=np.float32)
200
+ self._out_len = 0
201
+ self._ola = np.zeros(self.n_fft, dtype=np.float32)
202
+ self._inp_tensor = np.zeros((1, 2, self.hist_frames, self.n_bins), dtype=np.float32)
203
+ self._curr_hist = 0
204
+ self._up_zero = np.zeros(480, dtype=np.float32)
205
+ # Scratch buffers for safe, non-overlapping shift operations
206
+ self._shift_scratch = np.zeros(4096, dtype=np.float32)
207
+
208
+ # Polyphase filter states
209
+ if self._fir_h is not None:
210
+ self._zi_down = np.zeros(len(self._fir_h) - 1, dtype=np.float32)
211
+ self._zi_up = np.zeros(len(self._fir_h) - 1, dtype=np.float32)
212
+ else:
213
+ self._zi_down = None
214
+ self._zi_up = None
215
+
216
+ self._reduction_db = 0.0
217
+ self._silence_blocks = 0
218
+ self._is_napping = False
219
+
220
+ def learn(self, noise: np.ndarray) -> np.ndarray:
221
+ """Measure room noise floor and spectral profile."""
222
+ x = np.asarray(noise, dtype=np.float32)
223
+ if self.sample_rate != self.model_sr and self.sample_rate == 48000:
224
+ if self._fir_h is not None:
225
+ x_16k = np.convolve(x, self._fir_h, mode="same")[::3].astype(np.float32)
226
+ else:
227
+ x_16k = x[::3]
228
+ elif self.sample_rate != self.model_sr:
229
+ import scipy.signal as sig
230
+ x_16k = sig.resample_poly(x, 160, self.sample_rate // 100).astype(np.float32)
231
+ else:
232
+ x_16k = x
233
+ self._noise_floor_est = rms_db(x_16k)
234
+ # Compute average PSD
235
+ frames = np.lib.stride_tricks.sliding_window_view(x_16k, self.n_fft)[:: self.hop]
236
+ spec = np.fft.rfft(frames * self.win, axis=1)
237
+ psd = np.mean(spec.real**2 + spec.imag**2, axis=0)
238
+ self._profile = psd
239
+ return psd
240
+
241
+ def set_profile(self, psd: np.ndarray | None) -> bool:
242
+ if psd is None or psd.shape != (self.n_bins,):
243
+ return False
244
+ self._profile = np.asarray(psd, dtype=np.float32)
245
+ return True
246
+
247
+ @property
248
+ def profile(self) -> np.ndarray | None:
249
+ return self._profile
250
+
251
+ def noise_floor_db(self) -> float:
252
+ return self._noise_floor_est
253
+
254
+ # ---------------------------------------------------------------- process
255
+ def process(self, x: np.ndarray) -> np.ndarray:
256
+ if not self.enabled or self._session is None or x.size == 0:
257
+ return x
258
+
259
+ n_in = x.shape[0]
260
+
261
+ # Activity check for smart idle nap state
262
+ x_energy = float(np.mean(x * x))
263
+ x_db = 10.0 * math.log10(max(x_energy, 1e-12)) if x_energy > 0 else -120.0
264
+
265
+ if self.nap_enabled:
266
+ if x_db < self.speech_threshold_db:
267
+ self._silence_blocks += 1
268
+ if self._silence_blocks >= self._nap_blocks_limit:
269
+ self._is_napping = True
270
+ else:
271
+ self._silence_blocks = 0
272
+ self._is_napping = False
273
+
274
+ # 1. Downsample to 16 kHz
275
+ if self.sample_rate == 48000:
276
+ # Fast stateful decimation 3:1 with zero boundary clicks
277
+ ext_len = len(self._zi_down) + len(x)
278
+ if self._ext_down.shape[0] != ext_len:
279
+ self._ext_down = np.empty(ext_len, dtype=np.float32)
280
+ self._ext_down[: len(self._zi_down)] = self._zi_down
281
+ self._ext_down[len(self._zi_down) :] = x
282
+ down = np.convolve(self._ext_down, self._fir_h, mode="valid")[: len(x)]
283
+ self._zi_down[:] = self._ext_down[-len(self._zi_down) :]
284
+ x_16k = down[::3].astype(np.float32, copy=False)
285
+ elif self.sample_rate == 16000:
286
+ x_16k = x.astype(np.float32, copy=False)
287
+ else:
288
+ import scipy.signal as sig
289
+ x_16k = sig.resample_poly(x, 160, self.sample_rate // 100).astype(np.float32)
290
+
291
+ n_16k = x_16k.shape[0]
292
+
293
+ # Ensure input buffer capacity
294
+ if self._in_len + n_16k > self._in_buf.shape[0]:
295
+ new_in = np.zeros(max(self._in_buf.shape[0] * 2, self._in_len + n_16k + 1024), dtype=np.float32)
296
+ new_in[: self._in_len] = self._in_buf[: self._in_len]
297
+ self._in_buf = new_in
298
+
299
+ self._in_buf[self._in_len : self._in_len + n_16k] = x_16k
300
+ self._in_len += n_16k
301
+
302
+ # 2. Process STFT frames
303
+ min_gain = db2lin(-self.max_reduction_db)
304
+ while self._in_len >= self.n_fft:
305
+ frame = self._in_buf[: self.n_fft].copy() # copy so shift doesn't corrupt it
306
+
307
+ # Safe non-overlapping shift: copy to scratch first, then back
308
+ remain = self._in_len - self.hop
309
+ if self._shift_scratch.shape[0] < remain:
310
+ self._shift_scratch = np.zeros(remain + 1024, dtype=np.float32)
311
+ self._shift_scratch[:remain] = self._in_buf[self.hop : self._in_len]
312
+ self._in_buf[:remain] = self._shift_scratch[:remain]
313
+ self._in_len = remain
314
+
315
+ spec = np.fft.rfft(frame * self.win, n=self.n_fft)
316
+
317
+ # Update temporal context tensor in-place (avoids np.stack allocation)
318
+ if self._curr_hist < self.hist_frames:
319
+ self._inp_tensor[0, 0, self._curr_hist, :] = spec.real
320
+ self._inp_tensor[0, 1, self._curr_hist, :] = spec.imag
321
+ self._curr_hist += 1
322
+ else:
323
+ self._inp_tensor[0, :, :-1, :] = self._inp_tensor[0, :, 1:, :]
324
+ self._inp_tensor[0, 0, -1, :] = spec.real
325
+ self._inp_tensor[0, 1, -1, :] = spec.imag
326
+
327
+ # Smart Nap State: maintain temporal history but skip expensive ONNX inference!
328
+ if self._is_napping:
329
+ self._reduction_db = 0.9 * self._reduction_db + 0.1 * 40.0
330
+ if self._out_len + self.hop > self._out_buf.shape[0]:
331
+ new_out = np.zeros(max(self._out_buf.shape[0] * 2, self._out_len + self.hop + 1024), dtype=np.float32)
332
+ new_out[: self._out_len] = self._out_buf[: self._out_len]
333
+ self._out_buf = new_out
334
+ self._out_buf[self._out_len : self._out_len + self.hop] = 0.0
335
+ self._out_len += self.hop
336
+ self._ola.fill(0.0)
337
+ continue
338
+
339
+ inp = self._inp_tensor[:, :, : self._curr_hist, :]
340
+
341
+ # ONNX inference for current frame
342
+ enh = self._session.run(None, {"input": inp})[0] # (1, F, T, 2)
343
+ curr_enh = enh[0, :, -1, :] # (F, 2)
344
+
345
+ dry_mag = np.hypot(spec.real, spec.imag) + EPS
346
+ enh_mag = np.hypot(curr_enh[:, 0], curr_enh[:, 1])
347
+
348
+ # Direct phase-aware complex output:
349
+ # At strength >= 0.95 (and by default in neural mode), emit pure clean neural speech
350
+ # without leaking raw microphone background hiss or pressure-cooker noise.
351
+ # Below 0.95, smoothly blend with raw audio using a quadratic taper.
352
+ blend = max(0.0, min(1.0, self.strength))
353
+ if blend >= 0.95:
354
+ final_real = curr_enh[:, 0]
355
+ final_imag = curr_enh[:, 1]
356
+ else:
357
+ alpha = blend ** 2 # quadratic curve avoids linear ambient noise bleed
358
+ final_real = (1.0 - alpha) * spec.real + alpha * curr_enh[:, 0]
359
+ final_imag = (1.0 - alpha) * spec.imag + alpha * curr_enh[:, 1]
360
+
361
+ # Telemetry tracking
362
+ dry_p = np.sum(dry_mag**2) + EPS
363
+ enh_p = np.sum(final_real**2 + final_imag**2) + EPS
364
+ red = float(np.clip(10.0 * np.log10(dry_p / enh_p), 0.0, 40.0))
365
+ self._reduction_db = 0.9 * self._reduction_db + 0.1 * red
366
+
367
+ c_spec = final_real + 1j * final_imag
368
+ out_frame = np.fft.irfft(c_spec, n=self.n_fft).real * self.win * self._scale
369
+
370
+ self._ola += out_frame
371
+
372
+ # Ensure output buffer capacity
373
+ if self._out_len + self.hop > self._out_buf.shape[0]:
374
+ new_out = np.zeros(max(self._out_buf.shape[0] * 2, self._out_len + self.hop + 1024), dtype=np.float32)
375
+ new_out[: self._out_len] = self._out_buf[: self._out_len]
376
+ self._out_buf = new_out
377
+
378
+ self._out_buf[self._out_len : self._out_len + self.hop] = self._ola[: self.hop]
379
+ self._out_len += self.hop
380
+
381
+ # Shift OLA buffer: non-overlapping since hop < n_fft and direction is left
382
+ self._ola[: self.n_fft - self.hop] = self._ola[self.hop :].copy()
383
+ self._ola[self.n_fft - self.hop :] = 0.0
384
+
385
+ # 3. Pull required samples at 16k
386
+ if self._out_len >= n_16k:
387
+ chunk_16k = self._out_buf[:n_16k].copy()
388
+ # Safe non-overlapping shift via scratch buffer
389
+ out_remain = self._out_len - n_16k
390
+ if out_remain > 0:
391
+ if self._shift_scratch.shape[0] < out_remain:
392
+ self._shift_scratch = np.zeros(out_remain + 1024, dtype=np.float32)
393
+ self._shift_scratch[:out_remain] = self._out_buf[n_16k : self._out_len]
394
+ self._out_buf[:out_remain] = self._shift_scratch[:out_remain]
395
+ self._out_len = out_remain
396
+ else:
397
+ # Underflow guard (e.g. at startup priming)
398
+ chunk_16k = np.zeros(n_16k, dtype=np.float32)
399
+
400
+ # 4. Upsample back to target sample rate
401
+ if self.sample_rate == 48000:
402
+ if self._up_zero.shape[0] != n_16k * 3:
403
+ self._up_zero = np.zeros(n_16k * 3, dtype=np.float32)
404
+ else:
405
+ self._up_zero.fill(0)
406
+ self._up_zero[::3] = chunk_16k
407
+ ext_up_len = len(self._zi_up) + len(self._up_zero)
408
+ if self._ext_up.shape[0] != ext_up_len:
409
+ self._ext_up = np.empty(ext_up_len, dtype=np.float32)
410
+ self._ext_up[: len(self._zi_up)] = self._zi_up
411
+ self._ext_up[len(self._zi_up) :] = self._up_zero
412
+ out_48k = np.convolve(self._ext_up, self._fir_h_up, mode="valid")[: len(self._up_zero)]
413
+ self._zi_up[:] = self._ext_up[-len(self._zi_up) :]
414
+ return out_48k[:n_in].astype(np.float32, copy=False)
415
+ elif self.sample_rate == 16000:
416
+ return chunk_16k[:n_in]
417
+ else:
418
+ import scipy.signal as sig
419
+ out_target = sig.resample_poly(chunk_16k, self.sample_rate // 100, 160).astype(np.float32)
420
+ return out_target[:n_in]
421
+
422
+ def telemetry(self) -> dict[str, float]:
423
+ return {
424
+ "reduction_db": self._reduction_db,
425
+ "noise_floor_db": self._noise_floor_est,
426
+ "napping": 1.0 if self._is_napping else 0.0,
427
+ }
@@ -0,0 +1,109 @@
1
+ """Feed-forward compressor with soft knee, and a safety limiter."""
2
+ from __future__ import annotations
3
+
4
+ import numpy as np
5
+
6
+ from .base import Processor
7
+ from .util import block_peak, block_rms, db2lin_a, expand_gain, lin2db_a, time_const_coeff
8
+
9
+
10
+ class Compressor(Processor):
11
+ name = "compressor"
12
+
13
+ def __init__(
14
+ self,
15
+ sample_rate: int,
16
+ threshold_db: float = -22.0,
17
+ ratio: float = 3.0,
18
+ attack_ms: float = 6.0,
19
+ release_ms: float = 90.0,
20
+ knee_db: float = 6.0,
21
+ makeup_db: float = 3.0,
22
+ enabled: bool = True,
23
+ ) -> None:
24
+ super().__init__(sample_rate, enabled)
25
+ self.threshold_db = threshold_db
26
+ self.ratio = max(1.0, ratio)
27
+ self.attack_ms = attack_ms
28
+ self.release_ms = release_ms
29
+ self.knee_db = knee_db
30
+ self.makeup_db = makeup_db
31
+ self._size = max(8, int(sample_rate * 0.001))
32
+ self.reset()
33
+
34
+ def reset(self) -> None:
35
+ self._gr = 0.0 # smoothed gain change in dB (<= 0)
36
+ self._prev_gain = 1.0
37
+ self._last_min = 0.0
38
+
39
+ def process(self, x: np.ndarray) -> np.ndarray:
40
+ n = x.shape[0]
41
+ if n == 0:
42
+ return x
43
+ size = self._size
44
+ over = lin2db_a(block_rms(x, size)) - self.threshold_db
45
+ slope = 1.0 / self.ratio - 1.0
46
+ k = self.knee_db
47
+ if k > 0:
48
+ soft = slope * np.square(over + k / 2.0) / (2.0 * k)
49
+ target = np.where(over <= -k / 2.0, 0.0, np.where(over >= k / 2.0, slope * over, soft))
50
+ else:
51
+ target = np.where(over > 0.0, slope * over, 0.0)
52
+
53
+ rate = self.sample_rate / size
54
+ a_att = time_const_coeff(self.attack_ms, rate)
55
+ a_rel = time_const_coeff(self.release_ms, rate)
56
+ g = self._gr
57
+ smoothed = np.empty_like(target)
58
+ for i, t in enumerate(target):
59
+ coef = a_att if t < g else a_rel
60
+ g = t + coef * (g - t)
61
+ smoothed[i] = g
62
+ self._gr = float(g)
63
+ self._last_min = float(smoothed.min())
64
+
65
+ gains = db2lin_a(smoothed + self.makeup_db)
66
+ gain = expand_gain(self._prev_gain, gains, n, size)
67
+ self._prev_gain = float(gains[-1])
68
+ return x * gain
69
+
70
+ def telemetry(self) -> dict[str, float]:
71
+ return {"gain_reduction_db": -self._last_min}
72
+
73
+
74
+ class Limiter(Processor):
75
+ """Brick-wall-ish peak limiter: instant attack, smoothed release, final clip."""
76
+
77
+ name = "limiter"
78
+
79
+ def __init__(self, sample_rate: int, ceiling_db: float = -1.5, release_ms: float = 60.0, enabled: bool = True):
80
+ super().__init__(sample_rate, enabled)
81
+ self.ceiling = float(10.0 ** (ceiling_db / 20.0))
82
+ self.release_ms = release_ms
83
+ self._size = max(4, int(sample_rate * 0.0005))
84
+ self.reset()
85
+
86
+ def reset(self) -> None:
87
+ self._g = 1.0
88
+ self._last_min = 1.0
89
+
90
+ def process(self, x: np.ndarray) -> np.ndarray:
91
+ n = x.shape[0]
92
+ if n == 0:
93
+ return x
94
+ size = self._size
95
+ peaks = block_peak(x, size)
96
+ wanted = np.minimum(1.0, self.ceiling / np.maximum(peaks, 1e-9))
97
+ a_rel = time_const_coeff(self.release_ms, self.sample_rate / size)
98
+ g = self._g
99
+ gains = np.empty_like(wanted)
100
+ for i, w in enumerate(wanted):
101
+ g = w if w < g else w + a_rel * (g - w)
102
+ gains[i] = g
103
+ self._g = float(g)
104
+ self._last_min = float(gains.min())
105
+ y = x * np.repeat(gains, size)[:n].astype(np.float32)
106
+ return np.clip(y, -self.ceiling, self.ceiling)
107
+
108
+ def telemetry(self) -> dict[str, float]:
109
+ return {"gain_reduction_db": -20.0 * np.log10(max(self._last_min, 1e-6))}