Skip to content

Commit

Permalink
Added secondary filter with_q methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xwillxw committed Oct 6, 2023
1 parent d7188be commit 926301f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
49 changes: 45 additions & 4 deletions src/source/blt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I>(input: I, freq: u32, q: f32) -> BltFilter<I>
pub fn low_pass<I>(input: I, freq: u32) -> BltFilter<I>
where
I: Source<Item = f32>,
{
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<I>(input: I, freq: u32) -> BltFilter<I>
where
I: Source<Item = f32>,
{
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<I>(input: I, freq: u32, q: f32) -> BltFilter<I>
where
I: Source<Item = f32>,
{
Expand All @@ -21,7 +51,7 @@ where
}
}

pub fn high_pass<I>(input: I, freq: u32, q: f32) -> BltFilter<I>
pub fn high_pass_with_q<I>(input: I, freq: u32, q: f32) -> BltFilter<I>
where
I: Source<Item = f32>,
{
Expand Down Expand Up @@ -49,13 +79,24 @@ pub struct BltFilter<I> {

impl<I> BltFilter<I> {
/// 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;
}
Expand Down
25 changes: 21 additions & 4 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>
fn low_pass(self, freq: u32) -> BltFilter<Self>
where
Self: Sized,
Self: Source<Item = f32>,
{
blt::low_pass(self, freq, q)
blt::low_pass(self, freq)
}

#[inline]
fn high_pass(self, freq: u32, q: f32) -> BltFilter<Self>
fn high_pass(self, freq: u32) -> BltFilter<Self>
where
Self: Sized,
Self: Source<Item = f32>,
{
blt::high_pass(self, freq, q)
blt::high_pass(self, freq)
}

fn low_pass_with_q(self, freq: u32, q: f32) -> BltFilter<Self>
where
Self: Sized,
Self: Source<Item = f32>,
{
blt::low_pass_with_q(self, freq, q)
}

#[inline]
fn high_pass_with_q(self, freq: u32, q: f32) -> BltFilter<Self>
where
Self: Sized,
Self: Source<Item = f32>,
{
blt::high_pass_with_q(self, freq, q)
}
}

Expand Down

0 comments on commit 926301f

Please sign in to comment.