1
0
Fork 0

Add an update_smoother() parameter method

This commit is contained in:
Robbert van der Helm 2022-02-02 21:26:34 +01:00
parent ea7dc2ffa3
commit e30a72888a
2 changed files with 36 additions and 0 deletions

View file

@ -34,6 +34,10 @@ pub trait Param {
/// The plain parameter type. /// The plain parameter type.
type Plain; type Plain;
/// Update the smoother state to point to the current value. Used when initializing and
/// restoring a plugin so everything is in sync.
fn update_smoother(&mut self, sample_rate: f32);
/// Set this parameter based on a string. Returns whether the updating succeeded. That can fail /// Set this parameter based on a string. Returns whether the updating succeeded. That can fail
/// if the string cannot be parsed. /// if the string cannot be parsed.
/// ///
@ -44,12 +48,19 @@ pub trait Param {
fn plain_value(&self) -> Self::Plain; fn plain_value(&self) -> Self::Plain;
/// Set this parameter based on a plain, unnormalized value. /// Set this parameter based on a plain, unnormalized value.
///
/// This does **not** update the smoother.
///
/// TDOO: Decide on whether this should update the smoother or not. That wouldn't be compatible
/// with sample accurate automation when we add that.
fn set_plain_value(&mut self, plain: Self::Plain); fn set_plain_value(&mut self, plain: Self::Plain);
/// Get the normalized `[0, 1]` value for this parameter. /// Get the normalized `[0, 1]` value for this parameter.
fn normalized_value(&self) -> f32; fn normalized_value(&self) -> f32;
/// Set this parameter based on a normalized value. /// Set this parameter based on a normalized value.
///
/// This does **not** update the smoother.
fn set_normalized_value(&mut self, normalized: f32); fn set_normalized_value(&mut self, normalized: f32);
/// Get the string representation for a normalized value. Used as part of the wrappers. Most /// Get the string representation for a normalized value. Used as part of the wrappers. Most
@ -151,6 +162,10 @@ macro_rules! impl_plainparam {
impl Param for $ty { impl Param for $ty {
type Plain = $plain; type Plain = $plain;
fn update_smoother(&mut self, sample_rate: f32) {
self.smoothed.set_target(sample_rate, self.value);
}
fn set_from_string(&mut self, string: &str) -> bool { fn set_from_string(&mut self, string: &str) -> bool {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string),
@ -219,6 +234,10 @@ impl_plainparam!(IntParam, i32);
impl Param for BoolParam { impl Param for BoolParam {
type Plain = bool; type Plain = bool;
fn update_smoother(&mut self, _sample_rate: f32) {
// Can't really smooth a binary parameter now can you
}
fn set_from_string(&mut self, string: &str) -> bool { fn set_from_string(&mut self, string: &str) -> bool {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string),

View file

@ -119,6 +119,21 @@ impl ParamPtr {
} }
} }
/// Update the smoother state to point to the current value. Used when initializing and
/// restoring a plugin so everything is in sync.
///
/// # Safety
///
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
/// still alive.
pub unsafe fn update_smoother(&mut self, sample_rate: f32) {
match &self {
ParamPtr::FloatParam(p) => (**p).update_smoother(sample_rate),
ParamPtr::IntParam(p) => (**p).update_smoother(sample_rate),
ParamPtr::BoolParam(p) => (**p).update_smoother(sample_rate),
}
}
/// Set this parameter based on a string. Returns whether the updating succeeded. That can fail /// Set this parameter based on a string. Returns whether the updating succeeded. That can fail
/// if the string cannot be parsed. /// if the string cannot be parsed.
/// ///
@ -150,6 +165,8 @@ impl ParamPtr {
/// Set this parameter based on a normalized value. /// Set this parameter based on a normalized value.
/// ///
/// This does **not** update the smoother.
///
/// # Safety /// # Safety
/// ///
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is /// Calling this function is only safe as long as the object this `ParamPtr` was created for is