1
0
Fork 0

Move the PlainParam functions to a trait

We can use this for setting parameter values, as a &Param can be turned
into a pointer which can then later be mapped to a parameter ID to
handle outputting parameter values.
This commit is contained in:
Robbert van der Helm 2022-01-29 13:37:14 +01:00
parent 5697183918
commit ef021915e5
2 changed files with 34 additions and 23 deletions

View file

@ -19,7 +19,7 @@ extern crate nih_plug;
use nih_plug::{ use nih_plug::{
formatters, formatters,
params::{FloatParam, Params, Range}, params::{FloatParam, Param, Params, Range},
plugin::{BufferConfig, BusConfig, Plugin, ProcessStatus, Vst3Plugin}, plugin::{BufferConfig, BusConfig, Plugin, ProcessStatus, Vst3Plugin},
util, util,
}; };

View file

@ -62,14 +62,36 @@ pub struct PlainParam<T> {
pub string_to_value: Option<Box<dyn Send + Sync + Fn(&str) -> Option<T>>>, pub string_to_value: Option<Box<dyn Send + Sync + Fn(&str) -> Option<T>>>,
} }
macro_rules! impl_plainparam { /// Describes a single parmaetre of any type.
($ty:ident) => { pub trait Param {
impl $ty { /// Set this parameter based on a string. Returns whether the updating succeeded. That can fail
/// Set this parameter based on a string. Returns whether the updating succeeded. That /// if the string cannot be parsed.
/// can fail if the string cannot be parsed.
/// ///
/// TODO: After implementing VST3, check if we handle parsing failures correctly /// TODO: After implementing VST3, check if we handle parsing failures correctly
pub fn set_from_string(&mut self, string: &str) -> bool { fn set_from_string(&mut self, string: &str) -> bool;
/// 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;
}
macro_rules! impl_plainparam {
($ty:ident) => {
impl Param for $ty {
fn set_from_string(&mut self, string: &str) -> bool {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string),
// TODO: Check how Rust's parse function handles trailing garbage // TODO: Check how Rust's parse function handles trailing garbage
@ -85,24 +107,15 @@ macro_rules! impl_plainparam {
} }
} }
/// Get the normalized `[0, 1]` value for this parameter. fn normalized_value(&self) -> f32 {
pub fn normalized_value(&self) -> f32 {
self.range.normalize(self.value) self.range.normalize(self.value)
} }
/// Set this parameter based on a normalized value. fn set_normalized_value(&mut self, normalized: f32) {
pub fn set_normalized_value(&mut self, normalized: f32) {
self.value = self.range.unnormalize(normalized); self.value = self.range.unnormalize(normalized);
} }
/// Get the string representation for a normalized value. Used as part of the wrappers. fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String {
/// 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.
pub fn normalized_value_to_string(
&self,
normalized: f32,
include_unit: bool,
) -> String {
let value = self.range.unnormalize(normalized); let value = self.range.unnormalize(normalized);
match (&self.value_to_string, include_unit) { match (&self.value_to_string, include_unit) {
(Some(f), true) => format!("{}{}", f(value), self.unit), (Some(f), true) => format!("{}{}", f(value), self.unit),
@ -112,8 +125,7 @@ macro_rules! impl_plainparam {
} }
} }
/// Get the string representation for a normalized value. Used as part of the wrappers. fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
pub fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
let value = match &self.string_to_value { let value = match &self.string_to_value {
Some(f) => f(string), Some(f) => f(string),
// TODO: Check how Rust's parse function handles trailing garbage // TODO: Check how Rust's parse function handles trailing garbage
@ -123,8 +135,7 @@ macro_rules! impl_plainparam {
Some(self.range.normalize(value)) Some(self.range.normalize(value))
} }
/// Implementation detail for implementing [Params]. This should not be used directly. fn as_ptr(&self) -> ParamPtr {
pub fn as_ptr(&self) -> ParamPtr {
ParamPtr::$ty(self as *const $ty as *mut $ty) ParamPtr::$ty(self as *const $ty as *mut $ty)
} }
} }