From 3cb2c612fb030278aaf5e4d49d42cf689bdc9cc0 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 12 Jun 2021 15:58:44 +0200 Subject: re-add noise gate --- mumd/src/audio/transformers.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 mumd/src/audio/transformers.rs (limited to 'mumd/src/audio/transformers.rs') diff --git a/mumd/src/audio/transformers.rs b/mumd/src/audio/transformers.rs new file mode 100644 index 0000000..5c9681b --- /dev/null +++ b/mumd/src/audio/transformers.rs @@ -0,0 +1,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 + } + } +} -- cgit v1.2.1 From 5b2e2c4e266c61f35f7bcfbc603e02070b54e7d0 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 12 Jun 2021 16:47:36 +0200 Subject: FnMut -> Trait --- mumd/src/audio/transformers.rs | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'mumd/src/audio/transformers.rs') diff --git a/mumd/src/audio/transformers.rs b/mumd/src/audio/transformers.rs index 5c9681b..6994a40 100644 --- a/mumd/src/audio/transformers.rs +++ b/mumd/src/audio/transformers.rs @@ -1,17 +1,43 @@ -use dasp_ring_buffer::Bounded; +pub trait Transformer { + fn transform<'a>(&mut self, buf: &'a mut [f32]) -> Option<&'a mut [f32]>; +} + +pub struct NoiseGate { + alltime_high: f32, + open: usize, + deactivation_delay: usize, +} + +impl NoiseGate { + pub fn new(deactivation_delay: usize) -> Self { + Self { + alltime_high: 0.0, + open: 0, + deactivation_delay, + } + } +} +impl Transformer for NoiseGate { + fn transform<'a>(&mut self, buf: &'a mut [f32]) -> Option<&'a mut [f32]> { + const MUTE_PERCENTAGE: f32 = 0.1; -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 { + + if max > self.alltime_high { + self.alltime_high = max; + } + + if max > self.alltime_high * MUTE_PERCENTAGE { + self.open = self.deactivation_delay; + } else if self.open > 0 { + self.open -= 1; + } + + if self.open == 0 { None + } else { + Some(buf) } } } -- cgit v1.2.1 From c143931f9948a0249213bdaab2658a31d4dddaf2 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 12 Jun 2021 17:40:41 +0200 Subject: add documentation --- mumd/src/audio/transformers.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'mumd/src/audio/transformers.rs') diff --git a/mumd/src/audio/transformers.rs b/mumd/src/audio/transformers.rs index 6994a40..25e28b8 100644 --- a/mumd/src/audio/transformers.rs +++ b/mumd/src/audio/transformers.rs @@ -1,7 +1,11 @@ +/// A trait that represents a transform of a audio buffer in some way. pub trait Transformer { + /// Do the transform. Returning `None` is interpreted as "the buffer is unwanted". + /// The implementor is free to modify the buffer however it wants to. fn transform<'a>(&mut self, buf: &'a mut [f32]) -> Option<&'a mut [f32]>; } +/// A struct representing a noise gate transform. pub struct NoiseGate { alltime_high: f32, open: usize, @@ -9,6 +13,8 @@ pub struct NoiseGate { } impl NoiseGate { + /// Create a new noise gate. `deactivation_delay` is defined in terms of + /// how many quiet frames it receives before closing the noise gate. pub fn new(deactivation_delay: usize) -> Self { Self { alltime_high: 0.0, -- cgit v1.2.1