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-31 03:16:12 +11:00
|
|
|
//! TODO: Document how to use the [Param] trait. For the moment, just look at the gain example.
|
|
|
|
|
2022-01-25 12:17:30 +11:00
|
|
|
use std::fmt::Display;
|
2022-02-01 06:44:10 +11:00
|
|
|
use std::sync::Arc;
|
2022-01-25 06:59:46 +11:00
|
|
|
|
2022-02-02 07:06:13 +11:00
|
|
|
use self::range::NormalizebleRange;
|
|
|
|
|
2022-02-02 07:01:28 +11:00
|
|
|
pub mod internals;
|
2022-02-02 07:06:13 +11:00
|
|
|
pub mod range;
|
2022-02-02 07:01:28 +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-02-02 07:01:28 +11:00
|
|
|
pub use internals::Params;
|
2022-02-02 07:06:13 +11:00
|
|
|
pub use range::Range;
|
2022-01-25 06:18:37 +11:00
|
|
|
|
2022-02-02 07:02:58 +11:00
|
|
|
/// Describes a single parmaetre of any type.
|
|
|
|
pub trait Param {
|
|
|
|
/// The plain parameter type.
|
|
|
|
type Plain;
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
/// 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);
|
|
|
|
|
|
|
|
/// 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) -> internals::ParamPtr;
|
|
|
|
}
|
|
|
|
|
2022-01-25 06:18:37 +11:00
|
|
|
/// 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> {
|
2022-02-01 07:02:47 +11:00
|
|
|
/// The field's current plain, unnormalized value. Should be initialized with the default value.
|
|
|
|
/// Storing parameter values like this instead of in a single contiguous array is bad for cache
|
2022-01-25 12:17:30 +11:00
|
|
|
/// locality, but it does allow for a much nicer declarative API.
|
|
|
|
pub value: T,
|
2022-01-25 06:18:37 +11:00
|
|
|
|
2022-02-01 10:14:33 +11:00
|
|
|
// // TODO: Add optional value smoothing using an Enum. This would need to include at least
|
|
|
|
// // - `Smoothing::None`: Don't do any work, `value` is just the most recent vlaue in the
|
|
|
|
// // block
|
|
|
|
// // - `Smoothing::Smooth(f32)`: Automatically smooth to `f32` milliseconds. The host will
|
|
|
|
// // provide this as an iterator (would probably be much faster than precalculating
|
|
|
|
// // verything).
|
|
|
|
// // - `Smoothing::SampleAccurate(f32)`: Same as `Smooth`, but uses sample accurate
|
|
|
|
// // automation values if the host provides those instead of the last value.
|
|
|
|
// //
|
|
|
|
// // And this would need to integrate nicely with the sample buffer iterator adapter when
|
|
|
|
// // that gets added
|
|
|
|
// pub smoothed: Smoothing<T>,
|
2022-02-01 07:02:47 +11:00
|
|
|
/// 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.
|
2022-02-01 07:19:40 +11:00
|
|
|
///
|
|
|
|
/// To use this, you'll probably want to store an `Arc<Atomic*>` alongside the parmater in the
|
|
|
|
/// parmaeters struct, move a clone of that `Arc` into this closure, and then modify that.
|
2022-02-02 22:39:38 +11:00
|
|
|
pub value_changed: Option<Arc<dyn Fn(T) + Send + Sync>>,
|
2022-02-01 07:02:47 +11:00
|
|
|
|
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-02-01 06:44:10 +11:00
|
|
|
pub value_to_string: Option<Arc<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-02-01 06:44:10 +11:00
|
|
|
pub string_to_value: Option<Arc<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,
|
|
|
|
|
2022-02-01 07:02:47 +11:00
|
|
|
/// 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
|
|
|
|
/// multiple times in rapid succession.
|
2022-02-02 22:39:38 +11:00
|
|
|
pub value_changed: Option<Arc<dyn Fn(bool) + Send + Sync>>,
|
2022-02-01 07:02:47 +11:00
|
|
|
|
2022-01-30 12:17:40 +11:00
|
|
|
/// The parameter's human readable display name.
|
|
|
|
pub name: &'static str,
|
|
|
|
/// Optional custom conversion function from a boolean value to a string.
|
2022-02-01 06:44:10 +11:00
|
|
|
pub value_to_string: Option<Arc<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-02-01 06:44:10 +11:00
|
|
|
pub string_to_value: Option<Arc<dyn Fn(&str) -> Option<bool> + Send + Sync>>,
|
2022-01-30 12:17:40 +11:00
|
|
|
}
|
|
|
|
|
2022-02-01 07:19:40 +11:00
|
|
|
impl<T> Default for PlainParam<T>
|
|
|
|
where
|
|
|
|
T: Default,
|
|
|
|
Range<T>: Default,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
value: T::default(),
|
|
|
|
value_changed: None,
|
|
|
|
range: Range::default(),
|
|
|
|
name: "",
|
|
|
|
unit: "",
|
|
|
|
value_to_string: None,
|
|
|
|
string_to_value: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 22:39:38 +11:00
|
|
|
#[allow(clippy::derivable_impls)]
|
2022-02-01 07:19:40 +11:00
|
|
|
impl Default for BoolParam {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
value: false,
|
|
|
|
value_changed: None,
|
|
|
|
name: "",
|
|
|
|
value_to_string: None,
|
|
|
|
string_to_value: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-01 07:02:47 +11:00
|
|
|
if let Some(f) = &self.value_changed {
|
|
|
|
f(plain);
|
|
|
|
}
|
2022-01-30 00:59:27 +11:00
|
|
|
}
|
|
|
|
|
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-02-01 07:02:47 +11:00
|
|
|
self.set_plain_value(self.range.unnormalize(normalized));
|
2022-01-25 12:17:30 +11:00
|
|
|
}
|
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-02-02 07:01:28 +11:00
|
|
|
fn as_ptr(&self) -> internals::ParamPtr {
|
|
|
|
internals::ParamPtr::$ty(self as *const $ty as *mut $ty)
|
2022-01-26 05:54:27 +11:00
|
|
|
}
|
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),
|
2022-02-01 08:36:58 +11:00
|
|
|
None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")),
|
2022-01-30 12:17:40 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2022-02-01 07:02:47 +11:00
|
|
|
if let Some(f) = &self.value_changed {
|
|
|
|
f(plain);
|
|
|
|
}
|
2022-01-30 12:17:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn normalized_value(&self) -> f32 {
|
|
|
|
if self.value {
|
|
|
|
1.0
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_normalized_value(&mut self, normalized: f32) {
|
2022-02-01 07:02:47 +11:00
|
|
|
self.set_plain_value(normalized > 0.5);
|
2022-01-30 12:17:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn normalized_value_to_string(&self, normalized: f32, _include_unit: bool) -> String {
|
|
|
|
let value = normalized > 0.5;
|
2022-02-01 08:36:58 +11:00
|
|
|
match (value, &self.value_to_string) {
|
2022-02-02 22:39:38 +11:00
|
|
|
(v, Some(f)) => f(v),
|
2022-02-01 08:36:58 +11:00
|
|
|
(true, None) => String::from("On"),
|
|
|
|
(false, None) => String::from("Off"),
|
2022-01-30 12:17:40 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
|
|
|
let value = match &self.string_to_value {
|
|
|
|
Some(f) => f(string),
|
2022-02-01 08:36:58 +11:00
|
|
|
None => Some(string.eq_ignore_ascii_case("true") || string.eq_ignore_ascii_case("on")),
|
2022-01-30 12:17:40 +11:00
|
|
|
}?;
|
|
|
|
|
|
|
|
Some(if value { 1.0 } else { 0.0 })
|
|
|
|
}
|
|
|
|
|
2022-02-02 07:01:28 +11:00
|
|
|
fn as_ptr(&self) -> internals::ParamPtr {
|
|
|
|
internals::ParamPtr::BoolParam(self as *const BoolParam as *mut BoolParam)
|
2022-01-30 12:17:40 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-01 08:36:58 +11:00
|
|
|
impl Display for BoolParam {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match (self.value, &self.value_to_string) {
|
|
|
|
(v, Some(func)) => write!(f, "{}", func(v)),
|
|
|
|
(true, None) => write!(f, "On"),
|
|
|
|
(false, None) => write!(f, "Off"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|