From 204c415c0674a137038c6c66077d919da8a01dd9 Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Sat, 28 Aug 2021 16:28:03 +0200 Subject: [PATCH] simplified the quantizer and wrote more tests --- src/dsp/helpers.rs | 34 ++++++++++----------------------- tests/quant.rs | 47 +++++++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/dsp/helpers.rs b/src/dsp/helpers.rs index d4b62e8..2f30be2 100644 --- a/src/dsp/helpers.rs +++ b/src/dsp/helpers.rs @@ -2137,16 +2137,14 @@ impl TriSawLFO { #[derive(Debug, Clone)] pub struct Quantizer { old_mask: i64, - keys: [f32; 12], - lkup_tbl: [(i8, i8); 24], + lkup_tbl: [f32; 24], } impl Quantizer { pub fn new() -> Self { Self { old_mask: 0xFFFF_FFFF, - keys: [0.0; 12], - lkup_tbl: [(0, 0); 24] + lkup_tbl: [0.0; 24] } } @@ -2157,10 +2155,6 @@ impl Quantizer { } self.old_mask = keys_mask; - for i in 0..12 { - self.keys[i] = (i as f32 / 12.0) * 0.1; - } - self.setup_lookup_table(); } @@ -2222,7 +2216,6 @@ impl Quantizer { dist); if dist < min_dist { -// min_d_note_idx = note_idx; min_d_note_idx = note; min_dist = dist; } else { @@ -2230,16 +2223,13 @@ impl Quantizer { } } - self.lkup_tbl[i as usize] = ( - if min_d_note_idx < 0 { -1 } - else if min_d_note_idx > 11 { 1 } - else { 0 }, - min_d_note_idx.rem_euclid(12) as i8 - ); + self.lkup_tbl[i as usize] = + min_d_note_idx.rem_euclid(12) as f32 * (0.1 / 12.0) + + ( if min_d_note_idx < 0 { -0.1 } + else if min_d_note_idx > 11 { 0.1 } + else { 0.0 }); } - - println!("KEYS: {:?}", self.keys); - println!("TBL: {:?}", self.lkup_tbl); + //d// println!("TBL: {:?}", self.lkup_tbl); } //# float pitch = inputs[PITCH_INPUT].getVoltage(c); @@ -2261,14 +2251,10 @@ impl Quantizer { // 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); //d// println!("TBL: {:?}", self.lkup_tbl); - let (oct_offs, note_idx) = - self.lkup_tbl[note_idx as usize % 24]; - let pitch = self.keys[note_idx as usize]; - - pitch + (oct_offs as i64 + octave) as f32 * 0.1 + let note_pitch = self.lkup_tbl[note_idx as usize % 24]; + note_pitch + octave as f32 * 0.1 } } diff --git a/tests/quant.rs b/tests/quant.rs index aff8033..6249f03 100644 --- a/tests/quant.rs +++ b/tests/quant.rs @@ -130,18 +130,39 @@ fn check_quant_edge_oct_offs() { }).collect::>(); assert_vec_feq!(v, vec![ - 311.12698, 311.12698, - 329.62756, 329.62756, 329.62756, 329.62756, - 349.22824, 349.22824, 349.22824, 349.22824, - 369.99442, 369.99442, 369.99442, 369.99442, - 391.99542, 391.99542, 391.99542, 391.99542, - 415.3047, 415.3047, 415.3047, 415.3047, - 440.0, 440.0, 440.0, - 466.1638, 466.1638, 466.1638, 466.1638, - 493.8833, 493.8833, 493.8833, 493.8833, - 523.2511, 523.2511, 523.2511, 523.2511, - 554.3653, 554.3653, 554.3653, 554.3653, - 587.3295, 587.3295, 587.3295, 587.3295, - 622.25397, 622.25397, 622.25397, 622.25397 + 415.3047, + 493.8833, 493.8833, 493.8833, 493.8833, 493.8833, + 493.8833, 493.8833, 493.8833, 493.8833, 493.8833, + 493.8833, 493.8833, + 830.6094, 830.6094, 830.6094, 830.6094, 830.6094, + 830.6094, 830.6094, 830.6094, 830.6094, 830.6094, + 830.6094, 830.6094 + ]); +} + + +#[test] +fn check_quant_one_key() { + let mut q = Quantizer::new(); + q.set_keys(0b0010_0000_0000); + + let v = + (0..=48).map(|i| { + let i = i - 24; + d_pit!( + q.process( + i as f32 * ((0.1 * 0.5) / 12.0))) + }).collect::>(); + + assert_vec_feq!(v, vec![ + 220.0, 220.0, 220.0, 220.0, 220.0, 220.0, + 220.0, 220.0, 220.0, 220.0, 220.0, 220.0, + 220.0, + 440.0, 440.0, 440.0, 440.0, 440.0, 440.0, + 440.0, 440.0, 440.0, 440.0, 440.0, 440.0, + 440.0, 440.0, 440.0, 440.0, 440.0, 440.0, + 440.0, 440.0, 440.0, 440.0, 440.0, 440.0, + 880.0, 880.0, 880.0, 880.0, 880.0, 880.0, + 880.0, 880.0, 880.0, 880.0, 880.0, 880.0 ]); }