Skip to content

Commit

Permalink
refactor: move OpAmp saturator into dsp
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarLiner committed Feb 7, 2024
1 parent c3efa8c commit fba8a19
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
41 changes: 37 additions & 4 deletions examples/svfmixer/src/dsp.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::OpAmp;
use enum_map::{enum_map, Enum, EnumMap};
use nalgebra::SMatrix;
use nih_plug::util::db_to_gain_fast;
use valib::dsp::blocks::ModMatrix;
use valib::dsp::parameter::{HasParameters, Parameter, SmoothedParam};
use valib::dsp::DSP;
use valib::filters::svf::Svf;
use valib::oversample::Oversampled;
use valib::saturators::{Clipper, Saturator, Slew};
use valib::simd::{AutoSimd, SimdValue};
use valib::Scalar;

type Sample = AutoSimd<[f32; 2]>;

Expand Down Expand Up @@ -81,16 +83,47 @@ impl HasParameters for DspInner {
impl DSP<1, 1> for DspInner {
type Sample = Sample;

fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] {
fn process(&mut self, [x]: [Self::Sample; 1]) -> [Self::Sample; 1] {
self.filter
.set_cutoff(Sample::splat(self.params[DspParam::Cutoff].next_sample()));
self.filter.set_r(Sample::splat(
self.params[DspParam::Resonance].next_sample(),
1.0 - self.params[DspParam::Resonance].next_sample(),
));
self.mod_matrix.weights.x = Sample::splat(self.params[DspParam::LpGain].next_sample());
self.mod_matrix.weights.y = Sample::splat(self.params[DspParam::BpGain].next_sample());
self.mod_matrix.weights.z = Sample::splat(self.params[DspParam::HpGain].next_sample());

self.mod_matrix.process(self.filter.process(x))
let drive = Sample::splat(db_to_gain_fast(self.params[DspParam::Drive].next_sample()));
let [out] = self.mod_matrix.process(self.filter.process([x * drive]));
[out / drive]
}
}

#[derive(Debug, Clone, Copy)]
struct OpAmp<T>(Clipper, Slew<T>);

impl<T: Scalar> Default for OpAmp<T> {
fn default() -> Self {
Self(Default::default(), Default::default())
}
}

impl<T: Scalar> Saturator<T> for OpAmp<T>
where
Clipper: Saturator<T>,
Slew<T>: Saturator<T>,
{
fn saturate(&self, x: T) -> T {
self.1.saturate(self.0.saturate(x))
}

fn sat_diff(&self, x: T) -> T {
self.0.sat_diff(x) * self.1.sat_diff(self.0.saturate(x))
}

fn update_state(&mut self, x: T, y: T) {
let xc = self.0.saturate(x);
self.0.update_state(x, xc);
self.1.update_state(xc, y);
}
}
38 changes: 5 additions & 33 deletions examples/svfmixer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use valib::dsp::parameter::HasParameters;
use valib::dsp::utils::{slice_to_mono_block, slice_to_mono_block_mut};
use valib::dsp::DSPBlock;
use valib::oversample::Oversample;
use valib::saturators::{Clipper, Saturator, Slew};
use valib::saturators::Saturator;

Check warning on line 10 in examples/svfmixer/src/lib.rs

View workflow job for this annotation

GitHub Actions / Build and test all components (ubuntu-22.04)

unused import: `valib::saturators::Saturator`
use valib::simd::SimdValue;
use valib::{dsp::DSP, Scalar};

Expand Down Expand Up @@ -67,6 +67,7 @@ impl PluginParams {
)
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
.with_string_to_value(formatters::s2v_f32_gain_to_db())
.with_unit(" dB")
.bind_to_parameter(dsp.get_parameter(DspParam::Drive)),
fc: FloatParam::new(
"Frequency",
Expand Down Expand Up @@ -94,7 +95,7 @@ impl PluginParams {
)
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
.with_string_to_value(formatters::s2v_f32_gain_to_db())
.with_unit("dB")
.with_unit(" dB")
.bind_to_parameter(dsp.get_parameter(DspParam::LpGain)),
bp_gain: FloatParam::new(
"BP Gain",
Expand All @@ -108,7 +109,7 @@ impl PluginParams {
)
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
.with_string_to_value(formatters::s2v_f32_gain_to_db())
.with_unit("dB")
.with_unit(" dB")
.bind_to_parameter(dsp.get_parameter(DspParam::BpGain)),
hp_gain: FloatParam::new(
"HP Gain",
Expand All @@ -122,41 +123,12 @@ impl PluginParams {
)
.with_value_to_string(formatters::v2s_f32_gain_to_db(2))
.with_string_to_value(formatters::s2v_f32_gain_to_db())
.with_unit("dB")
.with_unit(" dB")
.bind_to_parameter(dsp.get_parameter(DspParam::HpGain)),
}
}
}

#[derive(Debug, Clone, Copy)]
struct OpAmp<T>(Clipper, Slew<T>);

impl<T: Scalar> Default for OpAmp<T> {
fn default() -> Self {
Self(Default::default(), Default::default())
}
}

impl<T: Scalar> Saturator<T> for OpAmp<T>
where
Clipper: Saturator<T>,
Slew<T>: Saturator<T>,
{
fn saturate(&self, x: T) -> T {
self.1.saturate(self.0.saturate(x))
}

fn sat_diff(&self, x: T) -> T {
self.0.sat_diff(x) * self.1.sat_diff(self.0.saturate(x))
}

fn update_state(&mut self, x: T, y: T) {
let xc = self.0.saturate(x);
self.0.update_state(x, xc);
self.1.update_state(xc, y);
}
}

struct SvfMixerPlugin {
params: Arc<PluginParams>,
dsp: Dsp,
Expand Down

0 comments on commit fba8a19

Please sign in to comment.