testingfaces commited on
Commit
45eae2c
Β·
verified Β·
1 Parent(s): db2a40c

Update denoiser.py

Browse files
Files changed (1) hide show
  1. 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=200)
 
 
 
 
 
 
 
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 = 200) -> np.ndarray:
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
- return np.clip(audio, -1.0, 1.0).astype(np.float32)
 
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