From 92ce73700005255565c6be45412609ea87eb8b41 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 15 Jan 2023 17:15:56 +0100 Subject: [PATCH] Add a gain_to_db_fast() with a lower limit These functions probably need some better organization at some point. --- src/util.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/util.rs b/src/util.rs index c36c912a..a902f20c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -71,6 +71,14 @@ pub fn gain_to_db_fast(gain: f32) -> f32 { f32::max(gain, MINUS_INFINITY_GAIN).ln() * CONVERSION_FACTOR } +/// [`db_to_gain_fast()`], but the minimum gain value is set to [`f32::EPSILON`]instead of +/// [`MINUS_INFINITY_GAIN`]. Useful in conjunction with [`db_to_gain_fast()`]. +#[inline] +pub fn gain_to_db_fast_epsilon(gain: f32) -> f32 { + const CONVERSION_FACTOR: f32 = std::f32::consts::LOG10_E * 20.0; + f32::max(gain, MINUS_INFINITY_GAIN).ln() * CONVERSION_FACTOR +} + /// Convert a MIDI note ID to a frequency at A4 = 440 Hz equal temperament and middle C = note 60 = /// C4. #[inline]