From 5d0fbcd60d13b6d6c239cd0aa8d5fcb8e873fdea Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 25 Jan 2022 22:29:40 +0100 Subject: [PATCH] Clamp parameter values in (un)normalization In case the host or the plugin does something weird. --- nih_plug/src/params.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nih_plug/src/params.rs b/nih_plug/src/params.rs index 3558c94a..e8582828 100644 --- a/nih_plug/src/params.rs +++ b/nih_plug/src/params.rs @@ -116,15 +116,16 @@ impl Display for PlainParam { } } -// TODO: Clamping impl NormalizebleRange for Range { fn normalize(&self, unnormalized: f32) -> f32 { match &self { Range::Linear { min, max } => (unnormalized - min) / (max - min), } + .clamp(0.0, 1.0) } fn unnormalize(&self, normalized: f32) -> f32 { + let normalized = normalized.clamp(0.0, 1.0); match &self { Range::Linear { min, max } => (normalized * (max - min)) + min, } @@ -136,9 +137,11 @@ impl NormalizebleRange for Range { match &self { Range::Linear { min, max } => (unnormalized - min) as f32 / (max - min) as f32, } + .clamp(0.0, 1.0) } fn unnormalize(&self, normalized: f32) -> i32 { + let normalized = normalized.clamp(0.0, 1.0); match &self { Range::Linear { min, max } => (normalized * (max - min) as f32).round() as i32 + min, }