detect_and_trim

Function detect_and_trim 

Source
pub fn detect_and_trim(
    samples: &[f32],
    sample_rate: u32,
    detector: SilenceDetector,
) -> Result<(Vec<f32>, Vec<(usize, usize)>), AnalysisError>
Expand description

Detect and trim silence from audio

This function detects silent regions in audio by analyzing RMS energy per frame, then trims leading and trailing silence. It also returns a map of all silence regions found.

§Arguments

  • samples - Audio samples (mono, normalized to [-1.0, 1.0])
  • sample_rate - Sample rate in Hz
  • detector - Silence detection configuration

§Returns

Tuple of:

  • Vec<f32>: Trimmed samples (leading/trailing silence removed)
  • Vec<(usize, usize)>: Silence map as (start_sample, end_sample) pairs

§Errors

Returns AnalysisError if parameters are invalid or processing fails

§Example

use stratum_dsp::preprocessing::silence::{detect_and_trim, SilenceDetector};

let mut samples = vec![0.0f32; 44100 * 5];
// Add some audio in the middle
for i in 22050..66150 {
    samples[i] = 0.5;
}

let detector = SilenceDetector::default();
let (trimmed, silence_map) = detect_and_trim(&samples, 44100, detector)?;

println!("Trimmed from {} to {} samples", samples.len(), trimmed.len());
println!("Found {} silence regions", silence_map.len());