diff --git a/tests/common/mod.rs b/tests/common/mod.rs index f56ef15..74d3b20 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -197,6 +197,37 @@ assertion failed: `(left[{}] == right[{}])` } } +#[macro_export] +macro_rules! assert_decimated_slope_feq_sfine { + ($vec:expr, $decimate:expr, $cmp_vec:expr) => { + let cmp_vec = $cmp_vec; + let mut res : Vec = vec![]; + let mut prev = 0.0; + for (i, s) in $vec.iter().enumerate() { + let delta = *s - prev; + if i > 0 { + res.push(delta); + } + prev = *s; + } + + let res : Vec = res.iter().step_by($decimate).copied().collect(); + + for (i, (s, scmp)) in res.iter().zip(cmp_vec.iter()).enumerate() { + if (s - scmp).abs() > 0.000000001 { + panic!(r#" +table_left: {:?} + +table_right: {:?} + +assertion failed: `(left[{}] == right[{}])` + left: `{:?}`, + right: `{:?}`"#, &res[i..], &(cmp_vec[i..]), i, i, s, scmp) + } + } + } +} + #[allow(dead_code)] pub fn collect_signal_changes(inp: &[f32], thres: i64) -> Vec<(usize, i64)> { let mut idxs = vec![]; diff --git a/tests/node_tslfo.rs b/tests/node_tslfo.rs new file mode 100644 index 0000000..6fa16d8 --- /dev/null +++ b/tests/node_tslfo.rs @@ -0,0 +1,100 @@ +// Copyright (c) 2021 Weird Constructor +// This file is a part of HexoDSP. Released under GPL-3.0-or-later. +// See README.md and COPYING for details. + +mod common; +use common::*; + +#[test] +fn check_node_tslfo_1() { + let (node_conf, mut node_exec) = new_node_engine(); + let mut matrix = Matrix::new(node_conf, 3, 3); + + let tsl = NodeId::TsLfo(0); + let out = NodeId::Out(0); + matrix.place(0, 0, Cell::empty(tsl) + .out(None, None, tsl.out("sig"))); + matrix.place(0, 1, Cell::empty(out) + .input(out.inp("ch1"), None, None)); + pset_d(&mut matrix, tsl, "time", 0.1); + matrix.sync().unwrap(); + + // Test shortest time 0.1ms: + let (out_l, _) = run_for_ms(&mut node_exec, 1.0); + assert_decimated_feq!(out_l, 1, vec![ + 0.0, 0.4535, 0.9070, 0.6394, 0.1859, + 0.2675, 0.7210, 0.8253, 0.37188, 0.0816 + ]); + + // Test 1ms: + pset_d_wait(&mut matrix, &mut node_exec, tsl, "time", 1.0); + let (out_l, _) = run_for_ms(&mut node_exec, 5.0); + assert_decimated_feq!(out_l, 10, vec![ + 0.7103, 0.8361, + 0.3826, 0.0708, 0.5244, 0.9779, 0.5685, 0.1150, + 0.3384 + ]); + + // Test 300000.0 ms + pset_d_wait(&mut matrix, &mut node_exec, tsl, "time", 300000.0); + let (out_l, _) = run_for_ms(&mut node_exec, 3000.0); + let ramp_slope = 1.0_f64 / (300.0 * 44100.0); + let tri_slope = ramp_slope * 2.0; + assert_float_eq!( + (out_l[0] - out_l[10000]).abs(), + (tri_slope * 10000.0) as f32); + assert_float_eq!( + (out_l[10000] - out_l[11000]).abs(), + (tri_slope * 1000.0) as f32); + assert_decimated_feq!(out_l, 10000, vec![ + 0.7566, // => Slope is ~0.0015 per 10000 samples + 0.7551, + 0.7536, + 0.7521, + 0.7506, + 0.7490, + 0.7475, + ]); +} + +#[test] +fn check_node_tslfo_trig_slopes() { + let (node_conf, mut node_exec) = new_node_engine(); + let mut matrix = Matrix::new(node_conf, 3, 3); + + let tsl = NodeId::TsLfo(0); + let out = NodeId::Out(0); + matrix.place(0, 0, Cell::empty(tsl) + .out(None, None, tsl.out("sig"))); + matrix.place(0, 1, Cell::empty(out) + .input(out.inp("ch1"), None, None)); + matrix.sync().unwrap(); + + // Test 1ms but at full ramp up and resync/trigger: + pset_d(&mut matrix, tsl, "rev", 1.0); + pset_d_wait(&mut matrix, &mut node_exec, tsl, "time", 10.0); + pset_d_wait(&mut matrix, &mut node_exec, tsl, "trig", 1.0); + let (out_l, _) = run_for_ms(&mut node_exec, 15.0); + let ramp_slope = 1.0_f64 / ((10.0 / 1000.0) * 44100.0); + assert_float_eq!( + (out_l[0] - out_l[1]).abs(), + ramp_slope as f32); + assert_decimated_feq!(out_l, 50, vec![ + 0.7513184, 0.8648106, 0.9783028, 0.09079399, 0.20428619, 0.3177784 + ]); + + pset_d_wait(&mut matrix, &mut node_exec, tsl, "trig", 0.0); + + // Test 1ms but at full ramp down and resync/trigger: + pset_d(&mut matrix, tsl, "rev", 0.0); + pset_d_wait(&mut matrix, &mut node_exec, tsl, "time", 10.0); + pset_d_wait(&mut matrix, &mut node_exec, tsl, "trig", 1.0); + let (out_l, _) = run_for_ms(&mut node_exec, 15.0); + let ramp_slope = 1.0_f64 / ((10.0 / 1000.0) * 44100.0); + assert_float_eq!( + (out_l[0] - out_l[1]).abs(), + ramp_slope as f32); + assert_decimated_feq!(out_l, 50, vec![ + 0.24945788, 0.13606784, 0.022677781, 0.90938777, 0.7959977 + ]); +}