2022-01-25 12:19:53 +11:00
|
|
|
// nih-plug: plugins, but rewritten in Rust
|
2022-01-25 06:18:37 +11:00
|
|
|
// Copyright (C) 2022 Robbert van der Helm
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-01-25 12:17:30 +11:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::pin::Pin;
|
2022-01-31 03:07:50 +11:00
|
|
|
use std::sync::{Mutex, RwLock};
|
2022-01-25 06:59:46 +11:00
|
|
|
|
2022-01-25 12:17:30 +11:00
|
|
|
pub type FloatParam = PlainParam<f32>;
|
|
|
|
pub type IntParam = PlainParam<i32>;
|
2022-01-25 06:18:37 +11:00
|
|
|
|
2022-01-31 02:14:52 +11:00
|
|
|
/// Re-export for use in the [Params] proc-macro.
|
|
|
|
pub use serde_json::from_slice as deserialize_field;
|
|
|
|
/// Re-export for use in the [Params] proc-macro.
|
|
|
|
pub use serde_json::to_vec as serialize_field;
|
|
|
|
|
2022-01-31 03:07:50 +11:00
|
|
|
/// The functinoality needed for persisting a field to the plugin's state, and for restoring values
|
|
|
|
/// when loading old state.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T> PersistentField<'a, T> for 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 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"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 06:18:37 +11:00
|
|
|
/// A distribution for a parameter's range. Probably need to add some forms of skewed ranges and
|
|
|
|
/// maybe a callback based implementation at some point.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Range<T> {
|
|
|
|
Linear { min: T, max: T },
|
|
|
|
}
|
|
|
|
|
2022-01-25 06:22:31 +11:00
|
|
|
/// A normalizable range for type `T`, where `self` is expected to be a type `R<T>`. Higher kinded
|
|
|
|
/// types would have made this trait definition a lot clearer.
|
|
|
|
trait NormalizebleRange<T> {
|
2022-01-25 06:18:37 +11:00
|
|
|
/// Normalize an unnormalized value. Will be clamped to the bounds of the range if the
|
|
|
|
/// normalized value exceeds `[0, 1]`.
|
2022-01-30 00:54:48 +11:00
|
|
|
fn normalize(&self, plain: T) -> f32;
|
2022-01-25 06:18:37 +11:00
|
|
|
|
2022-01-30 00:54:48 +11:00
|
|
|
/// Unnormalize a normalized value. Will be clamped to `[0, 1]` if the plain, unnormalized value
|
|
|
|
/// would exceed that range.
|
2022-01-25 06:18:37 +11:00
|
|
|
fn unnormalize(&self, normalized: f32) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A numerical parameter that's stored unnormalized. The range is used for the normalization
|
|
|
|
/// process.
|
2022-01-25 12:17:30 +11:00
|
|
|
pub struct PlainParam<T> {
|
|
|
|
/// The field's current, normalized value. Should be initialized with the default value. 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: T,
|
2022-01-25 06:18:37 +11:00
|
|
|
|
|
|
|
/// The distribution of the parameter's values.
|
|
|
|
pub range: Range<T>,
|
|
|
|
/// The parameter's human readable display name.
|
|
|
|
pub name: &'static str,
|
|
|
|
/// The parameter value's unit, added after `value_to_string` if that is set.
|
|
|
|
pub unit: &'static str,
|
2022-01-30 00:54:48 +11:00
|
|
|
/// Optional custom conversion function from a plain **unnormalized** value to a string.
|
2022-01-30 23:15:42 +11:00
|
|
|
pub value_to_string: Option<Box<dyn Fn(T) -> String + Send + Sync>>,
|
2022-01-30 00:54:48 +11:00
|
|
|
/// Optional custom conversion function from a string to a plain **unnormalized** value. If the
|
2022-01-25 06:59:46 +11:00
|
|
|
/// string cannot be parsed, then this should return a `None`. If this happens while the
|
|
|
|
/// parameter is being updated then the update will be canceled.
|
2022-01-30 23:15:42 +11:00
|
|
|
pub string_to_value: Option<Box<dyn Fn(&str) -> Option<T> + Send + Sync>>,
|
2022-01-25 06:59:46 +11:00
|
|
|
}
|
|
|
|
|
2022-01-30 12:17:40 +11:00
|
|
|
/// A simple boolean parmaeter.
|
|
|
|
pub struct BoolParam {
|
|
|
|
/// The field's current, normalized value. Should be initialized with the default value.
|
|
|
|
pub value: bool,
|
|
|
|
|
|
|
|
/// The parameter's human readable display name.
|
|
|
|
pub name: &'static str,
|
|
|
|
/// Optional custom conversion function from a boolean value to a string.
|
2022-01-30 23:15:42 +11:00
|
|
|
pub value_to_string: Option<Box<dyn Fn(bool) -> String + Send + Sync>>,
|
2022-01-30 12:17:40 +11:00
|
|
|
/// Optional custom conversion function from a string to a boolean value. If the string cannot
|
|
|
|
/// be parsed, then this should return a `None`. If this happens while the parameter is being
|
|
|
|
/// updated then the update will be canceled.
|
2022-01-30 23:15:42 +11:00
|
|
|
pub string_to_value: Option<Box<dyn Fn(&str) -> Option<bool> + Send + Sync>>,
|
2022-01-30 12:17:40 +11:00
|
|
|
}
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
/// Describes a single parmaetre of any type.
|
|
|
|
pub trait Param {
|
2022-01-30 00:59:27 +11:00
|
|
|
/// The plain parameter type.
|
|
|
|
type Plain;
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
/// 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;
|
|
|
|
|
2022-01-30 00:59:27 +11:00
|
|
|
/// Get the unnormalized value for this parameter.
|
|
|
|
fn plain_value(&self) -> Self::Plain;
|
|
|
|
|
|
|
|
/// Set this parameter based on a plain, unnormalized value.
|
|
|
|
fn set_plain_value(&mut self, plain: Self::Plain);
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
/// Get the normalized `[0, 1]` value for this parameter.
|
|
|
|
fn normalized_value(&self) -> f32;
|
|
|
|
|
|
|
|
/// Set this parameter based on a normalized value.
|
|
|
|
fn set_normalized_value(&mut self, normalized: f32);
|
|
|
|
|
|
|
|
/// Get the string representation for a normalized value. Used as part of the wrappers. Most
|
|
|
|
/// plugin formats already have support for units, in which case it shouldn't be part of this
|
|
|
|
/// string or some DAWs may show duplicate units.
|
|
|
|
fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String;
|
|
|
|
|
|
|
|
/// Get the string representation for a normalized value. Used as part of the wrappers.
|
|
|
|
fn string_to_normalized_value(&self, string: &str) -> Option<f32>;
|
|
|
|
|
|
|
|
/// Internal implementation detail for implementing [Params]. This should not be used directly.
|
|
|
|
fn as_ptr(&self) -> ParamPtr;
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:17:30 +11:00
|
|
|
macro_rules! impl_plainparam {
|
2022-01-30 00:59:27 +11:00
|
|
|
($ty:ident, $plain:ty) => {
|
2022-01-29 23:37:14 +11:00
|
|
|
impl Param for $ty {
|
2022-01-30 00:59:27 +11:00
|
|
|
type Plain = $plain;
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
fn set_from_string(&mut self, string: &str) -> bool {
|
2022-01-25 12:17:30 +11:00
|
|
|
let value = match &self.string_to_value {
|
2022-01-25 06:59:46 +11:00
|
|
|
Some(f) => f(string),
|
|
|
|
// TODO: Check how Rust's parse function handles trailing garbage
|
|
|
|
None => string.parse().ok(),
|
|
|
|
};
|
|
|
|
|
|
|
|
match value {
|
2022-01-30 00:54:48 +11:00
|
|
|
Some(plain) => {
|
|
|
|
self.value = plain;
|
2022-01-25 06:59:46 +11:00
|
|
|
true
|
|
|
|
}
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:59:27 +11:00
|
|
|
fn plain_value(&self) -> Self::Plain {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_plain_value(&mut self, plain: Self::Plain) {
|
|
|
|
self.value = plain;
|
|
|
|
}
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
fn normalized_value(&self) -> f32 {
|
2022-01-25 12:17:30 +11:00
|
|
|
self.range.normalize(self.value)
|
2022-01-25 06:59:46 +11:00
|
|
|
}
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
fn set_normalized_value(&mut self, normalized: f32) {
|
2022-01-25 12:17:30 +11:00
|
|
|
self.value = self.range.unnormalize(normalized);
|
|
|
|
}
|
2022-01-26 05:54:27 +11:00
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String {
|
2022-01-27 10:15:11 +11:00
|
|
|
let value = self.range.unnormalize(normalized);
|
2022-01-29 00:06:51 +11:00
|
|
|
match (&self.value_to_string, include_unit) {
|
|
|
|
(Some(f), true) => format!("{}{}", f(value), self.unit),
|
|
|
|
(Some(f), false) => format!("{}", f(value)),
|
|
|
|
(None, true) => format!("{}{}", value, self.unit),
|
|
|
|
(None, false) => format!("{}", value),
|
2022-01-27 10:15:11 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
2022-01-27 10:15:11 +11:00
|
|
|
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(),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
Some(self.range.normalize(value))
|
|
|
|
}
|
|
|
|
|
2022-01-29 23:37:14 +11:00
|
|
|
fn as_ptr(&self) -> ParamPtr {
|
2022-01-26 05:54:27 +11:00
|
|
|
ParamPtr::$ty(self as *const $ty as *mut $ty)
|
|
|
|
}
|
2022-01-25 06:59:46 +11:00
|
|
|
}
|
2022-01-25 12:17:30 +11:00
|
|
|
};
|
2022-01-25 06:59:46 +11:00
|
|
|
}
|
|
|
|
|
2022-01-30 00:59:27 +11:00
|
|
|
impl_plainparam!(FloatParam, f32);
|
|
|
|
impl_plainparam!(IntParam, i32);
|
2022-01-25 12:17:30 +11:00
|
|
|
|
2022-01-30 12:17:40 +11:00
|
|
|
impl Param for BoolParam {
|
|
|
|
type Plain = bool;
|
|
|
|
|
|
|
|
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")),
|
|
|
|
};
|
|
|
|
|
|
|
|
match value {
|
|
|
|
Some(plain) => {
|
|
|
|
self.value = plain;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
None => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn plain_value(&self) -> Self::Plain {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_plain_value(&mut self, plain: Self::Plain) {
|
|
|
|
self.value = plain;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn normalized_value(&self) -> f32 {
|
|
|
|
if self.value {
|
|
|
|
1.0
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_normalized_value(&mut self, normalized: f32) {
|
|
|
|
self.value = normalized > 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn normalized_value_to_string(&self, normalized: f32, _include_unit: bool) -> String {
|
|
|
|
let value = normalized > 0.5;
|
|
|
|
match &self.value_to_string {
|
|
|
|
Some(f) => format!("{}", f(value)),
|
|
|
|
None => format!("{}", value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
|
|
|
let value = match &self.string_to_value {
|
|
|
|
Some(f) => f(string),
|
|
|
|
None => Some(string.eq_ignore_ascii_case("true")),
|
|
|
|
}?;
|
|
|
|
|
|
|
|
Some(if value { 1.0 } else { 0.0 })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_ptr(&self) -> ParamPtr {
|
|
|
|
ParamPtr::BoolParam(self as *const BoolParam as *mut BoolParam)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 12:17:30 +11:00
|
|
|
impl<T: Display + Copy> Display for PlainParam<T> {
|
2022-01-25 06:59:46 +11:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-01-25 12:17:30 +11:00
|
|
|
match &self.value_to_string {
|
|
|
|
Some(func) => write!(f, "{}{}", func(self.value), self.unit),
|
|
|
|
None => write!(f, "{}{}", self.value, self.unit),
|
2022-01-25 06:59:46 +11:00
|
|
|
}
|
|
|
|
}
|
2022-01-25 06:18:37 +11:00
|
|
|
}
|
|
|
|
|
2022-01-25 06:22:31 +11:00
|
|
|
impl NormalizebleRange<f32> for Range<f32> {
|
2022-01-30 00:54:48 +11:00
|
|
|
fn normalize(&self, plain: f32) -> f32 {
|
2022-01-25 06:18:37 +11:00
|
|
|
match &self {
|
2022-01-30 00:54:48 +11:00
|
|
|
Range::Linear { min, max } => (plain - min) / (max - min),
|
2022-01-25 06:18:37 +11:00
|
|
|
}
|
2022-01-26 08:29:40 +11:00
|
|
|
.clamp(0.0, 1.0)
|
2022-01-25 06:18:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unnormalize(&self, normalized: f32) -> f32 {
|
2022-01-26 08:29:40 +11:00
|
|
|
let normalized = normalized.clamp(0.0, 1.0);
|
2022-01-25 06:18:37 +11:00
|
|
|
match &self {
|
|
|
|
Range::Linear { min, max } => (normalized * (max - min)) + min,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 06:22:31 +11:00
|
|
|
impl NormalizebleRange<i32> for Range<i32> {
|
2022-01-30 00:54:48 +11:00
|
|
|
fn normalize(&self, plain: i32) -> f32 {
|
2022-01-25 06:18:37 +11:00
|
|
|
match &self {
|
2022-01-30 00:54:48 +11:00
|
|
|
Range::Linear { min, max } => (plain - min) as f32 / (max - min) as f32,
|
2022-01-25 06:18:37 +11:00
|
|
|
}
|
2022-01-26 08:29:40 +11:00
|
|
|
.clamp(0.0, 1.0)
|
2022-01-25 06:18:37 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unnormalize(&self, normalized: f32) -> i32 {
|
2022-01-26 08:29:40 +11:00
|
|
|
let normalized = normalized.clamp(0.0, 1.0);
|
2022-01-25 06:18:37 +11:00
|
|
|
match &self {
|
2022-01-26 08:26:38 +11:00
|
|
|
Range::Linear { min, max } => (normalized * (max - min) as f32).round() as i32 + min,
|
2022-01-25 06:18:37 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 03:07:50 +11:00
|
|
|
/// Describes a struct containing parameters and other persistent fields. The idea is that we can
|
|
|
|
/// have a normal struct containing [FloatParam] and other parameter types with attributes assigning
|
|
|
|
/// a unique identifier to each parameter. We can then build a mapping from those parameter IDs to
|
|
|
|
/// the parameters using the [Params::param_map] function. That way we can have easy to work with
|
|
|
|
/// JUCE-style parameter objects in the plugin without needing to manually register each parameter,
|
|
|
|
/// like you would in JUCE.
|
|
|
|
///
|
|
|
|
/// The other persistent parameters should be [PersistentField]s containing types that can be
|
|
|
|
/// serialized and deserialized with Serde.
|
2022-01-26 08:23:57 +11:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// This implementation is safe when using from the wrapper because the plugin object needs to be
|
|
|
|
/// pinned, and it can never outlive the wrapper.
|
|
|
|
pub trait Params {
|
2022-01-31 02:14:52 +11:00
|
|
|
/// Create a mapping from unique parameter IDs to parameters. This is done for every parameter
|
|
|
|
/// field marked with `#[id = "stable_name"]`. Dereferencing the pointers stored in the values
|
|
|
|
/// is only valid as long as this pinned object is valid.
|
2022-01-26 08:23:57 +11:00
|
|
|
fn param_map(self: Pin<&Self>) -> HashMap<&'static str, ParamPtr>;
|
2022-01-28 11:04:25 +11:00
|
|
|
|
2022-01-31 02:14:52 +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
|
|
|
|
/// recalled later. This uses [serialize_field] under the hood.
|
|
|
|
fn serialize_fields(&self) -> HashMap<String, Vec<u8>>;
|
|
|
|
|
|
|
|
/// Restore all fields marked with `#[persist = "stable_name"]` from a hashmap created by
|
2022-01-31 03:07:50 +11:00
|
|
|
/// [Self::serialize_fields]. All of thse fields should be wrapped in a [PersistentFieldq] 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.
|
|
|
|
fn deserialize_fields(&self, serialized: &HashMap<String, Vec<u8>>);
|
2022-01-26 08:23:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal pointers to parameters. This is an implementation detail used by the wrappers.
|
2022-01-28 08:31:53 +11:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2022-01-26 08:23:57 +11:00
|
|
|
pub enum ParamPtr {
|
|
|
|
FloatParam(*mut FloatParam),
|
|
|
|
IntParam(*mut IntParam),
|
2022-01-30 12:17:40 +11:00
|
|
|
BoolParam(*mut BoolParam),
|
2022-01-26 08:23:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParamPtr {
|
|
|
|
/// Get the human readable name for this parameter.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn name(&self) -> &'static str {
|
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).name,
|
|
|
|
ParamPtr::IntParam(p) => (**p).name,
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(p) => (**p).name,
|
2022-01-26 08:23:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:37:41 +11:00
|
|
|
/// Get the unit label for this parameter.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn unit(&self) -> &'static str {
|
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).unit,
|
|
|
|
ParamPtr::IntParam(p) => (**p).unit,
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(_) => "",
|
2022-01-27 09:37:41 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 08:23:57 +11:00
|
|
|
/// Set this parameter based on a string. Returns whether the updating succeeded. That can fail
|
|
|
|
/// if the string cannot be parsed.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
2022-01-29 23:32:31 +11:00
|
|
|
pub unsafe fn set_from_string(&mut self, string: &str) -> bool {
|
2022-01-26 08:23:57 +11:00
|
|
|
match &self {
|
2022-01-29 23:32:31 +11:00
|
|
|
ParamPtr::FloatParam(p) => (**p).set_from_string(string),
|
|
|
|
ParamPtr::IntParam(p) => (**p).set_from_string(string),
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(p) => (**p).set_from_string(string),
|
2022-01-26 08:23:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the normalized `[0, 1]` value for this parameter.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn normalized_value(&self) -> f32 {
|
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).normalized_value(),
|
|
|
|
ParamPtr::IntParam(p) => (**p).normalized_value(),
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(p) => (**p).normalized_value(),
|
2022-01-26 08:23:57 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set this parameter based on a normalized value.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
|
|
|
pub unsafe fn set_normalized_value(&self, normalized: f32) {
|
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).set_normalized_value(normalized),
|
|
|
|
ParamPtr::IntParam(p) => (**p).set_normalized_value(normalized),
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(p) => (**p).set_normalized_value(normalized),
|
2022-01-26 08:23:57 +11:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 10:15:11 +11:00
|
|
|
|
2022-01-30 00:54:48 +11:00
|
|
|
/// Get the normalized value for a plain, unnormalized value, as a float. Used as part of the
|
2022-01-28 05:43:19 +11:00
|
|
|
/// wrappers.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
2022-01-30 00:54:48 +11:00
|
|
|
pub unsafe fn preview_normalized(&self, plain: f32) -> f32 {
|
2022-01-28 05:43:19 +11:00
|
|
|
match &self {
|
2022-01-30 00:54:48 +11:00
|
|
|
ParamPtr::FloatParam(p) => (**p).range.normalize(plain),
|
|
|
|
ParamPtr::IntParam(p) => (**p).range.normalize(plain as i32),
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(_) => plain,
|
2022-01-28 05:43:19 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 00:54:48 +11:00
|
|
|
/// Get the plain, unnormalized value for a normalized value, as a float. Used as part of the
|
|
|
|
/// wrappers.
|
2022-01-28 05:43:19 +11:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
2022-01-30 00:54:48 +11:00
|
|
|
pub unsafe fn preview_plain(&self, normalized: f32) -> f32 {
|
2022-01-28 05:43:19 +11:00
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).range.unnormalize(normalized),
|
|
|
|
ParamPtr::IntParam(p) => (**p).range.unnormalize(normalized) as f32,
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(_) => normalized,
|
2022-01-28 05:43:19 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 00:06:51 +11:00
|
|
|
/// Get the string representation for a normalized value. Used as part of the wrappers. Most
|
|
|
|
/// plugin formats already have support for units, in which case it shouldn't be part of this
|
|
|
|
/// string or some DAWs may show duplicate units.
|
2022-01-27 10:15:11 +11:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Calling this function is only safe as long as the object this `ParamPtr` was created for is
|
|
|
|
/// still alive.
|
2022-01-29 00:06:51 +11:00
|
|
|
pub unsafe fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String {
|
2022-01-27 10:15:11 +11:00
|
|
|
match &self {
|
2022-01-29 00:06:51 +11:00
|
|
|
ParamPtr::FloatParam(p) => (**p).normalized_value_to_string(normalized, include_unit),
|
|
|
|
ParamPtr::IntParam(p) => (**p).normalized_value_to_string(normalized, include_unit),
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(p) => (**p).normalized_value_to_string(normalized, include_unit),
|
2022-01-27 10:15:11 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the string representation for a normalized value. 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 string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
|
|
|
match &self {
|
|
|
|
ParamPtr::FloatParam(p) => (**p).string_to_normalized_value(string),
|
|
|
|
ParamPtr::IntParam(p) => (**p).string_to_normalized_value(string),
|
2022-01-30 12:17:40 +11:00
|
|
|
ParamPtr::BoolParam(p) => (**p).string_to_normalized_value(string),
|
2022-01-27 10:15:11 +11:00
|
|
|
}
|
|
|
|
}
|
2022-01-26 08:23:57 +11:00
|
|
|
}
|
|
|
|
|
2022-01-25 06:18:37 +11:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn make_linear_float_range() -> Range<f32> {
|
|
|
|
Range::Linear {
|
|
|
|
min: 10.0,
|
|
|
|
max: 20.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_linear_int_range() -> Range<i32> {
|
|
|
|
Range::Linear { min: -10, max: 10 }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_normalize_linear_float() {
|
|
|
|
let range = make_linear_float_range();
|
|
|
|
assert_eq!(range.normalize(17.5), 0.75);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_normalize_linear_int() {
|
|
|
|
let range = make_linear_int_range();
|
|
|
|
assert_eq!(range.normalize(-5), 0.25);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_unnormalize_linear_float() {
|
|
|
|
let range = make_linear_float_range();
|
|
|
|
assert_eq!(range.unnormalize(0.25), 12.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_unnormalize_linear_int() {
|
|
|
|
let range = make_linear_int_range();
|
|
|
|
assert_eq!(range.unnormalize(0.75), 5);
|
|
|
|
}
|
2022-01-26 08:26:38 +11:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_unnormalize_linear_int_rounding() {
|
|
|
|
let range = make_linear_int_range();
|
|
|
|
assert_eq!(range.unnormalize(0.73), 5);
|
|
|
|
}
|
2022-01-25 06:18:37 +11:00
|
|
|
}
|