unnieversal/crates/threebandeq/src/lib.rs

112 lines
2.7 KiB
Rust

#![allow(incomplete_features)]
#![feature(generic_associated_types)]
// https://www.musicdsp.org/en/latest/Filters/236-3-band-equaliser.html
use baseplug::{Plugin, ProcessContext};
use serde::{Deserialize, Serialize};
use utils::threeband::*;
baseplug::model! {
#[derive(Debug, Serialize, Deserialize)]
struct ThreeBandEqModel {
#[model(min = 0.0, max = 15000.0)]
#[parameter(name = "low band")]
low_band: f32,
#[model(min = 0.0, max = 15000.0)]
#[parameter(name = "mid band")]
high_band: f32,
#[model(min = 0.0, max = 3.0)]
#[parameter(name = "low gain")]
low_gain: f32,
#[model(min = 0.0, max = 3.0)]
#[parameter(name = "mid gain")]
mid_gain: f32,
#[model(min = 0.0, max = 3.0)]
#[parameter(name = "high gain")]
high_gain: f32,
}
}
impl Default for ThreeBandEqModel {
fn default() -> Self {
Self {
low_band: 800.0,
high_band: 5000.0,
low_gain: 1.0,
mid_gain: 1.0,
high_gain: 1.0,
}
}
}
#[derive(Default)]
struct Eq(ThreeBandSplitter);
impl Eq {
#[allow(clippy::too_many_arguments)]
pub fn process(
&mut self,
input: f32,
lf: f32,
hf: f32,
lg: f32,
mg: f32,
hg: f32,
sample_rate: f32,
) -> f32 {
let (l, m, h) = self.0.process(input, lf, hf, sample_rate);
l * lg + m * mg + h * hg
}
}
#[derive(Default)]
struct ThreeBandEq {
l_eq: Eq,
r_eq: Eq,
}
impl Plugin for ThreeBandEq {
const NAME: &'static str = "threebandeq";
const PRODUCT: &'static str = "threebandeq";
const VENDOR: &'static str = "unnieversal";
const INPUT_CHANNELS: usize = 2;
const OUTPUT_CHANNELS: usize = 2;
type Model = ThreeBandEqModel;
#[inline]
fn new(_sample_rate: f32, _model: &ThreeBandEqModel) -> Self {
// Default cause we want to start with 0s in everything
Self::default()
}
#[inline]
fn process(&mut self, model: &ThreeBandEqModelProcess, ctx: &mut ProcessContext<Self>) {
let input = &ctx.inputs[0].buffers;
let output = &mut ctx.outputs[0].buffers;
let sr = ctx.sample_rate;
for i in 0..ctx.nframes {
// frequencies
let lf = model.low_band[i];
let hf = model.high_band[i];
// gains
let lg = model.low_gain[i];
let mg = model.mid_gain[i];
let hg = model.high_gain[i];
output[0][i] = self.l_eq.process(input[0][i], lf, hf, lg, mg, hg, sr);
output[1][i] = self.r_eq.process(input[1][i], lf, hf, lg, mg, hg, sr);
}
}
}
baseplug::vst2!(ThreeBandEq, b"tAnE");