Move frequency to note number conversion to utils
This commit is contained in:
parent
e5211ced4c
commit
6d0c1eebbe
2 changed files with 8 additions and 1 deletions
|
@ -143,7 +143,7 @@ pub fn v2s_f32_hz_then_khz_with_note_name(
|
||||||
) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
) -> Arc<dyn Fn(f32) -> String + Send + Sync> {
|
||||||
Arc::new(move |value| {
|
Arc::new(move |value| {
|
||||||
// This is the inverse of the formula in `f32_midi_note_to_freq`
|
// This is the inverse of the formula in `f32_midi_note_to_freq`
|
||||||
let fractional_note = ((value / 440.0).log2() * 12.0) + 69.0;
|
let fractional_note = util::freq_to_midi_note(value);
|
||||||
let note = fractional_note.round();
|
let note = fractional_note.round();
|
||||||
let cents = ((fractional_note - note) * 100.0) as i32;
|
let cents = ((fractional_note - note) * 100.0) as i32;
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,13 @@ pub fn f32_midi_note_to_freq(note: f32) -> f32 {
|
||||||
2.0f32.powf((note - 69.0) / 12.0) * 440.0
|
2.0f32.powf((note - 69.0) / 12.0) * 440.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The inverse of [`f32_midi_note_to_freq()`]. This returns a fractional note number. Round to a
|
||||||
|
/// whole number, subtract that from the result, and multiply the fractional part by 100 to get the
|
||||||
|
/// number of cents.
|
||||||
|
pub fn freq_to_midi_note(freq: f32) -> f32 {
|
||||||
|
((freq / 440.0).log2() * 12.0) + 69.0
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Add table
Reference in a new issue