From a9ac7ec1e8ff030632a0f9ee6c99ea75369580eb Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Sat, 14 Aug 2021 00:39:46 +0200 Subject: [PATCH] Fix the top reflection in the RndWk max --- src/dsp/node_rndwk.rs | 12 ++++++++++-- tests/node_rndwk.rs | 11 ++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/dsp/node_rndwk.rs b/src/dsp/node_rndwk.rs index 7511b78..e97a8af 100644 --- a/src/dsp/node_rndwk.rs +++ b/src/dsp/node_rndwk.rs @@ -128,11 +128,19 @@ impl DspNode for RndWk { let step = denorm::RndWk::step(step, frame).clamp(-1.0, 1.0); let offs = denorm::RndWk::offs(offs, frame).clamp(-1.0, 1.0); - let target = + let mut target = self.slew_val.value() as f32 + ((self.rng.next() * 2.0 * step) - step) + offs; - self.target = (((target - min) % delta).abs() + min) as f64; + + // println!("{:8.6} {:8.6} {:8.6}", min, max, target); + // clamp target into a range we can reflect + target = target.clamp(min - (delta * 0.99), max + (delta * 0.99)); + // reflect back the overshoots: + if target > max { target = max - (max - target).abs(); } + if target < min { target = min + (min - target).abs(); } + + self.target = target as f64; } let slew_time_ms = denorm::RndWk::slew(slew, frame); diff --git a/tests/node_rndwk.rs b/tests/node_rndwk.rs index 11ce057..a2f2d90 100644 --- a/tests/node_rndwk.rs +++ b/tests/node_rndwk.rs @@ -152,14 +152,15 @@ fn check_node_rndwk_max() { pset_n(&mut matrix, rwk, "trig", 1.0); run_for_ms(&mut node_exec, 7.0); // wait for trigger... - let (out_l, _) = run_for_ms(&mut node_exec, 20.0); - assert_decimated_feq!(out_l, 60, vec![ + let (out_l, _) = run_for_ms(&mut node_exec, 50.0); + assert_decimated_feq!(out_l, 200, vec![ 0.0, // start value // slew ramp: - 0.011791383, 0.029931974, 0.04807256, 0.06621315, + 0.054119427, 0.11458806, 0.1750567, 0.23552532, 0.29599395, + 0.3564626, 0.4169312, // end value - // which is 0.5 - 0.56891763 - 0.06891763, 0.06891763, + // which is (0.5 - 0.43108237) == 0.06891763 + 0.43108237, 0.43108237, 0.43108237, 0.43108237 ]); }