From c3efa8ca62a7a21fb29bb73d1353d8e3ccd1c1a4 Mon Sep 17 00:00:00 2001 From: Nathan Graule Date: Tue, 6 Feb 2024 23:17:07 +0100 Subject: [PATCH] fix: use samplerate to calculate slew rate in parameters --- examples/svfmixer/src/dsp.rs | 12 ++++++------ src/dsp/parameter.rs | 25 ++++++++++++++++--------- src/saturators/mod.rs | 4 ++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/examples/svfmixer/src/dsp.rs b/examples/svfmixer/src/dsp.rs index 23f5f73..d453772 100644 --- a/examples/svfmixer/src/dsp.rs +++ b/examples/svfmixer/src/dsp.rs @@ -42,12 +42,12 @@ impl DspInner { impl DspInner { pub fn new(samplerate: f32) -> Self { let params = enum_map! { - DspParam::Drive => Parameter::new(1.0).named("Drive").smoothed_linear(10.0), - DspParam::Cutoff => Parameter::new(3000.0).named("Cutoff").smoothed_linear(10.0), - DspParam::Resonance => Parameter::new(0.5).named("Resonance").smoothed_linear(10.0), - DspParam::LpGain => Parameter::new(1.0).named("LP Gain").smoothed_linear(10.0), - DspParam::BpGain => Parameter::new(0.0).named("BP Gain").smoothed_linear(10.0), - DspParam::HpGain => Parameter::new(0.0).named("HP Gain").smoothed_linear(10.0), + DspParam::Drive => Parameter::new(1.0).named("Drive").smoothed_linear(samplerate, 10.0), + DspParam::Cutoff => Parameter::new(3000.0).named("Cutoff").smoothed_linear(samplerate, 1e-6), + DspParam::Resonance => Parameter::new(0.5).named("Resonance").smoothed_linear(samplerate, 10.0), + DspParam::LpGain => Parameter::new(1.0).named("LP Gain").smoothed_linear(samplerate, 10.0), + DspParam::BpGain => Parameter::new(0.0).named("BP Gain").smoothed_linear(samplerate, 10.0), + DspParam::HpGain => Parameter::new(0.0).named("HP Gain").smoothed_linear(samplerate, 10.0), }; let filter = Filter::new( Sample::splat(samplerate), diff --git a/src/dsp/parameter.rs b/src/dsp/parameter.rs index 2b33257..2fc8028 100644 --- a/src/dsp/parameter.rs +++ b/src/dsp/parameter.rs @@ -74,8 +74,8 @@ impl Parameter { } } - pub fn smoothed_linear(&self, max_dur_ms: f32) -> SmoothedParam { - SmoothedParam::linear(self.clone(), max_dur_ms) + pub fn smoothed_linear(&self, samplerate: f32, max_dur_ms: f32) -> SmoothedParam { + SmoothedParam::linear(self.clone(), samplerate, max_dur_ms) } pub fn smoothed_exponential(&self, samplerate: f32, t60_ms: f32) -> SmoothedParam { @@ -107,14 +107,16 @@ impl> DSP<0, 1> for FilteredParam

{ #[derive(Debug, Copy, Clone)] enum Smoothing { Exponential(Series2, ModMatrix, 3>), - Linear(Slew), + Linear { slew: Slew, max_diff: f32 }, } impl Smoothing { - fn set_samplerate(&mut self, samplerate: f32) { + fn set_samplerate(&mut self, new_sr: f32) { match self { - Self::Exponential(s) => s.left_mut().set_samplerate(samplerate), - _ => {} + Self::Exponential(s) => s.left_mut().set_samplerate(new_sr), + Self::Linear { slew, max_diff } => { + slew.set_max_diff(*max_diff, new_sr); + } } } } @@ -126,7 +128,7 @@ impl DSP<1, 1> for Smoothing { fn process(&mut self, x: [Self::Sample; 1]) -> [Self::Sample; 1] { match self { Self::Exponential(s) => s.process(x), - Self::Linear(s) => s.process(x), + Self::Linear { slew: s, .. } => s.process(x), } } } @@ -153,11 +155,16 @@ impl SmoothedParam { /// # Arguments /// /// * `param`: Inner parameter to tap values from. + /// * `samplerate`: Samplerate at which the smoother will run. /// * `duration_ms`: Maximum duration of a sweep, that is the duration it would take to go from one extreme to the other. - pub fn linear(param: Parameter, duration_ms: f32) -> Self { + pub fn linear(param: Parameter, samplerate: f32, duration_ms: f32) -> Self { + let max_diff = 1000.0 / duration_ms; Self { param, - smoothing: Smoothing::Linear(Slew::new(1000.0 / duration_ms)), + smoothing: Smoothing::Linear { + slew: Slew::new(samplerate, max_diff), + max_diff, + }, } } diff --git a/src/saturators/mod.rs b/src/saturators/mod.rs index aa4effc..3e34130 100644 --- a/src/saturators/mod.rs +++ b/src/saturators/mod.rs @@ -177,9 +177,9 @@ impl DSP<1, 1> for Slew { } impl Slew { - pub fn new(max_diff: T) -> Self { + pub fn new(samplerate: T, max_diff: T) -> Self { Self { - max_diff, + max_diff: max_diff / samplerate, last_out: T::from_f64(0.0), } }