1
0
Fork 0

Store defaults on Param objects

And add methods for querying them.
This commit is contained in:
Robbert van der Helm 2022-03-21 12:49:59 +01:00
parent d5d54f0c06
commit a844051054
7 changed files with 64 additions and 3 deletions

View file

@ -58,6 +58,7 @@ impl Default for GainParams {
// ...either manually specify all fields: // ...either manually specify all fields:
gain: FloatParam { gain: FloatParam {
value: 0.0, value: 0.0,
default: 0.0,
smoothed: Smoother::new(SmoothingStyle::Linear(50.0)), smoothed: Smoother::new(SmoothingStyle::Linear(50.0)),
value_changed: None, value_changed: None,
range: FloatRange::Linear { range: FloatRange::Linear {

View file

@ -34,8 +34,17 @@ pub trait Param: Display {
fn plain_value(&self) -> Self::Plain; fn plain_value(&self) -> Self::Plain;
/// Get the normalized `[0, 1]` value for this parameter. /// Get the normalized `[0, 1]` value for this parameter.
/// TODO: Add a default implementation for this one as well
fn normalized_value(&self) -> f32; fn normalized_value(&self) -> f32;
/// Get the unnormalized default value for this parameter.
fn default_plain_value(&self) -> Self::Plain;
/// Get the normalized `[0, 1]` default value for this parameter.
fn default_normalized_value(&self) -> f32 {
self.preview_normalized(self.default_plain_value())
}
/// Get the number of steps for this paramter, if it is discrete. Used for the host's generic /// Get the number of steps for this paramter, if it is discrete. Used for the host's generic
/// UI. /// UI.
fn step_count(&self) -> Option<usize>; fn step_count(&self) -> Option<usize>;

View file

@ -9,8 +9,10 @@ use super::Param;
/// A simple boolean parmaeter. /// A simple boolean parmaeter.
#[repr(C, align(4))] #[repr(C, align(4))]
pub struct BoolParam { pub struct BoolParam {
/// The field's current, normalized value. Should be initialized with the default value. /// The field's current value. Should be initialized with the default value.
pub value: bool, pub value: bool,
/// The field's default value.
pub default: bool,
/// Optional callback for listening to value changes. The argument passed to this function is /// Optional callback for listening to value changes. The argument passed to this function is
/// the parameter's new value. This should not do anything expensive as it may be called /// the parameter's new value. This should not do anything expensive as it may be called
@ -33,6 +35,7 @@ impl Default for BoolParam {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: false, value: false,
default: false,
value_changed: None, value_changed: None,
name: "", name: "",
value_to_string: None, value_to_string: None,
@ -70,6 +73,10 @@ impl Param for BoolParam {
self.preview_normalized(self.value) self.preview_normalized(self.value)
} }
fn default_plain_value(&self) -> Self::Plain {
self.default
}
fn step_count(&self) -> Option<usize> { fn step_count(&self) -> Option<usize> {
Some(1) Some(1)
} }
@ -141,6 +148,7 @@ impl BoolParam {
pub fn new(name: &'static str, default: bool) -> Self { pub fn new(name: &'static str, default: bool) -> Self {
Self { Self {
value: default, value: default,
default,
name, name,
..Default::default() ..Default::default()
} }

View file

@ -73,6 +73,7 @@ impl<T: Enum + PartialEq + Default> Default for EnumParam<T> {
inner: EnumParamInner { inner: EnumParamInner {
inner: IntParam { inner: IntParam {
value: T::default().to_index() as i32, value: T::default().to_index() as i32,
default: T::default().to_index() as i32,
range: IntRange::Linear { range: IntRange::Linear {
min: 0, min: 0,
max: variants.len() as i32 - 1, max: variants.len() as i32 - 1,
@ -117,6 +118,10 @@ impl<T: Enum + PartialEq> Param for EnumParam<T> {
self.inner.normalized_value() self.inner.normalized_value()
} }
fn default_plain_value(&self) -> Self::Plain {
T::from_index(self.inner.default_plain_value() as usize)
}
fn step_count(&self) -> Option<usize> { fn step_count(&self) -> Option<usize> {
self.inner.step_count() self.inner.step_count()
} }
@ -186,6 +191,10 @@ impl Param for EnumParamInner {
self.inner.normalized_value() self.inner.normalized_value()
} }
fn default_plain_value(&self) -> Self::Plain {
self.inner.default_plain_value()
}
fn step_count(&self) -> Option<usize> { fn step_count(&self) -> Option<usize> {
Some(self.len() - 1) Some(self.len() - 1)
} }

View file

@ -28,9 +28,12 @@ pub struct FloatParam {
/// Storing parameter values like this instead of in a single contiguous array is bad for cache /// Storing parameter values like this instead of in a single contiguous array is bad for cache
/// locality, but it does allow for a much nicer declarative API. /// locality, but it does allow for a much nicer declarative API.
pub value: f32, pub value: f32,
/// The field's default plain, unnormalized value.
pub default: f32,
/// An optional smoother that will automatically interpolate between the new automation values /// An optional smoother that will automatically interpolate between the new automation values
/// set by the host. /// set by the host.
pub smoothed: Smoother<f32>, pub smoothed: Smoother<f32>,
/// Optional callback for listening to value changes. The argument passed to this function is /// Optional callback for listening to value changes. The argument passed to this function is
/// the parameter's new **plain** value. This should not do anything expensive as it may be /// the parameter's new **plain** value. This should not do anything expensive as it may be
/// called multiple times in rapid succession. /// called multiple times in rapid succession.
@ -67,6 +70,7 @@ impl Default for FloatParam {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0.0, value: 0.0,
default: 0.0,
smoothed: Smoother::none(), smoothed: Smoother::none(),
value_changed: None, value_changed: None,
range: FloatRange::default(), range: FloatRange::default(),
@ -111,6 +115,10 @@ impl Param for FloatParam {
self.preview_normalized(self.value) self.preview_normalized(self.value)
} }
fn default_plain_value(&self) -> Self::Plain {
self.default
}
fn step_count(&self) -> Option<usize> { fn step_count(&self) -> Option<usize> {
None None
} }
@ -212,6 +220,7 @@ impl FloatParam {
pub fn new(name: &'static str, default: f32, range: FloatRange) -> Self { pub fn new(name: &'static str, default: f32, range: FloatRange) -> Self {
Self { Self {
value: default, value: default,
default,
range, range,
name, name,
..Default::default() ..Default::default()

View file

@ -28,9 +28,12 @@ pub struct IntParam {
/// Storing parameter values like this instead of in a single contiguous array is bad for cache /// Storing parameter values like this instead of in a single contiguous array is bad for cache
/// locality, but it does allow for a much nicer declarative API. /// locality, but it does allow for a much nicer declarative API.
pub value: i32, pub value: i32,
/// The field's default plain, unnormalized value.
pub default: i32,
/// An optional smoother that will automatically interpolate between the new automation values /// An optional smoother that will automatically interpolate between the new automation values
/// set by the host. /// set by the host.
pub smoothed: Smoother<i32>, pub smoothed: Smoother<i32>,
/// Optional callback for listening to value changes. The argument passed to this function is /// Optional callback for listening to value changes. The argument passed to this function is
/// the parameter's new **plain** value. This should not do anything expensive as it may be /// the parameter's new **plain** value. This should not do anything expensive as it may be
/// called multiple times in rapid succession. /// called multiple times in rapid succession.
@ -63,6 +66,7 @@ impl Default for IntParam {
fn default() -> Self { fn default() -> Self {
Self { Self {
value: 0, value: 0,
default: 0,
smoothed: Smoother::none(), smoothed: Smoother::none(),
value_changed: None, value_changed: None,
range: IntRange::default(), range: IntRange::default(),
@ -102,6 +106,10 @@ impl Param for IntParam {
self.preview_normalized(self.value) self.preview_normalized(self.value)
} }
fn default_plain_value(&self) -> Self::Plain {
self.default
}
fn step_count(&self) -> Option<usize> { fn step_count(&self) -> Option<usize> {
Some(self.range.step_count()) Some(self.range.step_count())
} }
@ -176,6 +184,7 @@ impl IntParam {
pub fn new(name: &'static str, default: i32, range: IntRange) -> Self { pub fn new(name: &'static str, default: i32, range: IntRange) -> Self {
Self { Self {
value: default, value: default,
default,
range, range,
name, name,
..Default::default() ..Default::default()

View file

@ -148,6 +148,7 @@ impl ParamPtr {
param_ptr_forward!(pub unsafe fn name(&self) -> &'static str); param_ptr_forward!(pub unsafe fn name(&self) -> &'static str);
param_ptr_forward!(pub unsafe fn unit(&self) -> &'static str); param_ptr_forward!(pub unsafe fn unit(&self) -> &'static str);
param_ptr_forward!(pub unsafe fn normalized_value(&self) -> f32); param_ptr_forward!(pub unsafe fn normalized_value(&self) -> f32);
param_ptr_forward!(pub unsafe fn default_normalized_value(&self) -> f32);
param_ptr_forward!(pub unsafe fn step_count(&self) -> Option<usize>); param_ptr_forward!(pub unsafe fn step_count(&self) -> Option<usize>);
param_ptr_forward!(pub unsafe fn previous_normalized_step(&self, from: f32) -> f32); param_ptr_forward!(pub unsafe fn previous_normalized_step(&self, from: f32) -> f32);
param_ptr_forward!(pub unsafe fn next_normalized_step(&self, from: f32) -> f32); param_ptr_forward!(pub unsafe fn next_normalized_step(&self, from: f32) -> f32);
@ -160,8 +161,8 @@ impl ParamPtr {
// These functions involve casts since the plugin formats only do floating point types, so we // These functions involve casts since the plugin formats only do floating point types, so we
// can't generate them with the macro: // can't generate them with the macro:
/// Get the plain value for a plain, unnormalized value, as a float. Useful in conjunction with /// Get the parameter's plain, unnormalized value, converted to a float. Useful in conjunction
/// [`preview_plain()`][Self::preview_plain()] to compare a snapped discrete value to a /// with [`preview_plain()`][Self::preview_plain()] to compare a snapped discrete value to a
/// parameter's current snapped value without having to do a back and forth conversion using /// parameter's current snapped value without having to do a back and forth conversion using
/// normalized values. /// normalized values.
/// ///
@ -178,6 +179,21 @@ impl ParamPtr {
} }
} }
/// Get the parameter's default value as a plain, unnormalized value, converted to a float.
///
/// # Safety
///
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
/// still alive.
pub unsafe fn default_plain_value(&self) -> f32 {
match &self {
ParamPtr::FloatParam(p) => (**p).default_plain_value(),
ParamPtr::IntParam(p) => (**p).default_plain_value() as f32,
ParamPtr::BoolParam(p) => (**p).normalized_value(),
ParamPtr::EnumParam(p) => (**p).default_plain_value() as f32,
}
}
/// Get the normalized value for a plain, unnormalized value, as a float. Used as part of the /// Get the normalized value for a plain, unnormalized value, as a float. Used as part of the
/// wrappers. /// wrappers.
/// ///