Skip to main content

truce_params/
lib.rs

1#![forbid(unsafe_code)]
2
3mod info;
4mod range;
5mod smooth;
6mod types;
7
8pub use info::{ParamFlags, ParamInfo, ParamUnit};
9pub use range::ParamRange;
10pub use smooth::{Smoother, SmoothingStyle};
11pub use types::{BoolParam, EnumParam, FloatParam, IntParam, ParamEnum};
12
13/// Format a plain parameter value as a display string based on the parameter's unit.
14///
15/// Used by the `#[derive(Params)]` macro for default `format_value` implementations
16/// on `FloatParam` and `IntParam` fields.
17pub fn format_param_value(info: &ParamInfo, value: f64) -> String {
18    match info.unit {
19        ParamUnit::Db => format!("{:.1} dB", value),
20        ParamUnit::Hz => {
21            if value >= 1000.0 {
22                format!("{:.1} kHz", value / 1000.0)
23            } else {
24                format!("{:.0} Hz", value)
25            }
26        }
27        ParamUnit::Milliseconds => format!("{:.1} ms", value),
28        ParamUnit::Seconds => {
29            if value >= 1.0 {
30                format!("{:.2} s", value)
31            } else {
32                format!("{:.0} ms", value * 1000.0)
33            }
34        }
35        ParamUnit::Percent => format!("{:.0}%", value * 100.0),
36        ParamUnit::Semitones => format!("{:.1} st", value),
37        ParamUnit::Pan => {
38            if value.abs() < 0.01 {
39                "C".to_string()
40            } else if value < 0.0 {
41                format!("{:.0}L", -value * 100.0)
42            } else {
43                format!("{:.0}R", value * 100.0)
44            }
45        }
46        ParamUnit::None => format!("{:.2}", value),
47    }
48}
49
50/// Trait implemented by #[derive(Params)] on a struct.
51/// Format wrappers use this to enumerate, read, and write parameters.
52pub trait Params: Send + Sync + 'static {
53    /// All parameter infos, in declaration order.
54    fn param_infos(&self) -> Vec<ParamInfo>;
55
56    /// Number of parameters.
57    fn count(&self) -> usize;
58
59    /// Get normalized value (0.0–1.0) by ID.
60    fn get_normalized(&self, id: u32) -> Option<f64>;
61
62    /// Set normalized value (0.0–1.0) by ID.
63    fn set_normalized(&self, id: u32, value: f64);
64
65    /// Get plain value by ID.
66    fn get_plain(&self, id: u32) -> Option<f64>;
67
68    /// Set plain value by ID.
69    fn set_plain(&self, id: u32, value: f64);
70
71    /// Format a plain value to display string.
72    fn format_value(&self, id: u32, value: f64) -> Option<String>;
73
74    /// Parse a display string to plain value.
75    fn parse_value(&self, id: u32, text: &str) -> Option<f64>;
76
77    /// Reset all smoothers to current values.
78    fn snap_smoothers(&self);
79
80    /// Update smoother sample rates.
81    fn set_sample_rate(&self, sample_rate: f64);
82
83    /// Collect all parameter IDs and their current plain values.
84    fn collect_values(&self) -> (Vec<u32>, Vec<f64>);
85
86    /// Restore parameter values from a list of (id, value) pairs.
87    fn restore_values(&self, values: &[(u32, f64)]);
88
89    /// Create a default instance for GUI parameter display.
90    /// The GUI reads values via atomic reads from this instance.
91    fn default_for_gui() -> Self
92    where
93        Self: Sized;
94}