Add a decibel to gain conversion function
This commit is contained in:
parent
98e326d390
commit
7fe47dfbbd
2 changed files with 47 additions and 0 deletions
|
@ -17,3 +17,4 @@
|
||||||
pub mod debug;
|
pub mod debug;
|
||||||
pub mod params;
|
pub mod params;
|
||||||
pub mod plugin;
|
pub mod plugin;
|
||||||
|
pub mod util;
|
||||||
|
|
46
nih_plug/src/util.rs
Normal file
46
nih_plug/src/util.rs
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
// nih-plug: plugins, but rewritten in Rust
|
||||||
|
// Copyright (C) 2022 Robbert van der Helm
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
pub const MINUS_INFINITY_DB: f32 = -100.0;
|
||||||
|
|
||||||
|
/// Convert decibels to gain, treating anything below -100 dB as minus infinity.
|
||||||
|
pub fn db_to_gain(dbs: f32) -> f32 {
|
||||||
|
if dbs > MINUS_INFINITY_DB {
|
||||||
|
10.0f32.powf(dbs * 0.05)
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_db_to_gain_positive() {
|
||||||
|
assert_eq!(db_to_gain(3.0), 1.4125376);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_db_to_gain_negative() {
|
||||||
|
assert_eq!(db_to_gain(-3.0), 1.4125376f32.recip());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_db_to_gain_minus_infinity() {
|
||||||
|
assert_eq!(db_to_gain(-100.0), 0.0);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue