Spaces:
Paused
Paused
Update denoiser.py
Browse files- denoiser.py +11 -3
denoiser.py
CHANGED
|
@@ -103,7 +103,14 @@ class Denoiser:
|
|
| 103 |
# Applied BEFORE denoising for best results
|
| 104 |
# Cuts electrical hum, fan/AC vibration, traffic rumble
|
| 105 |
# Voice clarity is at 300-3400Hz β not affected at all
|
| 106 |
-
mono = self._highpass_filter(mono, sr, cutoff_hz=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# ββ 3. Background Noise Removal ββββββββββββββββββββββββββββββ
|
| 109 |
mono, noise_method = self._remove_background_noise(mono, sr)
|
|
@@ -205,7 +212,7 @@ class Denoiser:
|
|
| 205 |
# HIGH-PASS FILTER β removes low-frequency hum/rumble
|
| 206 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 207 |
def _highpass_filter(self, audio: np.ndarray, sr: int,
|
| 208 |
-
cutoff_hz: float =
|
| 209 |
"""
|
| 210 |
High-pass filter to remove low-frequency noise:
|
| 211 |
- Electrical hum (50/60 Hz and harmonics at 100-300 Hz)
|
|
@@ -574,7 +581,8 @@ class Denoiser:
|
|
| 574 |
if rms > 1e-9:
|
| 575 |
target_rms = 10 ** (TARGET_LOUDNESS / 20.0) # β 0.126
|
| 576 |
audio = audio * (target_rms / rms) # correct ratio
|
| 577 |
-
|
|
|
|
| 578 |
|
| 579 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 580 |
# HELPERS
|
|
|
|
| 103 |
# Applied BEFORE denoising for best results
|
| 104 |
# Cuts electrical hum, fan/AC vibration, traffic rumble
|
| 105 |
# Voice clarity is at 300-3400Hz β not affected at all
|
| 106 |
+
mono = self._highpass_filter(mono, sr, cutoff_hz=100)
|
| 107 |
+
|
| 108 |
+
# ββ 2b. Clip protection β normalize if signal is clipping ββ
|
| 109 |
+
# Clipping causes distortion and ruins all downstream steps
|
| 110 |
+
peak = np.max(np.abs(mono))
|
| 111 |
+
if peak > 0.95:
|
| 112 |
+
mono = mono * (0.95 / peak)
|
| 113 |
+
print(f"[Denoiser] β οΈ Clipping detected (peak={peak:.3f}) β normalized to 0.95")
|
| 114 |
|
| 115 |
# ββ 3. Background Noise Removal ββββββββββββββββββββββββββββββ
|
| 116 |
mono, noise_method = self._remove_background_noise(mono, sr)
|
|
|
|
| 212 |
# HIGH-PASS FILTER β removes low-frequency hum/rumble
|
| 213 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 214 |
def _highpass_filter(self, audio: np.ndarray, sr: int,
|
| 215 |
+
cutoff_hz: float = 100) -> np.ndarray:
|
| 216 |
"""
|
| 217 |
High-pass filter to remove low-frequency noise:
|
| 218 |
- Electrical hum (50/60 Hz and harmonics at 100-300 Hz)
|
|
|
|
| 581 |
if rms > 1e-9:
|
| 582 |
target_rms = 10 ** (TARGET_LOUDNESS / 20.0) # β 0.126
|
| 583 |
audio = audio * (target_rms / rms) # correct ratio
|
| 584 |
+
# Clip to safe range β never let signal exceed 0.99
|
| 585 |
+
return np.clip(audio, -0.99, 0.99).astype(np.float32)
|
| 586 |
|
| 587 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 588 |
# HELPERS
|