1
0
Fork 0

Fix NaNs in negative out of bounds skewed params

This would result in a NaN, and the clamping wouldn't catch that.
This commit is contained in:
Robbert van der Helm 2022-05-29 14:32:56 +02:00
parent c1d72f0e80
commit c7f5cd1cec
2 changed files with 5 additions and 5 deletions

View file

@ -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.

View file

@ -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