#[inline(always)]
pub fn freq_to_midi(frequency: f64) -> f64 {
f64::log2(frequency / 440.0) * 12.0 + 69.0
}
#[inline(always)]
pub fn midi_to_freq(midi: f64) -> f64 {
440.0 * f64::powf(2.0, (midi - 69.0) / 12.0)
}
#[inline(always)]
pub fn midi_ratio(interval: f64) -> f64 {
f64::powf(2.0, interval / 12.0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tuning() {
let freq = 100.0;
let midi = 65.0;
let interval = 4.0;
assert_eq!(freq_to_midi(freq), f64::log2(freq / 440.0) * 12.0 + 69.0);
assert_eq!(midi_to_freq(midi), 440.0 * f64::powf(2.0, (midi - 69.0) / 12.0));
assert_eq!(midi_ratio(interval), f64::powf(2.0, interval / 12.0));
}
}