mod stft;
pub mod window;
pub use stft::StftHelper;
pub const MINUS_INFINITY_DB: f32 = -100.0;
pub const MINUS_INFINITY_GAIN: f32 = 1e-5; pub const NOTES: [&str; 12] = [
"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B",
];
#[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)
}
#[cfg(not(all(debug_assertions, feature = "assert_process_allocs")))]
pub fn permit_alloc<T, F: FnOnce() -> T>(func: F) -> T {
func()
}
#[inline]
pub fn db_to_gain(dbs: f32) -> f32 {
if dbs > MINUS_INFINITY_DB {
10.0f32.powf(dbs * 0.05)
} else {
0.0
}
}
#[inline]
pub fn gain_to_db(gain: f32) -> f32 {
f32::max(gain, MINUS_INFINITY_GAIN).log10() * 20.0
}
#[inline]
pub fn db_to_gain_fast(dbs: f32) -> f32 {
const CONVERSION_FACTOR: f32 = std::f32::consts::LN_10 / 20.0;
(dbs * CONVERSION_FACTOR).exp()
}
#[inline]
pub fn db_to_gain_fast_branching(dbs: f32) -> f32 {
if dbs > MINUS_INFINITY_DB {
db_to_gain_fast(dbs)
} else {
0.0
}
}
#[inline]
pub fn gain_to_db_fast(gain: f32) -> f32 {
const CONVERSION_FACTOR: f32 = std::f32::consts::LOG10_E * 20.0;
f32::max(gain, MINUS_INFINITY_GAIN).ln() * CONVERSION_FACTOR
}
#[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
}
#[inline]
pub fn midi_note_to_freq(note: u8) -> f32 {
f32_midi_note_to_freq(note as f32)
}
#[inline]
pub fn f32_midi_note_to_freq(note: f32) -> f32 {
2.0f32.powf((note - 69.0) / 12.0) * 440.0
}
#[inline]
pub fn freq_to_midi_note(freq: f32) -> f32 {
((freq / 440.0).log2() * 12.0) + 69.0
}
#[cfg(test)]
mod tests {
mod db_gain_conversion {
use super::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);
}
#[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);
}
}
mod fast_db_gain_conversion {
use super::super::*;
#[test]
fn test_db_to_gain_positive() {
approx::assert_relative_eq!(
db_to_gain(3.0),
db_to_gain_fast_branching(3.0),
epsilon = 1e-7
);
}
#[test]
fn test_db_to_gain_negative() {
approx::assert_relative_eq!(
db_to_gain(-3.0),
db_to_gain_fast_branching(-3.0),
epsilon = 1e-7
);
}
#[test]
fn test_db_to_gain_minus_infinity() {
approx::assert_relative_eq!(
db_to_gain(-100.0),
db_to_gain_fast_branching(-100.0),
epsilon = 1e-7
);
}
#[test]
fn test_gain_to_db_positive() {
approx::assert_relative_eq!(gain_to_db(4.0), gain_to_db_fast(4.0), epsilon = 1e-7);
}
#[test]
fn test_gain_to_db_negative() {
approx::assert_relative_eq!(gain_to_db(0.25), gain_to_db_fast(0.25), epsilon = 1e-7);
}
#[test]
fn test_gain_to_db_minus_infinity_zero() {
approx::assert_relative_eq!(gain_to_db(0.0), gain_to_db_fast(0.0), epsilon = 1e-7);
}
#[test]
fn test_gain_to_db_minus_infinity_negative() {
approx::assert_relative_eq!(gain_to_db(-2.0), gain_to_db_fast(-2.0), epsilon = 1e-7);
}
}
}