2022-02-02 07:01:28 +11:00
|
|
|
//! Implementation details for the parameter management.
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-05-02 02:45:35 +10:00
|
|
|
use super::{Param, ParamFlags, ParamMut};
|
2022-02-02 07:01:28 +11:00
|
|
|
|
2022-03-17 01:53:25 +11:00
|
|
|
pub use nih_plug_derive::Params;
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Re-export for use in the [`Params`] proc-macro.
|
2022-02-02 07:01:28 +11:00
|
|
|
pub use serde_json::from_str as deserialize_field;
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Re-export for use in the [`Params`] proc-macro.
|
2022-02-02 07:01:28 +11:00
|
|
|
pub use serde_json::to_string as serialize_field;
|
|
|
|
|
2022-03-17 03:04:38 +11:00
|
|
|
/// Describes a struct containing parameters and other persistent fields.
|
2022-02-02 07:01:28 +11:00
|
|
|
///
|
2022-03-17 03:04:38 +11:00
|
|
|
/// This trait can be derived on a struct containing [`FloatParam`][super::FloatParam] and other
|
|
|
|
/// parameter fields. When deriving this trait, any of those parameter fields should have the `#[id
|
|
|
|
/// = "stable"]` attribute, where `stable` is an up to 6 character long string (to avoid collisions)
|
|
|
|
/// that will be used to identify the parameter internall so you can safely move it around and
|
|
|
|
/// rename the field without breaking compatibility with old presets.
|
|
|
|
///
|
|
|
|
/// The struct can also contain other fields that should be persisted along with the rest of the
|
|
|
|
/// preset data. These fields should be [`PersistentField`]s annotated with the `#[persist = "key"]`
|
|
|
|
/// attribute containing types that can be serialized and deserialized with
|
|
|
|
/// [Serde](https://serde.rs/).
|
2022-03-04 06:37:01 +11:00
|
|
|
///
|
|
|
|
/// And finally when deriving this trait, it is also possible to inherit the parameters from other
|
2022-03-17 03:04:38 +11:00
|
|
|
/// `Params` objects by adding the `#[nested = "Group Name"]` attribute to those fields. These
|
|
|
|
/// groups will be displayed as a tree-like structure if your DAW supports it. Parameter IDs and
|
2022-03-04 06:37:01 +11:00
|
|
|
/// persisting keys still need to be **unique** when usting nested parameter structs. This currently
|
|
|
|
/// has the following caveats:
|
|
|
|
///
|
|
|
|
/// - Enforcing that parameter IDs and persist keys are unique does not work across nested structs.
|
|
|
|
/// - Deserializing persisted fields will give false positives about fields not existing.
|
2022-02-02 07:01:28 +11:00
|
|
|
///
|
2022-02-04 03:05:38 +11:00
|
|
|
/// Take a look at the example gain plugin to see how this should be used.
|
|
|
|
///
|
2022-02-02 07:01:28 +11:00
|
|
|
/// # Safety
|
|
|
|
///
|
2022-04-07 23:31:46 +10:00
|
|
|
/// This implementation is safe when using from the wrapper because the plugin's returned `Params`
|
|
|
|
/// object lives in an `Arc`, and the wrapper also holds a reference to this `Arc`.
|
|
|
|
pub unsafe trait Params: 'static + Send + Sync {
|
2022-04-14 04:20:27 +10:00
|
|
|
/// Create a mapping from unique parameter IDs to parameter pointers along with the name of the
|
|
|
|
/// group/unit/module they are in, as a `(param_id, param_ptr, group)` triple. The order of the
|
|
|
|
/// `Vec` determines the display order in the (host's) generic UI. The group name is either an
|
|
|
|
/// empty string for top level parameters, or a slash/delimited `"Group Name 1/Group Name 2"` if
|
|
|
|
/// this `Params` object contains nested child objects. All components of a group path must
|
|
|
|
/// exist or you may encounter panics. The derive macro does this for every parameter field
|
|
|
|
/// marked with `#[id = "stable"]`, and it also inlines all fields from nested child `Params`
|
|
|
|
/// structs marked with `#[nested = "Group Name"]` while prefixing that group name before the
|
|
|
|
/// parameter's originanl group name. Dereferencing the pointers stored in the values is only
|
|
|
|
/// valid as long as this object is valid.
|
2022-03-20 23:03:03 +11:00
|
|
|
///
|
|
|
|
/// # Note
|
2022-03-04 06:37:01 +11:00
|
|
|
///
|
2022-03-20 23:03:03 +11:00
|
|
|
/// This uses `String` even though for the `Params` derive macro `&'static str` would have been
|
|
|
|
/// fine to be able to support custom reusable Params implemnetations.
|
2022-04-07 23:31:46 +10:00
|
|
|
fn param_map(&self) -> Vec<(String, ParamPtr, String)>;
|
2022-02-02 07:01:28 +11:00
|
|
|
|
|
|
|
/// Serialize all fields marked with `#[persist = "stable_name"]` into a hash map containing
|
|
|
|
/// JSON-representations of those fields so they can be written to the plugin's state and
|
2022-03-04 09:05:01 +11:00
|
|
|
/// recalled later. This uses [`serialize_field()`] under the hood.
|
2022-03-20 23:03:03 +11:00
|
|
|
fn serialize_fields(&self) -> HashMap<String, String> {
|
|
|
|
HashMap::new()
|
|
|
|
}
|
2022-02-02 07:01:28 +11:00
|
|
|
|
|
|
|
/// Restore all fields marked with `#[persist = "stable_name"]` from a hashmap created by
|
2022-03-04 09:05:01 +11:00
|
|
|
/// [`serialize_fields()`][Self::serialize_fields()]. All of thse fields should be wrapped in a
|
|
|
|
/// [`PersistentField`] with thread safe interior mutability, like an `RwLock` or a `Mutex`.
|
|
|
|
/// This gets called when the plugin's state is being restored. This uses [deserialize_field()]
|
|
|
|
/// under the hood.
|
2022-03-20 23:03:03 +11:00
|
|
|
#[allow(unused_variables)]
|
|
|
|
fn deserialize_fields(&self, serialized: &HashMap<String, String>) {}
|
2022-02-02 07:01:28 +11:00
|
|
|
}
|
|
|
|
|
2022-03-04 00:28:52 +11:00
|
|
|
/// Internal pointers to parameters. This is an implementation detail used by the wrappers for type
|
|
|
|
/// erasure.
|
2022-02-06 03:43:15 +11:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
2022-02-02 07:01:28 +11:00
|
|
|
pub enum ParamPtr {
|
|
|
|
FloatParam(*mut super::FloatParam),
|
|
|
|
IntParam(*mut super::IntParam),
|
|
|
|
BoolParam(*mut super::BoolParam),
|
2022-02-15 01:30:10 +11:00
|
|
|
/// Since we can't encode the actual enum here, this inner parameter struct contains all of the
|
|
|
|
/// relevant information from the enum so it can be type erased.
|
|
|
|
EnumParam(*mut super::enums::EnumParamInner),
|
2022-02-02 07:01:28 +11:00
|
|
|
}
|
|
|
|
|
2022-04-07 23:31:46 +10:00
|
|
|
// These pointers only point to fields on structs kept in an `Arc<dyn Params>`, and the caller
|
|
|
|
// always needs to make sure that dereferencing them is safe. To do that the plugin wrappers will
|
|
|
|
// keep references to that `Arc` around for the entire lifetime of the plugin.
|
2022-02-02 07:01:28 +11:00
|
|
|
unsafe impl Send for ParamPtr {}
|
|
|
|
unsafe impl Sync for ParamPtr {}
|
|
|
|
|
|
|
|
/// The functinoality needed for persisting a field to the plugin's state, and for restoring values
|
|
|
|
/// when loading old state.
|
|
|
|
///
|
|
|
|
/// TODO: Modifying these fields (or any parameter for that matter) should mark the plugin's state
|
|
|
|
/// as dirty.
|
|
|
|
pub trait PersistentField<'a, T>: Send + Sync
|
|
|
|
where
|
|
|
|
T: serde::Serialize + serde::Deserialize<'a>,
|
|
|
|
{
|
|
|
|
fn set(&self, new_value: T);
|
|
|
|
fn map<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: Fn(&T) -> R;
|
|
|
|
}
|
|
|
|
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Generate a [`ParamPtr`] function that forwards the function call to the underlying `Param`. We
|
2022-03-04 00:28:52 +11:00
|
|
|
/// can't have an `.as_param()` function since the return type would differ depending on the
|
|
|
|
/// underlying parameter type, so instead we need to type erase all of the functions individually.
|
|
|
|
macro_rules! param_ptr_forward(
|
2022-05-02 02:45:35 +10:00
|
|
|
($vis:vis unsafe fn $method:ident(&self $(, $arg_name:ident: $arg_ty:ty)*) $(-> $ret:ty)?) => {
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Calls the corresponding method on the underlying [`Param`] object.
|
2022-03-04 00:28:52 +11:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Calling this function is only safe as long as the object this [`ParamPtr`] was created
|
|
|
|
/// for is still alive.
|
2022-05-02 02:45:35 +10:00
|
|
|
$vis unsafe fn $method(&self $(, $arg_name: $arg_ty)*) $(-> $ret)? {
|
2022-03-04 00:28:52 +11:00
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
ParamPtr::IntParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
ParamPtr::BoolParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
ParamPtr::EnumParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
}
|
2022-02-02 07:01:28 +11:00
|
|
|
}
|
2022-03-04 00:28:52 +11:00
|
|
|
};
|
|
|
|
// XXX: Is there a way to combine these two? Hygienic macros don't let you call `&self` without
|
|
|
|
// it being defined in the macro.
|
2022-05-02 02:45:35 +10:00
|
|
|
($vis:vis unsafe fn $method:ident(&mut self $(, $arg_name:ident: $arg_ty:ty)*) $(-> $ret:ty)?) => {
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Calls the corresponding method on the underlying [`Param`] object.
|
2022-03-04 00:28:52 +11:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
2022-03-04 09:05:01 +11:00
|
|
|
/// Calling this function is only safe as long as the object this [`ParamPtr`] was created
|
|
|
|
/// for is still alive.
|
2022-05-02 02:45:35 +10:00
|
|
|
$vis unsafe fn $method(&mut self $(, $arg_name: $arg_ty)*) $(-> $ret)? {
|
2022-03-04 00:28:52 +11:00
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
ParamPtr::IntParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
ParamPtr::BoolParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
ParamPtr::EnumParam(p) => (**p).$method($($arg_name),*),
|
|
|
|
}
|
2022-02-02 07:01:28 +11:00
|
|
|
}
|
2022-03-04 00:28:52 +11:00
|
|
|
};
|
|
|
|
);
|
2022-02-02 07:01:28 +11:00
|
|
|
|
2022-03-04 00:28:52 +11:00
|
|
|
impl ParamPtr {
|
2022-04-12 07:27:36 +10:00
|
|
|
param_ptr_forward!(pub unsafe fn name(&self) -> &str);
|
2022-03-04 00:28:52 +11:00
|
|
|
param_ptr_forward!(pub unsafe fn unit(&self) -> &'static str);
|
|
|
|
param_ptr_forward!(pub unsafe fn normalized_value(&self) -> f32);
|
2022-05-02 02:30:30 +10:00
|
|
|
param_ptr_forward!(pub unsafe fn unmodulated_normalized_value(&self) -> f32);
|
2022-03-21 22:49:59 +11:00
|
|
|
param_ptr_forward!(pub unsafe fn default_normalized_value(&self) -> f32);
|
2022-03-20 02:09:31 +11:00
|
|
|
param_ptr_forward!(pub unsafe fn step_count(&self) -> Option<usize>);
|
2022-03-20 05:24:08 +11:00
|
|
|
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);
|
2022-03-20 02:09:31 +11:00
|
|
|
param_ptr_forward!(pub unsafe fn initialize_block_smoother(&mut self, max_block_size: usize));
|
2022-03-04 00:28:52 +11:00
|
|
|
param_ptr_forward!(pub unsafe fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String);
|
|
|
|
param_ptr_forward!(pub unsafe fn string_to_normalized_value(&self, string: &str) -> Option<f32>);
|
2022-03-23 23:02:54 +11:00
|
|
|
param_ptr_forward!(pub unsafe fn flags(&self) -> ParamFlags);
|
2022-03-04 00:28:52 +11:00
|
|
|
|
2022-05-02 02:45:35 +10:00
|
|
|
param_ptr_forward!(pub(crate) unsafe fn set_normalized_value(&self, normalized: f32));
|
|
|
|
param_ptr_forward!(pub(crate) unsafe fn update_smoother(&self, sample_rate: f32, reset: bool));
|
|
|
|
|
2022-03-04 00:28:52 +11:00
|
|
|
// These functions involve casts since the plugin formats only do floating point types, so we
|
|
|
|
// can't generate them with the macro:
|
2022-02-02 07:01:28 +11:00
|
|
|
|
2022-03-21 22:49:59 +11:00
|
|
|
/// 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
|
2022-03-19 04:10:22 +11:00
|
|
|
/// parameter's current snapped value without having to do a back and forth conversion using
|
|
|
|
/// normalized values.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn plain_value(&self) -> f32 {
|
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).plain_value(),
|
|
|
|
ParamPtr::IntParam(p) => (**p).plain_value() as f32,
|
|
|
|
ParamPtr::BoolParam(p) => (**p).normalized_value(),
|
|
|
|
ParamPtr::EnumParam(p) => (**p).plain_value() as f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 02:30:30 +10:00
|
|
|
/// Get the parameter's plain, unnormalized value, converted to a float, before any monophonic
|
|
|
|
/// host modulation has been applied. This is useful for handling modulated parameters for CLAP
|
|
|
|
/// plugins in Bitwig in a way where the actual parameter does not move in the GUI while the
|
|
|
|
/// parameter is being modulated. You can also use this to show the difference between the
|
|
|
|
/// unmodulated value and the current value. 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.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn unmodulated_plain_value(&self) -> f32 {
|
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).unmodulated_plain_value(),
|
|
|
|
ParamPtr::IntParam(p) => (**p).unmodulated_plain_value() as f32,
|
|
|
|
ParamPtr::BoolParam(p) => (**p).unmodulated_normalized_value(),
|
|
|
|
ParamPtr::EnumParam(p) => (**p).unmodulated_plain_value() as f32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:49:59 +11:00
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 07:01:28 +11:00
|
|
|
/// Get the normalized value for a plain, unnormalized value, as a float. Used as part of the
|
|
|
|
/// wrappers.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn preview_normalized(&self, plain: f32) -> f32 {
|
|
|
|
match &self {
|
2022-02-06 03:31:45 +11:00
|
|
|
ParamPtr::FloatParam(p) => (**p).preview_normalized(plain),
|
|
|
|
ParamPtr::IntParam(p) => (**p).preview_normalized(plain as i32),
|
2022-02-02 07:01:28 +11:00
|
|
|
ParamPtr::BoolParam(_) => plain,
|
2022-02-15 01:30:10 +11:00
|
|
|
ParamPtr::EnumParam(p) => (**p).preview_normalized(plain as i32),
|
2022-02-02 07:01:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the plain, unnormalized value for a normalized value, as a float. Used as part of the
|
|
|
|
/// wrappers.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn preview_plain(&self, normalized: f32) -> f32 {
|
|
|
|
match &self {
|
2022-02-06 03:31:45 +11:00
|
|
|
ParamPtr::FloatParam(p) => (**p).preview_plain(normalized),
|
|
|
|
ParamPtr::IntParam(p) => (**p).preview_plain(normalized) as f32,
|
2022-02-02 07:01:28 +11:00
|
|
|
ParamPtr::BoolParam(_) => normalized,
|
2022-02-15 01:30:10 +11:00
|
|
|
ParamPtr::EnumParam(p) => (**p).preview_plain(normalized) as f32,
|
2022-02-02 07:01:28 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> PersistentField<'a, T> for std::sync::RwLock<T>
|
|
|
|
where
|
|
|
|
T: serde::Serialize + serde::Deserialize<'a> + Send + Sync,
|
|
|
|
{
|
|
|
|
fn set(&self, new_value: T) {
|
|
|
|
*self.write().expect("Poisoned RwLock on write") = new_value;
|
|
|
|
}
|
|
|
|
fn map<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: Fn(&T) -> R,
|
|
|
|
{
|
|
|
|
f(&self.read().expect("Poisoned RwLock on read"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> PersistentField<'a, T> for parking_lot::RwLock<T>
|
|
|
|
where
|
|
|
|
T: serde::Serialize + serde::Deserialize<'a> + Send + Sync,
|
|
|
|
{
|
|
|
|
fn set(&self, new_value: T) {
|
|
|
|
*self.write() = new_value;
|
|
|
|
}
|
|
|
|
fn map<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: Fn(&T) -> R,
|
|
|
|
{
|
|
|
|
f(&self.read())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> PersistentField<'a, T> for std::sync::Mutex<T>
|
|
|
|
where
|
|
|
|
T: serde::Serialize + serde::Deserialize<'a> + Send + Sync,
|
|
|
|
{
|
|
|
|
fn set(&self, new_value: T) {
|
|
|
|
*self.lock().expect("Poisoned Mutex") = new_value;
|
|
|
|
}
|
|
|
|
fn map<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: Fn(&T) -> R,
|
|
|
|
{
|
|
|
|
f(&self.lock().expect("Poisoned Mutex"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_persistent_field_parking_lot_mutex {
|
|
|
|
($ty:ty) => {
|
|
|
|
impl<'a, T> PersistentField<'a, T> for $ty
|
|
|
|
where
|
|
|
|
T: serde::Serialize + serde::Deserialize<'a> + Send + Sync,
|
|
|
|
{
|
|
|
|
fn set(&self, new_value: T) {
|
|
|
|
*self.lock() = new_value;
|
|
|
|
}
|
|
|
|
fn map<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: Fn(&T) -> R,
|
|
|
|
{
|
|
|
|
f(&self.lock())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-04 07:20:44 +11:00
|
|
|
impl<'a, T> PersistentField<'a, T> for atomic_refcell::AtomicRefCell<T>
|
|
|
|
where
|
|
|
|
T: serde::Serialize + serde::Deserialize<'a> + Send + Sync,
|
|
|
|
{
|
|
|
|
fn set(&self, new_value: T) {
|
|
|
|
*self.borrow_mut() = new_value;
|
|
|
|
}
|
|
|
|
fn map<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: Fn(&T) -> R,
|
|
|
|
{
|
|
|
|
f(&self.borrow())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 07:01:28 +11:00
|
|
|
impl_persistent_field_parking_lot_mutex!(parking_lot::Mutex<T>);
|
|
|
|
impl_persistent_field_parking_lot_mutex!(parking_lot::FairMutex<T>);
|