Skip to main content

rill_core_dsp/lang/
biquad.rs

1use rill_core::builtin::BlockBuiltin;
2use rill_core::math::Transcendental;
3use rill_core::traits::algorithm::Algorithm;
4use rill_core::traits::ProcessResult;
5
6use crate::algorithm::ParameterizedAlgorithm;
7use crate::filters::{Biquad, Filter, FilterType};
8use crate::lang::pv_f32;
9
10pub struct BiquadBuiltin<T: Transcendental> {
11    pub inner: Biquad<T>,
12}
13
14impl<T: Transcendental> Algorithm<T> for BiquadBuiltin<T> {
15    fn process(&mut self, input: Option<&[T]>, output: &mut [T]) -> ProcessResult<()> {
16        self.inner.process(input, output)
17    }
18    fn reset(&mut self) {
19        Algorithm::reset(&mut self.inner);
20    }
21    fn init(&mut self, sample_rate: f32) {
22        Algorithm::init(&mut self.inner, sample_rate);
23    }
24    fn apply_command(&mut self, value: T) {
25        Algorithm::apply_command(&mut self.inner, value);
26    }
27}
28
29impl<T: Transcendental> BlockBuiltin<T> for BiquadBuiltin<T> {
30    fn set_param(&mut self, index: usize, value: &rill_core::traits::ParamValue) {
31        let v = pv_f32(value);
32        match index {
33            0 => Filter::set_cutoff(&mut self.inner, v),
34            1 => Filter::set_q(&mut self.inner, v),
35            _ => {}
36        }
37    }
38}
39
40pub struct GeneralBiquadBuiltin<T: Transcendental> {
41    pub inner: Biquad<T>,
42}
43
44impl<T: Transcendental> Algorithm<T> for GeneralBiquadBuiltin<T> {
45    fn process(&mut self, input: Option<&[T]>, output: &mut [T]) -> ProcessResult<()> {
46        self.inner.process(input, output)
47    }
48    fn reset(&mut self) {
49        Algorithm::reset(&mut self.inner);
50    }
51    fn init(&mut self, sample_rate: f32) {
52        Algorithm::init(&mut self.inner, sample_rate);
53    }
54    fn apply_command(&mut self, value: T) {
55        Algorithm::apply_command(&mut self.inner, value);
56    }
57}
58
59impl<T: Transcendental> BlockBuiltin<T> for GeneralBiquadBuiltin<T> {
60    fn set_param(&mut self, index: usize, value: &rill_core::traits::ParamValue) {
61        let v = pv_f32(value);
62        match index {
63            0 => {
64                let ft = match v as u8 {
65                    0 => FilterType::LowPass,
66                    1 => FilterType::HighPass,
67                    2 => FilterType::BandPass,
68                    3 => FilterType::Notch,
69                    4 => FilterType::Peak,
70                    5 => FilterType::LowShelf,
71                    6 => FilterType::HighShelf,
72                    _ => FilterType::LowPass,
73                };
74                let mut params = self.inner.params().clone();
75                params.filter_type = ft;
76                self.inner.set_params(params);
77            }
78            1 => Filter::set_cutoff(&mut self.inner, v),
79            2 => Filter::set_q(&mut self.inner, v),
80            3 => Filter::set_gain_db(&mut self.inner, v),
81            _ => {}
82        }
83    }
84}