use crate::{Filter, FilterType};
use rill_core_dsp::filters::FilterParams;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BandType {
Peak,
LowShelf,
HighShelf,
LowPass,
HighPass,
BandPass,
Notch,
}
impl BandType {
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Option<Self> {
match s {
"peak" => Some(BandType::Peak),
"lowshelf" | "low_shelf" => Some(BandType::LowShelf),
"highshelf" | "high_shelf" => Some(BandType::HighShelf),
"lowpass" | "low_pass" => Some(BandType::LowPass),
"highpass" | "high_pass" => Some(BandType::HighPass),
"bandpass" | "band_pass" => Some(BandType::BandPass),
"notch" => Some(BandType::Notch),
_ => None,
}
}
pub fn as_str(&self) -> &'static str {
match self {
BandType::Peak => "peak",
BandType::LowShelf => "low_shelf",
BandType::HighShelf => "high_shelf",
BandType::LowPass => "low_pass",
BandType::HighPass => "high_pass",
BandType::BandPass => "band_pass",
BandType::Notch => "notch",
}
}
pub fn to_filter_type(&self) -> FilterType {
match self {
BandType::Peak => FilterType::Peak,
BandType::LowShelf => FilterType::LowShelf,
BandType::HighShelf => FilterType::HighShelf,
BandType::LowPass => FilterType::LowPass,
BandType::HighPass => FilterType::HighPass,
BandType::BandPass => FilterType::BandPass,
BandType::Notch => FilterType::Notch,
}
}
}
pub struct EqBand<F: Filter<f32>> {
pub(crate) filter: F,
pub(crate) frequency: f32,
pub(crate) q: f32,
pub(crate) gain_db: f32,
pub(crate) enabled: bool,
pub(crate) band_type: BandType,
}
impl<F: Filter<f32>> EqBand<F> {
pub fn new(filter: F, band_type: BandType, frequency: f32, q: f32, gain_db: f32) -> Self {
Self {
filter,
band_type,
frequency,
q,
gain_db,
enabled: true,
}
}
pub fn process(&mut self, input: f32) -> f32 {
if !self.enabled {
return input;
}
let input_slice = [input];
let mut output = [0.0];
self.filter
.process(Some(&input_slice[..]), &mut output)
.unwrap();
output[0]
}
pub fn update_filter(&mut self) {
let params = FilterParams {
filter_type: self.band_type.to_filter_type(),
cutoff: self.frequency,
q: self.q,
gain_db: self.gain_db,
};
self.filter.set_params(params);
}
pub fn set_frequency(&mut self, freq: f32) {
self.frequency = freq.clamp(20.0, 20000.0);
}
pub fn set_q(&mut self, q: f32) {
self.q = q.clamp(0.1, 20.0);
}
pub fn set_gain_db(&mut self, gain: f32) {
self.gain_db = gain.clamp(-24.0, 24.0);
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub fn frequency(&self) -> f32 {
self.frequency
}
pub fn q(&self) -> f32 {
self.q
}
pub fn gain_db(&self) -> f32 {
self.gain_db
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn band_type(&self) -> BandType {
self.band_type
}
pub fn init(&mut self, sample_rate: f32) {
self.filter.init(sample_rate);
}
pub fn reset(&mut self) {
self.filter.reset();
}
}