2022-03-03 19:24:40 +01:00
|
|
|
//! Continuous (or discrete, with a step size) floating point parameters.
|
2022-02-14 14:19:46 +01:00
|
|
|
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use super::internals::ParamPtr;
|
2022-03-03 19:24:40 +01:00
|
|
|
use super::range::FloatRange;
|
2022-02-14 14:19:46 +01:00
|
|
|
use super::smoothing::{Smoother, SmoothingStyle};
|
2022-05-01 18:45:35 +02:00
|
|
|
use super::{Param, ParamFlags, ParamMut};
|
2022-02-14 14:19:46 +01:00
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
/// A floating point parameter that's stored unnormalized. The range is used for the normalization
|
2022-02-14 14:19:46 +01:00
|
|
|
/// process.
|
|
|
|
//
|
|
|
|
// XXX: To keep the API simple and to allow the optimizer to do its thing, the values are stored as
|
|
|
|
// plain primitive values that are modified through the `*mut` pointers from the plugin's
|
|
|
|
// `Params` object. Technically modifying these while the GUI is open is unsound. We could
|
|
|
|
// remedy this by changing `value` to be an atomic type and adding a function also called
|
|
|
|
// `value()` to load that value, but in practice that should not be necessary if we don't do
|
|
|
|
// anything crazy other than modifying this value. On both AArch64 and x86(_64) reads and
|
|
|
|
// writes to naturally aligned values up to word size are atomic, so there's no risk of reading
|
|
|
|
// a partially written to value here. We should probably reconsider this at some point though.
|
|
|
|
#[repr(C, align(4))]
|
2022-03-03 19:24:40 +01:00
|
|
|
pub struct FloatParam {
|
2022-05-01 17:34:59 +02:00
|
|
|
/// The field's current plain, unnormalized value.
|
2022-03-03 19:24:40 +01:00
|
|
|
pub value: f32,
|
2022-05-01 17:34:59 +02:00
|
|
|
/// The field's current value normalized to the `[0, 1]` range.
|
|
|
|
normalized_value: f32,
|
2022-05-01 18:30:30 +02:00
|
|
|
/// The field's plain, unnormalized value before any monophonic automation coming from the host
|
|
|
|
/// has been applied. This will always be the same as `value` for VST3 plugins.
|
|
|
|
unmodulated_value: f32,
|
|
|
|
/// The field's value normalized to the `[0, 1]` range before any monophonic automation coming
|
|
|
|
/// from the host has been applied. This will always be the same as `value` for VST3 plugins.
|
|
|
|
unmodulated_normalized_value: f32,
|
2022-03-21 12:49:59 +01:00
|
|
|
/// The field's default plain, unnormalized value.
|
2022-05-01 17:08:08 +02:00
|
|
|
default: f32,
|
2022-02-14 14:19:46 +01:00
|
|
|
/// An optional smoother that will automatically interpolate between the new automation values
|
|
|
|
/// set by the host.
|
2022-03-03 19:24:40 +01:00
|
|
|
pub smoothed: Smoother<f32>,
|
2022-03-21 12:49:59 +01:00
|
|
|
|
2022-03-23 13:02:54 +01:00
|
|
|
/// Flags to control the parameter's behavior. See [`ParamFlags`].
|
2022-05-01 17:08:08 +02:00
|
|
|
flags: ParamFlags,
|
2022-02-14 14:19:46 +01: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.
|
|
|
|
///
|
|
|
|
/// To use this, you'll probably want to store an `Arc<Atomic*>` alongside the parmater in the
|
2022-04-24 18:34:40 +02:00
|
|
|
/// parameters struct, move a clone of that `Arc` into this closure, and then modify that.
|
2022-02-14 14:19:46 +01:00
|
|
|
///
|
|
|
|
/// TODO: We probably also want to pass the old value to this function.
|
2022-05-01 17:08:08 +02:00
|
|
|
value_changed: Option<Arc<dyn Fn(f32) + Send + Sync>>,
|
2022-02-14 14:19:46 +01:00
|
|
|
|
|
|
|
/// The distribution of the parameter's values.
|
2022-05-01 17:08:08 +02:00
|
|
|
range: FloatRange,
|
2022-03-03 19:24:40 +01:00
|
|
|
/// The distance between discrete steps in this parameter. Mostly useful for quantizing GUI
|
2022-03-03 23:05:01 +01:00
|
|
|
/// input. If this is set and if [`value_to_string`][Self::value_to_string] is not set, then
|
|
|
|
/// this is also used when formatting the parameter. This must be a positive, nonzero number.
|
2022-05-01 17:08:08 +02:00
|
|
|
step_size: Option<f32>,
|
2022-02-14 14:19:46 +01:00
|
|
|
/// The parameter's human readable display name.
|
2022-05-01 17:08:08 +02:00
|
|
|
name: String,
|
2022-03-03 23:05:01 +01:00
|
|
|
/// The parameter value's unit, added after [`value_to_string`][Self::value_to_string] if that
|
|
|
|
/// is set. NIH-plug will not automatically add a space before the unit.
|
2022-05-01 17:08:08 +02:00
|
|
|
unit: &'static str,
|
2022-02-14 14:19:46 +01:00
|
|
|
/// Optional custom conversion function from a plain **unnormalized** value to a string.
|
2022-05-01 17:08:08 +02:00
|
|
|
value_to_string: Option<Arc<dyn Fn(f32) -> String + Send + Sync>>,
|
2022-02-14 14:19:46 +01:00
|
|
|
/// Optional custom conversion function from a string to a plain **unnormalized** 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-03-08 18:47:28 +01:00
|
|
|
///
|
|
|
|
/// The input string may or may not contain the unit, so you will need to be able to handle
|
|
|
|
/// that.
|
2022-05-01 17:08:08 +02:00
|
|
|
string_to_value: Option<Arc<dyn Fn(&str) -> Option<f32> + Send + Sync>>,
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
impl Display for FloatParam {
|
2022-02-14 14:19:46 +01:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match (&self.value_to_string, &self.step_size) {
|
|
|
|
(Some(func), _) => write!(f, "{}{}", func(self.value), self.unit),
|
|
|
|
(None, Some(step_size)) => {
|
|
|
|
let num_digits = decimals_from_step_size(*step_size);
|
|
|
|
write!(f, "{:.num_digits$}{}", self.value, self.unit)
|
|
|
|
}
|
|
|
|
_ => write!(f, "{}{}", self.value, self.unit),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
impl Param for FloatParam {
|
|
|
|
type Plain = f32;
|
2022-02-14 14:19:46 +01:00
|
|
|
|
2022-04-11 23:27:36 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
2022-03-03 19:24:40 +01:00
|
|
|
}
|
2022-03-03 13:55:54 +01:00
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn unit(&self) -> &'static str {
|
|
|
|
self.unit
|
|
|
|
}
|
2022-03-03 13:55:54 +01:00
|
|
|
|
2022-05-01 16:32:01 +02:00
|
|
|
#[inline]
|
2022-03-19 16:09:31 +01:00
|
|
|
fn plain_value(&self) -> Self::Plain {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
|
2022-05-01 17:34:59 +02:00
|
|
|
#[inline]
|
|
|
|
fn normalized_value(&self) -> Self::Plain {
|
|
|
|
self.normalized_value
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:30:30 +02:00
|
|
|
#[inline]
|
|
|
|
fn unmodulated_plain_value(&self) -> Self::Plain {
|
|
|
|
self.unmodulated_value
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn unmodulated_normalized_value(&self) -> f32 {
|
|
|
|
self.unmodulated_normalized_value
|
|
|
|
}
|
|
|
|
|
2022-05-01 16:32:01 +02:00
|
|
|
#[inline]
|
2022-03-21 12:49:59 +01:00
|
|
|
fn default_plain_value(&self) -> Self::Plain {
|
|
|
|
self.default
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn step_count(&self) -> Option<usize> {
|
|
|
|
None
|
|
|
|
}
|
2022-03-03 13:55:54 +01:00
|
|
|
|
2022-03-19 16:06:20 +01:00
|
|
|
fn previous_step(&self, from: Self::Plain) -> Self::Plain {
|
|
|
|
// This one's slightly more involved. We'll split the normalized range up into 100 segments,
|
|
|
|
// but if `self.step_size` is set then we'll use that. Ideally we might want to split the
|
|
|
|
// range up into at most 100 segments, falling back to the step size if the total number of
|
|
|
|
// steps would be smaller than that, but since ranges can be nonlienar that's a bit
|
|
|
|
// difficult to pull off.
|
|
|
|
// TODO: At some point, implement the above mentioned step size quantization
|
|
|
|
match self.step_size {
|
|
|
|
Some(step_size) => from - step_size,
|
|
|
|
None => self.preview_plain(self.preview_normalized(from) - 0.01),
|
|
|
|
}
|
|
|
|
.clamp(self.range.min(), self.range.max())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_step(&self, from: Self::Plain) -> Self::Plain {
|
|
|
|
// See above
|
|
|
|
match self.step_size {
|
|
|
|
Some(step_size) => from + step_size,
|
|
|
|
None => self.preview_plain(self.preview_normalized(from) + 0.01),
|
|
|
|
}
|
|
|
|
.clamp(self.range.min(), self.range.max())
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String {
|
|
|
|
let value = self.preview_plain(normalized);
|
|
|
|
match (&self.value_to_string, &self.step_size, include_unit) {
|
|
|
|
(Some(f), _, true) => format!("{}{}", f(value), self.unit),
|
|
|
|
(Some(f), _, false) => f(value),
|
|
|
|
(None, Some(step_size), true) => {
|
|
|
|
let num_digits = decimals_from_step_size(*step_size);
|
|
|
|
format!("{:.num_digits$}{}", value, self.unit)
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
2022-03-03 19:24:40 +01:00
|
|
|
(None, Some(step_size), false) => {
|
|
|
|
let num_digits = decimals_from_step_size(*step_size);
|
|
|
|
format!("{:.num_digits$}", value)
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
2022-03-03 19:24:40 +01:00
|
|
|
(None, None, true) => format!("{}{}", value, self.unit),
|
|
|
|
(None, None, false) => format!("{}", value),
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 14:19:46 +01:00
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
|
|
|
|
let value = match &self.string_to_value {
|
2022-03-08 18:53:15 +01:00
|
|
|
Some(f) => f(string.trim()),
|
|
|
|
// In the CLAP wrapper the unit will be included, so make sure to handle that
|
|
|
|
None => string.trim().trim_end_matches(self.unit).parse().ok(),
|
2022-03-03 19:24:40 +01:00
|
|
|
}?;
|
2022-02-14 14:19:46 +01:00
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
Some(self.preview_normalized(value))
|
|
|
|
}
|
2022-02-14 14:19:46 +01:00
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn preview_normalized(&self, plain: Self::Plain) -> f32 {
|
|
|
|
self.range.normalize(plain)
|
|
|
|
}
|
2022-03-01 16:53:18 +01:00
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn preview_plain(&self, normalized: f32) -> Self::Plain {
|
|
|
|
let value = self.range.unnormalize(normalized);
|
|
|
|
match &self.step_size {
|
|
|
|
Some(step_size) => self.range.snap_to_step(value, *step_size as Self::Plain),
|
|
|
|
None => value,
|
|
|
|
}
|
|
|
|
}
|
2022-03-01 16:53:18 +01:00
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn initialize_block_smoother(&mut self, max_block_size: usize) {
|
|
|
|
self.smoothed.initialize_block_smoother(max_block_size);
|
|
|
|
}
|
2022-02-14 14:19:46 +01:00
|
|
|
|
2022-03-23 13:02:54 +01:00
|
|
|
fn flags(&self) -> ParamFlags {
|
|
|
|
self.flags
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
fn as_ptr(&self) -> ParamPtr {
|
|
|
|
ParamPtr::FloatParam(self as *const _ as *mut _)
|
|
|
|
}
|
|
|
|
}
|
2022-02-14 14:19:46 +01:00
|
|
|
|
2022-05-01 18:45:35 +02:00
|
|
|
impl ParamMut for FloatParam {
|
|
|
|
fn set_plain_value(&mut self, plain: Self::Plain) {
|
|
|
|
self.unmodulated_value = plain;
|
|
|
|
self.unmodulated_normalized_value = self.preview_normalized(plain);
|
|
|
|
self.value = self.unmodulated_value;
|
|
|
|
self.normalized_value = self.unmodulated_normalized_value;
|
|
|
|
if let Some(f) = &self.value_changed {
|
|
|
|
f(self.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_normalized_value(&mut self, normalized: f32) {
|
|
|
|
self.unmodulated_value = self.preview_plain(normalized);
|
|
|
|
self.unmodulated_normalized_value = normalized;
|
|
|
|
self.value = self.unmodulated_value;
|
|
|
|
self.normalized_value = self.unmodulated_normalized_value;
|
|
|
|
if let Some(f) = &self.value_changed {
|
|
|
|
f(self.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:53:16 +02:00
|
|
|
fn modulate_value(&mut self, modulation_offset: f32) {
|
|
|
|
self.normalized_value =
|
|
|
|
(self.unmodulated_normalized_value + modulation_offset).clamp(0.0, 1.0);
|
|
|
|
self.value = self.preview_plain(self.normalized_value);
|
|
|
|
if let Some(f) = &self.value_changed {
|
|
|
|
f(self.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:45:35 +02:00
|
|
|
fn update_smoother(&mut self, sample_rate: f32, reset: bool) {
|
|
|
|
if reset {
|
|
|
|
self.smoothed.reset(self.value);
|
|
|
|
} else {
|
|
|
|
self.smoothed.set_target(sample_rate, self.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
impl FloatParam {
|
2022-03-03 23:05:01 +01:00
|
|
|
/// Build a new [`FloatParam`]. Use the other associated functions to modify the behavior of the
|
2022-02-14 14:19:46 +01:00
|
|
|
/// parameter.
|
2022-04-11 23:27:36 +02:00
|
|
|
pub fn new(name: impl Into<String>, default: f32, range: FloatRange) -> Self {
|
2022-02-14 14:19:46 +01:00
|
|
|
Self {
|
|
|
|
value: default,
|
2022-05-01 17:34:59 +02:00
|
|
|
normalized_value: range.normalize(default),
|
2022-05-01 18:30:30 +02:00
|
|
|
unmodulated_value: default,
|
|
|
|
unmodulated_normalized_value: range.normalize(default),
|
2022-03-21 12:49:59 +01:00
|
|
|
default,
|
2022-05-01 17:36:22 +02:00
|
|
|
smoothed: Smoother::none(),
|
|
|
|
|
|
|
|
flags: ParamFlags::default(),
|
|
|
|
value_changed: None,
|
|
|
|
|
2022-02-14 14:19:46 +01:00
|
|
|
range,
|
2022-05-01 17:36:22 +02:00
|
|
|
step_size: None,
|
2022-04-11 23:27:36 +02:00
|
|
|
name: name.into(),
|
2022-05-01 17:36:22 +02:00
|
|
|
unit: "",
|
|
|
|
value_to_string: None,
|
|
|
|
string_to_value: None,
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
/// Set up a smoother that can gradually interpolate changes made to this parameter, preventing
|
|
|
|
/// clicks and zipper noises.
|
2022-02-14 14:19:46 +01:00
|
|
|
pub fn with_smoother(mut self, style: SmoothingStyle) -> Self {
|
2022-03-06 12:27:52 +01:00
|
|
|
// Logarithmic smoothing will cause problems if the range goes through zero since then you
|
|
|
|
// end up multplying by zero
|
|
|
|
let goes_through_zero = match (&style, &self.range) {
|
|
|
|
(
|
|
|
|
SmoothingStyle::Logarithmic(_),
|
|
|
|
FloatRange::Linear { min, max }
|
|
|
|
| FloatRange::Skewed { min, max, .. }
|
|
|
|
| FloatRange::SymmetricalSkewed { min, max, .. },
|
|
|
|
) => *min == 0.0 || *max == 0.0 || min.signum() != max.signum(),
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
nih_debug_assert!(
|
|
|
|
!goes_through_zero,
|
|
|
|
"Logarithmic smoothing does not work with ranges that go through zero"
|
|
|
|
);
|
|
|
|
|
2022-02-14 14:19:46 +01:00
|
|
|
self.smoothed = Smoother::new(style);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run a callback whenever this parameter's 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, and it can be run from both the GUI and the audio
|
|
|
|
/// thread.
|
2022-03-03 19:24:40 +01:00
|
|
|
pub fn with_callback(mut self, callback: Arc<dyn Fn(f32) + Send + Sync>) -> Self {
|
2022-02-14 14:19:46 +01:00
|
|
|
self.value_changed = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Display a unit when rendering this parameter to a string. Appended after the
|
2022-03-03 23:05:01 +01:00
|
|
|
/// [`value_to_string`][Self::value_to_string] function if that is also set. NIH-plug will not
|
2022-02-14 14:19:46 +01:00
|
|
|
/// automatically add a space before the unit.
|
|
|
|
pub fn with_unit(mut self, unit: &'static str) -> Self {
|
|
|
|
self.unit = unit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-03-03 19:24:40 +01:00
|
|
|
/// Set the distance between steps of a [FloatParam]. Mostly useful for quantizing GUI input. If
|
2022-03-03 23:05:01 +01:00
|
|
|
/// this is set and if [`value_to_string`][Self::value_to_string] is not set, then this is also
|
|
|
|
/// used when formatting the parameter. This must be a positive, nonzero number.
|
2022-03-03 19:24:40 +01:00
|
|
|
pub fn with_step_size(mut self, step_size: f32) -> Self {
|
|
|
|
self.step_size = Some(step_size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-02-14 14:19:46 +01:00
|
|
|
/// Use a custom conversion function to convert the plain, unnormalized value to a
|
|
|
|
/// string.
|
|
|
|
pub fn with_value_to_string(
|
|
|
|
mut self,
|
2022-03-03 19:24:40 +01:00
|
|
|
callback: Arc<dyn Fn(f32) -> String + Send + Sync>,
|
2022-02-14 14:19:46 +01:00
|
|
|
) -> Self {
|
|
|
|
self.value_to_string = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use a custom conversion function to convert from a string to a plain, unnormalized
|
|
|
|
/// 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-03-08 18:47:28 +01:00
|
|
|
///
|
|
|
|
/// The input string may or may not contain the unit, so you will need to be able to handle
|
|
|
|
/// that.
|
2022-03-07 21:00:39 +01:00
|
|
|
pub fn with_string_to_value(
|
2022-02-14 14:19:46 +01:00
|
|
|
mut self,
|
2022-03-03 19:24:40 +01:00
|
|
|
callback: Arc<dyn Fn(&str) -> Option<f32> + Send + Sync>,
|
2022-02-14 14:19:46 +01:00
|
|
|
) -> Self {
|
|
|
|
self.string_to_value = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
2022-03-23 13:02:54 +01:00
|
|
|
|
|
|
|
/// Mark the paramter as non-automatable. This means that the parameter cannot be automated from
|
|
|
|
/// the host. Setting this flag also prevents it from showing up in the host's own generic UI
|
|
|
|
/// for this plugin. The parameter can still be changed from the plugin's editor GUI.
|
|
|
|
pub fn non_automatable(mut self) -> Self {
|
|
|
|
self.flags.insert(ParamFlags::NON_AUTOMATABLE);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Don't show this parameter when generating a generic UI for the plugin using one of
|
|
|
|
/// NIH-plug's generic UI widgets.
|
|
|
|
pub fn hide_in_generic_ui(mut self) -> Self {
|
|
|
|
self.flags.insert(ParamFlags::HIDE_IN_GENERIC_UI);
|
|
|
|
self
|
|
|
|
}
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Caldculate how many decimals to round to when displaying a floating point value with a specific
|
|
|
|
/// step size. We'll perform some rounding to ignore spurious extra precision caused by the floating
|
|
|
|
/// point quantization.
|
|
|
|
fn decimals_from_step_size(step_size: f32) -> usize {
|
|
|
|
const SCALE: f32 = 1_000_000.0; // 10.0f32.powi(f32::DIGITS as i32)
|
|
|
|
let step_size = (step_size * SCALE).round() / SCALE;
|
|
|
|
|
|
|
|
let mut num_digits = 0;
|
|
|
|
for decimals in 0..f32::DIGITS as i32 {
|
|
|
|
if step_size * 10.0f32.powi(decimals) as f32 >= 1.0 {
|
|
|
|
num_digits = decimals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
num_digits as usize
|
|
|
|
}
|