Spaces:
Running
Running
refactor: replace abstract denoising strength slider with granular controls for post-processing and noise gate parameters
Browse files150 unmodified lines15115215315415515615715815916016116216316416516616716816917017113 unmodified lines18518618718818919019119219319419519619719819920020120220320420520620720820921021121221335 unmodified lines249250251252253254255256257258259260261262263264265266174 unmodified lines44144244344444544644744844945045145245345412 unmodified lines467468469470471472473474475476150 unmodified linesif treble_gain != 0:waveform = F_audio.equalizer_biquad(waveform, sample_rate=sr, center_freq=6000.0, gain=treble_gain, Q=0.707)# 4. Soft Noise Gate with a floor to keep background noise natural and not too dryif gate_threshold > -60.0:threshold = 10 ** (gate_threshold / 20.0)envelope = waveform.abs()# Fast 15ms window to open instantly at the start of words (avoids muddy onset delay)win_size = int(sr * 0.015)if win_size % 2 == 0:win_size += 1env_padded = torch.nn.functional.pad(envelope, (win_size//2, win_size//2), mode='reflect')env_smooth = torch.nn.functional.avg_pool1d(env_padded.unsqueeze(0), kernel_size=win_size, stride=1).squeeze(0)# Soft sigmoid gating to prevent clickingraw_gain = torch.sigmoid((env_smooth - threshold) / (threshold * 0.25))# Keep a floor of 35% gain (approx -9dB) so background noise remains continuous & controllablegate_floor = 0.35gain = gate_floor + (1.0 - gate_floor) * raw_gainwaveform = waveform * gain# 5. Analog Warmth (Soft Saturation / Tube Limiting)13 unmodified linesspeech_upl: str,noise_type: str,snr: int,denoising_strength: float,vocal_polish: bool,mic_input: Optional[str] = None,):if input_type == "mic":speech_upl = mic_inputsr = config("sr", 48000, int, section="df")logger.info(f"Got parameters input_type: {input_type}, speech_upl: {speech_upl}, noise: {noise_type}, snr: {snr}, strength: {denoising_strength}, polish: {vocal_polish}")snr = int(snr)noise_fn = NOISES[noise_type]meta = AudioMetaData(-1, -1, -1, -1, "")# Map the simple 0-100% denoising strength to advanced model parametersif denoising_strength == 0:atten_lim_db = 0.0wet_dry_mix = 0.0else:# Interpolate maximum attenuation between 6dB and 30dBatten_lim_db = 6.0 + (denoising_strength / 100.0) * 24.0# Interpolate dry/wet mix between 50% and 100%wet_dry_mix = 50.0 + (denoising_strength / 100.0) * 50.0max_s = 3600 # allow up to 1 hour (3600 seconds)if speech_upl is not None:35 unmodified linesalpha = wet_dry_mix / 100.0enhanced = alpha * enhanced + (1 - alpha) * sample# Apply professional post-processing (Low-cut filter + bass EQ + treble EQ + gate + warmth) using optimized studio defaultsif vocal_polish:# We set low_cut_freq=110.0 to cut muddy frequency spikes at the start of wordsenhanced = post_process_audio(enhanced,sr,low_cut_freq=110.0,bass_gain=0.0,treble_gain=3.0,gate_threshold=-45.0,enable_warmth=True)lim = torch.linspace(0.0, 1.0, int(sr * 0.15)).unsqueeze(0)lim = torch.cat((lim, torch.ones(1, enhanced.shape[-1] - lim.shape[1])), dim=1)174 unmodified linesvalue="10",),gr.Slider(minimum=0,maximum=100,step=5,value=80,label="Denoising Strength (%) - higher removes more noise, lower is more natural",),gr.Checkbox(label="Vocal Polish (80Hz Low-Cut, Treble EQ & Analog Warmth)",value=True,),mic_input,12 unmodified linesradio.change(toggle, radio, [mic_input, audio_file])gr.Examples([["file", "./samples/p232_013_clean.wav", "Kitchen", "10", 80, True, None],["file", "./samples/p232_013_clean.wav", "Cafe", "10", 80, True, None],["file", "./samples/p232_019_clean.wav", "Cafe", "10", 80, True, None],["file", "./samples/p232_019_clean.wav", "River", "10", 80, True, None],],fn=demo_fn,inputs=inputs,150 unmodified lines15115215315415515615715815916016116216316416516616716813 unmodified lines18218318418518618718818919019119219319419519619719819920020120220320420520635 unmodified lines242243244245246247248249250174 unmodified lines42542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647712 unmodified lines490491492493494495496497498499150 unmodified linesif treble_gain != 0:waveform = F_audio.equalizer_biquad(waveform, sample_rate=sr, center_freq=6000.0, gain=treble_gain, Q=0.707)# 4. Soft Noise Gate to remove background gating/watery artifacts during silenceif gate_threshold > -60.0:threshold = 10 ** (gate_threshold / 20.0)envelope = waveform.abs()# Smooth envelope using 50ms average poolingwin_size = int(sr * 0.05)if win_size % 2 == 0:win_size += 1env_padded = torch.nn.functional.pad(envelope, (win_size//2, win_size//2), mode='reflect')env_smooth = torch.nn.functional.avg_pool1d(env_padded.unsqueeze(0), kernel_size=win_size, stride=1).squeeze(0)# Soft sigmoid gating to prevent clickinggain = torch.sigmoid((env_smooth - threshold) / (threshold * 0.25))waveform = waveform * gain# 5. Analog Warmth (Soft Saturation / Tube Limiting)13 unmodified linesspeech_upl: str,noise_type: str,snr: int,atten_lim_db: float,wet_dry_mix: float,post_process: bool,low_cut_freq: float,bass_gain: float,treble_gain: float,gate_threshold: float,enable_warmth: bool,mic_input: Optional[str] = None,):if input_type == "mic":speech_upl = mic_inputsr = config("sr", 48000, int, section="df")logger.info(f"Got parameters input_type: {input_type}, speech_upl: {speech_upl}, noise: {noise_type}, snr: {snr}, atten_lim_db: {atten_lim_db}, wet_dry_mix: {wet_dry_mix}, post_process: {post_process}, low_cut: {low_cut_freq}, bass: {bass_gain}, treble: {treble_gain}, gate: {gate_threshold}, warmth: {enable_warmth}")snr = int(snr)noise_fn = NOISES[noise_type]meta = AudioMetaData(-1, -1, -1, -1, "")max_s = 3600 # allow up to 1 hour (3600 seconds)if speech_upl is not None:35 unmodified linesalpha = wet_dry_mix / 100.0enhanced = alpha * enhanced + (1 - alpha) * sample# Apply professional post-processing (Low-cut filter + bass EQ + treble EQ + gate + warmth)if post_process:enhanced = post_process_audio(enhanced, sr, low_cut_freq, bass_gain, treble_gain, gate_threshold, enable_warmth)lim = torch.linspace(0.0, 1.0, int(sr * 0.15)).unsqueeze(0)lim = torch.cat((lim, torch.ones(1, enhanced.shape[-1] - lim.shape[1])), dim=1)174 unmodified linesvalue="10",),gr.Slider(minimum=6,maximum=35,step=1,value=15,label="Max Attenuation (dB) - lower is more natural/clear, higher reduces more noise",),gr.Slider(minimum=50,maximum=100,step=5,value=90,label="Voice Naturalness Mix (%) - 100% is fully denoised, 90% blends back some original voice",),gr.Checkbox(label="Enable Post-Processing EQ & Filters",value=True,),gr.Slider(minimum=20,maximum=200,step=10,value=80,label="Low-Cut Filter Cutoff (Hz) - cuts muddy AC rumble/handling noise",),gr.Slider(minimum=-10,maximum=10,step=1,value=0,label="Voice Bass EQ Boost (dB) at 150Hz - adds warmth",),gr.Slider(minimum=-10,maximum=10,step=1,value=3,label="Voice Treble EQ Boost (dB) at 6kHz - adds crispness/air",),gr.Slider(minimum=-60,maximum=-30,step=5,value=-45,label="Noise Gate Threshold (dB) - lower keeps quiet sound, higher silences gaps",),gr.Checkbox(label="Enable Analog Warmth (Soft Saturation)",value=True,),mic_input,12 unmodified linesradio.change(toggle, radio, [mic_input, audio_file])gr.Examples([["file", "./samples/p232_013_clean.wav", "Kitchen", "10", 15, 90, True, 80, 0, 3, -45, True, None],["file", "./samples/p232_013_clean.wav", "Cafe", "10", 15, 90, True, 80, 0, 3, -45, True, None],["file", "./samples/p232_019_clean.wav", "Cafe", "10", 15, 90, True, 80, 0, 3, -45, True, None],["file", "./samples/p232_019_clean.wav", "River", "10", 15, 90, True, 80, 0, 3, -45, True, None],],fn=demo_fn,inputs=inputs,
app.py+62-39