aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/audio/transformers.rs
blob: 5c9681b0d7cb170449883b74653ffc61de66537d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use dasp_ring_buffer::Bounded;


pub fn create_noise_gate(chunks: usize, mute_percentage: f32) -> impl FnMut(&mut [f32]) -> Option<&mut [f32]> {
    let mut peaks = Bounded::from_full(vec![0.0; chunks]);
    let mut alltime_high: f32 = 0.0;
    move |buf: &mut [f32]| {
        let max = buf.iter().map(|e| e.abs()).max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap();
        peaks.push(max);
        alltime_high = alltime_high.max(max);
        if peaks.iter().any(|e| *e >= alltime_high * mute_percentage) {
            Some(buf)
        } else {
            None
        }
    }
}