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
|
|
|
|
2022-09-06 21:55:14 +02:00
|
|
|
use atomic_float::AtomicF32;
|
2022-11-26 12:58:32 +01:00
|
|
|
use std::fmt::{Debug, Display};
|
2022-09-06 21:55:14 +02:00
|
|
|
use std::sync::atomic::Ordering;
|
2022-02-14 14:19:46 +01:00
|
|
|
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.
|
2022-03-03 19:24:40 +01:00
|
|
|
pub struct FloatParam {
|
2022-07-04 18:01:55 +02:00
|
|
|
/// The field's current plain value, after monophonic modulation has been applied.
|
2022-09-06 21:55:14 +02:00
|
|
|
value: AtomicF32,
|
2022-05-01 17:34:59 +02:00
|
|
|
/// The field's current value normalized to the `[0, 1]` range.
|
2022-09-06 21:55:14 +02:00
|
|
|
normalized_value: AtomicF32,
|
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.
|
2022-09-06 21:55:14 +02:00
|
|
|
unmodulated_value: AtomicF32,
|
2022-05-01 18:30:30 +02:00
|
|
|
/// 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.
|
2022-09-06 21:55:14 +02:00
|
|
|
unmodulated_normalized_value: AtomicF32,
|
2022-05-02 15:46:57 +02:00
|
|
|
/// A value in `[-1, 1]` indicating the amount of modulation applied to
|
2022-09-29 12:28:56 +02:00
|
|
|
/// `unmodulated_normalized_`. This needs to be stored separately since the normalized values are
|
2022-05-02 15:46:57 +02:00
|
|
|
/// clamped, and this value persists after new automation events.
|
2022-09-06 21:55:14 +02:00
|
|
|
modulation_offset: AtomicF32,
|
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.
|
|
|
|
///
|
2022-09-29 12:28:56 +02:00
|
|
|
/// To use this, you'll probably want to store an `Arc<Atomic*>` alongside the parameter 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-07-05 19:39:18 +02:00
|
|
|
/// If this parameter has been marked as polyphonically modulatable, then this will be a unique
|
|
|
|
/// integer identifying the parameter. Because this value is determined by the plugin itself,
|
|
|
|
/// the plugin can easily map
|
2022-10-20 12:21:24 +02:00
|
|
|
/// [`NoteEvent::PolyModulation`][crate::prelude::NoteEvent::PolyModulation] events to the
|
2022-07-05 19:39:18 +02:00
|
|
|
/// correct parameter by pattern matching on a constant.
|
|
|
|
poly_modulation_id: Option<u32>,
|
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) {
|
2022-09-06 21:55:14 +02:00
|
|
|
(Some(func), _) => write!(f, "{}{}", func(self.value()), self.unit),
|
2022-02-14 14:19:46 +01:00
|
|
|
(None, Some(step_size)) => {
|
|
|
|
let num_digits = decimals_from_step_size(*step_size);
|
2022-09-06 21:55:14 +02:00
|
|
|
write!(f, "{:.num_digits$}{}", self.value(), self.unit)
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
2022-09-06 21:55:14 +02:00
|
|
|
_ => write!(f, "{}{}", self.value(), self.unit),
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-26 12:58:32 +01:00
|
|
|
impl Debug for FloatParam {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
// This uses the above `Display` instance to show the value
|
|
|
|
if self.modulated_plain_value() != self.unmodulated_plain_value() {
|
|
|
|
write!(f, "{}: {} (modulated)", &self.name, &self)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}: {}", &self.name, &self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 17:43:26 +01:00
|
|
|
// `Params` can not be implemented outside of NIH-plug itself because `ParamPtr` is also closed
|
|
|
|
impl super::Sealed for FloatParam {}
|
|
|
|
|
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-07-05 19:39:18 +02:00
|
|
|
fn poly_modulation_id(&self) -> Option<u32> {
|
|
|
|
self.poly_modulation_id
|
|
|
|
}
|
|
|
|
|
2022-05-01 16:32:01 +02:00
|
|
|
#[inline]
|
2022-11-07 13:03:44 +01:00
|
|
|
fn modulated_plain_value(&self) -> Self::Plain {
|
2022-09-06 21:55:14 +02:00
|
|
|
self.value.load(Ordering::Relaxed)
|
2022-03-19 16:09:31 +01:00
|
|
|
}
|
|
|
|
|
2022-05-01 17:34:59 +02:00
|
|
|
#[inline]
|
2022-11-07 13:03:44 +01:00
|
|
|
fn modulated_normalized_value(&self) -> f32 {
|
2022-09-06 21:55:14 +02:00
|
|
|
self.normalized_value.load(Ordering::Relaxed)
|
2022-05-01 17:34:59 +02:00
|
|
|
}
|
|
|
|
|
2022-05-01 18:30:30 +02:00
|
|
|
#[inline]
|
|
|
|
fn unmodulated_plain_value(&self) -> Self::Plain {
|
2022-09-06 21:55:14 +02:00
|
|
|
self.unmodulated_value.load(Ordering::Relaxed)
|
2022-05-01 18:30:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn unmodulated_normalized_value(&self) -> f32 {
|
2022-09-06 21:55:14 +02:00
|
|
|
self.unmodulated_normalized_value.load(Ordering::Relaxed)
|
2022-05-01 18:30:30 +02:00
|
|
|
}
|
|
|
|
|
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-11-08 22:03:26 +01:00
|
|
|
fn previous_step(&self, from: Self::Plain, finer: bool) -> Self::Plain {
|
|
|
|
self.range.previous_step(from, self.step_size, finer)
|
2022-03-19 16:06:20 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 22:03:26 +01:00
|
|
|
fn next_step(&self, from: Self::Plain, finer: bool) -> Self::Plain {
|
|
|
|
self.range.next_step(from, self.step_size, finer)
|
2022-03-19 16:06:20 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2023-01-06 16:07:42 +01:00
|
|
|
format!("{value:.num_digits$}")
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
2022-03-03 19:24:40 +01:00
|
|
|
(None, None, true) => format!("{}{}", value, self.unit),
|
2023-01-06 16:07:42 +01:00
|
|
|
(None, None, false) => format!("{value}"),
|
2022-03-03 19:24:40 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2023-03-03 22:55:35 +01:00
|
|
|
#[inline]
|
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
|
|
|
|
2023-03-03 22:55:35 +01:00
|
|
|
#[inline]
|
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-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 {
|
2023-01-31 16:31:29 +01:00
|
|
|
fn set_plain_value(&self, plain: Self::Plain) -> bool {
|
2022-09-06 21:55:14 +02:00
|
|
|
let unmodulated_value = plain;
|
|
|
|
let unmodulated_normalized_value = self.preview_normalized(plain);
|
|
|
|
|
|
|
|
let modulation_offset = self.modulation_offset.load(Ordering::Relaxed);
|
|
|
|
let (value, normalized_value) = if modulation_offset == 0.0 {
|
|
|
|
(unmodulated_value, unmodulated_normalized_value)
|
2022-05-02 15:46:57 +02:00
|
|
|
} else {
|
2022-09-06 21:55:14 +02:00
|
|
|
let normalized_value =
|
|
|
|
(unmodulated_normalized_value + modulation_offset).clamp(0.0, 1.0);
|
|
|
|
|
|
|
|
(self.preview_plain(normalized_value), normalized_value)
|
|
|
|
};
|
|
|
|
|
2023-01-31 16:31:29 +01:00
|
|
|
// REAPER spams automation events with the same value. This prevents callbacks from firing
|
|
|
|
// multiple times. This can be problematic when they're used to trigger expensive
|
|
|
|
// computations when a parameter changes.
|
|
|
|
let old_value = self.value.swap(value, Ordering::Relaxed);
|
|
|
|
if value != old_value {
|
|
|
|
self.normalized_value
|
|
|
|
.store(normalized_value, Ordering::Relaxed);
|
|
|
|
self.unmodulated_value
|
|
|
|
.store(unmodulated_value, Ordering::Relaxed);
|
|
|
|
self.unmodulated_normalized_value
|
|
|
|
.store(unmodulated_normalized_value, Ordering::Relaxed);
|
|
|
|
if let Some(f) = &self.value_changed {
|
|
|
|
f(value);
|
|
|
|
}
|
2022-09-06 21:55:14 +02:00
|
|
|
|
2023-01-31 16:31:29 +01:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2022-05-01 18:45:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 16:31:29 +01:00
|
|
|
fn set_normalized_value(&self, normalized: f32) -> bool {
|
2022-07-02 19:04:43 +02:00
|
|
|
// NOTE: The double conversion here is to make sure the state is reproducible. State is
|
|
|
|
// saved and restored using plain values, and the new normalized value will be
|
2022-09-29 12:33:08 +02:00
|
|
|
// different from `normalized`. This is not necessary for the modulation as these
|
2022-07-02 19:04:43 +02:00
|
|
|
// values are never shown to the host.
|
2022-09-06 21:55:14 +02:00
|
|
|
self.set_plain_value(self.preview_plain(normalized))
|
2022-05-01 18:45:35 +02:00
|
|
|
}
|
|
|
|
|
2023-01-31 16:31:29 +01:00
|
|
|
fn modulate_value(&self, modulation_offset: f32) -> bool {
|
2022-09-06 21:55:14 +02:00
|
|
|
self.modulation_offset
|
|
|
|
.store(modulation_offset, Ordering::Relaxed);
|
|
|
|
|
|
|
|
// TODO: This renormalizes this value, which is not necessary
|
2023-01-31 16:31:29 +01:00
|
|
|
self.set_plain_value(self.unmodulated_plain_value())
|
2022-05-01 18:53:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 21:55:14 +02:00
|
|
|
fn update_smoother(&self, sample_rate: f32, reset: bool) {
|
2022-05-01 18:45:35 +02:00
|
|
|
if reset {
|
2022-11-07 13:03:44 +01:00
|
|
|
self.smoothed.reset(self.modulated_plain_value());
|
2022-05-01 18:45:35 +02:00
|
|
|
} else {
|
2022-11-07 13:03:44 +01:00
|
|
|
self.smoothed
|
|
|
|
.set_target(sample_rate, self.modulated_plain_value());
|
2022-05-01 18:45:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-04-30 21:28:23 +02:00
|
|
|
range.assert_validity();
|
|
|
|
|
2022-02-14 14:19:46 +01:00
|
|
|
Self {
|
2022-09-06 21:55:14 +02:00
|
|
|
value: AtomicF32::new(default),
|
|
|
|
normalized_value: AtomicF32::new(range.normalize(default)),
|
|
|
|
unmodulated_value: AtomicF32::new(default),
|
|
|
|
unmodulated_normalized_value: AtomicF32::new(range.normalize(default)),
|
|
|
|
modulation_offset: AtomicF32::new(0.0),
|
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: "",
|
2022-07-05 19:39:18 +02:00
|
|
|
poly_modulation_id: None,
|
2022-05-01 17:36:22 +02:00
|
|
|
value_to_string: None,
|
|
|
|
string_to_value: None,
|
2022-02-14 14:19:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 21:55:14 +02:00
|
|
|
/// The field's current plain value, after monophonic modulation has been applied. Equivalent to
|
|
|
|
/// calling `param.plain_value()`.
|
|
|
|
#[inline]
|
|
|
|
pub fn value(&self) -> f32 {
|
2022-11-07 13:03:44 +01:00
|
|
|
self.modulated_plain_value()
|
2022-09-06 21:55:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 19:39:18 +02:00
|
|
|
/// Enable polyphonic modulation for this parameter. The ID is used to uniquely identify this
|
2022-10-20 12:21:24 +02:00
|
|
|
/// parameter in [`NoteEvent::PolyModulation`][crate::prelude::NoteEvent::PolyModulation]
|
2022-07-05 22:53:14 +02:00
|
|
|
/// events, and must thus be unique between _all_ polyphonically modulatable parameters. See the
|
2022-07-06 13:37:47 +02:00
|
|
|
/// event's documentation on how to use polyphonic modulation. Also consider configuring the
|
2022-07-05 22:53:14 +02:00
|
|
|
/// [`ClapPlugin::CLAP_POLY_MODULATION_CONFIG`][crate::prelude::ClapPlugin::CLAP_POLY_MODULATION_CONFIG]
|
|
|
|
/// constant when enabling this.
|
2022-07-05 19:39:18 +02:00
|
|
|
///
|
|
|
|
/// # Important
|
|
|
|
///
|
|
|
|
/// After enabling polyphonic modulation, the plugin **must** start sending
|
2022-10-20 12:21:24 +02:00
|
|
|
/// [`NoteEvent::VoiceTerminated`][crate::prelude::NoteEvent::VoiceTerminated] events to the
|
|
|
|
/// host when a voice has fully ended. This allows the host to reuse its modulation resources.
|
2022-07-05 19:39:18 +02:00
|
|
|
pub fn with_poly_modulation_id(mut self, id: u32) -> Self {
|
|
|
|
self.poly_modulation_id = Some(id);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
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
|
2022-09-29 12:33:08 +02:00
|
|
|
// end up multiplying by zero
|
2022-03-06 12:27:52 +01:00
|
|
|
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-05-13 15:08:05 +02:00
|
|
|
/// [`value_to_string`][Self::with_value_to_string()] function if that is also set. NIH-plug
|
|
|
|
/// will not automatically add a space before the unit.
|
2022-02-14 14:19:46 +01:00
|
|
|
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-05-13 15:08:05 +02:00
|
|
|
/// this is set and a [`value_to_string`][Self::with_value_to_string()] function 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
|
|
|
|
2022-09-29 12:28:56 +02:00
|
|
|
/// Mark the parameter as non-automatable. This means that the parameter cannot be changed from
|
2022-05-22 12:37:30 +02:00
|
|
|
/// an automation lane. The parameter can however still be manually changed by the user from
|
|
|
|
/// either the plugin's own GUI or from the host's generic UI.
|
2022-03-23 13:02:54 +01:00
|
|
|
pub fn non_automatable(mut self) -> Self {
|
|
|
|
self.flags.insert(ParamFlags::NON_AUTOMATABLE);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:37:30 +02:00
|
|
|
/// Hide the parameter in the host's generic UI for this plugin. This also implies
|
|
|
|
/// `NON_AUTOMATABLE`. Setting this does not prevent you from changing the parameter in the
|
|
|
|
/// plugin's editor GUI.
|
|
|
|
pub fn hide(mut self) -> Self {
|
|
|
|
self.flags.insert(ParamFlags::HIDDEN);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:02:54 +01:00
|
|
|
/// 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
|
|
|
}
|
|
|
|
|
2022-09-29 12:28:56 +02:00
|
|
|
/// Calculate how many decimals to round to when displaying a floating point value with a specific
|
2022-02-14 14:19:46 +01:00
|
|
|
/// 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 {
|
2022-11-04 21:12:06 +01:00
|
|
|
if step_size * 10.0f32.powi(decimals) >= 1.0 {
|
2022-02-14 14:19:46 +01:00
|
|
|
num_digits = decimals;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
num_digits as usize
|
|
|
|
}
|