diff --git a/src/param/range.rs b/src/param/range.rs index 9ad1fc66..abe1c27d 100644 --- a/src/param/range.rs +++ b/src/param/range.rs @@ -1,5 +1,7 @@ //! Different ranges for numeric parameters. +use crate::util; + /// A distribution for a floating point parameter's range. All range endpoints are inclusive. #[derive(Debug, Clone, Copy)] pub enum FloatRange { @@ -43,6 +45,19 @@ impl FloatRange { 2.0f32.powf(factor) } + /// Calculate a skew factor for [`FloatRange::Skewed`] that makes a linear gain parameter range + /// appear as if it was linear when formatted as decibels. + pub fn gain_skew_factor(min_db: f32, max_db: f32) -> f32 { + let min_gain = util::db_to_gain(min_db); + let max_gain = util::db_to_gain(max_db); + let middle_db = (max_db + min_db) / 2.0; + let middle_gain = util::db_to_gain(middle_db); + + // Check the Skewed equation in the normalized function below, we need to solve the factor + // such that the a normalized value of 0.5 resolves to the middle of the range + 0.5f32.log((middle_gain - min_gain) / (max_gain - min_gain)) + } + /// Normalize a plain, unnormalized value. Will be clamped to the bounds of the range if the /// normalized value exceeds `[0, 1]`. pub fn normalize(&self, plain: f32) -> f32 {