From d7188bebcef69fe8a73bbd200cd6f054653e988b Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Fri, 6 Oct 2023 14:34:15 +0100 Subject: [PATCH 1/8] Allowed q value to be passed to filter functions I have added a 2nd parameter to the filter functions low_pass, high_pass, to_low_pass, and to_high pass; allowing a q value to be passed along with the frequency. --- src/source/blt.rs | 16 ++++++++-------- src/source/mod.rs | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/source/blt.rs b/src/source/blt.rs index fa3e33cd..c5068686 100644 --- a/src/source/blt.rs +++ b/src/source/blt.rs @@ -6,13 +6,13 @@ use crate::Source; // Implemented following http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt /// Internal function that builds a `BltFilter` object. -pub fn low_pass(input: I, freq: u32) -> BltFilter +pub fn low_pass(input: I, freq: u32, q: f32) -> BltFilter where I: Source, { BltFilter { input, - formula: BltFormula::LowPass { freq, q: 0.5 }, + formula: BltFormula::LowPass { freq, q }, applier: None, x_n1: 0.0, x_n2: 0.0, @@ -21,13 +21,13 @@ where } } -pub fn high_pass(input: I, freq: u32) -> BltFilter +pub fn high_pass(input: I, freq: u32, q: f32) -> BltFilter where I: Source, { BltFilter { input, - formula: BltFormula::HighPass { freq, q: 0.5 }, + formula: BltFormula::HighPass { freq, q }, applier: None, x_n1: 0.0, x_n2: 0.0, @@ -49,14 +49,14 @@ pub struct BltFilter { impl BltFilter { /// Modifies this filter so that it becomes a low-pass filter. - pub fn to_low_pass(&mut self, freq: u32) { - self.formula = BltFormula::LowPass { freq, q: 0.5 }; + pub fn to_low_pass(&mut self, freq: u32, q: f32) { + self.formula = BltFormula::LowPass { freq, q }; self.applier = None; } /// Modifies this filter so that it becomes a high-pass filter - pub fn to_high_pass(&mut self, freq: u32) { - self.formula = BltFormula::HighPass { freq, q: 0.5 }; + pub fn to_high_pass(&mut self, freq: u32, q: f32) { + self.formula = BltFormula::HighPass { freq, q }; self.applier = None; } diff --git a/src/source/mod.rs b/src/source/mod.rs index c2d595a4..dccd4fd5 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -335,21 +335,21 @@ where /// Applies a low-pass filter to the source. /// **Warning**: Probably buggy. #[inline] - fn low_pass(self, freq: u32) -> BltFilter + fn low_pass(self, freq: u32, q: f32) -> BltFilter where Self: Sized, Self: Source, { - blt::low_pass(self, freq) + blt::low_pass(self, freq, q) } #[inline] - fn high_pass(self, freq: u32) -> BltFilter + fn high_pass(self, freq: u32, q: f32) -> BltFilter where Self: Sized, Self: Source, { - blt::high_pass(self, freq) + blt::high_pass(self, freq, q) } } From 926301f9982719c4ed9bc5de9282ba9f693cbc53 Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Fri, 6 Oct 2023 17:06:36 +0100 Subject: [PATCH 2/8] Added secondary filter with_q methods --- src/source/blt.rs | 49 +++++++++++++++++++++++++++++++++++++++++++---- src/source/mod.rs | 25 ++++++++++++++++++++---- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/source/blt.rs b/src/source/blt.rs index c5068686..d32b6310 100644 --- a/src/source/blt.rs +++ b/src/source/blt.rs @@ -6,7 +6,37 @@ use crate::Source; // Implemented following http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt /// Internal function that builds a `BltFilter` object. -pub fn low_pass(input: I, freq: u32, q: f32) -> BltFilter +pub fn low_pass(input: I, freq: u32) -> BltFilter +where + I: Source, +{ + BltFilter { + input, + formula: BltFormula::LowPass { freq, q: 0.5 }, + applier: None, + x_n1: 0.0, + x_n2: 0.0, + y_n1: 0.0, + y_n2: 0.0, + } +} + +pub fn high_pass(input: I, freq: u32) -> BltFilter +where + I: Source, +{ + BltFilter { + input, + formula: BltFormula::HighPass { freq, q: 0.5 }, + applier: None, + x_n1: 0.0, + x_n2: 0.0, + y_n1: 0.0, + y_n2: 0.0, + } +} + +pub fn low_pass_with_q(input: I, freq: u32, q: f32) -> BltFilter where I: Source, { @@ -21,7 +51,7 @@ where } } -pub fn high_pass(input: I, freq: u32, q: f32) -> BltFilter +pub fn high_pass_with_q(input: I, freq: u32, q: f32) -> BltFilter where I: Source, { @@ -49,13 +79,24 @@ pub struct BltFilter { impl BltFilter { /// Modifies this filter so that it becomes a low-pass filter. - pub fn to_low_pass(&mut self, freq: u32, q: f32) { + pub fn to_low_pass(&mut self, freq: u32) { + self.formula = BltFormula::LowPass { freq, q: 0.5 }; + self.applier = None; + } + + /// Modifies this filter so that it becomes a high-pass filter + pub fn to_high_pass(&mut self, freq: u32) { + self.formula = BltFormula::HighPass { freq, q: 0.5 }; + self.applier = None; + } + + pub fn to_low_pass_with_q(&mut self, freq: u32, q: f32) { self.formula = BltFormula::LowPass { freq, q }; self.applier = None; } /// Modifies this filter so that it becomes a high-pass filter - pub fn to_high_pass(&mut self, freq: u32, q: f32) { + pub fn to_high_pass_with_q(&mut self, freq: u32, q: f32) { self.formula = BltFormula::HighPass { freq, q }; self.applier = None; } diff --git a/src/source/mod.rs b/src/source/mod.rs index dccd4fd5..318cf767 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -335,21 +335,38 @@ where /// Applies a low-pass filter to the source. /// **Warning**: Probably buggy. #[inline] - fn low_pass(self, freq: u32, q: f32) -> BltFilter + fn low_pass(self, freq: u32) -> BltFilter where Self: Sized, Self: Source, { - blt::low_pass(self, freq, q) + blt::low_pass(self, freq) } #[inline] - fn high_pass(self, freq: u32, q: f32) -> BltFilter + fn high_pass(self, freq: u32) -> BltFilter where Self: Sized, Self: Source, { - blt::high_pass(self, freq, q) + blt::high_pass(self, freq) + } + + fn low_pass_with_q(self, freq: u32, q: f32) -> BltFilter + where + Self: Sized, + Self: Source, + { + blt::low_pass_with_q(self, freq, q) + } + + #[inline] + fn high_pass_with_q(self, freq: u32, q: f32) -> BltFilter + where + Self: Sized, + Self: Source, + { + blt::high_pass_with_q(self, freq, q) } } From 4112e556832aa39d622da079b2eb2ddcbb83b130 Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Fri, 6 Oct 2023 17:08:55 +0100 Subject: [PATCH 3/8] Added comments --- src/source/blt.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/source/blt.rs b/src/source/blt.rs index d32b6310..ffbcbd47 100644 --- a/src/source/blt.rs +++ b/src/source/blt.rs @@ -36,6 +36,7 @@ where } } +/// Same as low_pass but allows the q value (bandwidth) to be changed pub fn low_pass_with_q(input: I, freq: u32, q: f32) -> BltFilter where I: Source, @@ -51,6 +52,7 @@ where } } +/// Same as low_pass but allows the q value (bandwidth) to be changed pub fn high_pass_with_q(input: I, freq: u32, q: f32) -> BltFilter where I: Source, @@ -90,12 +92,13 @@ impl BltFilter { self.applier = None; } + /// Same as to_low_pass but allows the q value (bandwidth) to be changed pub fn to_low_pass_with_q(&mut self, freq: u32, q: f32) { self.formula = BltFormula::LowPass { freq, q }; self.applier = None; } - /// Modifies this filter so that it becomes a high-pass filter + /// Same as to_high_pass but allows the q value (bandwidth) to be changed pub fn to_high_pass_with_q(&mut self, freq: u32, q: f32) { self.formula = BltFormula::HighPass { freq, q }; self.applier = None; From 0fc0aaa93b07ed84293584d854630aa26ca32a52 Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Fri, 6 Oct 2023 18:39:12 +0100 Subject: [PATCH 4/8] old functions now call with q functions The low_pass, etc. now just call low_pass_with_q while also passing the original default q value of 0.5 --- src/source/blt.rs | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/source/blt.rs b/src/source/blt.rs index ffbcbd47..adb567b5 100644 --- a/src/source/blt.rs +++ b/src/source/blt.rs @@ -10,30 +10,14 @@ pub fn low_pass(input: I, freq: u32) -> BltFilter where I: Source, { - BltFilter { - input, - formula: BltFormula::LowPass { freq, q: 0.5 }, - applier: None, - x_n1: 0.0, - x_n2: 0.0, - y_n1: 0.0, - y_n2: 0.0, - } + low_pass_with_q(input, freq, 0.5) } pub fn high_pass(input: I, freq: u32) -> BltFilter where I: Source, { - BltFilter { - input, - formula: BltFormula::HighPass { freq, q: 0.5 }, - applier: None, - x_n1: 0.0, - x_n2: 0.0, - y_n1: 0.0, - y_n2: 0.0, - } + high_pass_with_q(input, freq, 0.5) } /// Same as low_pass but allows the q value (bandwidth) to be changed @@ -82,14 +66,12 @@ pub struct BltFilter { impl BltFilter { /// Modifies this filter so that it becomes a low-pass filter. pub fn to_low_pass(&mut self, freq: u32) { - self.formula = BltFormula::LowPass { freq, q: 0.5 }; - self.applier = None; + self.to_low_pass_with_q(freq, 0.5); } /// Modifies this filter so that it becomes a high-pass filter pub fn to_high_pass(&mut self, freq: u32) { - self.formula = BltFormula::HighPass { freq, q: 0.5 }; - self.applier = None; + self.to_high_pass_with_q(freq, 0.5); } /// Same as to_low_pass but allows the q value (bandwidth) to be changed From 158cd8a9e9c696f492020f8f8f2214fbb01d9aab Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Sat, 7 Oct 2023 23:00:46 +0100 Subject: [PATCH 5/8] Update src/source/blt.rs Co-authored-by: David Kleingeld --- src/source/blt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/source/blt.rs b/src/source/blt.rs index adb567b5..fe1c2f3c 100644 --- a/src/source/blt.rs +++ b/src/source/blt.rs @@ -36,7 +36,7 @@ where } } -/// Same as low_pass but allows the q value (bandwidth) to be changed +/// Same as high_pass but allows the q value (bandwidth) to be changed pub fn high_pass_with_q(input: I, freq: u32, q: f32) -> BltFilter where I: Source, From 67e3369b96c97105ab75005521a948540c60ea30 Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Sat, 7 Oct 2023 23:01:02 +0100 Subject: [PATCH 6/8] Update src/source/mod.rs Co-authored-by: David Kleingeld --- src/source/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/source/mod.rs b/src/source/mod.rs index 318cf767..c8d19240 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -352,6 +352,7 @@ where blt::high_pass(self, freq) } +#[inline] fn low_pass_with_q(self, freq: u32, q: f32) -> BltFilter where Self: Sized, From 44f6c31c995423f51d950e88cb838774d1fba40e Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Sat, 7 Oct 2023 23:03:19 +0100 Subject: [PATCH 7/8] Update mod.rs --- src/source/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/source/mod.rs b/src/source/mod.rs index c8d19240..2976f208 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -352,7 +352,7 @@ where blt::high_pass(self, freq) } -#[inline] + #[inline] fn low_pass_with_q(self, freq: u32, q: f32) -> BltFilter where Self: Sized, From adfc5329f20235d127b17c85d7e351e61f4326fc Mon Sep 17 00:00:00 2001 From: Willow Hamid-Goodwin Date: Sat, 7 Oct 2023 23:04:59 +0100 Subject: [PATCH 8/8] Added comments for q value --- src/source/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/source/mod.rs b/src/source/mod.rs index 2976f208..2af80075 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -343,6 +343,7 @@ where blt::low_pass(self, freq) } + /// Applies a high-pass filter to the source. #[inline] fn high_pass(self, freq: u32) -> BltFilter where @@ -352,6 +353,7 @@ where blt::high_pass(self, freq) } + /// Applies a low-pass filter to the source while allowing the q (badnwidth) to be changed. #[inline] fn low_pass_with_q(self, freq: u32, q: f32) -> BltFilter where @@ -361,6 +363,7 @@ where blt::low_pass_with_q(self, freq, q) } + /// Applies a high-pass filter to the source while allowing the q (badnwidth) to be changed. #[inline] fn high_pass_with_q(self, freq: u32, q: f32) -> BltFilter where