simplified the quantizer and wrote more tests

This commit is contained in:
Weird Constructor 2021-08-28 16:28:03 +02:00
parent b22c084b08
commit 204c415c06
2 changed files with 44 additions and 37 deletions

View file

@ -2137,16 +2137,14 @@ impl<F: Flt> TriSawLFO<F> {
#[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
}
}

View file

@ -130,18 +130,39 @@ fn check_quant_edge_oct_offs() {
}).collect::<Vec<f32>>();
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::<Vec<f32>>();
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
]);
}