Skip to content

Commit

Permalink
wip use svf as main filter
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Dec 23, 2024
1 parent 746307c commit bbd3b58
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/delay_tap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct DelayTap {

pub filter_params: Arc<FilterParams>,
pub ladders: LadderFilter,
pub lowpass: SVFSimper<4>,
pub shelving_eq: SVFSimper<4>,
pub mute_in_delayed: Box<[bool]>,
/// Fades between 0 and 1 with timings based on the global attack and release settings.
Expand Down Expand Up @@ -57,6 +58,7 @@ impl DelayTap {
delayed_audio_r: vec![0.0; MAX_BLOCK_SIZE].into_boxed_slice(),
filter_params: filter_params.clone(),
ladders: LadderFilter::new(filter_params),
lowpass: SVFSimper::new(440.0, 0.5, 48000.0),
shelving_eq: SVFSimper::new(PANNER_EQ_FREQ, PANNER_EQ_RES, 48000.0),
mute_in_delayed: vec![false; MAX_BLOCK_SIZE].into_boxed_slice(),
amp_envelope: Smoother::new(SmoothingStyle::Linear(13.0)),
Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ impl Plugin for Del2 {
// Initialize filter parameters for each tap
self.initialize_filter_parameters();
for delay_tap in &mut self.delay_taps {
delay_tap.lowpass.reset(440.0, 0.5, sample_rate);
delay_tap
.shelving_eq
.reset(PANNER_EQ_FREQ, PANNER_EQ_RES, sample_rate);
Expand All @@ -655,7 +656,6 @@ impl Plugin for Del2 {
delay_tap.amp_envelope.reset(0.0);
delay_tap.ladders.s = [f32x4::splat(0.); 4];
});

self.drive_main_smoother.reset(0.0);
// then fill the array
for tap_index in 0..tap_counter {
Expand Down Expand Up @@ -956,6 +956,12 @@ impl Plugin for Del2 {
+ value_idx as f32,
) * pre_filter_gain;
}

// TODO: smooth
let cutoff = taps_params.cutoff_main.value();
let res = taps_params.res_main.value();
delay_tap.lowpass.set(cutoff, res * 2.0);

// HQ mode:
for i in block_start..block_end {
let frame = f32x4::from_array([
Expand All @@ -972,8 +978,10 @@ impl Plugin for Del2 {
0.0,
]);

let frame_filtered = *delay_tap.ladders.tick_pivotal(frame).as_array();
// let frame_filtered = *delay_tap.ladders.tick_pivotal(frame).as_array();
// let frame_filtered = *delay_tap.ladders.tick_linear(frame).as_array();
let frame_filtered = delay_tap.lowpass.lowpass(frame);
// let frame_filtered = delay_tap.lowpass.bandpass(frame);
let frame_out = delay_tap
.shelving_eq
.highshelf_cheap(frame_filtered.into(), gain_values);
Expand Down
19 changes: 11 additions & 8 deletions src/svf_simper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ where
#[inline]
fn compute_parameters(cutoff: f32, resonance: f32, pi_over_sr: f32) -> (f32, f32, f32, f32) {
let g = (cutoff * pi_over_sr).tan();
let k = 2.0 * (1.0 - resonance.clamp(0.0, 1.0));
// let k = 2.0 * (1.0 - resonance.clamp(0.0, 1.0));
// let k = (2.0 * (1.0 - resonance)).min(2.0);
let k = 2.0 * (1.0 - resonance);
let a1 = g.mul_add(g + k, 1.0).recip();
let a2 = g * a1;
let a3 = g * a2;
Expand All @@ -108,9 +110,8 @@ where
#[inline]
pub fn fast_tanh(v: Simd<f32, LANES>) -> Simd<f32, LANES> {
let square = v * v; // Element-wise squaring
v * (Simd::splat(25.95) + square)
/ (Simd::splat(26.369) + Simd::splat(8.78) * square)
.simd_clamp(Simd::splat(-1.0), Simd::splat(1.0))
(v * (Simd::splat(25.95) + square) / (Simd::splat(26.369) + Simd::splat(8.78) * square))
.simd_clamp(Simd::splat(-1.0), Simd::splat(1.0))
}

// quickerTanh / quickerTanh64 credits to mopo synthesis library:
Expand All @@ -126,11 +127,13 @@ where
#[inline]
fn process(&mut self, v0: Simd<f32, LANES>) -> (Simd<f32, LANES>, Simd<f32, LANES>) {
let v3 = v0 - self.ic2eq;
let v1 = Self::fast_tanh((self.a1 * self.ic1eq) + (self.a2 * v3));
let v1 = (self.a1 * self.ic1eq) + (self.a2 * v3);
let v2 = self.ic2eq + (self.a2 * self.ic1eq) + (self.a3 * v3);

self.ic1eq = (Simd::splat(2.0) * v1) - self.ic1eq;
self.ic2eq = (Simd::splat(2.0) * v2) - self.ic2eq;
// self.ic1eq = (Simd::splat(2.0) * v1) - self.ic1eq;
self.ic1eq = Self::fast_tanh((Simd::splat(2.0) * v1) - self.ic1eq);
// self.ic2eq = (Simd::splat(2.0) * v2) - self.ic2eq;
self.ic2eq = Self::fast_tanh((Simd::splat(2.0) * v2) - self.ic2eq);

(v1, v2)
}
Expand Down Expand Up @@ -190,7 +193,7 @@ where
v0: Simd<f32, LANES>,
lin_gain: Simd<f32, LANES>,
) -> Simd<f32, LANES> {
let (v1, v2) = self.process(v0);
let (_, v2) = self.process(v0);
lin_gain.mul_add(v2, v0 - v2)
}
#[inline]
Expand Down

0 comments on commit bbd3b58

Please sign in to comment.