nice_plug_core/params.rs
1//! nice-plug can handle floating point, integer, boolean, and enum parameters. Parameters are
2//! managed by creating a struct deriving the [`Params`] trait containing fields
3//! for those parameter types, and then returning a reference to that object from your
4//! [`Plugin::params()`][crate::plugin::Plugin::params()] method. See the `Params` trait for more
5//! information.
6
7use std::collections::BTreeMap;
8use std::fmt::{Debug, Display};
9use std::sync::Arc;
10
11use self::internals::ParamPtr;
12
13// The proc-macro for deriving `Params`
14pub use nice_plug_derive::Params;
15
16// Parameter types
17mod boolean;
18pub mod enums;
19mod float;
20mod integer;
21
22pub mod internals;
23pub mod persist;
24pub mod range;
25pub mod smoothing;
26
27pub use boolean::BoolParam;
28pub use enums::EnumParam;
29pub use float::FloatParam;
30pub use integer::IntParam;
31
32bitflags::bitflags! {
33 /// Flags for controlling a parameter's behavior.
34 #[repr(transparent)]
35 #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
36 pub struct ParamFlags: u32 {
37 /// When applied to a [`BoolParam`], this will cause the parameter to be linked to the
38 /// host's bypass control. Only a single parameter can be marked as a bypass parameter. If
39 /// you don't have a bypass parameter, then nice-plug will add one for you. You will need to
40 /// implement this yourself if your plugin introduces latency.
41 const BYPASS = 1 << 0;
42 /// The parameter cannot be changed from an automation lane. The parameter can however still
43 /// be manually changed by the user from either the plugin's own GUI or from the host's
44 /// generic UI.
45 const NON_AUTOMATABLE = 1 << 1;
46 /// Hides the parameter in the host's generic UI for this plugin. This also implies
47 /// `NON_AUTOMATABLE`. Setting this does not prevent you from changing the parameter in the
48 /// plugin's editor GUI.
49 const HIDDEN = 1 << 2;
50 /// Don't show this parameter when generating a generic UI for the plugin using one of
51 /// nice-plug's generic UI widgets.
52 const HIDE_IN_GENERIC_UI = 1 << 3;
53 }
54}
55
56// See https://rust-lang.github.io/api-guidelines/future-proofing.html for more information
57mod sealed {
58 /// Dummy trait to prevent [`Param`] from being implemented outside of nice-plug. This is not
59 /// possible because of the way `ParamPtr` works, so it's best to just make it flat out impossible.
60 pub trait Sealed {}
61}
62pub(crate) use sealed::Sealed;
63
64/// Describes a single parameter of any type. Most parameter implementations also have a field
65/// called `value` that and a field called `smoothed`. The former stores the latest unsmoothed
66/// value, and the latter can be used to access the smoother. These two fields should be used in DSP
67/// code to either get the parameter's current (smoothed) value. In UI code the getters from this
68/// trait should be used instead.
69///
70/// # Sealed
71///
72/// This trait cannot be implemented outside of nice-plug itself. If you want to create new
73/// abstractions around parameters, consider wrapping them in a struct instead. Then use the
74/// `#[nested(id_prefix = "foo")]` syntax from the [`Params`] trait to reuse that wrapper in
75/// multiple places.
76pub trait Param: Display + Debug + sealed::Sealed {
77 /// The plain parameter type.
78 type Plain: PartialEq;
79
80 /// Get the human readable name for this parameter.
81 fn name(&self) -> &str;
82
83 /// Get the unit label for this parameter, if any.
84 fn unit(&self) -> &'static str;
85
86 /// Get this parameter's polyphonic modulation ID. If this is set for a parameter in a CLAP
87 /// plugin, then polyphonic modulation will be enabled for that parameter. Polyphonic modulation
88 /// is communicated to the plugin through
89 /// [`NoteEvent::PolyModulation`][crate::midi::NoteEvent::PolyModulation] and
90 /// [`NoteEvent::MonoAutomation`][crate::midi::NoteEvent::MonoAutomation] events. See the
91 /// documentation on those events for more information.
92 ///
93 /// # Important
94 ///
95 /// After enabling polyphonic modulation, the plugin **must** start sending
96 /// [`NoteEvent::VoiceTerminated`][crate::midi::NoteEvent::VoiceTerminated] events to the
97 /// host when a voice has fully ended. This allows the host to reuse its modulation resources.
98 fn poly_modulation_id(&self) -> Option<u32>;
99
100 /// Get the unnormalized value for this parameter.
101 fn modulated_plain_value(&self) -> Self::Plain;
102
103 /// Get the normalized `[0, 1]` value for this parameter.
104 fn modulated_normalized_value(&self) -> f32;
105
106 /// Get the unnormalized value for this parameter before any (monophonic) modulation coming from
107 /// the host has been applied. If the host is not currently modulating this parameter than this
108 /// will be the same as [`modulated_plain_value()`][Self::modulated_plain_value()]. This may be
109 /// useful for displaying modulation differently in plugin GUIs. Right now only CLAP plugins in
110 /// Bitwig Studio use modulation.
111 fn unmodulated_plain_value(&self) -> Self::Plain;
112
113 /// Get the normalized `[0, 1]` value for this parameter before any (monophonic) modulation
114 /// coming from the host has been applied. If the host is not currently modulating this
115 /// parameter than this will be the same as
116 /// [`modulated_normalized_value()`][Self::modulated_normalized_value()]. This may be useful for
117 /// displaying modulation differently in plugin GUIs. Right now only CLAP plugins in Bitwig
118 /// Studio use modulation.
119 fn unmodulated_normalized_value(&self) -> f32;
120
121 /// Get the unnormalized default value for this parameter.
122 fn default_plain_value(&self) -> Self::Plain;
123
124 /// Get the normalized `[0, 1]` default value for this parameter.
125 #[inline]
126 fn default_normalized_value(&self) -> f32 {
127 self.preview_normalized(self.default_plain_value())
128 }
129
130 /// Get the number of steps for this parameter, if it is discrete. Used for the host's generic
131 /// UI.
132 fn step_count(&self) -> Option<usize>;
133
134 /// Returns the previous step from a specific value for this parameter. This can be the same as
135 /// `from` if the value is at the start of its range. This is mainly used for scroll wheel
136 /// interaction in plugin GUIs. When the parameter is not discrete then a step should cover one
137 /// hundredth of the normalized range instead.
138 ///
139 /// If `finer` is true, then the step size should be decreased if the parameter is continuous.
140 fn previous_step(&self, from: Self::Plain, finer: bool) -> Self::Plain;
141
142 /// Returns the next step from a specific value for this parameter. This can be the same as
143 /// `from` if the value is at the end of its range. This is mainly used for scroll wheel
144 /// interaction in plugin GUIs. When the parameter is not discrete then a step should cover one
145 /// hundredth of the normalized range instead.
146 ///
147 /// If `finer` is true, then the step size should be decreased if the parameter is continuous.
148 fn next_step(&self, from: Self::Plain, finer: bool) -> Self::Plain;
149
150 /// The same as [`previous_step()`][Self::previous_step()], but for normalized values. This is
151 /// mostly useful for GUI widgets.
152 fn previous_normalized_step(&self, from: f32, finer: bool) -> f32 {
153 self.preview_normalized(self.previous_step(self.preview_plain(from), finer))
154 }
155
156 /// The same as [`next_step()`][Self::next_step()], but for normalized values. This is mostly
157 /// useful for GUI widgets.
158 fn next_normalized_step(&self, from: f32, finer: bool) -> f32 {
159 self.preview_normalized(self.next_step(self.preview_plain(from), finer))
160 }
161
162 /// Get the string representation for a normalized value. Used as part of the wrappers. Most
163 /// plugin formats already have support for units, in which case it shouldn't be part of this
164 /// string or some DAWs may show duplicate units.
165 fn normalized_value_to_string(&self, normalized: f32, include_unit: bool) -> String;
166
167 /// Get the string representation for a normalized value. Used as part of the wrappers.
168 fn string_to_normalized_value(&self, string: &str) -> Option<f32>;
169
170 /// Get the normalized value for a plain, unnormalized value, as a float. Used as part of the
171 /// wrappers.
172 fn preview_normalized(&self, plain: Self::Plain) -> f32;
173
174 /// Get the plain, unnormalized value for a normalized value, as a float. Used as part of the
175 /// wrappers. This **does** snap to step sizes for continuous parameters (i.e. [`FloatParam`]).
176 fn preview_plain(&self, normalized: f32) -> Self::Plain;
177
178 /// Get the plain, unnormalized value for this parameter after polyphonic modulation has been
179 /// applied. This is a convenience method for calling [`preview_plain()`][Self::preview_plain()]
180 /// with `unmodulated_normalized_value() + normalized_offset`.
181 #[inline]
182 fn preview_modulated(&self, normalized_offset: f32) -> Self::Plain {
183 self.preview_plain(self.unmodulated_normalized_value() + normalized_offset)
184 }
185
186 /// Flags to control the parameter's behavior. See [`ParamFlags`].
187 fn flags(&self) -> ParamFlags;
188
189 /// Internal implementation detail for implementing [`Params`]. This should
190 /// not be used directly.
191 fn as_ptr(&self) -> internals::ParamPtr;
192}
193
194/// Contains the setters for parameters. Only to be used by nice-plug's internal libraries.
195/// These are exposed as unsafe methods to avoid confusion.
196pub trait InternalParamMut: Param {
197 /// Set this parameter based on a plain, unnormalized value. This does not snap to step sizes
198 /// for continuous parameters (i.e. [`FloatParam`]). If
199 /// [`modulate_value()`][Self::_internal_modulate_value()] has previously been called with a non
200 /// zero value then this offset is taken into account to form the effective value.
201 ///
202 /// Returns whether or not the value has changed. Any parameter callbacks are only run the value
203 /// has actually changed.
204 ///
205 /// This does **not** update the smoother.
206 ///
207 /// # Safety
208 /// This is only allowed to be used by nice-plug's internal libraries.
209 unsafe fn _internal_set_plain_value(&self, plain: Self::Plain) -> bool;
210
211 /// Set this parameter based on a normalized value. The normalized value will be snapped to the
212 /// step size for continuous parameters (i.e. [`FloatParam`]). If
213 /// [`modulate_value()`][Self::_internal_modulate_value()] has previously been called with a non
214 /// zero value then this offset is taken into account to form the effective value.
215 ///
216 /// Returns whether or not the value has changed. Any parameter callbacks are only run the value
217 /// has actually changed.
218 ///
219 /// This does **not** update the smoother.
220 ///
221 /// # Safety
222 /// This is only allowed to be used by nice-plug's internal libraries.
223 unsafe fn _internal_set_normalized_value(&self, normalized: f32) -> bool;
224
225 /// Add a modulation offset to the value's unmodulated value. This value sticks until this
226 /// function is called again with a 0.0 value. Out of bound values will be clamped to the
227 /// parameter's range. The normalized value will be snapped to the step size for continuous
228 /// parameters (i.e. [`FloatParam`]).
229 ///
230 /// Returns whether or not the value has changed. Any parameter callbacks are only run the
231 /// value has actually changed.
232 ///
233 /// This does **not** update the smoother.
234 ///
235 /// # Safety
236 /// This is only allowed to be used by nice-plug's internal libraries.
237 unsafe fn _internal_modulate_value(&self, modulation_offset: f32) -> bool;
238
239 /// Update the smoother state to point to the current value. Also used when initializing and
240 /// restoring a plugin so everything is in sync. In that case the smoother should completely
241 /// reset to the current value.
242 ///
243 /// # Safety
244 /// This is only allowed to be used by nice-plug's internal libraries.
245 unsafe fn _internal_update_smoother(&self, sample_rate: f32, reset: bool);
246}
247
248/// Describes a struct containing parameters and other persistent fields.
249///
250/// # Deriving `Params` and `#[id = "stable"]`
251///
252/// This trait can be derived on a struct containing [`FloatParam`] and other parameter fields by
253/// adding `#[derive(Params)]`. When deriving this trait, any of those parameter fields should have
254/// the `#[id = "stable"]` attribute, where `stable` is an up to 6 character long string (to avoid
255/// collisions) that will be used to identify the parameter internally so you can safely move it
256/// around and rename the field without breaking compatibility with old presets.
257///
258/// ## `#[persist = "key"]`
259///
260/// The struct can also contain other fields that should be persisted along with the rest of the
261/// preset data. These fields should be [`PersistentField`][persist::PersistentField]s annotated
262/// with the `#[persist = "key"]` attribute containing types that can be serialized and deserialized
263/// with [Serde](https://serde.rs/).
264///
265/// ## `#[nested]`, `#[nested(group_name = "group name")]`
266///
267/// Finally, the `Params` object may include parameters from other objects. Setting a group name is
268/// optional, but some hosts can use this information to display the parameters in a tree structure.
269/// Parameter IDs and persisting keys still need to be **unique** when using nested parameter
270/// structs.
271///
272/// Take a look at the example gain example plugin to see how this is used.
273///
274/// ## `#[nested(id_prefix = "foo", group_name = "Foo")]`
275///
276/// Adding this attribute to a `Params` sub-object works similarly to the regular `#[nested]`
277/// attribute, but it also adds an ID to all parameters from the nested object. If a parameter in
278/// the nested nested object normally has parameter ID `bar`, the parameter's ID will now be renamed
279/// to `foo_bar`. The same thing happens with persistent field keys to support multiple copies of
280/// the field. _This makes it possible to reuse the same parameter struct with different names and
281/// parameter indices._
282///
283/// ## `#[nested(array, group_name = "Foo")]`
284///
285/// This can be applied to an array-like data structure and it works similar to a `nested` attribute
286/// with an `id_name`, except that it will iterate over the array and create unique indices for all
287/// nested parameters. If the nested parameters object has a parameter called `bar`, then that
288/// parameter will belong to the group `Foo {array_index + 1}`, and it will have the renamed
289/// parameter ID `bar_{array_index + 1}`. The same thing applies to persistent field keys.
290///
291/// # Safety
292///
293/// This implementation is safe when using from the wrapper because the plugin's returned `Params`
294/// object lives in an `Arc`, and the wrapper also holds a reference to this `Arc`.
295pub unsafe trait Params: 'static + Send + Sync {
296 /// Create a mapping from unique parameter IDs to parameter pointers along with the name of the
297 /// group/unit/module they are in, as a `(param_id, param_ptr, group)` triple. The order of the
298 /// `Vec` determines the display order in the (host's) generic UI. The group name is either an
299 /// empty string for top level parameters, or a slash/delimited `"group name 1/Group Name 2"` if
300 /// this `Params` object contains nested child objects. All components of a group path must
301 /// exist or you may encounter panics. The derive macro does this for every parameter field
302 /// marked with `#[id = "stable"]`, and it also inlines all fields from nested child `Params`
303 /// structs marked with `#[nested(...)]` while prefixing that group name before the parameter's
304 /// original group name. Dereferencing the pointers stored in the values is only valid as long
305 /// as this object is valid.
306 ///
307 /// # Note
308 ///
309 /// This uses `String` even though for the `Params` derive macro `&'static str` would have been
310 /// fine to be able to support custom reusable Params implementations.
311 fn param_map(&self) -> Vec<(String, ParamPtr, String)>;
312
313 /// Serialize all fields marked with `#[persist = "stable_name"]` into a hash map containing
314 /// JSON-representations of those fields so they can be written to the plugin's state and
315 /// recalled later. This uses [`persist::serialize_field()`] under the hood.
316 fn serialize_fields(&self) -> BTreeMap<String, String> {
317 BTreeMap::new()
318 }
319
320 /// Restore all fields marked with `#[persist = "stable_name"]` from a hashmap created by
321 /// [`serialize_fields()`][Self::serialize_fields()]. All of these fields should be wrapped in a
322 /// [`persist::PersistentField`] with thread safe interior mutability, like an `RwLock` or a
323 /// `Mutex`. This gets called when the plugin's state is being restored. This uses
324 /// [`persist::deserialize_field()`] under the hood.
325 #[allow(unused_variables)]
326 fn deserialize_fields(&self, serialized: &BTreeMap<String, String>) {}
327}
328
329/// This may be useful when building generic UIs using nested `Params` objects.
330unsafe impl<P: Params> Params for Arc<P> {
331 fn param_map(&self) -> Vec<(String, ParamPtr, String)> {
332 self.as_ref().param_map()
333 }
334
335 fn serialize_fields(&self) -> BTreeMap<String, String> {
336 self.as_ref().serialize_fields()
337 }
338
339 fn deserialize_fields(&self, serialized: &BTreeMap<String, String>) {
340 self.as_ref().deserialize_fields(serialized)
341 }
342}