2022-03-03 23:34:06 +01:00
|
|
|
//! General conversion functions and utilities.
|
|
|
|
|
2022-03-06 02:07:23 +01:00
|
|
|
mod stft;
|
2022-03-06 14:41:40 +01:00
|
|
|
pub mod window;
|
2022-03-06 02:07:23 +01:00
|
|
|
|
|
|
|
pub use stft::StftHelper;
|
|
|
|
|
2022-01-26 11:48:40 +01:00
|
|
|
pub const MINUS_INFINITY_DB: f32 = -100.0;
|
2022-03-21 19:17:41 +01:00
|
|
|
pub const MINUS_INFINITY_GAIN: f32 = 1e-5; // 10f32.powf(MINUS_INFINITY_DB / 20)
|
2022-03-21 14:40:17 +01:00
|
|
|
pub const NOTES: [&str; 12] = [
|
|
|
|
"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B",
|
|
|
|
];
|
2022-01-26 11:48:40 +01:00
|
|
|
|
2022-03-06 15:00:20 +01:00
|
|
|
/// Temporarily allow allocations within `func` if NIH-plug was configured with the
|
|
|
|
/// `assert_process_allocs` feature.
|
|
|
|
#[cfg(all(debug_assertions, feature = "assert_process_allocs"))]
|
|
|
|
pub fn permit_alloc<T, F: FnOnce() -> T>(func: F) -> T {
|
|
|
|
assert_no_alloc::permit_alloc(func)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Temporarily allow allocations within `func` if NIH-plug was configured with the
|
|
|
|
/// `assert_process_allocs` feature.
|
|
|
|
#[cfg(not(all(debug_assertions, feature = "assert_process_allocs")))]
|
|
|
|
pub fn permit_alloc<T, F: FnOnce() -> T>(func: F) -> T {
|
2022-03-06 15:42:32 +01:00
|
|
|
func()
|
2022-03-06 15:00:20 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 11:57:47 +01:00
|
|
|
/// Convert decibels to a voltage gain ratio, treating anything below -100 dB as minus infinity.
|
2022-01-26 11:48:40 +01:00
|
|
|
pub fn db_to_gain(dbs: f32) -> f32 {
|
|
|
|
if dbs > MINUS_INFINITY_DB {
|
|
|
|
10.0f32.powf(dbs * 0.05)
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 11:57:47 +01:00
|
|
|
/// Convert a voltage gain ratio to decibels. Gain ratios that aren't positive will be treated as
|
2022-03-03 23:05:01 +01:00
|
|
|
/// [`MINUS_INFINITY_DB`].
|
2022-01-26 11:57:47 +01:00
|
|
|
pub fn gain_to_db(gain: f32) -> f32 {
|
2022-03-21 19:17:41 +01:00
|
|
|
if gain > MINUS_INFINITY_GAIN {
|
2022-01-26 11:57:47 +01:00
|
|
|
gain.log10() * 20.0
|
|
|
|
} else {
|
|
|
|
MINUS_INFINITY_DB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 02:37:40 +01:00
|
|
|
/// Convert a MIDI note ID to a frequency at A4 = 440 Hz equal temperament and middle C = note 60 =
|
|
|
|
/// C4.
|
2022-07-06 16:33:33 +02:00
|
|
|
pub fn midi_note_to_freq(note: u8) -> f32 {
|
|
|
|
2.0f32.powf((note as f32 - 69.0) / 12.0) * 440.0
|
2022-02-04 02:37:40 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 11:48:40 +01:00
|
|
|
#[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);
|
|
|
|
}
|
2022-01-26 11:57:47 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gain_to_db_positive() {
|
|
|
|
assert_eq!(gain_to_db(4.0), 12.041201);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gain_to_db_negative() {
|
|
|
|
assert_eq!(gain_to_db(0.25), -12.041201);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gain_to_db_minus_infinity_zero() {
|
|
|
|
assert_eq!(gain_to_db(0.0), MINUS_INFINITY_DB);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gain_to_db_minus_infinity_negative() {
|
|
|
|
assert_eq!(gain_to_db(-2.0), MINUS_INFINITY_DB);
|
|
|
|
}
|
2022-01-26 11:48:40 +01:00
|
|
|
}
|