1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use ::std::sync::Arc;
use ::anyhow::{Result, ensure};
use crate::atomic::AtomicF32;
/// A setting of a stage that the control interface changes while the stage runs in the audio
/// path: the stage reads [`Param::get`] at the start of every frame and applies a change with it.
/// Clones share the value.
#[derive(Clone, Debug)]
pub struct Param {
/// Unique within its stage, for the control interface.
pub name: &'static str,
/// For the app.
pub title: &'static str,
/// Shown after the value, e.g. `dB`, `Hz` or `:1`.
pub unit: &'static str,
pub min: f32,
pub max: f32,
/// The step the app offers.
pub step: f32,
pub default: f32,
value: Arc<AtomicF32>,
}
impl Param {
pub fn new(
name: &'static str,
title: &'static str,
unit: &'static str,
(min, max, step): (f32, f32, f32),
default: f32,
) -> Self {
Self {
name,
title,
unit,
min,
max,
step,
default,
value: Arc::new(AtomicF32::new(default)),
}
}
/// The parameter `name` of `stage` among the parameters of a chain, as
/// [`crate::chain::Chain::params`] gives them.
pub fn find<'a>(
params: &'a [(&'static str, Param)],
stage: &str,
name: &str,
) -> Option<&'a Param> {
params
.iter()
.find(|(of, param)| *of == stage && param.name == name)
.map(|(_, param)| param)
}
pub fn get(&self) -> f32 {
self.value.load()
}
/// Fails outside `min..=max` (and for NaN), the value stays as it is then.
pub fn set(&self, value: f32) -> Result<()> {
ensure!(
(self.min..=self.max).contains(&value),
"{} must be {} to {} {}, not {value}",
self.name,
self.min,
self.max,
self.unit,
);
self.value.store(value);
Ok(())
}
}