From b9604f08d2a329cc810ab62706f598d5fcc0da7d Mon Sep 17 00:00:00 2001 From: ozpv <39195175+ozpv@users.noreply.github.com> Date: Mon, 22 Jun 2026 04:26:18 +0000 Subject: Fix duplicating the plugin instance --- src/retain.rs | 109 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 55 insertions(+), 54 deletions(-) (limited to 'src/retain.rs') diff --git a/src/retain.rs b/src/retain.rs index 8e11164..ad166be 100644 --- a/src/retain.rs +++ b/src/retain.rs @@ -3,68 +3,69 @@ 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)>, - top_indices: FxHashSet, + indexed_spectrum: Vec<(usize, Complex)>, + top_indices: FxHashSet, } 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], n: usize, complement: bool) { - // you'd be keeping the entire signal - if n >= spectrum.len() && !complement { - return; - } + pub fn new() -> Self { + let max_size = (32768 / 2) + 1; - if n == 0 && complement { - return; - } + Self { + indexed_spectrum: Vec::with_capacity(max_size), + top_indices: FxHashSet::with_capacity_and_hasher(max_size, FxBuildHasher::default()), + } + } - // keeping nothing at all - if n == 0 && !complement { - for phasor in spectrum { - *phasor = Complex::ZERO; - } + pub fn calculate(&mut self, spectrum: &mut [Complex], n: usize, complement: bool) { + // you'd be keeping the entire signal + if n >= spectrum.len() && !complement { + return; + } - return; - } + if n == 0 && complement { + return; + } - if n >= spectrum.len() && complement { - for phasor in spectrum { - *phasor = Complex::ZERO; - } + // keeping nothing at all + if n == 0 && !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)); - } + return; + } - 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); - } + if n >= spectrum.len() && complement { + for phasor in spectrum { + *phasor = Complex::ZERO; + } - for (i, phasor) in spectrum.iter_mut().enumerate() { - if self.top_indices.contains(&i) && complement { - *phasor = Complex::ZERO; - } + return; + } - if !self.top_indices.contains(&i) && !complement { - *phasor = Complex::ZERO; - } - } - } -} \ No newline at end of file + 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; + } + } + } +} -- cgit v1.2.3