1
0
Fork 0

Add a way to compute skew factors for linear gain

Needed for #18.
This commit is contained in:
Robbert van der Helm 2022-07-24 20:39:48 +02:00
parent 42c42dff9b
commit 44b9a4cad0

View file

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