pub struct NoiseReducer { /* private fields */ }Expand description
Noise reduction via spectral subtraction with adaptive noise profiling.
Implements the noise reduction specification:
- STFT-based processing (25ms window, 10ms hop)
- Adaptive noise profile estimation during VAD-detected silence
- Magnitude-only spectral subtraction (preserves phase)
- Achieves ≥6 dB SNR improvement target
§Performance
- Target: <15ms per 500ms chunk (8000 samples @ 16kHz)
- Expected: ~7ms (2x headroom)
- Optimization: Precomputed FFT plans, reused buffers
§Example
use speech_prep::preprocessing::{NoiseReducer, NoiseReductionConfig, VadContext};
let config = NoiseReductionConfig::default();
let mut reducer = NoiseReducer::new(config)?;
let audio_stream = vec![vec![0.0; 8000], vec![0.05; 8080]];
// Process streaming chunks with VAD context
for chunk in audio_stream {
let vad_ctx = VadContext { is_silence: detect_silence(&chunk) };
let _denoised = reducer.reduce(&chunk, Some(vad_ctx))?;
}Implementations§
Source§impl NoiseReducer
impl NoiseReducer
Sourcepub fn new(config: NoiseReductionConfig) -> Result<Self>
pub fn new(config: NoiseReductionConfig) -> Result<Self>
Create a new noise reducer.
§Arguments
config- Configuration parameters (window size, hop, α, β)
§Errors
Returns Error::Configuration if configuration is invalid.
§Example
use speech_prep::preprocessing::{NoiseReducer, NoiseReductionConfig};
let config = NoiseReductionConfig {
oversubtraction_factor: 2.5, // Aggressive
..Default::default()
};
let reducer = NoiseReducer::new(config)?;Sourcepub fn reduce(
&mut self,
samples: &[f32],
vad_context: Option<VadContext>,
) -> Result<Vec<f32>>
pub fn reduce( &mut self, samples: &[f32], vad_context: Option<VadContext>, ) -> Result<Vec<f32>>
Apply noise reduction to audio samples.
§Arguments
samples- Input audio samples (typically 500ms chunk = 8000 samples @ 16kHz)vad_context- Optional VAD state for noise profile updates
§Returns
Denoised audio with ≥6 dB SNR improvement on noisy input.
§Performance
- Expected: ~7ms for 8000 samples (2x better than <15ms target)
- Complexity: O(n log n) for FFT operations
§Example
use speech_prep::preprocessing::{NoiseReducer, NoiseReductionConfig, VadContext};
let mut reducer = NoiseReducer::new(NoiseReductionConfig::default())?;
// Chunk 1 (silence - initialize noise profile)
let chunk1 = vec![0.001; 8000];
let vad1 = VadContext { is_silence: true };
let output1 = reducer.reduce(&chunk1, Some(vad1))?;
// Chunk 2 (speech - apply noise reduction)
let chunk2 = vec![0.1; 8000];
let vad2 = VadContext { is_silence: false };
let output2 = reducer.reduce(&chunk2, Some(vad2))?;Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset noise profile for new audio stream.
Clears noise estimate and overlap-add state. Use this when starting a new, independent audio stream.
Sourcepub fn noise_floor(&self) -> f32
pub fn noise_floor(&self) -> f32
Get current average noise floor (for debugging/observability).
Sourcepub fn config(&self) -> &NoiseReductionConfig
pub fn config(&self) -> &NoiseReductionConfig
Get current configuration.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for NoiseReducer
impl !RefUnwindSafe for NoiseReducer
impl Send for NoiseReducer
impl Sync for NoiseReducer
impl Unpin for NoiseReducer
impl UnsafeUnpin for NoiseReducer
impl !UnwindSafe for NoiseReducer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more