diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 913db05..bdc4eea 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -66,6 +66,32 @@ impl ValueScaling { } .clamp(0., 1.) } + + pub fn value_to_normalized_optional(&self, value: f32, min: f32, max: f32) -> Option { + 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