atome 0.8.8

An async, `cpal`-based audio engine for Rust. `atome` gives you a small, composable API for building real-time audio applications — sample playback, mixing, routing, and DSP graphs — without wrestling with platform audio callbacks, device enumeration, or buffer management yourself.
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! One parameter vocabulary, for every plugin format.
//!
//! A compressor's threshold is set the same way whether the compressor is a
//! [built-in](super::atome), a VST3, or an Audio Unit:
//!
//! ```
//! use ::atome::plugins::{atome, ParamError, Params};
//!
//! let mut compressor = atome::compressor(48_000);
//! compressor.set_params(&Params::parse(r#"{ "threshold_db": -18, "ratio": 4 }"#)?)?;
//! # Ok::<(), ParamError>(())
//! ```
//!
//! The formats do not agree on what a parameter *is* — a VST3 addresses them by
//! index and normalises to `0.0..=1.0`, an AU uses an address and native units,
//! a built-in has named fields — so [`Params`] is a name-to-value map and each
//! backend translates. Names are how they meet: for the hosted formats the name
//! is what the plugin calls the parameter in its own UI.
//!
//! # The syntax
//!
//! A flat JSON object. Deliberately not all of JSON — no nesting, no arrays,
//! no `null` — because a parameter set is flat, and accepting more would only
//! move the error further from the mistake:
//!
//! ```text
//! { "threshold_db": -18.0, "ratio": 4, "auto_makeup": true, "mode": "peak" }
//! ```
//!
//! Three things are accepted beyond strict JSON, because this is as often
//! typed by hand as generated: the outer braces may be left off, keys need not
//! be quoted, and a trailing comma is allowed.
//!
//! ```text
//! threshold_db: -18, ratio: 4
//! ```
//!
//! Nothing here is on the audio path. Parsing allocates and reports errors by
//! `String`; parameters are applied between blocks, not during one.

use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;

// -----------------------------------------------------------------------------
//  Values
// -----------------------------------------------------------------------------

/// A single parameter value.
///
/// Numbers are `f64` whatever they were written as: `4` and `4.0` are the same
/// value, since a plugin parameter that is conceptually an integer (a mode, a
/// voice count) still arrives through the same `f64` door in every format.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    Number(f64),
    Bool(bool),
    Text(String),
}

impl Value {
    /// A number that was stored as an `f32`, without the noise widening adds.
    ///
    /// `1.4f32 as f64` is 1.399999976158142 — true, and not what anyone typed.
    /// Going through the shortest `f32` representation gives back the 1.4 that
    /// was written, which is what a parameter listing should show and what a
    /// saved chain should contain.
    pub fn from_f32(value: f32) -> Self {
        Self::Number(value.to_string().parse().unwrap_or(value as f64))
    }

    /// The value as a number, if it is one.
    ///
    /// A bool counts: `true` is 1.0. Formats that address every parameter as a
    /// number need somewhere for a toggle to go, and this is it.
    pub fn as_number(&self) -> Option<f64> {
        match self {
            Self::Number(number) => Some(*number),
            Self::Bool(true) => Some(1.0),
            Self::Bool(false) => Some(0.0),
            Self::Text(_) => None,
        }
    }

    /// The value as a bool, if it is one.
    ///
    /// A number counts, on the same reasoning as [`as_number`](Self::as_number)
    /// in reverse: anything not equal to zero is true.
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(flag) => Some(*flag),
            Self::Number(number) => Some(*number != 0.0),
            Self::Text(_) => None,
        }
    }

    /// The value as text, if it is text. Numbers are not stringified.
    pub fn as_text(&self) -> Option<&str> {
        match self {
            Self::Text(text) => Some(text),
            _ => None,
        }
    }

    /// What this value is, for an error message.
    fn kind(&self) -> &'static str {
        match self {
            Self::Number(_) => "a number",
            Self::Bool(_) => "a boolean",
            Self::Text(_) => "text",
        }
    }
}

impl From<f64> for Value {
    fn from(value: f64) -> Self {
        Self::Number(value)
    }
}

impl From<f32> for Value {
    fn from(value: f32) -> Self {
        Self::from_f32(value)
    }
}

impl From<i32> for Value {
    fn from(value: i32) -> Self {
        Self::Number(value as f64)
    }
}

impl From<bool> for Value {
    fn from(value: bool) -> Self {
        Self::Bool(value)
    }
}

impl From<&str> for Value {
    fn from(value: &str) -> Self {
        Self::Text(value.to_string())
    }
}

impl From<String> for Value {
    fn from(value: String) -> Self {
        Self::Text(value)
    }
}

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            // `{}` on an f64 already drops a trailing `.0`, which keeps a
            // round-trip through `Display` and `parse` from growing noise.
            Self::Number(number) => write!(f, "{number}"),
            Self::Bool(flag) => write!(f, "{flag}"),
            Self::Text(text) => write!(f, "\"{}\"", escape(text)),
        }
    }
}

// -----------------------------------------------------------------------------
//  Errors
// -----------------------------------------------------------------------------

/// What went wrong setting or parsing parameters.
#[derive(Clone, Debug, PartialEq)]
pub enum ParamError {
    /// The text is not a parameter object.
    Syntax { at: usize, message: String },
    /// The plugin has no parameter by that name.
    Unknown { key: String, known: Vec<String> },
    /// The parameter exists but does not take a value of that kind.
    Type {
        key: String,
        wanted: &'static str,
        got: &'static str,
    },
    /// The value is outside what the parameter accepts.
    Range {
        key: String,
        value: f64,
        min: f64,
        max: f64,
    },
    /// The plugin has parameters but this build cannot reach them.
    Unsupported { message: String },
}

impl fmt::Display for ParamError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Syntax { at, message } => {
                write!(f, "parameter syntax error at byte {at}: {message}")
            }
            Self::Unknown { key, known } => {
                write!(f, "no parameter called '{key}'")?;
                // A misspelling is the usual cause, and the fix is almost
                // always visible in the list — so print it, up to the point
                // where it stops being readable.
                if !known.is_empty() {
                    let shown = known.len().min(12);
                    write!(f, " (has: {}", known[..shown].join(", "))?;
                    if known.len() > shown {
                        write!(f, ", and {} more", known.len() - shown)?;
                    }
                    write!(f, ")")?;
                }
                Ok(())
            }
            Self::Type { key, wanted, got } => {
                write!(f, "'{key}' wants {wanted}, got {got}")
            }
            Self::Range {
                key,
                value,
                min,
                max,
            } => write!(f, "'{key}' is {value}, outside {min}..={max}"),
            Self::Unsupported { message } => write!(f, "{message}"),
        }
    }
}

impl std::error::Error for ParamError {}

// -----------------------------------------------------------------------------
//  Schema
// -----------------------------------------------------------------------------

/// Whether a parameter is a quantity or a switch.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParamKind {
    Number,
    Toggle,
}

/// What a plugin says about one of its parameters.
///
/// Enough to build a UI, validate a value, or print a help listing — see
/// [`Plugin::param_schema`](super::Plugin::param_schema).
#[derive(Clone, Debug, PartialEq)]
pub struct ParamSpec {
    pub name: String,
    /// `"dB"`, `"Hz"`, `"ms"`, or empty for a ratio or a switch.
    pub unit: String,
    pub kind: ParamKind,
    pub min: f64,
    pub max: f64,
    pub default: f64,
    /// One line on what the parameter does.
    pub about: String,
}

impl ParamSpec {
    /// A numeric parameter.
    pub fn number(
        name: &str,
        unit: &str,
        min: f64,
        max: f64,
        default: f64,
        about: &str,
    ) -> Self {
        Self {
            name: name.to_string(),
            unit: unit.to_string(),
            kind: ParamKind::Number,
            min,
            max,
            default,
            about: about.to_string(),
        }
    }

    /// A switch.
    pub fn toggle(name: &str, default: bool, about: &str) -> Self {
        Self {
            name: name.to_string(),
            unit: String::new(),
            kind: ParamKind::Toggle,
            min: 0.0,
            max: 1.0,
            default: if default { 1.0 } else { 0.0 },
            about: about.to_string(),
        }
    }

    /// Checks a value against this parameter, returning it as a number.
    ///
    /// Out-of-range is an error rather than a clamp. A threshold of `-180 dB`
    /// asked for by mistake is silence, and silence that was quietly corrected
    /// to the nearest legal value is harder to find than one that refused.
    pub fn check(&self, value: &Value) -> Result<f64, ParamError> {
        let number = match self.kind {
            ParamKind::Toggle => value
                .as_bool()
                .map(|flag| if flag { 1.0 } else { 0.0 })
                .ok_or_else(|| ParamError::Type {
                    key: self.name.clone(),
                    wanted: "a boolean",
                    got: value.kind(),
                })?,
            ParamKind::Number => value.as_number().ok_or_else(|| ParamError::Type {
                key: self.name.clone(),
                wanted: "a number",
                got: value.kind(),
            })?,
        };

        if number < self.min || number > self.max {
            return Err(ParamError::Range {
                key: self.name.clone(),
                value: number,
                min: self.min,
                max: self.max,
            });
        }

        Ok(number)
    }
}

impl fmt::Display for ParamSpec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.kind {
            ParamKind::Toggle => write!(
                f,
                "{:<16} {:<22} {}",
                self.name,
                format!("(default {})", self.default != 0.0),
                self.about
            ),
            ParamKind::Number => write!(
                f,
                "{:<16} {:<22} {}",
                self.name,
                format!(
                    "{}..={}{}{}",
                    self.min,
                    self.max,
                    if self.unit.is_empty() { "" } else { " " },
                    self.unit
                ),
                self.about
            ),
        }
    }
}

// -----------------------------------------------------------------------------
//  Params
// -----------------------------------------------------------------------------

/// A set of parameter values, by name.
///
/// Ordered, because the order a set prints in should not change between runs —
/// a chain's parameters end up in configuration files and diffs.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Params {
    values: BTreeMap<String, Value>,
}

impl Params {
    /// An empty set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Parses the [syntax described on this module](self).
    ///
    /// # Errors
    ///
    /// [`ParamError::Syntax`], with the byte offset of whatever stopped it.
    pub fn parse(text: &str) -> Result<Self, ParamError> {
        Parser::new(text).parse()
    }

    /// Sets one value, returning `self` — for building a set inline.
    #[must_use]
    pub fn with(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.set(key, value);
        self
    }

    /// Sets one value, replacing any already under that name.
    pub fn set(&mut self, key: impl Into<String>, value: impl Into<Value>) {
        self.values.insert(key.into(), value.into());
    }

    /// The value under `key`.
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.values.get(key)
    }

    /// The value under `key` as a number.
    pub fn number(&self, key: &str) -> Option<f64> {
        self.get(key)?.as_number()
    }

    /// The value under `key` as a bool.
    pub fn bool(&self, key: &str) -> Option<bool> {
        self.get(key)?.as_bool()
    }

    /// The value under `key` as text.
    pub fn text(&self, key: &str) -> Option<&str> {
        self.get(key)?.as_text()
    }

    /// Removes a value.
    pub fn remove(&mut self, key: &str) -> Option<Value> {
        self.values.remove(key)
    }

    /// Every name and value, in name order.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
        self.values.iter().map(|(key, value)| (key.as_str(), value))
    }

    pub fn len(&self) -> usize {
        self.values.len()
    }

    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }
}

impl FromStr for Params {
    type Err = ParamError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Self::parse(text)
    }
}

impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Params {
    fn from_iter<I: IntoIterator<Item = (K, V)>>(pairs: I) -> Self {
        Self {
            values: pairs
                .into_iter()
                .map(|(key, value)| (key.into(), value.into()))
                .collect(),
        }
    }
}

/// Prints as a JSON object, which [`Params::parse`] reads back.
impl fmt::Display for Params {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("{")?;
        for (index, (key, value)) in self.values.iter().enumerate() {
            if index > 0 {
                f.write_str(",")?;
            }
            write!(f, " \"{}\": {value}", escape(key))?;
        }
        f.write_str(if self.values.is_empty() { "}" } else { " }" })
    }
}

/// Escapes the two characters that would otherwise end a JSON string early.
///
/// Control characters are not escaped, and not expected: these are parameter
/// names and mode words, not arbitrary text.
fn escape(text: &str) -> String {
    text.replace('\\', "\\\\").replace('"', "\\\"")
}

// -----------------------------------------------------------------------------
//  Parser
// -----------------------------------------------------------------------------

/// A recursive-descent parser for the flat object described on the module.
///
/// Byte-oriented: every value in the grammar is ASCII-delimited, so the only
/// place a multi-byte character can appear is inside a quoted string, where it
/// is copied through without being looked at.
struct Parser<'a> {
    text: &'a [u8],
    source: &'a str,
    at: usize,
}

impl<'a> Parser<'a> {
    fn new(source: &'a str) -> Self {
        Self {
            text: source.as_bytes(),
            source,
            at: 0,
        }
    }

    fn parse(mut self) -> Result<Params, ParamError> {
        let mut values = BTreeMap::new();

        self.space();

        // The braces are optional, but they have to match: an opening brace
        // with no closing one is a truncated object, not a brace-less list
        // that happens to start with `{`.
        let braced = self.eat(b'{');
        self.space();

        while self.at < self.text.len() {
            if braced && self.peek() == Some(b'}') {
                break;
            }

            let key = self.key()?;
            self.space();

            if !self.eat(b':') && !self.eat(b'=') {
                return Err(self.error("expected ':' after the parameter name"));
            }

            self.space();
            let value = self.value()?;
            // Last one wins. Refusing a duplicate would be defensible, but a
            // set built by appending an override to a default is a reasonable
            // thing to write, and this is what makes it work.
            values.insert(key, value);

            self.space();
            if !self.eat(b',') {
                break;
            }
            self.space();
        }

        self.space();

        if braced && !self.eat(b'}') {
            return Err(self.error("expected '}' to close the parameter object"));
        }

        self.space();
        if self.at < self.text.len() {
            return Err(self.error("unexpected trailing text"));
        }

        Ok(Params { values })
    }

    fn key(&mut self) -> Result<String, ParamError> {
        if self.peek() == Some(b'"') {
            return self.string();
        }

        let start = self.at;
        while let Some(byte) = self.peek() {
            if byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-' || byte == b'.' {
                self.at += 1;
            } else {
                break;
            }
        }

        if self.at == start {
            return Err(self.error("expected a parameter name"));
        }

        Ok(self.source[start..self.at].to_string())
    }

    fn value(&mut self) -> Result<Value, ParamError> {
        match self.peek() {
            Some(b'"') => self.string().map(Value::Text),
            Some(b't') if self.word("true") => Ok(Value::Bool(true)),
            Some(b'f') if self.word("false") => Ok(Value::Bool(false)),
            Some(byte) if byte == b'-' || byte == b'+' || byte.is_ascii_digit() || byte == b'.' => {
                self.number()
            }
            Some(_) => Err(self.error("expected a number, a boolean, or a quoted string")),
            None => Err(self.error("expected a value")),
        }
    }

    fn number(&mut self) -> Result<Value, ParamError> {
        let start = self.at;

        if matches!(self.peek(), Some(b'-') | Some(b'+')) {
            self.at += 1;
        }
        while matches!(self.peek(), Some(byte) if byte.is_ascii_digit() || byte == b'.') {
            self.at += 1;
        }
        // Exponent, so a value pasted from something that prints in scientific
        // notation reads back.
        if matches!(self.peek(), Some(b'e') | Some(b'E')) {
            self.at += 1;
            if matches!(self.peek(), Some(b'-') | Some(b'+')) {
                self.at += 1;
            }
            while matches!(self.peek(), Some(byte) if byte.is_ascii_digit()) {
                self.at += 1;
            }
        }

        self.source[start..self.at]
            .parse::<f64>()
            .map(Value::Number)
            .map_err(|_| {
                let at = start;
                ParamError::Syntax {
                    at,
                    message: format!("'{}' is not a number", &self.source[start..self.at]),
                }
            })
    }

    fn string(&mut self) -> Result<String, ParamError> {
        // Caller has already seen the opening quote.
        self.at += 1;
        let mut out = String::new();

        loop {
            match self.peek() {
                None => return Err(self.error("unterminated string")),
                Some(b'"') => {
                    self.at += 1;
                    return Ok(out);
                }
                Some(b'\\') => {
                    self.at += 1;
                    match self.peek() {
                        Some(b'"') => out.push('"'),
                        Some(b'\\') => out.push('\\'),
                        Some(b'n') => out.push('\n'),
                        Some(b't') => out.push('\t'),
                        Some(_) => return Err(self.error("unknown escape")),
                        None => return Err(self.error("unterminated escape")),
                    }
                    self.at += 1;
                }
                Some(_) => {
                    // Step a whole character, not a byte: the rest of the
                    // grammar is ASCII but a string's contents need not be.
                    let rest = &self.source[self.at..];
                    let character = rest.chars().next().expect("non-empty by the match");
                    out.push(character);
                    self.at += character.len_utf8();
                }
            }
        }
    }

    fn peek(&self) -> Option<u8> {
        self.text.get(self.at).copied()
    }

    fn eat(&mut self, byte: u8) -> bool {
        if self.peek() == Some(byte) {
            self.at += 1;
            true
        } else {
            false
        }
    }

    /// Matches a bare word, but only where a delimiter follows it — so `truex`
    /// is not read as `true` with trailing rubbish.
    fn word(&mut self, word: &str) -> bool {
        let end = self.at + word.len();
        if self.source.get(self.at..end) != Some(word) {
            return false;
        }
        if matches!(self.text.get(end), Some(byte) if byte.is_ascii_alphanumeric() || *byte == b'_')
        {
            return false;
        }

        self.at = end;
        true
    }

    fn space(&mut self) {
        while matches!(self.peek(), Some(byte) if byte.is_ascii_whitespace()) {
            self.at += 1;
        }
    }

    fn error(&self, message: &str) -> ParamError {
        ParamError::Syntax {
            at: self.at,
            message: message.to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_a_json_object() {
        let params =
            Params::parse(r#"{ "threshold_db": -18.5, "ratio": 4, "auto": true, "mode": "peak" }"#)
                .expect("valid");

        assert_eq!(params.number("threshold_db"), Some(-18.5));
        assert_eq!(params.number("ratio"), Some(4.0));
        assert_eq!(params.bool("auto"), Some(true));
        assert_eq!(params.text("mode"), Some("peak"));
    }

    #[test]
    fn braces_and_quotes_are_optional() {
        let params = Params::parse("threshold_db: -18, ratio: 4").expect("valid");
        assert_eq!(params.number("threshold_db"), Some(-18.0));
        assert_eq!(params.number("ratio"), Some(4.0));
    }

    #[test]
    fn equals_reads_as_a_colon() {
        let params = Params::parse("gain_db = -6").expect("valid");
        assert_eq!(params.number("gain_db"), Some(-6.0));
    }

    #[test]
    fn a_trailing_comma_is_allowed() {
        let params = Params::parse("{ a: 1, b: 2, }").expect("valid");
        assert_eq!(params.len(), 2);
    }

    #[test]
    fn an_empty_object_is_an_empty_set() {
        assert!(Params::parse("{}").expect("valid").is_empty());
        assert!(Params::parse("").expect("valid").is_empty());
        assert!(Params::parse("   ").expect("valid").is_empty());
    }

    #[test]
    fn reads_exponents_and_signs() {
        let params = Params::parse("a: 1e3, b: -2.5e-2, c: +7").expect("valid");
        assert_eq!(params.number("a"), Some(1000.0));
        assert_eq!(params.number("b"), Some(-0.025));
        assert_eq!(params.number("c"), Some(7.0));
    }

    #[test]
    fn the_last_value_for_a_name_wins() {
        let params = Params::parse("gain_db: 0, gain_db: -6").expect("valid");
        assert_eq!(params.number("gain_db"), Some(-6.0));
    }

    #[test]
    fn round_trips_through_display() {
        let original =
            Params::parse(r#"{"a": 1.5, "b": true, "c": "text", "d": -2}"#).expect("valid");
        let printed = original.to_string();
        let reparsed = Params::parse(&printed).expect("its own output");

        assert_eq!(original, reparsed, "printed as {printed}");
    }

    #[test]
    fn an_empty_set_prints_as_an_empty_object() {
        assert_eq!(Params::new().to_string(), "{}");
        assert_eq!(Params::parse("{}").unwrap(), Params::new());
    }

    #[test]
    fn quotes_in_a_value_survive_a_round_trip() {
        let params = Params::new().with("mode", "a \"quoted\" word");
        let reparsed = Params::parse(&params.to_string()).expect("its own output");
        assert_eq!(reparsed.text("mode"), Some("a \"quoted\" word"));
    }

    #[test]
    fn rejects_what_is_not_a_parameter_object() {
        for bad in [
            "{",
            "{ a: 1",
            "a:",
            "a: ,",
            ": 1",
            "a: 1 b: 2",
            "{ a: 1 } trailing",
            "a: [1, 2]",
            "a: null",
            "a: \"unterminated",
            "a: truex",
        ] {
            assert!(
                Params::parse(bad).is_err(),
                "{bad:?} should not have parsed"
            );
        }
    }

    #[test]
    fn a_syntax_error_says_where() {
        let Err(ParamError::Syntax { at, .. }) = Params::parse("ratio: 4, : 2") else {
            panic!("expected a syntax error");
        };
        assert_eq!(at, 10, "should point at the missing name");
    }

    #[test]
    fn an_f32_does_not_widen_into_noise() {
        // The whole point: `1.4f32 as f64` is 1.399999976158142.
        assert_eq!(Value::from_f32(1.4), Value::Number(1.4));
        assert_eq!(Value::from_f32(0.3), Value::Number(0.3));
        assert_eq!(Value::from_f32(-24.0), Value::Number(-24.0));
        assert_eq!(Value::from_f32(1.4).to_string(), "1.4");
    }

    #[test]
    fn numbers_and_booleans_convert_between_each_other() {
        assert_eq!(Value::Bool(true).as_number(), Some(1.0));
        assert_eq!(Value::Number(0.0).as_bool(), Some(false));
        assert_eq!(Value::Number(0.5).as_bool(), Some(true));
        // Text does not, in either direction: a mode word is not a quantity.
        assert_eq!(Value::Text("4".into()).as_number(), None);
        assert_eq!(Value::Number(1.0).as_text(), None);
    }

    #[test]
    fn a_spec_checks_range_and_kind() {
        let spec = ParamSpec::number("ratio", "", 1.0, 20.0, 4.0, "");

        assert_eq!(spec.check(&Value::Number(4.0)), Ok(4.0));
        assert!(matches!(
            spec.check(&Value::Number(40.0)),
            Err(ParamError::Range { .. })
        ));
        assert!(matches!(
            spec.check(&Value::Text("loud".into())),
            Err(ParamError::Type { .. })
        ));

        // The bounds themselves are inside the range.
        assert_eq!(spec.check(&Value::Number(1.0)), Ok(1.0));
        assert_eq!(spec.check(&Value::Number(20.0)), Ok(20.0));
    }

    #[test]
    fn a_toggle_takes_a_bool_or_a_number() {
        let spec = ParamSpec::toggle("bypass", false, "");

        assert_eq!(spec.check(&Value::Bool(true)), Ok(1.0));
        assert_eq!(spec.check(&Value::Number(0.0)), Ok(0.0));
        assert!(matches!(
            spec.check(&Value::Text("on".into())),
            Err(ParamError::Type { .. })
        ));
    }

    #[test]
    fn an_unknown_parameter_lists_what_there_is() {
        let error = ParamError::Unknown {
            key: "ratioo".into(),
            known: vec!["ratio".into(), "threshold_db".into()],
        };

        let message = error.to_string();
        assert!(message.contains("ratioo"), "{message}");
        assert!(message.contains("ratio, threshold_db"), "{message}");
    }
}