Skip to content

Commit

Permalink
less unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetophon committed Nov 14, 2024
1 parent 2d5809d commit 0572421
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0572421

Please sign in to comment.