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::{ParamFlags, ParamInfo, ParamUnit, ParamValueKind};
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    /// Walk every parameter and meter ID reachable from `self`
244    /// (including nested `#[nested]` substructs) and panic on the
245    /// first duplicate.
246    ///
247    /// Why this isn't just a compile-time check: the
248    /// `#[derive(Params)]` collision check at expansion time only
249    /// sees IDs declared in the *current* struct. A parent param
250    /// `id = 5` and a nested-substruct param `id = 5` both compile,
251    /// because the parent derive doesn't see into the nested type.
252    /// At runtime, the `set_plain` / `get_plain` dispatcher matches
253    /// at the outer level first and silently never reaches the
254    /// nested one - preset round-trips would corrupt the nested
255    /// value. This method makes that bug surface as a panic at
256    /// plugin construction instead of as quiet state loss.
257    ///
258    /// Called automatically by the derive-generated `Self::new()`.
259    /// Plugin code shouldn't need to invoke it directly.
260    fn assert_no_id_collisions(&self) {
261        let mut all = self.param_infos();
262        // Borrow the names from the existing infos so the panic
263        // message can identify *which* IDs collided.
264        let mut seen: Vec<(u32, &'static str)> = Vec::with_capacity(all.len());
265        for info in all.drain(..) {
266            for (prev_id, prev_name) in &seen {
267                assert!(
268                    *prev_id != info.id,
269                    "duplicate parameter ID {}: '{}' and '{}' (likely a \
270                     parent / nested-struct collision; the per-struct \
271                     compile-time check can't see across nested types)",
272                    info.id,
273                    prev_name,
274                    info.name,
275                );
276            }
277            seen.push((info.id, info.name));
278        }
279        for meter_id in self.meter_ids() {
280            for (prev_id, prev_name) in &seen {
281                assert!(
282                    *prev_id != meter_id,
283                    "meter ID {meter_id} collides with parameter ID for '{prev_name}'",
284                );
285            }
286        }
287    }
288}
289
290#[cfg(test)]
291mod tests {
292    use super::*;
293    use crate::range::ParamRange;
294
295    fn pan_info() -> ParamInfo {
296        ParamInfo {
297            id: 0,
298            name: "Pan",
299            short_name: "Pan",
300            group: "",
301            range: ParamRange::Linear {
302                min: -1.0,
303                max: 1.0,
304            },
305            default_plain: 0.0,
306            flags: ParamFlags::empty(),
307            unit: ParamUnit::Pan,
308            kind: ParamValueKind::Float,
309        }
310    }
311
312    #[test]
313    fn pan_centre() {
314        let info = pan_info();
315        assert_eq!(format_param_value(&info, 0.0), "C");
316        assert_eq!(format_param_value(&info, 0.004), "C");
317        assert_eq!(format_param_value(&info, -0.004), "C");
318    }
319
320    #[test]
321    fn pan_left() {
322        let info = pan_info();
323        assert_eq!(format_param_value(&info, -0.5), "50L");
324        assert_eq!(format_param_value(&info, -1.0), "100L");
325        assert_eq!(format_param_value(&info, -0.006), "1L");
326    }
327
328    #[test]
329    fn pan_right() {
330        let info = pan_info();
331        assert_eq!(format_param_value(&info, 0.5), "50R");
332        assert_eq!(format_param_value(&info, 1.0), "100R");
333        assert_eq!(format_param_value(&info, 0.006), "1R");
334    }
335
336    fn int_info(unit: ParamUnit) -> ParamInfo {
337        ParamInfo {
338            id: 0,
339            name: "n",
340            short_name: "n",
341            group: "",
342            range: ParamRange::Discrete { min: -12, max: 12 },
343            default_plain: 0.0,
344            flags: ParamFlags::empty(),
345            unit,
346            kind: ParamValueKind::Int,
347        }
348    }
349
350    #[test]
351    fn int_param_no_fractional_zero() {
352        // IntParam values must render with no decimal places.
353        // A hard-coded `{:.1}` formatter (regardless of param kind)
354        // would render "0.0 st" / "-5.0 st" for semitone values.
355        assert_eq!(
356            format_param_value(&int_info(ParamUnit::Semitones), 0.0),
357            "0 st"
358        );
359        assert_eq!(
360            format_param_value(&int_info(ParamUnit::Semitones), -5.0),
361            "-5 st"
362        );
363        assert_eq!(format_param_value(&int_info(ParamUnit::None), 0.0), "0");
364        assert_eq!(format_param_value(&int_info(ParamUnit::Db), 6.0), "6 dB");
365        assert_eq!(
366            format_param_value(&int_info(ParamUnit::Milliseconds), 50.0),
367            "50 ms"
368        );
369    }
370}