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:
gain: FloatParam {
value: 0.0,
default: 0.0,
smoothed: Smoother::new(SmoothingStyle::Linear(50.0)),
value_changed: None,
range: FloatRange::Linear {

View file

@ -34,8 +34,17 @@ pub trait Param: Display {
fn plain_value(&self) -> Self::Plain;
/// Get the normalized `[0, 1]` value for this parameter.
/// TODO: Add a default implementation for this one as well
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
/// UI.
fn step_count(&self) -> Option<usize>;

View file

@ -9,8 +9,10 @@ use super::Param;
/// A simple boolean parmaeter.
#[repr(C, align(4))]
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,
/// The field's default value.
pub default: bool,
/// 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
@ -33,6 +35,7 @@ impl Default for BoolParam {
fn default() -> Self {
Self {
value: false,
default: false,
value_changed: None,
name: "",
value_to_string: None,
@ -70,6 +73,10 @@ impl Param for BoolParam {
self.preview_normalized(self.value)
}
fn default_plain_value(&self) -> Self::Plain {
self.default
}
fn step_count(&self) -> Option<usize> {
Some(1)
}
@ -141,6 +148,7 @@ impl BoolParam {
pub fn new(name: &'static str, default: bool) -> Self {
Self {
value: default,
default,
name,
..Default::default()
}

View file

@ -73,6 +73,7 @@ impl<T: Enum + PartialEq + Default> Default for EnumParam<T> {
inner: EnumParamInner {
inner: IntParam {
value: T::default().to_index() as i32,
default: T::default().to_index() as i32,
range: IntRange::Linear {
min: 0,
max: variants.len() as i32 - 1,
@ -117,6 +118,10 @@ impl<T: Enum + PartialEq> Param for EnumParam<T> {
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> {
self.inner.step_count()
}
@ -186,6 +191,10 @@ impl Param for EnumParamInner {
self.inner.normalized_value()
}
fn default_plain_value(&self) -> Self::Plain {
self.inner.default_plain_value()
}
fn step_count(&self) -> Option<usize> {
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
/// locality, but it does allow for a much nicer declarative API.
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
/// set by the host.
pub smoothed: Smoother<f32>,
/// 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
/// called multiple times in rapid succession.
@ -67,6 +70,7 @@ impl Default for FloatParam {
fn default() -> Self {
Self {
value: 0.0,
default: 0.0,
smoothed: Smoother::none(),
value_changed: None,
range: FloatRange::default(),
@ -111,6 +115,10 @@ impl Param for FloatParam {
self.preview_normalized(self.value)
}
fn default_plain_value(&self) -> Self::Plain {
self.default
}
fn step_count(&self) -> Option<usize> {
None
}
@ -212,6 +220,7 @@ impl FloatParam {
pub fn new(name: &'static str, default: f32, range: FloatRange) -> Self {
Self {
value: default,
default,
range,
name,
..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
/// locality, but it does allow for a much nicer declarative API.
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
/// set by the host.
pub smoothed: Smoother<i32>,
/// 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
/// called multiple times in rapid succession.
@ -63,6 +66,7 @@ impl Default for IntParam {
fn default() -> Self {
Self {
value: 0,
default: 0,
smoothed: Smoother::none(),
value_changed: None,
range: IntRange::default(),
@ -102,6 +106,10 @@ impl Param for IntParam {
self.preview_normalized(self.value)
}
fn default_plain_value(&self) -> Self::Plain {
self.default
}
fn step_count(&self) -> Option<usize> {
Some(self.range.step_count())
}
@ -176,6 +184,7 @@ impl IntParam {
pub fn new(name: &'static str, default: i32, range: IntRange) -> Self {
Self {
value: default,
default,
range,
name,
..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 unit(&self) -> &'static str);
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 previous_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
// can't generate them with the macro:
/// Get the plain value for a plain, unnormalized value, as a float. Useful in conjunction with
/// [`preview_plain()`][Self::preview_plain()] to compare a snapped discrete value to a
/// Get the parameter's plain, unnormalized value, converted to a float. Useful in conjunction
/// 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
/// 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
/// wrappers.
///