From 0572421d4bf11d6bec34eba69b29b7ba2c9a4e64 Mon Sep 17 00:00:00 2001 From: Bart Brouns Date: Thu, 14 Nov 2024 23:39:57 +0100 Subject: [PATCH] less unsafe --- src/lib.rs | 67 ++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index facdb3e..f032b1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1273,41 +1273,38 @@ impl Del2 { let velocity = self.delay_data.velocities[tap]; let velocity_params = &self.params.taps; - unsafe { - let filter_params = Arc::get_mut_unchecked(&mut self.filter_params[tap]); - - // Cache repeated calculations - let low_params = &velocity_params.velocity_low; - let high_params = &velocity_params.velocity_high; - - let res = Self::lerp(low_params.res.value(), high_params.res.value(), velocity); - let velocity_cutoff = Self::log_interpolate( - low_params.cutoff.value(), - high_params.cutoff.value(), - velocity, - ); - let note_cutoff = util::midi_note_to_freq(self.delay_data.notes[tap]); - let cutoff = note_cutoff - .mul_add( - self.params.taps.note_to_cutoff_amount.value(), - velocity_cutoff * self.params.taps.velocity_to_cutoff_amount.value(), - ) - .clamp(10.0, 20_000.0); - let drive_db = Self::lerp( - util::gain_to_db(low_params.drive.value()), - util::gain_to_db(high_params.drive.value()), - velocity, - ); - let drive = util::db_to_gain(drive_db); - let mode = - MyLadderMode::lerp(low_params.mode.value(), high_params.mode.value(), velocity); - - filter_params.set_resonance(res); - filter_params.set_frequency(cutoff); - filter_params.drive = drive; - filter_params.ladder_mode = mode; - self.ladders[tap].set_mix(mode); - } + let filter_params = unsafe { Arc::get_mut_unchecked(&mut self.filter_params[tap]) }; + + // Cache repeated calculations + let low_params = &velocity_params.velocity_low; + let high_params = &velocity_params.velocity_high; + + let res = Self::lerp(low_params.res.value(), high_params.res.value(), velocity); + let velocity_cutoff = Self::log_interpolate( + low_params.cutoff.value(), + high_params.cutoff.value(), + velocity, + ); + let note_cutoff = util::midi_note_to_freq(self.delay_data.notes[tap]); + let cutoff = note_cutoff + .mul_add( + self.params.taps.note_to_cutoff_amount.value(), + velocity_cutoff * self.params.taps.velocity_to_cutoff_amount.value(), + ) + .clamp(10.0, 20_000.0); + let drive_db = Self::lerp( + util::gain_to_db(low_params.drive.value()), + util::gain_to_db(high_params.drive.value()), + velocity, + ); + let drive = util::db_to_gain(drive_db); + let mode = MyLadderMode::lerp(low_params.mode.value(), high_params.mode.value(), velocity); + + filter_params.set_resonance(res); + filter_params.set_frequency(cutoff); + filter_params.drive = drive; + filter_params.ladder_mode = mode; + self.ladders[tap].set_mix(mode); } fn lerp(a: f32, b: f32, x: f32) -> f32 {