ongudidan commited on
Commit
42eb2c0
·
1 Parent(s): 9c7885d

refactor: optimize noise gate response time and introduce a gain floor for more natural background audio processing

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -151,18 +151,21 @@ def post_process_audio(
151
  if treble_gain != 0:
152
  waveform = F_audio.equalizer_biquad(waveform, sample_rate=sr, center_freq=6000.0, gain=treble_gain, Q=0.707)
153
 
154
- # 4. Soft Noise Gate to remove background gating/watery artifacts during silence
155
  if gate_threshold > -60.0:
156
  threshold = 10 ** (gate_threshold / 20.0)
157
  envelope = waveform.abs()
158
- # Smooth envelope using 50ms average pooling
159
- win_size = int(sr * 0.05)
160
  if win_size % 2 == 0:
161
  win_size += 1
162
  env_padded = torch.nn.functional.pad(envelope, (win_size//2, win_size//2), mode='reflect')
163
  env_smooth = torch.nn.functional.avg_pool1d(env_padded.unsqueeze(0), kernel_size=win_size, stride=1).squeeze(0)
164
  # Soft sigmoid gating to prevent clicking
165
- gain = torch.sigmoid((env_smooth - threshold) / (threshold * 0.25))
 
 
 
166
  waveform = waveform * gain
167
 
168
  # 5. Analog Warmth (Soft Saturation / Tube Limiting)
@@ -248,10 +251,11 @@ def demo_fn(
248
 
249
  # Apply professional post-processing (Low-cut filter + bass EQ + treble EQ + gate + warmth) using optimized studio defaults
250
  if vocal_polish:
 
251
  enhanced = post_process_audio(
252
  enhanced,
253
  sr,
254
- low_cut_freq=80.0,
255
  bass_gain=0.0,
256
  treble_gain=3.0,
257
  gate_threshold=-45.0,
 
151
  if treble_gain != 0:
152
  waveform = F_audio.equalizer_biquad(waveform, sample_rate=sr, center_freq=6000.0, gain=treble_gain, Q=0.707)
153
 
154
+ # 4. Soft Noise Gate with a floor to keep background noise natural and not too dry
155
  if gate_threshold > -60.0:
156
  threshold = 10 ** (gate_threshold / 20.0)
157
  envelope = waveform.abs()
158
+ # Fast 15ms window to open instantly at the start of words (avoids muddy onset delay)
159
+ win_size = int(sr * 0.015)
160
  if win_size % 2 == 0:
161
  win_size += 1
162
  env_padded = torch.nn.functional.pad(envelope, (win_size//2, win_size//2), mode='reflect')
163
  env_smooth = torch.nn.functional.avg_pool1d(env_padded.unsqueeze(0), kernel_size=win_size, stride=1).squeeze(0)
164
  # Soft sigmoid gating to prevent clicking
165
+ raw_gain = torch.sigmoid((env_smooth - threshold) / (threshold * 0.25))
166
+ # Keep a floor of 35% gain (approx -9dB) so background noise remains continuous & controllable
167
+ gate_floor = 0.35
168
+ gain = gate_floor + (1.0 - gate_floor) * raw_gain
169
  waveform = waveform * gain
170
 
171
  # 5. Analog Warmth (Soft Saturation / Tube Limiting)
 
251
 
252
  # Apply professional post-processing (Low-cut filter + bass EQ + treble EQ + gate + warmth) using optimized studio defaults
253
  if vocal_polish:
254
+ # We set low_cut_freq=110.0 to cut muddy frequency spikes at the start of words
255
  enhanced = post_process_audio(
256
  enhanced,
257
  sr,
258
+ low_cut_freq=110.0,
259
  bass_gain=0.0,
260
  treble_gain=3.0,
261
  gate_threshold=-45.0,