Skip to main content

truce_params/
lib.rs

1#![forbid(unsafe_code)]
2
3mod info;
4mod range;
5pub mod sample;
6mod smooth;
7mod types;
8
9pub use info::{MidiSource, ParamFlags, ParamInfo, ParamUnit, ParamValueKind, map_source_to_param};
10pub use range::ParamRange;
11pub use sample::{Float, Sample};
12pub use smooth::{Smoother, SmoothingStyle};
13pub use types::{
14    BoolParam, EnumParam, FloatParam, FloatParamReadF32, FloatParamReadF64, IntParam, MeterSlot,
15    ParamEnum,
16};
17
18/// Implementation detail - not part of the stable public API.
19/// Used by `truce-loader` to index into meter storage.
20#[doc(hidden)]
21pub const METER_ID_BASE: u32 = 1 << 24;
22
23/// Sealing module: external crates cannot implement [`Params`] or
24/// [`ParamEnum`] directly because they can't name `Sealed`. The
25/// `#[derive(Params)]` and `#[derive(ParamEnum)]` macros emit the
26/// `Sealed` impl alongside their trait impls, so derive users are
27/// unaffected.
28#[doc(hidden)]
29pub mod __private {
30    pub trait Sealed {}
31}
32
33/// Format a plain parameter value as a display string based on the parameter's unit.
34///
35/// Used by the `#[derive(Params)]` macro for default `format_value` implementations
36/// on `FloatParam` and `IntParam` fields. `IntParam` is identified by
37/// `ParamValueKind::Int`, set by the derive from the field type - its
38/// value is always integer-valued, so the fractional `{:.1}` / `{:.2}`
39/// formats float-typed params use would render "0.0 st" / "0.00"
40/// instead of "0 st" / "0".
41#[must_use]
42pub fn format_param_value(info: &ParamInfo, value: f64) -> String {
43    let is_int = info.kind == ParamValueKind::Int;
44    // Round to nearest integer before display so a smoothed IntParam
45    // that's mid-transition doesn't briefly render the rounded-down
46    // half-step (e.g. an `i32::from(value)` of -1 when value is -0.5
47    // mid-snap). `IntParam::value_i32` rounds the same way at the
48    // audio-thread read site.
49    #[allow(clippy::cast_possible_truncation)]
50    let int_value = value.round() as i64;
51    match info.unit {
52        ParamUnit::Db => {
53            if is_int {
54                format!("{int_value} dB")
55            } else {
56                format!("{value:.1} dB")
57            }
58        }
59        ParamUnit::Hz => {
60            if value >= 1000.0 {
61                format!("{:.1} kHz", value / 1000.0)
62            } else {
63                format!("{value:.0} Hz")
64            }
65        }
66        ParamUnit::Milliseconds => {
67            if is_int {
68                format!("{int_value} ms")
69            } else {
70                format!("{value:.1} ms")
71            }
72        }
73        ParamUnit::Seconds => {
74            if value >= 1.0 {
75                format!("{value:.2} s")
76            } else {
77                format!("{:.0} ms", value * 1000.0)
78            }
79        }
80        ParamUnit::Percent => format!("{:.0}%", value * 100.0),
81        ParamUnit::Semitones => {
82            if is_int {
83                format!("{int_value} st")
84            } else {
85                format!("{value:.1} st")
86            }
87        }
88        ParamUnit::Degrees => {
89            if is_int {
90                format!("{int_value}°")
91            } else {
92                format!("{value:.1}°")
93            }
94        }
95        ParamUnit::Pan => {
96            // Convention: pan params are normalized to [-1.0, 1.0]. Round
97            // to nearest integer percent first so the dead-zone test and
98            // L/R label agree (e.g. -0.004 → 0% → "C", -0.006 → -1% → "1L").
99            // Result is bounded by `[-100, 100]` after clamp to `[-1, 1]`.
100            #[allow(clippy::cast_possible_truncation)]
101            let pct = (value * 100.0).round() as i32;
102            match pct.cmp(&0) {
103                std::cmp::Ordering::Equal => "C".to_string(),
104                std::cmp::Ordering::Less => format!("{}L", -pct),
105                std::cmp::Ordering::Greater => format!("{pct}R"),
106            }
107        }
108        ParamUnit::None => {
109            if is_int {
110                format!("{int_value}")
111            } else {
112                format!("{value:.2}")
113            }
114        }
115    }
116}
117
118/// Trait implemented by #[derive(Params)] on a struct.
119/// Format wrappers use this to enumerate, read, and write parameters.
120///
121/// Stays dyn-compatible (every method dispatches through `&self`) so
122/// editors can pass `Arc<dyn Params>` into the screenshot pipeline
123/// without naming the concrete type. Generic code that needs to
124/// *construct* a fresh `Params` value should add a `Default` bound
125/// rather than expecting one on the trait - `#[derive(Params)]` emits
126/// `impl Default` alongside the trait impl, so that bound is free for
127/// derive users.
128pub trait Params: __private::Sealed + Send + Sync + 'static {
129    /// All parameter infos, in declaration order.
130    fn param_infos(&self) -> Vec<ParamInfo>;
131
132    /// Append parameter infos onto an existing buffer. Default impl
133    /// delegates to [`Self::param_infos`] and `extend`s; the derive
134    /// macro overrides for nested structs so deep trees don't pay
135    /// O(depth) intermediate `Vec` allocations per outer call.
136    fn append_param_infos(&self, into: &mut Vec<ParamInfo>) {
137        into.extend(self.param_infos());
138    }
139
140    /// Static parameter metadata, available without an instance.
141    ///
142    /// Format wrappers' `register_*` paths call this to learn the
143    /// parameter set without constructing a full plugin. The
144    /// instance-based alternative would pay for any allocation the
145    /// constructor does (DSP buffers, FFT plans, image atlases, etc.)
146    /// at static-init time, which is fragile under AAX's `Describe`
147    /// running before main. The derive macro overrides this with a
148    /// `LazyLock`-cached `Vec<ParamInfo>` built from the same
149    /// compile-time metadata it uses for [`Self::param_infos`], so
150    /// registration becomes allocation-free after the first call.
151    ///
152    /// Default impl returns an empty vec - hand-written `Params` impls
153    /// that don't override fall through to the runtime path inside
154    /// `PluginExport::param_infos_static`. Gated by `Self: Sized` so
155    /// adding the method preserves dyn-compatibility for the existing
156    /// `&self`-method shape (`&dyn Params` skips this slot).
157    #[must_use]
158    fn param_infos_static() -> Vec<ParamInfo>
159    where
160        Self: Sized,
161    {
162        Vec::new()
163    }
164
165    /// Number of parameters.
166    fn count(&self) -> usize;
167
168    /// IDs of every `#[meter]` slot declared on the params struct
169    /// (including nested subtrees), in declaration order. Default impl
170    /// returns empty - only structs that declare meters need to
171    /// override. The derive macro implements it automatically.
172    ///
173    /// Format wrappers that expose DSP-side meters back to the UI
174    /// (LV2's output control ports, for instance) use this to know
175    /// which IDs to poll each `process()`.
176    fn meter_ids(&self) -> Vec<u32> {
177        Vec::new()
178    }
179
180    /// Get normalized value (0.0–1.0) by ID.
181    fn get_normalized(&self, id: u32) -> Option<f64>;
182
183    /// Set normalized value (0.0–1.0) by ID.
184    ///
185    /// Takes `&self`, not `&mut self` - the per-param storage in
186    /// `FloatParam` / `BoolParam` / `IntParam` / `EnumParam` is built
187    /// on `AtomicU32` / `AtomicU64`, so writes go through interior
188    /// mutability. Format wrappers, GUI editors, and the audio thread
189    /// all hold `&Params` (or `Arc<Params>`) concurrently and write
190    /// without coordination - every implementation must be sound under
191    /// concurrent `&self` writes from multiple threads.
192    fn set_normalized(&self, id: u32, value: f64);
193
194    /// Set normalized value and read back the resulting plain value in
195    /// one call. CLAP / AU forward the plain value to the host's
196    /// automation channel after a GUI write. The default impl is the
197    /// obvious `set_normalized` then `get_plain`; concrete `Params`
198    /// implementations that can compute both in one trait dispatch
199    /// (e.g. the `#[derive(Params)]` output) should override for a
200    /// single match-arm walk.
201    fn set_normalized_returning_plain(&self, id: u32, value: f64) -> f64 {
202        self.set_normalized(id, value);
203        self.get_plain(id).unwrap_or(0.0)
204    }
205
206    /// Set normalized value and read back the (post-clamp / post-step)
207    /// normalized value in one call. VST3 / VST2 / AAX forward
208    /// normalized values to the host's automation channel. Same
209    /// override-for-single-dispatch contract as
210    /// [`Self::set_normalized_returning_plain`].
211    fn set_normalized_returning_normalized(&self, id: u32, value: f64) -> f64 {
212        self.set_normalized(id, value);
213        self.get_normalized(id).unwrap_or(0.0)
214    }
215
216    /// Get plain value by ID.
217    fn get_plain(&self, id: u32) -> Option<f64>;
218
219    /// Set plain value by ID.
220    ///
221    /// Same `&self` interior-mutability contract as
222    /// [`Self::set_normalized`].
223    fn set_plain(&self, id: u32, value: f64);
224
225    /// Format a plain value to display string.
226    fn format_value(&self, id: u32, value: f64) -> Option<String>;
227
228    /// Parse a display string to plain value.
229    fn parse_value(&self, id: u32, text: &str) -> Option<f64>;
230
231    /// Reset all smoothers to current values.
232    fn snap_smoothers(&self);
233
234    /// Update smoother sample rates.
235    fn set_sample_rate(&self, sample_rate: f64);
236
237    /// Collect all parameter IDs and their current plain values.
238    fn collect_values(&self) -> (Vec<u32>, Vec<f64>);
239
240    /// Restore parameter values from a list of (id, value) pairs.
241    fn restore_values(&self, values: &[(u32, f64)]);
242
243    /// Serialize this store's `#[persist]` fields into a keyed blob the
244    /// host saves alongside the parameter values. Default: empty (no
245    /// persist fields). The `#[derive(Params)]` macro overrides it when
246    /// any field carries `#[persist]`.
247    #[must_use]
248    fn serialize_persist(&self) -> Vec<u8> {
249        Vec::new()
250    }
251
252    /// Restore this store's `#[persist]` fields from a blob produced by
253    /// [`Self::serialize_persist`]. Unknown / missing keys are skipped,
254    /// leaving those fields at their current value. Default: no-op.
255    fn load_persist(&self, data: &[u8]) {
256        let _ = data;
257    }
258
259    /// Walk every parameter and meter ID reachable from `self`
260    /// (including nested `#[nested]` substructs) and panic on the
261    /// first duplicate.
262    ///
263    /// Why this isn't just a compile-time check: the
264    /// `#[derive(Params)]` collision check at expansion time only
265    /// sees IDs declared in the *current* struct. A parent param
266    /// `id = 5` and a nested-substruct param `id = 5` both compile,
267    /// because the parent derive doesn't see into the nested type.
268    /// At runtime, the `set_plain` / `get_plain` dispatcher matches
269    /// at the outer level first and silently never reaches the
270    /// nested one - preset round-trips would corrupt the nested
271    /// value. This method makes that bug surface as a panic at
272    /// plugin construction instead of as quiet state loss.
273    ///
274    /// Called automatically by the derive-generated `Self::new()`.
275    /// Plugin code shouldn't need to invoke it directly.
276    fn assert_no_id_collisions(&self) {
277        let mut all = self.param_infos();
278        // Borrow the names from the existing infos so the panic
279        // message can identify *which* IDs collided.
280        let mut seen: Vec<(u32, &'static str)> = Vec::with_capacity(all.len());
281        for info in all.drain(..) {
282            for (prev_id, prev_name) in &seen {
283                assert!(
284                    *prev_id != info.id,
285                    "duplicate parameter ID {}: '{}' and '{}' (likely a \
286                     parent / nested-struct collision; the per-struct \
287                     compile-time check can't see across nested types)",
288                    info.id,
289                    prev_name,
290                    info.name,
291                );
292            }
293            seen.push((info.id, info.name));
294        }
295        let mut seen_meters: Vec<u32> = Vec::new();
296        for meter_id in self.meter_ids() {
297            for (prev_id, prev_name) in &seen {
298                assert!(
299                    *prev_id != meter_id,
300                    "meter ID {meter_id} collides with parameter ID for '{prev_name}'",
301                );
302            }
303            // Meter IDs auto-assign per struct from a shared base, so two
304            // `#[nested]` structs that each declare a meter hand back the
305            // same ID and would alias in meter storage. The per-struct
306            // compile-time check can't see across nested types; surface it
307            // as a construction panic instead of silent aliasing.
308            assert!(
309                !seen_meters.contains(&meter_id),
310                "duplicate meter ID {meter_id} (two #[nested] structs each \
311                 declare a meter; nested meters aren't supported - keep \
312                 meters in a single Params struct)",
313            );
314            seen_meters.push(meter_id);
315        }
316    }
317}
318
319#[cfg(test)]
320mod tests {
321    use super::*;
322    use crate::range::ParamRange;
323
324    fn pan_info() -> ParamInfo {
325        ParamInfo {
326            id: 0,
327            name: "Pan",
328            short_name: "Pan",
329            group: "",
330            range: ParamRange::Linear {
331                min: -1.0,
332                max: 1.0,
333            },
334            default_plain: 0.0,
335            flags: ParamFlags::empty(),
336            unit: ParamUnit::Pan,
337            kind: ParamValueKind::Float,
338            midi_map: None,
339            midi_channel: None,
340        }
341    }
342
343    #[test]
344    fn pan_centre() {
345        let info = pan_info();
346        assert_eq!(format_param_value(&info, 0.0), "C");
347        assert_eq!(format_param_value(&info, 0.004), "C");
348        assert_eq!(format_param_value(&info, -0.004), "C");
349    }
350
351    #[test]
352    fn pan_left() {
353        let info = pan_info();
354        assert_eq!(format_param_value(&info, -0.5), "50L");
355        assert_eq!(format_param_value(&info, -1.0), "100L");
356        assert_eq!(format_param_value(&info, -0.006), "1L");
357    }
358
359    #[test]
360    fn pan_right() {
361        let info = pan_info();
362        assert_eq!(format_param_value(&info, 0.5), "50R");
363        assert_eq!(format_param_value(&info, 1.0), "100R");
364        assert_eq!(format_param_value(&info, 0.006), "1R");
365    }
366
367    fn int_info(unit: ParamUnit) -> ParamInfo {
368        ParamInfo {
369            id: 0,
370            name: "n",
371            short_name: "n",
372            group: "",
373            range: ParamRange::Discrete { min: -12, max: 12 },
374            default_plain: 0.0,
375            flags: ParamFlags::empty(),
376            unit,
377            kind: ParamValueKind::Int,
378            midi_map: None,
379            midi_channel: None,
380        }
381    }
382
383    #[test]
384    fn int_param_no_fractional_zero() {
385        // IntParam values must render with no decimal places.
386        // A hard-coded `{:.1}` formatter (regardless of param kind)
387        // would render "0.0 st" / "-5.0 st" for semitone values.
388        assert_eq!(
389            format_param_value(&int_info(ParamUnit::Semitones), 0.0),
390            "0 st"
391        );
392        assert_eq!(
393            format_param_value(&int_info(ParamUnit::Semitones), -5.0),
394            "-5 st"
395        );
396        assert_eq!(format_param_value(&int_info(ParamUnit::None), 0.0), "0");
397        assert_eq!(format_param_value(&int_info(ParamUnit::Db), 6.0), "6 dB");
398        assert_eq!(
399            format_param_value(&int_info(ParamUnit::Milliseconds), 50.0),
400            "50 ms"
401        );
402    }
403}