Skip to content

Commit

Permalink
Add optional value_to_normalized
Browse files Browse the repository at this point in the history
This version of value_to_optimized checks whether the resulting value is in range and returns an option containing Some(value) if it is, and None if it isn't.
  • Loading branch information
exa04 committed May 6, 2024
1 parent 0885af4 commit 7e03ec6
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ impl ValueScaling {
}
.clamp(0., 1.)
}

pub fn value_to_normalized_optional(&self, value: f32, min: f32, max: f32) -> Option<f32> {
let unmap = |x: f32| -> f32 { (x - min) / (max - min) };

let value = match self {
ValueScaling::Linear => unmap(value),

ValueScaling::Power(exponent) => unmap(value).powf(1.0 / *exponent),

ValueScaling::Frequency => {
let minl = min.log2();
let range = max.log2() - minl;
(value.log2() - minl) / range
}

ValueScaling::Decibels => unmap({
const CONVERSION_FACTOR: f32 = std::f32::consts::LOG10_E * 20.0;
value.ln() * CONVERSION_FACTOR
}),
};
if (0.0..=1.0).contains(&value) {
Some(value)
} else {
None
}
}
}

// We can't use impl_res_simple!() since we're using nih_plug's version of VIZIA
Expand Down

0 comments on commit 7e03ec6

Please sign in to comment.