From c7f5cd1cec8ffadb8fc76d8c5b0b936a4cfd19b7 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 29 May 2022 14:32:56 +0200 Subject: [PATCH] Fix NaNs in negative out of bounds skewed params This would result in a NaN, and the clamping wouldn't catch that. --- plugins/diopser/src/lib.rs | 1 - src/param/range.rs | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/diopser/src/lib.rs b/plugins/diopser/src/lib.rs index 7f58b087..a4598ebd 100644 --- a/plugins/diopser/src/lib.rs +++ b/plugins/diopser/src/lib.rs @@ -83,7 +83,6 @@ struct DiopserParams { /// The filter's center frequqency. When this is applied, the filters are spread around this /// frequency. - /// FIXME: Entering -3 or another invalid value will set the parameter to NaN #[id = "cutoff"] filter_frequency: FloatParam, /// The Q parameter for the filters. diff --git a/src/param/range.rs b/src/param/range.rs index 33d77924..b3ba7636 100644 --- a/src/param/range.rs +++ b/src/param/range.rs @@ -43,8 +43,10 @@ impl FloatRange { /// normalized value exceeds `[0, 1]`. pub fn normalize(&self, plain: f32) -> f32 { match &self { - FloatRange::Linear { min, max } => (plain - min) / (max - min), - FloatRange::Skewed { min, max, factor } => ((plain - min) / (max - min)).powf(*factor), + FloatRange::Linear { min, max } => (plain.clamp(*min, *max) - min) / (max - min), + FloatRange::Skewed { min, max, factor } => { + ((plain.clamp(*min, *max) - min) / (max - min)).powf(*factor) + } FloatRange::SymmetricalSkewed { min, max, @@ -53,7 +55,7 @@ impl FloatRange { } => { // There's probably a much faster equivalent way to write this. Also, I have no clue // how I managed to implement this correctly on the first try. - let unscaled_proportion = (plain - min) / (max - min); + let unscaled_proportion = (plain.clamp(*min, *max) - min) / (max - min); let center_proportion = (center - min) / (max - min); if unscaled_proportion > center_proportion { // The part above the center gets normalized to a [0, 1] range, skewed, and then @@ -72,7 +74,6 @@ impl FloatRange { } } } - .clamp(0.0, 1.0) } /// Unnormalize a normalized value. Will be clamped to `[0, 1]` if the plain, unnormalized value