summaryrefslogtreecommitdiff
path: root/src/retain.rs
blob: ad166bee856f03a2d918c25c65b6528dd478c7b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use num_complex::Complex;
use rustc_hash::{FxBuildHasher, FxHashSet};

/// I thought about doing this to avoid allocating new memory in the audio thread
pub struct RetainAlgorithm {
    indexed_spectrum: Vec<(usize, Complex<f32>)>,
    top_indices: FxHashSet<usize>,
}

impl RetainAlgorithm {
    pub fn new() -> Self {
        let max_size = (32768 / 2) + 1;

        Self {
            indexed_spectrum: Vec::with_capacity(max_size),
            top_indices: FxHashSet::with_capacity_and_hasher(max_size, FxBuildHasher::default()),
        }
    }

    pub fn calculate(&mut self, spectrum: &mut [Complex<f32>], n: usize, complement: bool) {
        // you'd be keeping the entire signal
        if n >= spectrum.len() && !complement {
            return;
        }

        if n == 0 && complement {
            return;
        }

        // keeping nothing at all
        if n == 0 && !complement {
            for phasor in spectrum {
                *phasor = Complex::ZERO;
            }

            return;
        }

        if n >= spectrum.len() && complement {
            for phasor in spectrum {
                *phasor = Complex::ZERO;
            }

            return;
        }

        self.indexed_spectrum.clear();
        for (i, phasor) in spectrum.iter().copied().enumerate() {
            self.indexed_spectrum.push((i, phasor));
        }

        let target = spectrum.len() - n;
        self.indexed_spectrum
            .select_nth_unstable_by(target, |(_, c0), (_, c1)| c0.norm().total_cmp(&c1.norm()));

        self.top_indices.clear();
        for i in self.indexed_spectrum[target..].iter().map(|&(i, _)| i) {
            self.top_indices.insert(i);
        }

        for (i, phasor) in spectrum.iter_mut().enumerate() {
            if self.top_indices.contains(&i) && complement {
                *phasor = Complex::ZERO;
            }

            if !self.top_indices.contains(&i) && !complement {
                *phasor = Complex::ZERO;
            }
        }
    }
}