From a24ae750d6a3893ee8dcd14cef3936e5f639b9ea Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Sat, 28 Aug 2021 11:04:28 +0200 Subject: [PATCH] fixed the note offset of the quantizer --- src/dsp/helpers.rs | 40 ++++++++++++++++++++++---------------- tests/quant.rs | 48 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/src/dsp/helpers.rs b/src/dsp/helpers.rs index 058c70e..0425e6b 100644 --- a/src/dsp/helpers.rs +++ b/src/dsp/helpers.rs @@ -2158,8 +2158,7 @@ impl Quantizer { self.old_mask = keys_mask; for i in 0..12 { - self.keys[i] = - ((i as f32 / 12.0) * 0.1) - QUANT_TUNE_TO_A4; + self.keys[i] = (i as f32 / 12.0) * 0.1; } self.setup_lookup_table(); @@ -2201,26 +2200,31 @@ impl Quantizer { let any_enabled = mask > 0x0; for i in 0..24 { - let mut min_dist_note = 0; - let mut min_dist = 1000000000; + let mut min_d_note_idx = 0; + let mut min_dist = 1000000000; for note in -12..=24 { - let dist = ((i + 1_i64) / 2 - note).abs(); - + let dist = ((i + 1_i64) / 2 - note).abs(); let note_idx = note.rem_euclid(12); - if any_enabled && (mask & (0x1 << note_idx)) == 0x0 { + + // XXX: We add 9 here for the mask lookup, + // to shift the keyboard, which starts at C! + // And first bit in the mask is the C note. 10th is the A note. + if any_enabled + && (mask & (0x1 << ((note_idx + 9) % 12))) == 0x0 + { continue; } if dist < min_dist { - min_dist_note = note_idx; - min_dist = dist; + min_d_note_idx = note_idx; + min_dist = dist; } else { break; } } - self.lkup_tbl[i as usize] = min_dist_note as u8; + self.lkup_tbl[i as usize] = min_d_note_idx as u8; } } @@ -2236,17 +2240,19 @@ impl Quantizer { #[inline] pub fn process(&self, inp: f32) -> f32 { - let note_num = (inp * 240.0).floor() as i64; - let octave = note_num.div_euclid(240); - let note_idx = note_num - octave * 240; + let note_num = (inp * 240.0).round() as i64; + let octave = note_num.div_euclid(24); + let note_idx = note_num - octave * 24; - println!("INP {:6.4} => octave={:2}, note_idx={:2}", inp, octave, note_idx); - println!("KEYS: {:?}", self.keys); + println!( + "INP {:7.4} => octave={:3}, note_idx={:3} note_num={:3} inp={:9.6}", + inp, octave, note_idx, note_num, inp * 240.0); + //d// println!("KEYS: {:?}", self.keys); - let note_idx = self.lkup_tbl[note_idx as usize % 24]; // + octave * 12; + let note_idx = self.lkup_tbl[note_idx as usize % 24]; let pitch = self.keys[note_idx as usize]; - pitch + pitch + octave as f32 * 0.1 } } diff --git a/tests/quant.rs b/tests/quant.rs index 7224580..1366ff4 100644 --- a/tests/quant.rs +++ b/tests/quant.rs @@ -15,21 +15,45 @@ fn check_quant_1() { let v = (0..=12).map(|i| - d_pit!(q.process(i as f32 * (0.1 / 12.0))) + d_pit!( + q.process( + i as f32 * (0.1 / 12.0))) ).collect::>(); assert_vec_feq!(v, vec![ 440.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, + 466.1638, + 493.8833, + 523.2511, + 554.3653, + 587.3295, + 622.25397, + 659.2551, + 698.4565, + 739.98883, + 783.9909, + 830.6094, + 880.0 + ]); + + let v = + (0..=12).map(|i| + d_pit!(q.process(i as f32 * (-0.1 / 12.0))) + ).collect::>(); + + assert_vec_feq!(v, vec![ + 440.0, + 415.3047, + 391.99542, + 369.99442, + 349.22824, + 329.62756, + 311.12698, + 293.66476, + 277.18265, + 261.62555, + 246.94165, + 233.08186, + 220.0 ]); }