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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
pub trait NormalizedMap: 'static + Clone {
    fn normalized_to_display(&self, normalized: f32) -> String;

    fn snap(&self, normalized: f32) -> f32 {
        normalized
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum DisplayDecimals {
    Zero,
    #[default]
    One,
    Two,
    Three,
    Four,
    Five,
}

impl DisplayDecimals {
    pub fn display_value(&self, value: f32) -> String {
        match self {
            DisplayDecimals::Zero => format!("{:.0}", value),
            DisplayDecimals::One => format!("{:.1}", value),
            DisplayDecimals::Two => format!("{:.2}", value),
            DisplayDecimals::Three => format!("{:.3}", value),
            DisplayDecimals::Four => format!("{:.4}", value),
            DisplayDecimals::Five => format!("{:.5}", value),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ValueScaling {
    Linear,
    Power(f32),
    Frequency,
}

impl ValueScaling {
    pub fn normalized_to_value(&self, normalized: f32, min: f32, max: f32) -> f32 {
        if normalized <= 0.0 {
            return min;
        } else if normalized >= 1.0 {
            return max;
        }

        let map = |x: f32| -> f32 { (x * (max - min)) + min };

        match self {
            ValueScaling::Linear => map(normalized),

            ValueScaling::Power(exponent) => map(normalized.powf(*exponent)),

            ValueScaling::Frequency => {
                let minl = min.log2();
                let range = max.log2() - minl;
                2.0f32.powf((normalized * range) + minl)
            }
        }
    }

    pub fn value_to_normalized(&self, value: f32, min: f32, max: f32) -> f32 {
        if value <= min {
            return 0.0;
        } else if value >= max {
            return 1.0;
        }

        let unmap = |x: f32| -> f32 { (x - min) / (max - min) };

        match self {
            ValueScaling::Linear => unmap(value),

            ValueScaling::Power(exponent) => unmap(value).powf(1.0 / *exponent),

            ValueScaling::Frequency => {
                let minl = min.log2();
                let range = max.log2() - minl;
                (value.log2() - minl) / range
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct GenericMap {
    min: f32,
    max: f32,
    _span_recip: f32, // Small optimization to avoid division operations.

    value_scaling: ValueScaling,

    display_decimals: DisplayDecimals,
    units: Option<String>,
}

impl GenericMap {
    pub fn new(
        min: f32,
        max: f32,
        value_scaling: ValueScaling,
        display_decimals: DisplayDecimals,
        units: Option<String>,
    ) -> Self {
        assert!(min < max);

        Self { min, max, _span_recip: 1.0 / (max - min), value_scaling, display_decimals, units }
    }

    pub fn min(&self) -> f32 {
        self.min
    }
    pub fn max(&self) -> f32 {
        self.max
    }

    pub fn value_scaling(&self) -> &ValueScaling {
        &self.value_scaling
    }

    #[inline]
    pub fn normalized_to_value(&self, normalized: f32) -> f32 {
        self.value_scaling.normalized_to_value(normalized, self.min, self.max)
    }

    #[inline]
    pub fn value_to_normalized(&self, value: f32) -> f32 {
        self.value_scaling.value_to_normalized(value, self.min, self.max)
    }

    #[inline]
    pub fn clamp_value(&self, value: f32) -> f32 {
        value.min(self.max).max(self.min)
    }
}

impl NormalizedMap for GenericMap {
    fn normalized_to_display(&self, normalized: f32) -> String {
        let mut s = self.display_decimals.display_value(self.normalized_to_value(normalized));
        if let Some(units) = &self.units {
            s += units;
        }
        s
    }
}

#[derive(Debug, Clone)]
pub struct DecibelMap {
    min: f32,
    max: f32,

    value_scaling: ValueScaling,

    display_decimals: DisplayDecimals,
    display_units: bool,
}

impl DecibelMap {
    pub fn new(
        min_db: f32,
        max_db: f32,
        value_scaling: ValueScaling,
        display_decimals: DisplayDecimals,
        display_units: bool,
    ) -> Self {
        assert!(min_db < max_db);

        Self { min: min_db, max: max_db, value_scaling, display_decimals, display_units }
    }

    pub fn min_db(&self) -> f32 {
        self.min
    }
    pub fn max_db(&self) -> f32 {
        self.max
    }

    pub fn value_scaling(&self) -> &ValueScaling {
        &self.value_scaling
    }

    #[inline]
    pub fn normalized_to_db(&self, normalized: f32) -> f32 {
        self.value_scaling.normalized_to_value(normalized, self.min, self.max)
    }

    #[inline]
    pub fn normalized_to_amplitude(&self, normalized: f32) -> f32 {
        db_to_amplitude(self.value_scaling.normalized_to_value(normalized, self.min, self.max))
    }

    #[inline]
    pub fn db_to_normalized(&self, db: f32) -> f32 {
        self.value_scaling.value_to_normalized(db, self.min, self.max)
    }

    #[inline]
    pub fn amplitude_to_normalized(&self, amplitude: f32) -> f32 {
        let db = amplitude_to_db(amplitude);
        self.db_to_normalized(db)
    }

    #[inline]
    pub fn clamp_db(&self, db: f32) -> f32 {
        db.min(self.max).max(self.min)
    }
}

impl NormalizedMap for DecibelMap {
    fn normalized_to_display(&self, normalized: f32) -> String {
        let mut s = self.display_decimals.display_value(self.normalized_to_db(normalized));
        if self.display_units {
            s += " dB"
        }
        s
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FrequencyDisplayMode {
    OnlyHz(DisplayDecimals),
    HzThenKHz { under_1k: DisplayDecimals, over_1k: DisplayDecimals },
}

impl Default for FrequencyDisplayMode {
    fn default() -> Self {
        FrequencyDisplayMode::HzThenKHz {
            under_1k: DisplayDecimals::One,
            over_1k: DisplayDecimals::Two,
        }
    }
}

#[derive(Debug, Clone)]
pub struct FrequencyMap {
    min: f32,
    max: f32,

    value_scaling: ValueScaling,

    display_mode: FrequencyDisplayMode,
    display_units: bool,
}

impl FrequencyMap {
    pub fn new(
        min_hz: f32,
        max_hz: f32,
        value_scaling: ValueScaling,
        display_mode: FrequencyDisplayMode,
        display_units: bool,
    ) -> Self {
        assert!(min_hz < max_hz);

        Self { min: min_hz, max: max_hz, value_scaling, display_mode, display_units }
    }

    pub fn min_hz(&self) -> f32 {
        self.min
    }
    pub fn max_hz(&self) -> f32 {
        self.max
    }

    pub fn value_scaling(&self) -> &ValueScaling {
        &self.value_scaling
    }

    #[inline]
    pub fn normalized_to_hz(&self, normalized: f32) -> f32 {
        self.value_scaling.normalized_to_value(normalized, self.min, self.max)
    }

    #[inline]
    pub fn hz_to_normalized(&self, hz: f32) -> f32 {
        self.value_scaling.value_to_normalized(hz, self.min, self.max)
    }

    #[inline]
    pub fn clamp_hz(&self, hz: f32) -> f32 {
        hz.min(self.max).max(self.min)
    }
}

impl NormalizedMap for FrequencyMap {
    fn normalized_to_display(&self, normalized: f32) -> String {
        let hz = self.normalized_to_hz(normalized);

        match self.display_mode {
            FrequencyDisplayMode::OnlyHz(display_decimals) => {
                let mut s = display_decimals.display_value(hz);
                if self.display_units {
                    s += " Hz"
                }
                s
            }
            FrequencyDisplayMode::HzThenKHz { under_1k, over_1k } => {
                if hz < 1_000.0 {
                    let mut s = under_1k.display_value(hz);
                    if self.display_units {
                        s += " Hz"
                    }
                    s
                } else {
                    let mut s = over_1k.display_value(hz / 1_000.0);
                    if self.display_units {
                        s += " kHz"
                    }
                    s
                }
            }
        }
    }
}

#[derive(Clone)]
pub struct IntMap {
    min: i32,
    max: i32,
    span: f32, // Small optimization.

    display_map: Option<&'static dyn Fn(i32) -> String>,
}

impl IntMap {
    pub fn new(min: i32, max: i32, display_map: Option<&'static dyn Fn(i32) -> String>) -> Self {
        assert!(min <= max);

        Self { min, max, span: (max - min) as f32, display_map }
    }

    pub fn min(&self) -> i32 {
        self.min
    }
    pub fn max(&self) -> i32 {
        self.max
    }

    #[inline]
    pub fn normalized_to_int(&self, normalized: f32) -> i32 {
        if normalized <= 0.0 {
            return self.min;
        } else if normalized >= 1.0 {
            return self.max;
        }

        (normalized * self.span).round() as i32
    }

    #[inline]
    pub fn int_to_normalized(&self, int: i32) -> f32 {
        if self.min == self.max {
            // Value will always be the same, so avoid a divide
            // by zero.
            0.0
        } else {
            if int <= self.min {
                return 0.0;
            } else if int >= self.max {
                return 1.0;
            }

            (int as f32 - self.min as f32) / self.span
        }
    }

    #[inline]
    pub fn clamp_int(&self, int: i32) -> i32 {
        int.min(self.max).max(self.min)
    }
}

impl NormalizedMap for IntMap {
    fn normalized_to_display(&self, normalized: f32) -> String {
        let int = self.normalized_to_int(normalized);

        if let Some(display_map) = self.display_map {
            (display_map)(int)
        } else {
            // Display the plain integer instead.
            format!("{}", int)
        }
    }

    fn snap(&self, normalized: f32) -> f32 {
        let int = self.normalized_to_int(normalized);
        self.int_to_normalized(int)
    }
}

#[inline]
pub fn db_to_amplitude(db: f32) -> f32 {
    10.0f32.powf(db * 1.0 / 20.0)
}

#[inline]
pub fn amplitude_to_db(amp: f32) -> f32 {
    20.0f32 * amp.log10()
}