From 963696cbff0a5b7ad3631419363141a3d3a164aa Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 6 Mar 2022 12:27:52 +0100 Subject: [PATCH] Warn on invalid ranges with logarithmic smoothing --- src/param/float.rs | 16 ++++++++++++++++ src/param/integer.rs | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/param/float.rs b/src/param/float.rs index 655df074..6f94370a 100644 --- a/src/param/float.rs +++ b/src/param/float.rs @@ -211,6 +211,22 @@ impl FloatParam { /// Set up a smoother that can gradually interpolate changes made to this parameter, preventing /// clicks and zipper noises. pub fn with_smoother(mut self, style: SmoothingStyle) -> Self { + // Logarithmic smoothing will cause problems if the range goes through zero since then you + // end up multplying by zero + let goes_through_zero = match (&style, &self.range) { + ( + SmoothingStyle::Logarithmic(_), + FloatRange::Linear { min, max } + | FloatRange::Skewed { min, max, .. } + | FloatRange::SymmetricalSkewed { min, max, .. }, + ) => *min == 0.0 || *max == 0.0 || min.signum() != max.signum(), + _ => false, + }; + nih_debug_assert!( + !goes_through_zero, + "Logarithmic smoothing does not work with ranges that go through zero" + ); + self.smoothed = Smoother::new(style); self } diff --git a/src/param/integer.rs b/src/param/integer.rs index a47f0cb7..f206fe28 100644 --- a/src/param/integer.rs +++ b/src/param/integer.rs @@ -190,6 +190,19 @@ impl IntParam { /// Set up a smoother that can gradually interpolate changes made to this parameter, preventing /// clicks and zipper noises. pub fn with_smoother(mut self, style: SmoothingStyle) -> Self { + // Logarithmic smoothing will cause problems if the range goes through zero since then you + // end up multplying by zero + let goes_through_zero = match (&style, &self.range) { + (SmoothingStyle::Logarithmic(_), IntRange::Linear { min, max }) => { + *min == 0 || *max == 0 || min.signum() != max.signum() + } + _ => false, + }; + nih_debug_assert!( + !goes_through_zero, + "Logarithmic smoothing does not work with ranges that go through zero" + ); + self.smoothed = Smoother::new(style); self }