Skip to main content

sva_engine/
meaning.rs

1// Concern: what each builtin's named arguments mean, their unit and the model part each moves | Non-concern: which names a builtin takes (vocabulary.rs) | IO: (builtin, name) -> Meaning
2
3use sva_ast::{FINITE_DIFFERENCE, MODAL};
4
5/// `unit` is `none` for a pure number.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct Meaning {
8    pub text: &'static str,
9    pub unit: &'static str,
10    pub part: Option<&'static str>,
11}
12
13const fn m(text: &'static str, unit: &'static str, part: &'static str) -> Meaning {
14    Meaning {
15        text,
16        unit,
17        part: Some(part),
18    }
19}
20
21const fn plain(text: &'static str, unit: &'static str) -> Meaning {
22    Meaning {
23        text,
24        unit,
25        part: None,
26    }
27}
28
29/// `None` for a name `builtin` does not take, by name or, for a solver or a bank, by position.
30pub fn meaning(builtin: &str, key: &str) -> Option<Meaning> {
31    if crate::vocabulary::shape(builtin).is_some() {
32        return filter(key);
33    }
34    if FINITE_DIFFERENCE.contains(&builtin) {
35        return solver(builtin, key);
36    }
37    if MODAL.contains(&builtin) {
38        return modal(builtin, key);
39    }
40    Some(match (builtin, key) {
41        ("saw" | "square" | "triangle", "tol") => plain(
42            "admitted and unused: the series is truncated where the profile's floor ends",
43            "none",
44        ),
45        ("sat", "drive") => plain("gain applied before the clip to [-1, 1]", "none"),
46        ("crop", "start") => plain("where the window opens, the second positional", "s"),
47        ("crop", "end") => plain("where the window closes, the third positional", "s"),
48        ("crop", "rise") => plain("raised-cosine fade-in from the window's start", "s"),
49        ("crop", "fall") => plain("raised-cosine fade-out into the window's end", "s"),
50        ("rand", "seed") => plain("which hash the key is drawn through", "none"),
51        ("delta", "k") => plain("derivative order of the impulse", "none"),
52        ("stft", "window") => plain(
53            "frame length, a whole number of steps of the rate in use",
54            "s",
55        ),
56        ("stft", "hop") => plain(
57            "frame advance, a whole number of steps of the rate in use",
58            "s",
59        ),
60        ("noise", "period") => plain("repeat time; the lines fall every 1/period Hz", "s"),
61        ("noise", "color") => plain("spectral tilt of the lines", "dB/octave"),
62        _ => return None,
63    })
64}
65
66fn filter(key: &str) -> Option<Meaning> {
67    Some(match key {
68        "cutoff" => plain("corner or centre frequency", "Hz"),
69        "q" => plain("resonance: centre frequency over bandwidth", "none"),
70        "gain" => plain("boost or cut of a shelf or peak", "dB"),
71        _ => return None,
72    })
73}
74
75/// A finite-difference model's losses act on its own grid: `damp_dc` on velocity,
76/// `damp_freq` on the rate of curvature, except the bore's, which scale two loss families.
77fn solver(builtin: &str, key: &str) -> Option<Meaning> {
78    let part = match builtin {
79        "chaigne_askenfelt" | "willemsen_bilbao_serafin" => "string",
80        "darabundit_scavone" => "bore",
81        "rhaouti_chaigne_joly" => "membrane",
82        "chaigne_doutaut" => "bar",
83        "botteldooren" => "room",
84        _ => return None,
85    };
86    if let Some(n) = numbered(key, "string") {
87        return match n.1 {
88            "_cents" => Some(m(
89                "string's offset from its unison placing",
90                "cents",
91                "string",
92            )),
93            "_hammer_k_ratio" => Some(m(
94                "felt stiffness this string meets, as a multiple of hammer_k",
95                "none",
96                "hammer",
97            )),
98            _ => None,
99        };
100    }
101    if let Some(n) = numbered(key, "hole") {
102        return match n.1 {
103            "_pos" => Some(m("tone hole position along the bore", "none", "tonehole")),
104            "_open" => Some(m("1 leaves the hole open, 0 closes it", "none", "tonehole")),
105            "_radius" => Some(m("tone hole radius", "m", "tonehole")),
106            "_height" => Some(m("tone hole chimney height", "m", "tonehole")),
107            _ => None,
108        };
109    }
110    Some(match (builtin, key) {
111        ("darabundit_scavone", "length") => m("bore length", "m", part),
112        (_, "f0") => m("fundamental frequency", "Hz", part),
113        ("darabundit_scavone", "damp_dc") => m("scale on the viscous wall loss", "none", part),
114        ("darabundit_scavone", "damp_freq") => m("scale on the thermal wall loss", "none", part),
115        (_, "damp_dc") => m("frequency-independent loss", "1/s", part),
116        (_, "damp_freq") => m("frequency-dependent loss", "m^2/s", part),
117        (_, "b") => m("string stiffness (inharmonicity)", "none", "string"),
118        (_, "strike_pos") => m(
119            "where the hammer strikes, along the length",
120            "none",
121            "hammer",
122        ),
123        (_, "strike_x") => m("where the hammer strikes, across x", "none", "hammer"),
124        (_, "strike_y") => m("where the hammer strikes, across y", "none", "hammer"),
125        (_, "vel") => m("hammer velocity at contact", "m/s", "hammer"),
126        (_, "hammer_mass") => m("hammer mass", "kg", "hammer"),
127        (_, "hammer_k") => m("felt stiffness K in F = K compression^p", "N/m^p", "hammer"),
128        (_, "hammer_p") => m("felt stiffness exponent p", "none", "hammer"),
129        (_, "unison_count") => m("strings struck together, 1 to 3", "none", "string"),
130        (_, "detune") => m(
131            "frequency ratio across the unison's outer strings",
132            "none",
133            "string",
134        ),
135        (_, "bridge_coupling") => m(
136            "bridge resistance, in string wave impedances",
137            "none",
138            "bridge",
139        ),
140        (_, "bridge_mass") => m("bridge mass; 0 is massless", "kg", "bridge"),
141        (_, "damper_pos") => m("where the felt presses, along the length", "none", "damper"),
142        (_, "damper_r") => m(
143            "felt dashpot pressing every string; 0 is lifted; may move every sample",
144            "N*s/m",
145            "damper",
146        ),
147        (_, "damper_k") => m(
148            "felt spring pressing every string; may only jump",
149            "N/m",
150            "damper",
151        ),
152        (_, "bow_pos") => m("where the bow touches, along the length", "none", "bow"),
153        (_, "bow_vel") => m("bow velocity; may move every sample", "m/s", "bow"),
154        (_, "bow_force") => m(
155            "force pressing the bow on the string; may move every sample",
156            "N",
157            "bow",
158        ),
159        (_, "mu_s") => m("static friction coefficient", "none", "friction"),
160        (_, "mu_c") => m("sliding (Coulomb) friction coefficient", "none", "friction"),
161        (_, "stribeck_vel") => m(
162            "slip speed over which static friction falls to sliding",
163            "m/s",
164            "friction",
165        ),
166        (_, "bristle_stiffness") => m("bristle stiffness s0", "N/m", "friction"),
167        (_, "bristle_damping") => m("bristle damping s1", "N*s/m", "friction"),
168        (_, "viscous_friction") => m("viscous friction s2", "N*s/m", "friction"),
169        (_, "radius_in") => m("bore radius at the excited end", "m", "bore"),
170        (_, "radius_out") => m("bore radius at the far end, a cone between", "m", "bore"),
171        (_, "excite_pos") => m("where the pulse enters, along the length", "none", "source"),
172        (_, "pulse_amp") => m("pressure pulse peak", "Pa", "source"),
173        (_, "pulse_width") => m("pressure pulse duration", "s", "source"),
174        (_, "aspect_ratio") => m("side ratio lx/ly at a fixed area", "none", "membrane"),
175        (_, "aspect_y") => m("room depth, in room lengths", "none", "room"),
176        (_, "aspect_z") => m("room height, in room lengths", "none", "room"),
177        (_, "listener_x") => m("listener position along the length", "none", "listener"),
178        (_, "listener_y") => m("listener position along the depth", "none", "listener"),
179        (_, "listener_z") => m("listener position along the height", "none", "listener"),
180        _ => return None,
181    })
182}
183
184/// `string2_cents` is `(2, "_cents")`.
185fn numbered<'k>(key: &'k str, stem: &str) -> Option<(u32, &'k str)> {
186    let rest = key.strip_prefix(stem)?;
187    let digits = rest.find(|c: char| !c.is_ascii_digit())?;
188    Some((rest[..digits].parse().ok()?, &rest[digits..]))
189}
190
191/// A strike's hammer, and the decay every mode shares: `1/tau = damp_dc + damp_freq f^2`.
192fn modal(builtin: &str, key: &str) -> Option<Meaning> {
193    Some(match (builtin, key) {
194        ("hammer_pulse", "vel") => m("strike velocity", "m/s", "hammer"),
195        ("helmholtz", "volume") => m("cavity volume", "m^3", "cavity"),
196        ("string", "f0") => m("fundamental frequency", "Hz", "string"),
197        ("membrane" | "room", "lx") => m("side along x", "m", builtin_part(builtin)),
198        ("membrane" | "room", "ly") => m("side along y", "m", builtin_part(builtin)),
199        ("room", "lz") => m("side along z", "m", "room"),
200        ("bar" | "bore", "length") => m("length", "m", builtin_part(builtin)),
201        (_, "damp_dc") => m("decay rate every mode shares", "1/s", "damping"),
202        (_, "damp_freq") => m("decay rate per squared hertz", "s", "damping"),
203        (_, "modes") => plain("how many modes the bank holds", "none"),
204        (_, "vel") => m(
205            "strike velocity; omitted, an impulse rings the bank",
206            "m/s",
207            "hammer",
208        ),
209        (_, "at") => m("when the strike lands", "s", "hammer"),
210        (_, "contact") => m("contact time at 3.2 m/s", "s", "hammer"),
211        (_, "p") => m("felt stiffness exponent", "none", "hammer"),
212        (_, "force") => m("peak force at 3.2 m/s", "N", "hammer"),
213        ("string", "inharmonicity") => m("string stiffness B", "none", "string"),
214        ("string", "strike") => m("where the strike lands, along the length", "none", "hammer"),
215        ("membrane", "tension") => m("membrane tension", "N/m", "membrane"),
216        ("membrane", "density") => m("membrane areal density", "kg/m^2", "membrane"),
217        ("membrane", "strike_x") => m("where the strike lands, across x", "none", "hammer"),
218        ("membrane", "strike_y") => m("where the strike lands, across y", "none", "hammer"),
219        ("bar", "thickness") => m("bar thickness", "m", "bar"),
220        ("bar", "young") => m("Young's modulus", "Pa", "bar"),
221        ("bar", "density") => m("bar density", "kg/m^3", "bar"),
222        ("bore", "radius") => m("bore radius", "m", "bore"),
223        ("bore" | "room" | "helmholtz", "speed") => m("speed of sound", "m/s", "air"),
224        ("bore", "closed") => m("1 closes one end, 0 leaves both open", "none", "bore"),
225        ("helmholtz", "neck_area") => m("neck cross-section", "m^2", "neck"),
226        ("helmholtz", "neck_length") => m("neck length", "m", "neck"),
227        ("helmholtz", "radius") => m("neck radius, for the 1.7 r end correction", "m", "neck"),
228        _ => return None,
229    })
230}
231
232fn builtin_part(builtin: &str) -> &'static str {
233    match builtin {
234        "membrane" => "membrane",
235        "room" => "room",
236        "bar" => "bar",
237        _ => "bore",
238    }
239}