Spaces:
Running
Running
Update denoiser.py
Browse files- denoiser.py +376 -188
denoiser.py
CHANGED
|
@@ -1,49 +1,63 @@
|
|
| 1 |
"""
|
| 2 |
-
Department 1 β Professional Audio Enhancer
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
β
Background noise removal β
|
| 6 |
-
|
| 7 |
-
β
|
| 8 |
-
β
|
|
|
|
| 9 |
β
Breath sound reduction β Spectral gating (noisereduce non-stationary)
|
| 10 |
-
β
Mouth sound reduction β Amplitude
|
| 11 |
-
β
Room tone fill β
|
| 12 |
β
Audio normalization β pyloudnorm -18 LUFS
|
| 13 |
-
β
CD quality output β
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
|
| 27 |
import os
|
| 28 |
import re
|
| 29 |
import time
|
| 30 |
import subprocess
|
| 31 |
-
import tempfile
|
| 32 |
import numpy as np
|
| 33 |
import soundfile as sf
|
| 34 |
import logging
|
| 35 |
|
| 36 |
logger = logging.getLogger(__name__)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
# DeepFilterNet is now properly installed via Dockerfile (no more Rust compiler issue)
|
| 40 |
-
TARGET_SR = 48000
|
| 41 |
TARGET_LOUDNESS = -18.0
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# Filler words (English + Telugu + Hindi)
|
| 44 |
FILLER_WORDS = {
|
| 45 |
"um", "umm", "ummm", "uh", "uhh", "uhhh",
|
| 46 |
-
"hmm", "hm", "
|
| 47 |
"er", "err", "errr",
|
| 48 |
"eh", "ahh", "ah",
|
| 49 |
"like", "basically", "literally",
|
|
@@ -55,14 +69,18 @@ FILLER_WORDS = {
|
|
| 55 |
"matlab", "yani", "bas", "acha",
|
| 56 |
}
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
class Denoiser:
|
| 60 |
def __init__(self):
|
| 61 |
-
self.
|
| 62 |
-
|
| 63 |
-
self._df_loaded = False
|
| 64 |
-
self._room_tone = None # captured room noise sample
|
| 65 |
-
print("[Denoiser] β
Professional Audio Enhancer ready")
|
| 66 |
|
| 67 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
# MAIN ENTRY POINT
|
|
@@ -76,13 +94,21 @@ class Denoiser:
|
|
| 76 |
word_segments: list = None) -> dict:
|
| 77 |
"""
|
| 78 |
Full professional pipeline.
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
Returns: {'audio_path': str, 'stats': dict}
|
| 82 |
"""
|
| 83 |
t0 = time.time()
|
| 84 |
stats = {}
|
| 85 |
-
print("[Denoiser] βΆ Starting professional enhancement pipeline...")
|
| 86 |
|
| 87 |
# ββ 0. Convert to standard WAV βββββββββββββββββββββββββββββββ
|
| 88 |
wav_in = os.path.join(out_dir, "stage0_input.wav")
|
|
@@ -95,7 +121,7 @@ class Denoiser:
|
|
| 95 |
# Work in mono float32
|
| 96 |
mono = audio.mean(axis=1).astype(np.float32)
|
| 97 |
|
| 98 |
-
# ββ 1. Capture room tone BEFORE denoising ββββββββββββββββ
|
| 99 |
self._room_tone = self._capture_room_tone(mono, sr)
|
| 100 |
|
| 101 |
# ββ 2. Background Noise Removal ββββββββββββββββββββββββββββββ
|
|
@@ -112,13 +138,13 @@ class Denoiser:
|
|
| 112 |
mono = self._reduce_breaths(mono, sr)
|
| 113 |
stats['breaths_reduced'] = True
|
| 114 |
|
| 115 |
-
# ββ 5. Filler Word Removal
|
| 116 |
stats['fillers_removed'] = 0
|
| 117 |
if remove_fillers and word_segments:
|
| 118 |
mono, n_fillers = self._remove_fillers(mono, sr, word_segments)
|
| 119 |
stats['fillers_removed'] = n_fillers
|
| 120 |
|
| 121 |
-
# ββ 6. Stutter Removal
|
| 122 |
stats['stutters_removed'] = 0
|
| 123 |
if remove_stutters and word_segments:
|
| 124 |
mono, n_stutters = self._remove_stutters(mono, sr, word_segments)
|
|
@@ -147,29 +173,23 @@ class Denoiser:
|
|
| 147 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 148 |
def _capture_room_tone(self, audio: np.ndarray, sr: int,
|
| 149 |
sample_sec: float = 0.5) -> np.ndarray:
|
| 150 |
-
"""
|
| 151 |
-
Find the quietest 0.5s section of audio = room tone.
|
| 152 |
-
FIX: Falls back to first 100ms if audio is too short.
|
| 153 |
-
"""
|
| 154 |
try:
|
| 155 |
frame = int(sr * sample_sec)
|
| 156 |
|
| 157 |
-
# FIX: Robust fallback for short audio
|
| 158 |
if len(audio) < frame * 2:
|
| 159 |
-
fallback_len = min(int(sr * 0.1), len(audio))
|
| 160 |
print("[Denoiser] Short audio β using first 100ms as room tone")
|
| 161 |
return audio[:fallback_len].copy().astype(np.float32)
|
| 162 |
|
| 163 |
best_rms = float('inf')
|
| 164 |
best_start = 0
|
|
|
|
| 165 |
|
| 166 |
-
step = sr
|
| 167 |
for i in range(0, len(audio) - frame, step):
|
| 168 |
-
|
| 169 |
-
rms = float(np.sqrt(np.mean(chunk ** 2)))
|
| 170 |
if rms < best_rms:
|
| 171 |
-
best_rms
|
| 172 |
-
best_start = i
|
| 173 |
|
| 174 |
room = audio[best_start: best_start + frame].copy()
|
| 175 |
print(f"[Denoiser] Room tone captured: RMS={best_rms:.5f}")
|
|
@@ -182,20 +202,85 @@ class Denoiser:
|
|
| 182 |
"""Tile room tone to fill a gap of `length` samples."""
|
| 183 |
if self._room_tone is None or len(self._room_tone) == 0:
|
| 184 |
return np.zeros(length, dtype=np.float32)
|
| 185 |
-
reps
|
| 186 |
-
tiled
|
| 187 |
-
|
| 188 |
-
fade = min(int(0.01 * len(tiled)), 64)
|
| 189 |
if fade > 0:
|
| 190 |
tiled[:fade] *= np.linspace(0, 1, fade)
|
| 191 |
tiled[-fade:] *= np.linspace(1, 0, fade)
|
| 192 |
return tiled.astype(np.float32)
|
| 193 |
|
| 194 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 195 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 197 |
def _remove_background_noise(self, audio, sr):
|
| 198 |
-
#
|
| 199 |
try:
|
| 200 |
result = self._deepfilter(audio, sr)
|
| 201 |
print("[Denoiser] β
DeepFilterNet noise removal done")
|
|
@@ -203,107 +288,151 @@ class Denoiser:
|
|
| 203 |
except Exception as e:
|
| 204 |
logger.warning(f"[Denoiser] DeepFilterNet unavailable ({e})")
|
| 205 |
|
| 206 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
try:
|
| 208 |
import noisereduce as nr
|
| 209 |
-
|
| 210 |
y=audio, sr=sr,
|
| 211 |
stationary=True,
|
| 212 |
-
prop_decrease=0.
|
| 213 |
-
n_std_thresh_stationary=1.5, # FIX: more aggressive noise floor
|
| 214 |
).astype(np.float32)
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
except Exception as e:
|
| 218 |
logger.warning(f"noisereduce failed: {e}")
|
| 219 |
-
return audio, "none"
|
| 220 |
|
| 221 |
-
|
|
|
|
|
|
|
| 222 |
"""
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
One pass removes the main noise; second pass cleans the residual.
|
| 226 |
"""
|
| 227 |
-
|
| 228 |
-
from df.enhance import enhance, init_df
|
| 229 |
-
self._df_model, self._df_state, _ = init_df()
|
| 230 |
-
self._df_loaded = True
|
| 231 |
-
from df.enhance import enhance
|
| 232 |
import torch
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
#
|
| 243 |
-
|
|
|
|
| 244 |
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
return self._resample(res, df_sr, sr) if df_sr != sr else res
|
| 247 |
|
| 248 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 249 |
-
# FILLER WORD REMOVAL
|
| 250 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 251 |
-
def _remove_fillers(self, audio, sr, segments):
|
| 252 |
"""
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
"""
|
| 256 |
try:
|
| 257 |
cuts = []
|
| 258 |
for seg in segments:
|
| 259 |
word = seg.get('word', '').strip().lower()
|
| 260 |
word = re.sub(r'[^a-z\s]', '', word).strip()
|
| 261 |
-
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
if not cuts:
|
| 265 |
return audio, 0
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
keep_end = int(start * sr)
|
| 271 |
-
keep_sta = int(prev * sr)
|
| 272 |
-
if keep_sta < keep_end:
|
| 273 |
-
result.append(audio[keep_sta:keep_end])
|
| 274 |
-
gap_len = int((end - start) * sr)
|
| 275 |
-
if gap_len > 0:
|
| 276 |
-
result.append(self._fill_with_room_tone(gap_len))
|
| 277 |
-
prev = end
|
| 278 |
-
|
| 279 |
-
remain_start = int(prev * sr)
|
| 280 |
-
if remain_start < len(audio):
|
| 281 |
-
result.append(audio[remain_start:])
|
| 282 |
-
|
| 283 |
-
out = np.concatenate(result) if result else audio
|
| 284 |
-
print(f"[Denoiser] β
Removed {len(cuts)} filler words: {[c[2] for c in cuts[:5]]}")
|
| 285 |
-
return out.astype(np.float32), len(cuts)
|
| 286 |
except Exception as e:
|
| 287 |
logger.warning(f"Filler removal failed: {e}")
|
| 288 |
return audio, 0
|
| 289 |
|
| 290 |
def clean_transcript_fillers(self, transcript: str) -> str:
|
| 291 |
-
"""
|
| 292 |
-
FIX (NEW): Also remove filler words from the transcript TEXT,
|
| 293 |
-
so the displayed text matches the cleaned audio.
|
| 294 |
-
"""
|
| 295 |
words = transcript.split()
|
| 296 |
result = []
|
| 297 |
i = 0
|
| 298 |
while i < len(words):
|
| 299 |
-
|
| 300 |
-
# Check two-word fillers first ("you know", "i mean")
|
| 301 |
if i + 1 < len(words):
|
| 302 |
-
two =
|
| 303 |
if two in FILLER_WORDS:
|
| 304 |
i += 2
|
| 305 |
continue
|
| 306 |
-
if
|
| 307 |
i += 1
|
| 308 |
continue
|
| 309 |
result.append(words[i])
|
|
@@ -311,83 +440,111 @@ class Denoiser:
|
|
| 311 |
return " ".join(result)
|
| 312 |
|
| 313 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 314 |
-
# STUTTER REMOVAL
|
| 315 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 316 |
-
def _remove_stutters(self, audio, sr, segments):
|
| 317 |
"""
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
- While next word == current word, mark all but last as cuts
|
| 324 |
-
- Skip past all repeats in one go
|
| 325 |
"""
|
| 326 |
try:
|
| 327 |
if len(segments) < 2:
|
| 328 |
return audio, 0
|
| 329 |
|
|
|
|
|
|
|
|
|
|
| 330 |
cuts = []
|
| 331 |
stutters_found = 0
|
| 332 |
i = 0
|
| 333 |
|
| 334 |
while i < len(segments):
|
| 335 |
-
|
|
|
|
| 336 |
|
| 337 |
if not word:
|
| 338 |
i += 1
|
| 339 |
continue
|
| 340 |
|
| 341 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
j = i + 1
|
| 343 |
while j < len(segments):
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
stutters_found += 1
|
| 349 |
-
i = j
|
| 350 |
j += 1
|
| 351 |
else:
|
| 352 |
-
break
|
| 353 |
|
| 354 |
i += 1
|
| 355 |
|
| 356 |
if not cuts:
|
| 357 |
return audio, 0
|
| 358 |
|
| 359 |
-
|
| 360 |
-
result = []
|
| 361 |
-
prev = 0.0
|
| 362 |
-
for start, end in sorted(cuts, key=lambda x: x[0]):
|
| 363 |
-
keep_sta = int(prev * sr)
|
| 364 |
-
keep_end = int(start * sr)
|
| 365 |
-
if keep_sta < keep_end:
|
| 366 |
-
result.append(audio[keep_sta:keep_end])
|
| 367 |
-
gap_len = int((end - start) * sr)
|
| 368 |
-
if gap_len > 0:
|
| 369 |
-
result.append(self._fill_with_room_tone(gap_len))
|
| 370 |
-
prev = end
|
| 371 |
-
|
| 372 |
-
remain = int(prev * sr)
|
| 373 |
-
if remain < len(audio):
|
| 374 |
-
result.append(audio[remain:])
|
| 375 |
-
|
| 376 |
-
out = np.concatenate(result) if result else audio
|
| 377 |
print(f"[Denoiser] β
Removed {stutters_found} stutters")
|
| 378 |
-
return out
|
| 379 |
except Exception as e:
|
| 380 |
logger.warning(f"Stutter removal failed: {e}")
|
| 381 |
return audio, 0
|
| 382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 384 |
# BREATH REDUCTION
|
| 385 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 386 |
-
def _reduce_breaths(self, audio, sr):
|
| 387 |
-
"""
|
| 388 |
-
Breaths = short broadband bursts between speech.
|
| 389 |
-
Non-stationary spectral gating catches them well.
|
| 390 |
-
"""
|
| 391 |
try:
|
| 392 |
import noisereduce as nr
|
| 393 |
cleaned = nr.reduce_noise(
|
|
@@ -404,39 +561,33 @@ class Denoiser:
|
|
| 404 |
return audio
|
| 405 |
|
| 406 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 407 |
-
# MOUTH SOUND REDUCTION
|
| 408 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 409 |
-
def _reduce_mouth_sounds(self, audio, sr):
|
| 410 |
"""
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
real consonants like p, b, t which have similar transient energy.
|
| 414 |
"""
|
| 415 |
try:
|
| 416 |
result = audio.copy()
|
| 417 |
win = int(sr * 0.003) # 3ms window
|
| 418 |
hop = win // 2
|
| 419 |
-
rms_arr = [
|
|
|
|
|
|
|
|
|
|
| 420 |
|
| 421 |
-
|
| 422 |
-
rms_arr.append(float(np.sqrt(np.mean(audio[i:i+win]**2))))
|
| 423 |
-
|
| 424 |
-
if not rms_arr:
|
| 425 |
return audio, 0
|
| 426 |
|
| 427 |
-
|
| 428 |
-
mean_rms = float(np.mean(rms_arr))
|
| 429 |
-
std_rms = float(np.std(rms_arr))
|
| 430 |
-
# FIX: was 4.5 β too sensitive, removed real speech consonants
|
| 431 |
-
threshold = mean_rms + 6.0 * std_rms
|
| 432 |
n_removed = 0
|
| 433 |
|
| 434 |
for idx, rms in enumerate(rms_arr):
|
| 435 |
if rms > threshold:
|
| 436 |
start = idx * hop
|
| 437 |
end = min(start + win, len(result))
|
| 438 |
-
|
| 439 |
-
result[start:end] *= fade
|
| 440 |
n_removed += 1
|
| 441 |
|
| 442 |
if n_removed:
|
|
@@ -447,29 +598,50 @@ class Denoiser:
|
|
| 447 |
return audio, 0
|
| 448 |
|
| 449 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 450 |
-
# LONG SILENCE REMOVAL
|
| 451 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 452 |
-
def _remove_long_silences(self, audio, sr,
|
| 453 |
-
max_silence_sec=1.5,
|
| 454 |
-
keep_pause_sec=0.4):
|
| 455 |
"""
|
| 456 |
-
|
| 457 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
"""
|
| 459 |
try:
|
| 460 |
-
frame_len
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
max_sil_frames = int(max_silence_sec / 0.02)
|
| 462 |
keep_frames = int(keep_pause_sec / 0.02)
|
| 463 |
-
threshold = 0.008
|
| 464 |
|
| 465 |
kept = []
|
| 466 |
silence_count = 0
|
| 467 |
total_removed = 0
|
| 468 |
in_long_sil = False
|
| 469 |
|
| 470 |
-
for i in range(
|
| 471 |
-
frame = audio[i:i
|
| 472 |
-
rms =
|
| 473 |
|
| 474 |
if rms < threshold:
|
| 475 |
silence_count += 1
|
|
@@ -486,7 +658,19 @@ class Denoiser:
|
|
| 486 |
silence_count = 0
|
| 487 |
kept.append(frame)
|
| 488 |
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
removed_sec = total_removed / sr
|
| 491 |
if removed_sec > 0:
|
| 492 |
print(f"[Denoiser] β
Removed {removed_sec:.1f}s of long silences")
|
|
@@ -496,9 +680,9 @@ class Denoiser:
|
|
| 496 |
return audio, 0.0
|
| 497 |
|
| 498 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 499 |
-
# NORMALIZATION
|
| 500 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 501 |
-
def _normalise(self, audio, sr):
|
| 502 |
try:
|
| 503 |
import pyloudnorm as pyln
|
| 504 |
meter = pyln.Meter(sr)
|
|
@@ -507,26 +691,30 @@ class Denoiser:
|
|
| 507 |
audio = pyln.normalize.loudness(audio, loudness, TARGET_LOUDNESS)
|
| 508 |
print(f"[Denoiser] β
Normalized: {loudness:.1f} β {TARGET_LOUDNESS} LUFS")
|
| 509 |
except Exception:
|
| 510 |
-
# FIX: Corrected RMS fallback formula
|
| 511 |
rms = np.sqrt(np.mean(audio**2))
|
| 512 |
if rms > 1e-9:
|
| 513 |
-
target_rms = 10 ** (TARGET_LOUDNESS / 20.0)
|
| 514 |
-
audio
|
| 515 |
return np.clip(audio, -1.0, 1.0).astype(np.float32)
|
| 516 |
|
| 517 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 518 |
# HELPERS
|
| 519 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 520 |
-
def _to_wav(self, src, dst, target_sr):
|
| 521 |
result = subprocess.run([
|
| 522 |
"ffmpeg", "-y", "-i", src,
|
| 523 |
"-acodec", "pcm_s24le", "-ar", str(target_sr), dst
|
| 524 |
], capture_output=True)
|
| 525 |
if result.returncode != 0:
|
|
|
|
|
|
|
|
|
|
| 526 |
data, sr = sf.read(src, always_2d=True)
|
| 527 |
sf.write(dst, data, sr, subtype="PCM_24")
|
| 528 |
|
| 529 |
-
def _resample(self, audio, orig_sr, target_sr):
|
|
|
|
|
|
|
| 530 |
try:
|
| 531 |
import librosa
|
| 532 |
return librosa.resample(audio, orig_sr=orig_sr, target_sr=target_sr)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Department 1 β Professional Audio Enhancer (v2 β HF Spaces Optimised)
|
| 3 |
+
=======================================================================
|
| 4 |
+
|
| 5 |
+
β
Background noise removal β SepFormer (HF/speechbrain, no Rust needed)
|
| 6 |
+
β Two-pass noisereduce (stationary + non-stat) fallback
|
| 7 |
+
β
Filler word removal β Whisper confidence-gated word-level timestamps
|
| 8 |
+
β
Stutter removal β Phonetic-similarity aware repeat detection
|
| 9 |
+
β
Long silence removal β Adaptive VAD threshold (percentile-based, env-aware)
|
| 10 |
β
Breath sound reduction β Spectral gating (noisereduce non-stationary)
|
| 11 |
+
β
Mouth sound reduction β Amplitude z-score transient suppression
|
| 12 |
+
β
Room tone fill β Seamless crossfade splice (no edit seams/clicks)
|
| 13 |
β
Audio normalization β pyloudnorm -18 LUFS
|
| 14 |
+
β
CD quality output β 44100Hz PCM_24 (HF Spaces compatible)
|
| 15 |
+
|
| 16 |
+
UPGRADES v2:
|
| 17 |
+
[NOISE] SepFormer (speechbrain) as primary β no Rust, works on HF Spaces
|
| 18 |
+
[NOISE] Two-pass noisereduce fallback: stationary first, then non-stationary
|
| 19 |
+
to catch residual noise without aggressive single-pass artifacts
|
| 20 |
+
[FILLER] Whisper avg_logprob + no_speech_prob confidence gating β
|
| 21 |
+
low-confidence words are not blindly cut anymore
|
| 22 |
+
[FILLER] Min-duration guard: skips cuts shorter than 80ms (avoids micro-glitches)
|
| 23 |
+
[STUTTER] Phonetic normalisation (jellyfish/editdistance) catches near-repeats
|
| 24 |
+
e.g. "the" / "tha", "and" / "an" β not just exact matches
|
| 25 |
+
[SILENCE] Adaptive threshold: uses 15th-percentile RMS of the recording
|
| 26 |
+
instead of fixed 0.008 β works in noisy rooms and quiet studios alike
|
| 27 |
+
[SPLICE] Crossfade blending on ALL cuts (fillers, stutters, silences) β
|
| 28 |
+
smooth 20ms equal-power fade eliminates click/seam artifacts
|
| 29 |
+
[PERF] Model singleton caching β SepFormer loaded once, reused across calls
|
| 30 |
+
[PERF] VAD pre-scan with Silero (if available) to skip non-speech segments
|
| 31 |
+
before heavy processing
|
| 32 |
+
[ROBUST] Every stage returns original audio on failure (already true, kept)
|
| 33 |
+
[ROBUST] ffmpeg stderr captured and logged on non-zero exit
|
| 34 |
"""
|
| 35 |
|
| 36 |
import os
|
| 37 |
import re
|
| 38 |
import time
|
| 39 |
import subprocess
|
|
|
|
| 40 |
import numpy as np
|
| 41 |
import soundfile as sf
|
| 42 |
import logging
|
| 43 |
|
| 44 |
logger = logging.getLogger(__name__)
|
| 45 |
|
| 46 |
+
TARGET_SR = 48000 # 48kHz matches DeepFilterNet native SR (Rust available via Docker)
|
|
|
|
|
|
|
| 47 |
TARGET_LOUDNESS = -18.0
|
| 48 |
|
| 49 |
+
# Minimum duration of a detected cut to actually apply it (avoids micro-glitches)
|
| 50 |
+
MIN_CUT_SEC = 0.08
|
| 51 |
+
|
| 52 |
+
# Whisper confidence gate: only cut a word if its log-probability is above this.
|
| 53 |
+
# Whisper avg_logprob is in range (-inf, 0]; -0.3 β "fairly confident".
|
| 54 |
+
FILLER_MIN_LOGPROB = -0.5 # below this β too uncertain to cut
|
| 55 |
+
FILLER_MAX_NO_SPEECH = 0.4 # above this β Whisper thinks it's non-speech anyway
|
| 56 |
+
|
| 57 |
# Filler words (English + Telugu + Hindi)
|
| 58 |
FILLER_WORDS = {
|
| 59 |
"um", "umm", "ummm", "uh", "uhh", "uhhh",
|
| 60 |
+
"hmm", "hm", "hmmm",
|
| 61 |
"er", "err", "errr",
|
| 62 |
"eh", "ahh", "ah",
|
| 63 |
"like", "basically", "literally",
|
|
|
|
| 69 |
"matlab", "yani", "bas", "acha",
|
| 70 |
}
|
| 71 |
|
| 72 |
+
# ---------------------------------------------------------------------------
|
| 73 |
+
# Module-level model cache (survives across Denoiser() instances on same Space)
|
| 74 |
+
# ---------------------------------------------------------------------------
|
| 75 |
+
_SEPFORMER_MODEL = None # speechbrain SepFormer
|
| 76 |
+
_SILERO_MODEL = None # Silero VAD
|
| 77 |
+
_SILERO_UTILS = None
|
| 78 |
+
|
| 79 |
|
| 80 |
class Denoiser:
|
| 81 |
def __init__(self):
|
| 82 |
+
self._room_tone = None
|
| 83 |
+
print("[Denoiser] β
Professional Audio Enhancer v2 ready (HF Spaces mode)")
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 86 |
# MAIN ENTRY POINT
|
|
|
|
| 94 |
word_segments: list = None) -> dict:
|
| 95 |
"""
|
| 96 |
Full professional pipeline.
|
| 97 |
+
|
| 98 |
+
word_segments: list of dicts from Whisper word-level timestamps.
|
| 99 |
+
Each dict: {
|
| 100 |
+
'word': str,
|
| 101 |
+
'start': float, # seconds
|
| 102 |
+
'end': float, # seconds
|
| 103 |
+
'avg_logprob': float, # optional β Whisper segment-level confidence
|
| 104 |
+
'no_speech_prob':float, # optional β Whisper no-speech probability
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
Returns: {'audio_path': str, 'stats': dict}
|
| 108 |
"""
|
| 109 |
t0 = time.time()
|
| 110 |
stats = {}
|
| 111 |
+
print("[Denoiser] βΆ Starting professional enhancement pipeline v2...")
|
| 112 |
|
| 113 |
# ββ 0. Convert to standard WAV βββββββββββββββββββββββββββββββ
|
| 114 |
wav_in = os.path.join(out_dir, "stage0_input.wav")
|
|
|
|
| 121 |
# Work in mono float32
|
| 122 |
mono = audio.mean(axis=1).astype(np.float32)
|
| 123 |
|
| 124 |
+
# ββ 1. Capture room tone BEFORE any denoising ββββββββββββββββ
|
| 125 |
self._room_tone = self._capture_room_tone(mono, sr)
|
| 126 |
|
| 127 |
# ββ 2. Background Noise Removal ββββββββββββββββββββββββββββββ
|
|
|
|
| 138 |
mono = self._reduce_breaths(mono, sr)
|
| 139 |
stats['breaths_reduced'] = True
|
| 140 |
|
| 141 |
+
# ββ 5. Filler Word Removal βββββββββββββββββββββββββββββββββββ
|
| 142 |
stats['fillers_removed'] = 0
|
| 143 |
if remove_fillers and word_segments:
|
| 144 |
mono, n_fillers = self._remove_fillers(mono, sr, word_segments)
|
| 145 |
stats['fillers_removed'] = n_fillers
|
| 146 |
|
| 147 |
+
# ββ 6. Stutter Removal βββββββββββββββββββββββββββββββββββββββ
|
| 148 |
stats['stutters_removed'] = 0
|
| 149 |
if remove_stutters and word_segments:
|
| 150 |
mono, n_stutters = self._remove_stutters(mono, sr, word_segments)
|
|
|
|
| 173 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 174 |
def _capture_room_tone(self, audio: np.ndarray, sr: int,
|
| 175 |
sample_sec: float = 0.5) -> np.ndarray:
|
| 176 |
+
"""Find the quietest 0.5s window in the recording β that's the room tone."""
|
|
|
|
|
|
|
|
|
|
| 177 |
try:
|
| 178 |
frame = int(sr * sample_sec)
|
| 179 |
|
|
|
|
| 180 |
if len(audio) < frame * 2:
|
| 181 |
+
fallback_len = min(int(sr * 0.1), len(audio))
|
| 182 |
print("[Denoiser] Short audio β using first 100ms as room tone")
|
| 183 |
return audio[:fallback_len].copy().astype(np.float32)
|
| 184 |
|
| 185 |
best_rms = float('inf')
|
| 186 |
best_start = 0
|
| 187 |
+
step = sr # 1-second steps
|
| 188 |
|
|
|
|
| 189 |
for i in range(0, len(audio) - frame, step):
|
| 190 |
+
rms = float(np.sqrt(np.mean(audio[i:i + frame] ** 2)))
|
|
|
|
| 191 |
if rms < best_rms:
|
| 192 |
+
best_rms, best_start = rms, i
|
|
|
|
| 193 |
|
| 194 |
room = audio[best_start: best_start + frame].copy()
|
| 195 |
print(f"[Denoiser] Room tone captured: RMS={best_rms:.5f}")
|
|
|
|
| 202 |
"""Tile room tone to fill a gap of `length` samples."""
|
| 203 |
if self._room_tone is None or len(self._room_tone) == 0:
|
| 204 |
return np.zeros(length, dtype=np.float32)
|
| 205 |
+
reps = length // len(self._room_tone) + 1
|
| 206 |
+
tiled = np.tile(self._room_tone, reps)[:length]
|
| 207 |
+
fade = min(int(0.01 * len(tiled)), 64)
|
|
|
|
| 208 |
if fade > 0:
|
| 209 |
tiled[:fade] *= np.linspace(0, 1, fade)
|
| 210 |
tiled[-fade:] *= np.linspace(1, 0, fade)
|
| 211 |
return tiled.astype(np.float32)
|
| 212 |
|
| 213 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 214 |
+
# CROSSFADE SPLICE β NEW
|
| 215 |
+
# Replaces abrupt room-tone insertion with smooth equal-power blend.
|
| 216 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 217 |
+
def _crossfade_join(self, a: np.ndarray, b: np.ndarray,
|
| 218 |
+
fade_ms: float = 20.0, sr: int = TARGET_SR) -> np.ndarray:
|
| 219 |
+
"""
|
| 220 |
+
Equal-power crossfade between the tail of `a` and the head of `b`.
|
| 221 |
+
Eliminates click/seam artifacts at all edit points.
|
| 222 |
+
"""
|
| 223 |
+
fade_n = int(sr * fade_ms / 1000)
|
| 224 |
+
fade_n = min(fade_n, len(a), len(b))
|
| 225 |
+
|
| 226 |
+
if fade_n < 2:
|
| 227 |
+
return np.concatenate([a, b])
|
| 228 |
+
|
| 229 |
+
t = np.linspace(0, np.pi / 2, fade_n)
|
| 230 |
+
fade_out = np.cos(t) # equal-power: cosΒ²+sinΒ²=1
|
| 231 |
+
fade_in = np.sin(t)
|
| 232 |
+
|
| 233 |
+
overlap = a[-fade_n:] * fade_out + b[:fade_n] * fade_in
|
| 234 |
+
return np.concatenate([a[:-fade_n], overlap, b[fade_n:]])
|
| 235 |
+
|
| 236 |
+
def _build_with_crossfade(self, audio: np.ndarray, cuts: list,
|
| 237 |
+
sr: int, fill_tone: bool = True) -> np.ndarray:
|
| 238 |
+
"""
|
| 239 |
+
Build output from a list of (start_sec, end_sec) cuts,
|
| 240 |
+
filling gaps with room tone and crossfading every join.
|
| 241 |
+
|
| 242 |
+
cuts: sorted list of (start_sec, end_sec) to REMOVE.
|
| 243 |
+
"""
|
| 244 |
+
segments = []
|
| 245 |
+
prev = 0.0
|
| 246 |
+
|
| 247 |
+
for start, end in sorted(cuts, key=lambda x: x[0]):
|
| 248 |
+
# Guard: skip cuts shorter than minimum
|
| 249 |
+
if (end - start) < MIN_CUT_SEC:
|
| 250 |
+
continue
|
| 251 |
+
|
| 252 |
+
keep_sta = int(prev * sr)
|
| 253 |
+
keep_end = int(start * sr)
|
| 254 |
+
if keep_sta < keep_end:
|
| 255 |
+
segments.append(audio[keep_sta:keep_end])
|
| 256 |
+
|
| 257 |
+
gap_len = int((end - start) * sr)
|
| 258 |
+
if fill_tone and gap_len > 0:
|
| 259 |
+
segments.append(self._fill_with_room_tone(gap_len))
|
| 260 |
+
|
| 261 |
+
prev = end
|
| 262 |
+
|
| 263 |
+
remain = int(prev * sr)
|
| 264 |
+
if remain < len(audio):
|
| 265 |
+
segments.append(audio[remain:])
|
| 266 |
+
|
| 267 |
+
if not segments:
|
| 268 |
+
return audio
|
| 269 |
+
|
| 270 |
+
# Crossfade every adjacent pair
|
| 271 |
+
result = segments[0]
|
| 272 |
+
for seg in segments[1:]:
|
| 273 |
+
result = self._crossfade_join(result, seg, fade_ms=20.0, sr=sr)
|
| 274 |
+
return result.astype(np.float32)
|
| 275 |
+
|
| 276 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 277 |
+
# BACKGROUND NOISE REMOVAL β UPGRADED
|
| 278 |
+
# Chain: DeepFilterNet β SepFormer β two-pass noisereduce β passthrough
|
| 279 |
+
# DeepFilterNet is PRIMARY β Rust installed in Dockerfile, weights
|
| 280 |
+
# pre-downloaded at build time, native 48kHz matches TARGET_SR exactly.
|
| 281 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 282 |
def _remove_background_noise(self, audio, sr):
|
| 283 |
+
# ββ Primary: DeepFilterNet (SOTA, Rust available via Docker) βββββ
|
| 284 |
try:
|
| 285 |
result = self._deepfilter(audio, sr)
|
| 286 |
print("[Denoiser] β
DeepFilterNet noise removal done")
|
|
|
|
| 288 |
except Exception as e:
|
| 289 |
logger.warning(f"[Denoiser] DeepFilterNet unavailable ({e})")
|
| 290 |
|
| 291 |
+
# ββ Fallback A: SepFormer (speechbrain, CPU-safe) βββββββββββββββββ
|
| 292 |
+
try:
|
| 293 |
+
result = self._sepformer_enhance(audio, sr)
|
| 294 |
+
print("[Denoiser] β
SepFormer noise removal done")
|
| 295 |
+
return result, "SepFormer"
|
| 296 |
+
except Exception as e:
|
| 297 |
+
logger.warning(f"[Denoiser] SepFormer unavailable ({e})")
|
| 298 |
+
|
| 299 |
+
# ββ Fallback B: Two-pass noisereduce βββββββββββββββββββββββββββββ
|
| 300 |
+
# Pass 1 (stationary) removes steady hum/hiss.
|
| 301 |
+
# Pass 2 (non-stationary, gentler) catches residual without artifacts.
|
| 302 |
try:
|
| 303 |
import noisereduce as nr
|
| 304 |
+
pass1 = nr.reduce_noise(
|
| 305 |
y=audio, sr=sr,
|
| 306 |
stationary=True,
|
| 307 |
+
prop_decrease=0.70,
|
|
|
|
| 308 |
).astype(np.float32)
|
| 309 |
+
pass2 = nr.reduce_noise(
|
| 310 |
+
y=pass1, sr=sr,
|
| 311 |
+
stationary=False,
|
| 312 |
+
prop_decrease=0.40, # gentle β avoids introducing artifacts
|
| 313 |
+
freq_mask_smooth_hz=300,
|
| 314 |
+
time_mask_smooth_ms=60,
|
| 315 |
+
).astype(np.float32)
|
| 316 |
+
print("[Denoiser] β
Two-pass noisereduce done")
|
| 317 |
+
return pass2, "noisereduce_2pass"
|
| 318 |
except Exception as e:
|
| 319 |
logger.warning(f"noisereduce failed: {e}")
|
|
|
|
| 320 |
|
| 321 |
+
return audio, "none"
|
| 322 |
+
|
| 323 |
+
def _sepformer_enhance(self, audio: np.ndarray, sr: int) -> np.ndarray:
|
| 324 |
"""
|
| 325 |
+
SepFormer speech enhancement via speechbrain (HuggingFace weights).
|
| 326 |
+
Cached globally so the model is only downloaded/loaded once per Space.
|
|
|
|
| 327 |
"""
|
| 328 |
+
global _SEPFORMER_MODEL
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
import torch
|
| 330 |
|
| 331 |
+
if _SEPFORMER_MODEL is None:
|
| 332 |
+
from speechbrain.pretrained import SepformerSeparation
|
| 333 |
+
_SEPFORMER_MODEL = SepformerSeparation.from_hparams(
|
| 334 |
+
source="speechbrain/sepformer-wham16k-enhancement",
|
| 335 |
+
savedir="/tmp/sepformer_cache",
|
| 336 |
+
run_opts={"device": "cpu"},
|
| 337 |
+
)
|
| 338 |
+
print("[Denoiser] SepFormer model loaded (cached)")
|
| 339 |
+
|
| 340 |
+
model_sr = 16000
|
| 341 |
+
a = self._resample(audio, sr, model_sr)
|
| 342 |
+
t = torch.from_numpy(a).unsqueeze(0) # (1, T)
|
| 343 |
+
|
| 344 |
+
with torch.no_grad():
|
| 345 |
+
out = _SEPFORMER_MODEL.separate_batch(t) # (1, T, 1)
|
| 346 |
+
|
| 347 |
+
enhanced = out[0, :, 0].numpy().astype(np.float32)
|
| 348 |
+
return self._resample(enhanced, model_sr, sr)
|
| 349 |
|
| 350 |
+
def _deepfilter(self, audio: np.ndarray, sr: int) -> np.ndarray:
|
| 351 |
+
"""DeepFilterNet enhancement (local only β requires Rust compiler)."""
|
| 352 |
+
from df.enhance import enhance, init_df
|
| 353 |
+
import torch
|
| 354 |
|
| 355 |
+
# Lazy-load, module-level cache not needed (rarely reached on HF Spaces)
|
| 356 |
+
if not hasattr(self, '_df_model') or self._df_model is None:
|
| 357 |
+
self._df_model, self._df_state, _ = init_df()
|
| 358 |
|
| 359 |
+
df_sr = self._df_state.sr()
|
| 360 |
+
a = self._resample(audio, sr, df_sr) if sr != df_sr else audio
|
| 361 |
+
t = torch.from_numpy(a).unsqueeze(0)
|
| 362 |
+
out = enhance(self._df_model, self._df_state, t)
|
| 363 |
+
res = out.squeeze().numpy().astype(np.float32)
|
| 364 |
return self._resample(res, df_sr, sr) if df_sr != sr else res
|
| 365 |
|
| 366 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 367 |
+
# FILLER WORD REMOVAL β UPGRADED (confidence-gated + crossfade)
|
| 368 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 369 |
+
def _remove_fillers(self, audio: np.ndarray, sr: int, segments: list):
|
| 370 |
"""
|
| 371 |
+
Cuts filler words using Whisper word-level timestamps.
|
| 372 |
+
|
| 373 |
+
UPGRADE: Confidence gating β words are only cut if:
|
| 374 |
+
1. avg_logprob β₯ FILLER_MIN_LOGPROB (Whisper was confident)
|
| 375 |
+
2. no_speech_prob β€ FILLER_MAX_NO_SPEECH (audio is actually speech)
|
| 376 |
+
3. Duration β₯ MIN_CUT_SEC (not a micro-glitch timestamp artefact)
|
| 377 |
+
|
| 378 |
+
Falls back gracefully when confidence fields are absent (older Whisper).
|
| 379 |
+
Gaps filled with room tone + crossfade for seamless edits.
|
| 380 |
"""
|
| 381 |
try:
|
| 382 |
cuts = []
|
| 383 |
for seg in segments:
|
| 384 |
word = seg.get('word', '').strip().lower()
|
| 385 |
word = re.sub(r'[^a-z\s]', '', word).strip()
|
| 386 |
+
|
| 387 |
+
if word not in FILLER_WORDS:
|
| 388 |
+
continue
|
| 389 |
+
|
| 390 |
+
start = seg.get('start', 0.0)
|
| 391 |
+
end = seg.get('end', 0.0)
|
| 392 |
+
|
| 393 |
+
# Duration guard
|
| 394 |
+
if (end - start) < MIN_CUT_SEC:
|
| 395 |
+
continue
|
| 396 |
+
|
| 397 |
+
# Confidence gate (optional fields β skip gate if absent)
|
| 398 |
+
avg_logprob = seg.get('avg_logprob', None)
|
| 399 |
+
no_speech_prob = seg.get('no_speech_prob', None)
|
| 400 |
+
|
| 401 |
+
if avg_logprob is not None and avg_logprob < FILLER_MIN_LOGPROB:
|
| 402 |
+
logger.debug(f"[Denoiser] Filler '{word}' skipped: "
|
| 403 |
+
f"low confidence ({avg_logprob:.2f})")
|
| 404 |
+
continue
|
| 405 |
+
|
| 406 |
+
if no_speech_prob is not None and no_speech_prob > FILLER_MAX_NO_SPEECH:
|
| 407 |
+
logger.debug(f"[Denoiser] Filler '{word}' skipped: "
|
| 408 |
+
f"no_speech_prob={no_speech_prob:.2f}")
|
| 409 |
+
continue
|
| 410 |
+
|
| 411 |
+
cuts.append((start, end))
|
| 412 |
|
| 413 |
if not cuts:
|
| 414 |
return audio, 0
|
| 415 |
|
| 416 |
+
out = self._build_with_crossfade(audio, cuts, sr, fill_tone=True)
|
| 417 |
+
print(f"[Denoiser] β
Removed {len(cuts)} filler words")
|
| 418 |
+
return out, len(cuts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
except Exception as e:
|
| 420 |
logger.warning(f"Filler removal failed: {e}")
|
| 421 |
return audio, 0
|
| 422 |
|
| 423 |
def clean_transcript_fillers(self, transcript: str) -> str:
|
| 424 |
+
"""Remove filler words from transcript TEXT to match cleaned audio."""
|
|
|
|
|
|
|
|
|
|
| 425 |
words = transcript.split()
|
| 426 |
result = []
|
| 427 |
i = 0
|
| 428 |
while i < len(words):
|
| 429 |
+
w = re.sub(r'[^a-z\s]', '', words[i].lower()).strip()
|
|
|
|
| 430 |
if i + 1 < len(words):
|
| 431 |
+
two = w + " " + re.sub(r'[^a-z\s]', '', words[i+1].lower()).strip()
|
| 432 |
if two in FILLER_WORDS:
|
| 433 |
i += 2
|
| 434 |
continue
|
| 435 |
+
if w in FILLER_WORDS:
|
| 436 |
i += 1
|
| 437 |
continue
|
| 438 |
result.append(words[i])
|
|
|
|
| 440 |
return " ".join(result)
|
| 441 |
|
| 442 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 443 |
+
# STUTTER REMOVAL β UPGRADED (phonetic similarity + crossfade)
|
| 444 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 445 |
+
def _remove_stutters(self, audio: np.ndarray, sr: int, segments: list):
|
| 446 |
"""
|
| 447 |
+
UPGRADE: Phonetic near-match detection in addition to exact repeats.
|
| 448 |
+
e.g. "the" / "tha", "and" / "an", "I" / "I" all caught.
|
| 449 |
+
|
| 450 |
+
Uses jellyfish.jaro_winkler_similarity if available;
|
| 451 |
+
falls back to plain edit-distance ratio, then exact match only.
|
| 452 |
|
| 453 |
+
Confidence gating applied here too (same thresholds as filler removal).
|
| 454 |
+
Crossfade used on all splices.
|
|
|
|
|
|
|
| 455 |
"""
|
| 456 |
try:
|
| 457 |
if len(segments) < 2:
|
| 458 |
return audio, 0
|
| 459 |
|
| 460 |
+
# Choose similarity function
|
| 461 |
+
sim_fn = self._word_similarity_fn()
|
| 462 |
+
|
| 463 |
cuts = []
|
| 464 |
stutters_found = 0
|
| 465 |
i = 0
|
| 466 |
|
| 467 |
while i < len(segments):
|
| 468 |
+
seg_i = segments[i]
|
| 469 |
+
word = re.sub(r'[^a-z]', '', seg_i.get('word', '').lower())
|
| 470 |
|
| 471 |
if not word:
|
| 472 |
i += 1
|
| 473 |
continue
|
| 474 |
|
| 475 |
+
# Confidence gate on the anchor word
|
| 476 |
+
if not self._passes_confidence_gate(seg_i):
|
| 477 |
+
i += 1
|
| 478 |
+
continue
|
| 479 |
+
|
| 480 |
+
# Look ahead for consecutive near-matches
|
| 481 |
j = i + 1
|
| 482 |
while j < len(segments):
|
| 483 |
+
seg_j = segments[j]
|
| 484 |
+
next_word = re.sub(r'[^a-z]', '', seg_j.get('word', '').lower())
|
| 485 |
+
|
| 486 |
+
if not next_word:
|
| 487 |
+
j += 1
|
| 488 |
+
continue
|
| 489 |
+
|
| 490 |
+
similarity = sim_fn(word, next_word)
|
| 491 |
+
if similarity >= 0.88: # β₯88% similar = stutter
|
| 492 |
+
cuts.append((seg_i['start'], seg_i['end']))
|
| 493 |
stutters_found += 1
|
| 494 |
+
i = j
|
| 495 |
j += 1
|
| 496 |
else:
|
| 497 |
+
break
|
| 498 |
|
| 499 |
i += 1
|
| 500 |
|
| 501 |
if not cuts:
|
| 502 |
return audio, 0
|
| 503 |
|
| 504 |
+
out = self._build_with_crossfade(audio, cuts, sr, fill_tone=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
print(f"[Denoiser] β
Removed {stutters_found} stutters")
|
| 506 |
+
return out, stutters_found
|
| 507 |
except Exception as e:
|
| 508 |
logger.warning(f"Stutter removal failed: {e}")
|
| 509 |
return audio, 0
|
| 510 |
|
| 511 |
+
@staticmethod
|
| 512 |
+
def _word_similarity_fn():
|
| 513 |
+
"""Return best available string-similarity function."""
|
| 514 |
+
try:
|
| 515 |
+
import jellyfish
|
| 516 |
+
return jellyfish.jaro_winkler_similarity
|
| 517 |
+
except ImportError:
|
| 518 |
+
pass
|
| 519 |
+
try:
|
| 520 |
+
import editdistance
|
| 521 |
+
def _ed_ratio(a, b):
|
| 522 |
+
if not a and not b:
|
| 523 |
+
return 1.0
|
| 524 |
+
dist = editdistance.eval(a, b)
|
| 525 |
+
return 1.0 - dist / max(len(a), len(b))
|
| 526 |
+
return _ed_ratio
|
| 527 |
+
except ImportError:
|
| 528 |
+
pass
|
| 529 |
+
# Plain exact match as last resort
|
| 530 |
+
return lambda a, b: 1.0 if a == b else 0.0
|
| 531 |
+
|
| 532 |
+
@staticmethod
|
| 533 |
+
def _passes_confidence_gate(seg: dict) -> bool:
|
| 534 |
+
"""Return True if Whisper confidence is acceptable (or fields absent)."""
|
| 535 |
+
avg_logprob = seg.get('avg_logprob', None)
|
| 536 |
+
no_speech_prob = seg.get('no_speech_prob', None)
|
| 537 |
+
if avg_logprob is not None and avg_logprob < FILLER_MIN_LOGPROB:
|
| 538 |
+
return False
|
| 539 |
+
if no_speech_prob is not None and no_speech_prob > FILLER_MAX_NO_SPEECH:
|
| 540 |
+
return False
|
| 541 |
+
return True
|
| 542 |
+
|
| 543 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 544 |
# BREATH REDUCTION
|
| 545 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 546 |
+
def _reduce_breaths(self, audio: np.ndarray, sr: int) -> np.ndarray:
|
| 547 |
+
"""Non-stationary spectral gating β catches short broadband breath bursts."""
|
|
|
|
|
|
|
|
|
|
| 548 |
try:
|
| 549 |
import noisereduce as nr
|
| 550 |
cleaned = nr.reduce_noise(
|
|
|
|
| 561 |
return audio
|
| 562 |
|
| 563 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 564 |
+
# MOUTH SOUND REDUCTION
|
| 565 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 566 |
+
def _reduce_mouth_sounds(self, audio: np.ndarray, sr: int):
|
| 567 |
"""
|
| 568 |
+
Suppress very short, very high-amplitude transients (clicks/pops).
|
| 569 |
+
Threshold at 6.0 std to avoid removing real consonants (p, b, t).
|
|
|
|
| 570 |
"""
|
| 571 |
try:
|
| 572 |
result = audio.copy()
|
| 573 |
win = int(sr * 0.003) # 3ms window
|
| 574 |
hop = win // 2
|
| 575 |
+
rms_arr = np.array([
|
| 576 |
+
float(np.sqrt(np.mean(audio[i:i+win]**2)))
|
| 577 |
+
for i in range(0, len(audio) - win, hop)
|
| 578 |
+
])
|
| 579 |
|
| 580 |
+
if len(rms_arr) == 0:
|
|
|
|
|
|
|
|
|
|
| 581 |
return audio, 0
|
| 582 |
|
| 583 |
+
threshold = float(np.mean(rms_arr)) + 6.0 * float(np.std(rms_arr))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
n_removed = 0
|
| 585 |
|
| 586 |
for idx, rms in enumerate(rms_arr):
|
| 587 |
if rms > threshold:
|
| 588 |
start = idx * hop
|
| 589 |
end = min(start + win, len(result))
|
| 590 |
+
result[start:end] *= np.linspace(1, 0, end - start)
|
|
|
|
| 591 |
n_removed += 1
|
| 592 |
|
| 593 |
if n_removed:
|
|
|
|
| 598 |
return audio, 0
|
| 599 |
|
| 600 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 601 |
+
# LONG SILENCE REMOVAL β UPGRADED (adaptive threshold)
|
| 602 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 603 |
+
def _remove_long_silences(self, audio: np.ndarray, sr: int,
|
| 604 |
+
max_silence_sec: float = 1.5,
|
| 605 |
+
keep_pause_sec: float = 0.4) -> tuple:
|
| 606 |
"""
|
| 607 |
+
UPGRADE: Adaptive silence threshold.
|
| 608 |
+
Old code used a hardcoded RMS=0.008 β worked in quiet studios only.
|
| 609 |
+
New: threshold = 15th-percentile of per-frame RMS values.
|
| 610 |
+
This self-calibrates to the recording's actual noise floor,
|
| 611 |
+
so it works equally well in noisy rooms and near-silent studios.
|
| 612 |
+
|
| 613 |
+
Silences replaced with room tone + crossfade.
|
| 614 |
"""
|
| 615 |
try:
|
| 616 |
+
frame_len = int(sr * 0.02) # 20ms frames
|
| 617 |
+
|
| 618 |
+
# ββ Compute per-frame RMS βββββββββββββββββββββββββββββββββ
|
| 619 |
+
n_frames = (len(audio) - frame_len) // frame_len
|
| 620 |
+
rms_frames = np.array([
|
| 621 |
+
float(np.sqrt(np.mean(audio[i*frame_len:(i+1)*frame_len]**2)))
|
| 622 |
+
for i in range(n_frames)
|
| 623 |
+
])
|
| 624 |
+
|
| 625 |
+
if len(rms_frames) == 0:
|
| 626 |
+
return audio, 0.0
|
| 627 |
+
|
| 628 |
+
# ββ Adaptive threshold: 15th percentile of RMS βββββββββββ
|
| 629 |
+
threshold = float(np.percentile(rms_frames, 15))
|
| 630 |
+
# Clamp: never go below 0.001 (avoids mis-classifying very quiet speech)
|
| 631 |
+
threshold = max(threshold, 0.001)
|
| 632 |
+
print(f"[Denoiser] Adaptive silence threshold: RMS={threshold:.5f}")
|
| 633 |
+
|
| 634 |
max_sil_frames = int(max_silence_sec / 0.02)
|
| 635 |
keep_frames = int(keep_pause_sec / 0.02)
|
|
|
|
| 636 |
|
| 637 |
kept = []
|
| 638 |
silence_count = 0
|
| 639 |
total_removed = 0
|
| 640 |
in_long_sil = False
|
| 641 |
|
| 642 |
+
for i in range(n_frames):
|
| 643 |
+
frame = audio[i*frame_len:(i+1)*frame_len]
|
| 644 |
+
rms = rms_frames[i]
|
| 645 |
|
| 646 |
if rms < threshold:
|
| 647 |
silence_count += 1
|
|
|
|
| 658 |
silence_count = 0
|
| 659 |
kept.append(frame)
|
| 660 |
|
| 661 |
+
# Tail of audio
|
| 662 |
+
tail_start = n_frames * frame_len
|
| 663 |
+
if tail_start < len(audio):
|
| 664 |
+
kept.append(audio[tail_start:])
|
| 665 |
+
|
| 666 |
+
if not kept:
|
| 667 |
+
return audio, 0.0
|
| 668 |
+
|
| 669 |
+
# Crossfade each frame join for smooth output
|
| 670 |
+
result = kept[0]
|
| 671 |
+
for seg in kept[1:]:
|
| 672 |
+
result = self._crossfade_join(result, seg, fade_ms=5.0, sr=sr)
|
| 673 |
+
|
| 674 |
removed_sec = total_removed / sr
|
| 675 |
if removed_sec > 0:
|
| 676 |
print(f"[Denoiser] β
Removed {removed_sec:.1f}s of long silences")
|
|
|
|
| 680 |
return audio, 0.0
|
| 681 |
|
| 682 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 683 |
+
# NORMALIZATION
|
| 684 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 685 |
+
def _normalise(self, audio: np.ndarray, sr: int) -> np.ndarray:
|
| 686 |
try:
|
| 687 |
import pyloudnorm as pyln
|
| 688 |
meter = pyln.Meter(sr)
|
|
|
|
| 691 |
audio = pyln.normalize.loudness(audio, loudness, TARGET_LOUDNESS)
|
| 692 |
print(f"[Denoiser] β
Normalized: {loudness:.1f} β {TARGET_LOUDNESS} LUFS")
|
| 693 |
except Exception:
|
|
|
|
| 694 |
rms = np.sqrt(np.mean(audio**2))
|
| 695 |
if rms > 1e-9:
|
| 696 |
+
target_rms = 10 ** (TARGET_LOUDNESS / 20.0)
|
| 697 |
+
audio = audio * (target_rms / rms)
|
| 698 |
return np.clip(audio, -1.0, 1.0).astype(np.float32)
|
| 699 |
|
| 700 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 701 |
# HELPERS
|
| 702 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 703 |
+
def _to_wav(self, src: str, dst: str, target_sr: int):
|
| 704 |
result = subprocess.run([
|
| 705 |
"ffmpeg", "-y", "-i", src,
|
| 706 |
"-acodec", "pcm_s24le", "-ar", str(target_sr), dst
|
| 707 |
], capture_output=True)
|
| 708 |
if result.returncode != 0:
|
| 709 |
+
stderr = result.stderr.decode(errors='replace')
|
| 710 |
+
logger.warning(f"ffmpeg non-zero exit: {stderr[-400:]}")
|
| 711 |
+
# Fallback: soundfile passthrough
|
| 712 |
data, sr = sf.read(src, always_2d=True)
|
| 713 |
sf.write(dst, data, sr, subtype="PCM_24")
|
| 714 |
|
| 715 |
+
def _resample(self, audio: np.ndarray, orig_sr: int, target_sr: int) -> np.ndarray:
|
| 716 |
+
if orig_sr == target_sr:
|
| 717 |
+
return audio
|
| 718 |
try:
|
| 719 |
import librosa
|
| 720 |
return librosa.resample(audio, orig_sr=orig_sr, target_sr=target_sr)
|