Skip to content

Commit

Permalink
fix: use samplerate to calculate slew rate in parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarLiner committed Feb 7, 2024
1 parent 3ed971a commit c3efa8c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
12 changes: 6 additions & 6 deletions examples/svfmixer/src/dsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
25 changes: 16 additions & 9 deletions src/dsp/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -107,14 +107,16 @@ impl<P: DSP<1, 1, Sample = f32>> DSP<0, 1> for FilteredParam<P> {
#[derive(Debug, Copy, Clone)]
enum Smoothing {
Exponential(Series2<P1<f32>, ModMatrix<f32, 3, 1>, 3>),
Linear(Slew<f32>),
Linear { slew: Slew<f32>, 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);
}
}
}
}
Expand All @@ -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),
}
}
}
Expand All @@ -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,
},
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/saturators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ impl<T: Scalar> DSP<1, 1> for Slew<T> {
}

impl<T: Scalar> Slew<T> {
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),
}
}
Expand Down

0 comments on commit c3efa8c

Please sign in to comment.