Skip to main content

nice_plug_core/params/
float.rs

1//! Continuous (or discrete, with a step size) floating point parameters.
2
3use atomic_float::AtomicF32;
4use std::fmt::{Debug, Display};
5use std::sync::Arc;
6use std::sync::atomic::Ordering;
7
8use crate::nice_debug_assert;
9
10use super::internals::ParamPtr;
11use super::range::FloatRange;
12use super::smoothing::{Smoother, SmoothingStyle};
13use super::{InternalParamMut, Param, ParamFlags, ParamInfo};
14
15/// A floating point parameter that's stored unnormalized. The range is used for the normalization
16/// process.
17pub struct FloatParam {
18    /// The field's current plain value, after monophonic modulation has been applied.
19    value: AtomicF32,
20    /// The field's current value normalized to the `[0, 1]` range.
21    normalized_value: AtomicF32,
22    /// The field's plain, unnormalized value before any monophonic automation coming from the host
23    /// has been applied. This will always be the same as `value` for VST3 plugins.
24    unmodulated_value: AtomicF32,
25    /// The field's value normalized to the `[0, 1]` range before any monophonic automation coming
26    /// from the host has been applied. This will always be the same as `value` for VST3 plugins.
27    unmodulated_normalized_value: AtomicF32,
28    /// A value in `[-1, 1]` indicating the amount of modulation applied to
29    /// `unmodulated_normalized_`. This needs to be stored separately since the normalized values are
30    /// clamped, and this value persists after new automation events.
31    modulation_offset: AtomicF32,
32    /// The field's default plain, unnormalized value.
33    default: f32,
34    /// An optional smoother that will automatically interpolate between the new automation values
35    /// set by the host.
36    pub smoothed: Smoother<f32>,
37
38    /// Optional callback for listening to value changes. The argument passed to this function is
39    /// the parameter's new **plain** value. This should not do anything expensive as it may be
40    /// called multiple times in rapid succession.
41    ///
42    /// To use this, you'll probably want to store an `Arc<Atomic*>` alongside the parameter in the
43    /// parameters struct, move a clone of that `Arc` into this closure, and then modify that.
44    ///
45    /// TODO: We probably also want to pass the old value to this function.
46    value_changed: Option<Arc<dyn Fn(f32) + Send + Sync>>,
47
48    /// The distribution of the parameter's values.
49    range: FloatRange,
50    /// The distance between discrete steps in this parameter. Mostly useful for quantizing GUI
51    /// input. If this is set and if [`value_to_string`][Self::with_value_to_string] is not set, then
52    /// this is also used when formatting the parameter. This must be a positive, nonzero number.
53    step_size: Option<f32>,
54    /// Metadata and conversion callbacks that are not used by the DSP hot path.
55    info: Box<ParamInfo<f32>>,
56    /// If this parameter has been marked as polyphonically modulatable, then this will be a unique
57    /// integer identifying the parameter. Because this value is determined by the plugin itself,
58    /// the plugin can easily map
59    /// [`NoteEvent::PolyModulation`][crate::prelude::NoteEvent::PolyModulation] events to the
60    /// correct parameter by pattern matching on a constant.
61    poly_modulation_id: Option<u32>,
62}
63
64impl Display for FloatParam {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        match (&self.info.value_to_string, &self.step_size) {
67            (Some(func), _) => write!(f, "{}{}", func(self.value()), self.info.unit),
68            (None, Some(step_size)) => {
69                let num_digits = decimals_from_step_size(*step_size);
70                write!(f, "{:.num_digits$}{}", self.value(), self.info.unit)
71            }
72            _ => write!(f, "{}{}", self.value(), self.info.unit),
73        }
74    }
75}
76
77impl Debug for FloatParam {
78    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79        // This uses the above `Display` instance to show the value
80        if self.modulated_plain_value() != self.unmodulated_plain_value() {
81            write!(f, "{}: {} (modulated)", self.info.name, self)
82        } else {
83            write!(f, "{}: {}", self.info.name, self)
84        }
85    }
86}
87
88// `Params` can not be implemented outside of nice-plug itself because `ParamPtr` is also closed
89impl super::Sealed for FloatParam {}
90
91impl Param for FloatParam {
92    type Plain = f32;
93
94    fn name(&self) -> &str {
95        &self.info.name
96    }
97
98    fn unit(&self) -> &'static str {
99        self.info.unit
100    }
101
102    fn poly_modulation_id(&self) -> Option<u32> {
103        self.poly_modulation_id
104    }
105
106    #[inline]
107    fn modulated_plain_value(&self) -> Self::Plain {
108        self.value.load(Ordering::Relaxed)
109    }
110
111    #[inline]
112    fn modulated_normalized_value(&self) -> f32 {
113        self.normalized_value.load(Ordering::Relaxed)
114    }
115
116    #[inline]
117    fn unmodulated_plain_value(&self) -> Self::Plain {
118        self.unmodulated_value.load(Ordering::Relaxed)
119    }
120
121    #[inline]
122    fn unmodulated_normalized_value(&self) -> f32 {
123        self.unmodulated_normalized_value.load(Ordering::Relaxed)
124    }
125
126    #[inline]
127    fn default_plain_value(&self) -> Self::Plain {
128        self.default
129    }
130
131    fn step_count(&self) -> Option<usize> {
132        None
133    }
134
135    fn previous_step(&self, from: Self::Plain, finer: bool) -> Self::Plain {
136        self.range.previous_step(from, self.step_size, finer)
137    }
138
139    fn next_step(&self, from: Self::Plain, finer: bool) -> Self::Plain {
140        self.range.next_step(from, self.step_size, finer)
141    }
142
143    fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String {
144        let value = self.preview_plain(normalized);
145        match (&self.info.value_to_string, &self.step_size, include_unit) {
146            (Some(f), _, true) => format!("{}{}", f(value), self.info.unit),
147            (Some(f), _, false) => f(value),
148            (None, Some(step_size), true) => {
149                let num_digits = decimals_from_step_size(*step_size);
150                format!("{:.num_digits$}{}", value, self.info.unit)
151            }
152            (None, Some(step_size), false) => {
153                let num_digits = decimals_from_step_size(*step_size);
154                format!("{value:.num_digits$}")
155            }
156            (None, None, true) => format!("{}{}", value, self.info.unit),
157            (None, None, false) => format!("{value}"),
158        }
159    }
160
161    fn string_to_normalized_value(&self, string: &str) -> Option<f32> {
162        let value = match &self.info.string_to_value {
163            Some(f) => f(string.trim()),
164            // In the CLAP wrapper the unit will be included, so make sure to handle that
165            None => string.trim().trim_end_matches(self.info.unit).parse().ok(),
166        }?;
167
168        Some(self.preview_normalized(value))
169    }
170
171    #[inline]
172    fn preview_normalized(&self, plain: Self::Plain) -> f32 {
173        self.range.normalize(plain)
174    }
175
176    #[inline]
177    fn preview_plain(&self, normalized: f32) -> Self::Plain {
178        let value = self.range.unnormalize(normalized);
179        match &self.step_size {
180            Some(step_size) => self.range.snap_to_step(value, *step_size as Self::Plain),
181            None => value,
182        }
183    }
184
185    fn flags(&self) -> ParamFlags {
186        self.info.flags
187    }
188
189    fn as_ptr(&self) -> ParamPtr {
190        ParamPtr::FloatParam(self as *const _ as *mut _)
191    }
192}
193
194impl InternalParamMut for FloatParam {
195    unsafe fn _internal_set_plain_value(&self, plain: Self::Plain) -> bool {
196        let unmodulated_value = plain;
197        let unmodulated_normalized_value = self.preview_normalized(plain);
198
199        let modulation_offset = self.modulation_offset.load(Ordering::Relaxed);
200        let (value, normalized_value) = if modulation_offset == 0.0 {
201            (unmodulated_value, unmodulated_normalized_value)
202        } else {
203            let normalized_value =
204                (unmodulated_normalized_value + modulation_offset).clamp(0.0, 1.0);
205
206            (self.preview_plain(normalized_value), normalized_value)
207        };
208
209        // REAPER spams automation events with the same value. This prevents callbacks from firing
210        // multiple times. This can be problematic when they're used to trigger expensive
211        // computations when a parameter changes.
212        let old_value = self.value.swap(value, Ordering::Relaxed);
213        if value != old_value {
214            self.normalized_value
215                .store(normalized_value, Ordering::Relaxed);
216            self.unmodulated_value
217                .store(unmodulated_value, Ordering::Relaxed);
218            self.unmodulated_normalized_value
219                .store(unmodulated_normalized_value, Ordering::Relaxed);
220            if let Some(f) = &self.value_changed {
221                f(value);
222            }
223
224            true
225        } else {
226            false
227        }
228    }
229
230    unsafe fn _internal_set_normalized_value(&self, normalized: f32) -> bool {
231        // NOTE: The double conversion here is to make sure the state is reproducible. State is
232        //       saved and restored using plain values, and the new normalized value will be
233        //       different from `normalized`. This is not necessary for the modulation as these
234        //       values are never shown to the host.
235        unsafe { self._internal_set_plain_value(self.preview_plain(normalized)) }
236    }
237
238    unsafe fn _internal_modulate_value(&self, modulation_offset: f32) -> bool {
239        self.modulation_offset
240            .store(modulation_offset, Ordering::Relaxed);
241
242        // TODO: This renormalizes this value, which is not necessary
243        unsafe { self._internal_set_plain_value(self.unmodulated_plain_value()) }
244    }
245
246    unsafe fn _internal_update_smoother(&self, sample_rate: f32, reset: bool) {
247        if reset {
248            self.smoothed.reset(self.modulated_plain_value());
249        } else {
250            self.smoothed
251                .set_target(sample_rate, self.modulated_plain_value());
252        }
253    }
254}
255
256impl FloatParam {
257    /// Build a new [`FloatParam`]. Use the other associated functions to modify the behavior of the
258    /// parameter.
259    pub fn new(name: impl Into<String>, default: f32, range: FloatRange) -> Self {
260        range.assert_validity();
261
262        Self {
263            value: AtomicF32::new(default),
264            normalized_value: AtomicF32::new(range.normalize(default)),
265            unmodulated_value: AtomicF32::new(default),
266            unmodulated_normalized_value: AtomicF32::new(range.normalize(default)),
267            modulation_offset: AtomicF32::new(0.0),
268            default,
269            smoothed: Smoother::none(),
270
271            value_changed: None,
272
273            range,
274            step_size: None,
275            info: Box::new(ParamInfo::new(name)),
276            poly_modulation_id: None,
277        }
278    }
279
280    /// The field's current plain value, after monophonic modulation has been applied. Equivalent to
281    /// calling `param.plain_value()`.
282    #[inline]
283    pub fn value(&self) -> f32 {
284        self.modulated_plain_value()
285    }
286
287    /// The range of valid plain values for this parameter.
288    #[inline]
289    pub fn range(&self) -> FloatRange {
290        self.range
291    }
292
293    /// Enable polyphonic modulation for this parameter. The ID is used to uniquely identify this
294    /// parameter in [`NoteEvent::PolyModulation`][crate::midi::NoteEvent::PolyModulation]
295    /// events, and must thus be unique between _all_ polyphonically modulatable parameters. See the
296    /// event's documentation on how to use polyphonic modulation. Also consider configuring the
297    /// `ClapPlugin::CLAP_POLY_MODULATION_CONFIG` constant when enabling this.
298    ///
299    /// # Important
300    ///
301    /// After enabling polyphonic modulation, the plugin **must** start sending
302    /// [`NoteEvent::VoiceTerminated`][crate::midi::NoteEvent::VoiceTerminated] events to the
303    /// host when a voice has fully ended. This allows the host to reuse its modulation resources.
304    pub fn with_poly_modulation_id(mut self, id: u32) -> Self {
305        self.poly_modulation_id = Some(id);
306        self
307    }
308
309    /// Set up a smoother that can gradually interpolate changes made to this parameter, preventing
310    /// clicks and zipper noises.
311    pub fn with_smoother(mut self, style: SmoothingStyle) -> Self {
312        // Logarithmic smoothing will cause problems if the range goes through zero since then you
313        // end up multiplying by zero
314        let goes_through_zero = match (&style, &self.range) {
315            (
316                SmoothingStyle::Logarithmic(_),
317                FloatRange::Linear { min, max }
318                | FloatRange::Skewed { min, max, .. }
319                | FloatRange::SymmetricalSkewed { min, max, .. },
320            ) => *min == 0.0 || *max == 0.0 || min.signum() != max.signum(),
321            _ => false,
322        };
323        nice_debug_assert!(
324            !goes_through_zero,
325            "Logarithmic smoothing does not work with ranges that go through zero"
326        );
327
328        self.smoothed = Smoother::new(style);
329        self
330    }
331
332    /// Run a callback whenever this parameter's value changes. The argument passed to this function
333    /// is the parameter's new value. This should not do anything expensive as it may be called
334    /// multiple times in rapid succession, and it can be run from both the GUI and the audio
335    /// thread.
336    pub fn with_callback(mut self, callback: Arc<dyn Fn(f32) + Send + Sync>) -> Self {
337        self.value_changed = Some(callback);
338        self
339    }
340
341    /// Display a unit when rendering this parameter to a string. Appended after the
342    /// [`value_to_string`][Self::with_value_to_string()] function if that is also set. nice-plug
343    /// will not automatically add a space before the unit.
344    pub fn with_unit(mut self, unit: &'static str) -> Self {
345        self.info.unit = unit;
346        self
347    }
348
349    /// Set the distance between steps of a [`FloatParam`]. Mostly useful for quantizing GUI input. If
350    /// this is set and a [`value_to_string`][Self::with_value_to_string()] function is not set,
351    /// then this is also used when formatting the parameter. This must be a positive, nonzero
352    /// number.
353    pub fn with_step_size(mut self, step_size: f32) -> Self {
354        self.step_size = Some(step_size);
355        self
356    }
357
358    /// Use a custom conversion function to convert the plain, unnormalized value to a
359    /// string.
360    pub fn with_value_to_string(
361        mut self,
362        callback: Arc<dyn Fn(f32) -> String + Send + Sync>,
363    ) -> Self {
364        self.info.value_to_string = Some(callback);
365        self
366    }
367
368    /// Use a custom conversion function to convert from a string to a plain, unnormalized
369    /// value. If the string cannot be parsed, then this should return a `None`. If this
370    /// happens while the parameter is being updated then the update will be canceled.
371    ///
372    /// The input string may or may not contain the unit, so you will need to be able to handle
373    /// that.
374    pub fn with_string_to_value(
375        mut self,
376        callback: Arc<dyn Fn(&str) -> Option<f32> + Send + Sync>,
377    ) -> Self {
378        self.info.string_to_value = Some(callback);
379        self
380    }
381
382    /// Mark the parameter as non-automatable. This means that the parameter cannot be changed from
383    /// an automation lane. The parameter can however still be manually changed by the user from
384    /// either the plugin's own GUI or from the host's generic UI.
385    pub fn non_automatable(mut self) -> Self {
386        self.info.flags.insert(ParamFlags::NON_AUTOMATABLE);
387        self
388    }
389
390    /// Hide the parameter in the host's generic UI for this plugin. This also implies
391    /// `NON_AUTOMATABLE`. Setting this does not prevent you from changing the parameter in the
392    /// plugin's editor GUI.
393    pub fn hide(mut self) -> Self {
394        self.info.flags.insert(ParamFlags::HIDDEN);
395        self
396    }
397
398    /// Don't show this parameter when generating a generic UI for the plugin using one of
399    /// nice-plug's generic UI widgets.
400    pub fn hide_in_generic_ui(mut self) -> Self {
401        self.info.flags.insert(ParamFlags::HIDE_IN_GENERIC_UI);
402        self
403    }
404}
405
406/// Calculate how many decimals to round to when displaying a floating point value with a specific
407/// step size. We'll perform some rounding to ignore spurious extra precision caused by the floating
408/// point quantization.
409fn decimals_from_step_size(step_size: f32) -> usize {
410    const SCALE: f32 = 1_000_000.0; // 10.0f32.powi(f32::DIGITS as i32)
411    let step_size = (step_size * SCALE).round() / SCALE;
412
413    let mut num_digits = 0;
414    for decimals in 0..f32::DIGITS as i32 {
415        if step_size * 10.0f32.powi(decimals) >= 1.0 {
416            num_digits = decimals;
417            break;
418        }
419    }
420
421    num_digits as usize
422}