miniaudio/filters/
low_shelf_filter.rs

1use super::biquad_filtering::Biquad;
2use crate::base::{Error, Format};
3use crate::frames::{Frames, FramesMut};
4use miniaudio_sys as sys;
5
6/// Configuration for a second order low shelf filter.
7#[repr(transparent)]
8#[derive(Clone)]
9pub struct LowShelf2Config(sys::ma_loshelf2_config);
10
11impl LowShelf2Config {
12    #[inline]
13    pub fn new(
14        format: Format,
15        channels: u32,
16        sample_rate: u32,
17        gain_db: f64,
18        shelf_slope: f64,
19        frequency: f64,
20    ) -> LowShelf2Config {
21        LowShelf2Config(unsafe {
22            sys::ma_loshelf2_config_init(
23                format as _,
24                channels,
25                sample_rate,
26                gain_db,
27                shelf_slope,
28                frequency,
29            )
30        })
31    }
32
33    #[inline]
34    pub fn format(&self) -> Format {
35        Format::from_c(self.0.format)
36    }
37
38    #[inline]
39    pub fn set_format(&mut self, format: Format) {
40        self.0.format = format as _;
41    }
42
43    #[inline]
44    pub fn channels(&self) -> u32 {
45        self.0.channels
46    }
47
48    #[inline]
49    pub fn set_channels(&mut self, channels: u32) {
50        self.0.channels = channels;
51    }
52
53    #[inline]
54    pub fn sample_rate(&self) -> u32 {
55        self.0.sampleRate
56    }
57
58    #[inline]
59    pub fn set_sample_rate(&mut self, sample_rate: u32) {
60        self.0.sampleRate = sample_rate;
61    }
62
63    #[inline]
64    pub fn frequency(&self) -> f64 {
65        self.0.frequency
66    }
67
68    #[inline]
69    pub fn set_frequency(&mut self, frequency: f64) {
70        self.0.frequency = frequency;
71    }
72
73    #[inline]
74    pub fn gain_db(&self) -> f64 {
75        self.0.gainDB
76    }
77
78    #[inline]
79    pub fn set_gain_db(&mut self, gain_db: f64) {
80        self.0.gainDB = gain_db;
81    }
82
83    #[inline]
84    pub fn shelf_slope(&self) -> f64 {
85        self.0.shelfSlope
86    }
87
88    #[inline]
89    pub fn set_shelf_slope(&mut self, shelf_slope: f64) {
90        self.0.shelfSlope = shelf_slope;
91    }
92}
93
94/// Second order low shelf filter.
95#[repr(transparent)]
96#[derive(Clone)]
97pub struct LowShelf2(sys::ma_loshelf2);
98
99impl LowShelf2 {
100    #[inline]
101    pub fn new(config: &LowShelf2Config) -> Result<LowShelf2, Error> {
102        let mut loshelf2 = std::mem::MaybeUninit::<LowShelf2>::uninit();
103        unsafe {
104            Error::from_c_result(sys::ma_loshelf2_init(
105                config as *const LowShelf2Config as *const _,
106                loshelf2.as_mut_ptr() as *mut _,
107            ))?;
108            Ok(loshelf2.assume_init())
109        }
110    }
111
112    pub fn reinit(&mut self, config: &LowShelf2Config) -> Result<(), Error> {
113        Error::from_c_result(unsafe {
114            sys::ma_loshelf2_reinit(config as *const LowShelf2Config as *const _, &mut self.0)
115        })
116    }
117
118    #[inline]
119    pub fn process_pcm_frames(
120        &mut self,
121        output: &mut FramesMut,
122        input: &Frames,
123    ) -> Result<(), Error> {
124        if output.format() != input.format() {
125            ma_debug_panic!(
126                "output and input format did not match (output: {:?}, input: {:?}",
127                output.format(),
128                input.format()
129            );
130            return Err(Error::InvalidArgs);
131        }
132
133        if output.frame_count() != input.frame_count() {
134            ma_debug_panic!("output and input buffers did not have the same frame count (output: {}, input: {})", output.frame_count(), input.frame_count());
135            return Err(Error::InvalidArgs);
136        }
137
138        Error::from_c_result(unsafe {
139            sys::ma_loshelf2_process_pcm_frames(
140                &mut self.0 as *mut _,
141                output.as_mut_ptr() as *mut _,
142                input.as_ptr() as *const _,
143                output.frame_count() as u64,
144            )
145        })
146    }
147
148    #[inline]
149    pub fn bq(&self) -> &Biquad {
150        unsafe { &*(&self.0.bq as *const sys::ma_biquad as *const Biquad) }
151    }
152
153    #[inline]
154    pub fn latency(&self) -> u32 {
155        unsafe { sys::ma_loshelf2_get_latency(&self.0 as *const _ as *mut _) }
156    }
157}