diff --git a/src/param.rs b/src/param.rs index cb5484ed..3ebd36e5 100644 --- a/src/param.rs +++ b/src/param.rs @@ -22,17 +22,6 @@ pub trait Param: Display { /// The plain parameter type. type Plain; - /// Update the smoother state to point to the current value. Also used when initializing and - /// restoring a plugin so everything is in sync. In that case the smoother should completely - /// reset to the current value. - fn update_smoother(&mut self, sample_rate: f32, reset: bool); - - /// Set this parameter based on a string. Returns whether the updating succeeded. That can fail - /// if the string cannot be parsed. - /// - /// TODO: After implementing VST3, check if we handle parsing failures correctly - fn set_from_string(&mut self, string: &str) -> bool; - /// Get the unnormalized value for this parameter. fn plain_value(&self) -> Self::Plain; @@ -67,6 +56,17 @@ pub trait Param: Display { /// wrappers. This **does** snap to step sizes for continuous parameters (i.e. [FloatParam]). fn preview_plain(&self, normalized: f32) -> Self::Plain; + /// Set this parameter based on a string. Returns whether the updating succeeded. That can fail + /// if the string cannot be parsed. + /// + /// TODO: After implementing VST3, check if we handle parsing failures correctly + fn set_from_string(&mut self, string: &str) -> bool; + + /// Update the smoother state to point to the current value. Also used when initializing and + /// restoring a plugin so everything is in sync. In that case the smoother should completely + /// reset to the current value. + fn update_smoother(&mut self, sample_rate: f32, reset: bool); + /// Internal implementation detail for implementing [internals::Params]. This should not be used /// directly. fn as_ptr(&self) -> internals::ParamPtr; diff --git a/src/param/boolean.rs b/src/param/boolean.rs index 1626be30..84bb9c1e 100644 --- a/src/param/boolean.rs +++ b/src/param/boolean.rs @@ -54,25 +54,6 @@ impl Display for BoolParam { impl Param for BoolParam { type Plain = bool; - fn update_smoother(&mut self, _sample_rate: f32, _init: bool) { - // Can't really smooth a binary parameter now can you - } - - fn set_from_string(&mut self, string: &str) -> bool { - let value = match &self.string_to_value { - Some(f) => f(string), - None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")), - }; - - match value { - Some(plain) => { - self.set_plain_value(plain); - true - } - None => false, - } - } - fn plain_value(&self) -> Self::Plain { self.value } @@ -122,6 +103,25 @@ impl Param for BoolParam { normalized > 0.5 } + fn set_from_string(&mut self, string: &str) -> bool { + let value = match &self.string_to_value { + Some(f) => f(string), + None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")), + }; + + match value { + Some(plain) => { + self.set_plain_value(plain); + true + } + None => false, + } + } + + fn update_smoother(&mut self, _sample_rate: f32, _init: bool) { + // Can't really smooth a binary parameter now can you + } + fn as_ptr(&self) -> ParamPtr { ParamPtr::BoolParam(self as *const BoolParam as *mut BoolParam) } diff --git a/src/param/enums.rs b/src/param/enums.rs index 106a4513..64a90445 100644 --- a/src/param/enums.rs +++ b/src/param/enums.rs @@ -99,14 +99,6 @@ impl Display for EnumParamInner { impl Param for EnumParam { type Plain = T; - fn update_smoother(&mut self, sample_rate: f32, reset: bool) { - self.inner.update_smoother(sample_rate, reset) - } - - fn set_from_string(&mut self, string: &str) -> bool { - self.inner.set_from_string(string) - } - fn plain_value(&self) -> Self::Plain { T::from_index(self.inner.plain_value() as usize) } @@ -140,6 +132,14 @@ impl Param for EnumParam { T::from_index(self.inner.preview_plain(normalized) as usize) } + fn set_from_string(&mut self, string: &str) -> bool { + self.inner.set_from_string(string) + } + + fn update_smoother(&mut self, sample_rate: f32, reset: bool) { + self.inner.update_smoother(sample_rate, reset) + } + fn as_ptr(&self) -> ParamPtr { self.inner.as_ptr() } @@ -148,20 +148,6 @@ impl Param for EnumParam { impl Param for EnumParamInner { type Plain = i32; - fn update_smoother(&mut self, sample_rate: f32, reset: bool) { - self.inner.update_smoother(sample_rate, reset) - } - - fn set_from_string(&mut self, string: &str) -> bool { - match self.variants.iter().position(|n| n == &string) { - Some(idx) => { - self.inner.set_plain_value(idx as i32); - true - } - None => false, - } - } - fn plain_value(&self) -> Self::Plain { self.inner.plain_value() } @@ -198,6 +184,20 @@ impl Param for EnumParamInner { self.inner.preview_plain(normalized) } + fn set_from_string(&mut self, string: &str) -> bool { + match self.variants.iter().position(|n| n == &string) { + Some(idx) => { + self.inner.set_plain_value(idx as i32); + true + } + None => false, + } + } + + fn update_smoother(&mut self, sample_rate: f32, reset: bool) { + self.inner.update_smoother(sample_rate, reset) + } + fn as_ptr(&self) -> ParamPtr { ParamPtr::EnumParam(self as *const EnumParamInner as *mut EnumParamInner) } diff --git a/src/param/plain.rs b/src/param/plain.rs index 1611bcf0..fee1bbb8 100644 --- a/src/param/plain.rs +++ b/src/param/plain.rs @@ -101,30 +101,6 @@ macro_rules! impl_plainparam { impl Param for $ty { type Plain = $plain; - fn update_smoother(&mut self, sample_rate: f32, reset: bool) { - if reset { - self.smoothed.reset(self.value); - } else { - self.smoothed.set_target(sample_rate, self.value); - } - } - - fn set_from_string(&mut self, string: &str) -> bool { - let value = match &self.string_to_value { - Some(f) => f(string), - // TODO: Check how Rust's parse function handles trailing garbage - None => string.parse().ok(), - }; - - match value { - Some(plain) => { - self.set_plain_value(plain); - true - } - None => false, - } - } - fn plain_value(&self) -> Self::Plain { self.value } @@ -186,6 +162,30 @@ macro_rules! impl_plainparam { } } + fn set_from_string(&mut self, string: &str) -> bool { + let value = match &self.string_to_value { + Some(f) => f(string), + // TODO: Check how Rust's parse function handles trailing garbage + None => string.parse().ok(), + }; + + match value { + Some(plain) => { + self.set_plain_value(plain); + true + } + None => false, + } + } + + fn update_smoother(&mut self, sample_rate: f32, reset: bool) { + if reset { + self.smoothed.reset(self.value); + } else { + self.smoothed.set_target(sample_rate, self.value); + } + } + fn as_ptr(&self) -> ParamPtr { ParamPtr::$ty(self as *const $ty as *mut $ty) }