1
0
Fork 0

Fix octave -1 edge case in v2s_f32_hz_then_khz_with_note_name

This commit is contained in:
Robbert van der Helm 2023-01-10 02:11:45 +01:00
parent 32903ac065
commit a9d0e40447

View file

@ -149,7 +149,10 @@ pub fn v2s_f32_hz_then_khz_with_note_name(
let cents = ((fractional_note - note) * 100.0).round() as i32; let cents = ((fractional_note - note) * 100.0).round() as i32;
let note_name = util::NOTES[(note as i32).rem_euclid(12) as usize]; let note_name = util::NOTES[(note as i32).rem_euclid(12) as usize];
let octave = (note as i32 / 12) - 1; // NOTE: This is different compared from `(note as i32 / 12) - 1` because of the signed 0 in
// floating point numbers. The signed zero makes sure this works correctly for the
// first negative octave number.
let octave = (note / 12.0).round() as i32 - 1;
let note_str = if cents == 0 || !include_cents { let note_str = if cents == 0 || !include_cents {
format!("{note_name}{octave}") format!("{note_name}{octave}")
} else { } else {
@ -320,7 +323,7 @@ mod tests {
let v2s = v2s_f32_hz_then_khz_with_note_name(1, true); let v2s = v2s_f32_hz_then_khz_with_note_name(1, true);
let s2v = s2v_f32_hz_then_khz(); let s2v = s2v_f32_hz_then_khz();
for freq in [0.0, 69.420, 18181.8, 133333.7] { for freq in [0.0, 5.0, 69.420, 18181.8, 133333.7] {
let string = v2s(freq); let string = v2s(freq);
// We can't compare `freq` and `roundtrip_freq` because the string is rounded on both // We can't compare `freq` and `roundtrip_freq` because the string is rounded on both
// cents and frequency and is thus lossy // cents and frequency and is thus lossy