koan-core 0.60.3

Core library for koan — bit-perfect music player. Audio engine, player, database, format strings.
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
//! Headphone target curves, and moving an AutoEQ correction from the target it
//! was made for to another.
//!
//! An AutoEQ result corrects a headphone to one target, on the rig it was
//! measured on. Its CSV carries that target, rig compensation and all. Moving
//! it to another target adds one curve after the correction: the difference
//! between the target chosen and the one the result was made for. Both are
//! taken from the same reference set, the files in `targets/` (see
//! `SOURCES.md`), so whatever compensation the result's rig carries is common
//! to both and cancels. The result's own target only says which of the set it
//! was made for, and when it matches none of them closely, no other target is
//! offered: a difference taken across rigs would correct the rig, not the
//! taste.
//!
//! The curve runs as a `graphic` filter, which `steps` makes a
//! minimum-phase FIR at the output rate.

use std::path::{Path, PathBuf};

use crate::config::{self, GraphicEq};

/// Which kind of headphone a target is for. A target for one is not offered
/// for the other: the measurements behind them differ.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ear {
    Over,
    In,
}

/// A target that ships with koan.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Target {
    pub id: &'static str,
    pub name: &'static str,
    /// What it does, in a few plain words: neutral, or what it adds.
    pub does: &'static str,
    /// What it sounds like, in a line.
    pub character: &'static str,
    pub ear: Ear,
    data: &'static str,
}

/// The targets offered, over-ear then in-ear: neutral first, then each
/// preference added to it, the most chosen first.
pub const TARGETS: &[Target] = &[
    Target {
        id: "diffuse-field-gras-kemar",
        name: "Neutral (diffuse field)",
        does: "flat, no preference",
        character: "Neutral: sound arriving evenly from every direction, as a room without reflections would give, with no bass or treble preference. Brighter than Harman.",
        ear: Ear::Over,
        data: include_str!("targets/diffuse-field-gras-kemar.csv"),
    },
    Target {
        id: "harman-over-ear-2018",
        name: "Harman over-ear 2018",
        does: "neutral plus Harman's bass shelf and brighter treble",
        character: "What most listeners in Harman's research preferred: a warm bass shelf, a forward upper midrange and a soft top end.",
        ear: Ear::Over,
        data: include_str!("targets/harman-over-ear-2018.csv"),
    },
    Target {
        id: "harman-over-ear-2018-without-bass",
        name: "Harman over-ear 2018, no bass shelf",
        does: "Harman's treble, no extra bass",
        character: "Harman's curve with a flat low end: leaner bass, the same mids and treble.",
        ear: Ear::Over,
        data: include_str!("targets/harman-over-ear-2018-without-bass.csv"),
    },
    Target {
        id: "oratory1990-over-ear",
        name: "oratory1990 over-ear",
        does: "oratory1990's preference, close to Harman's",
        character: "oratory1990's target for over-ears, close to Harman's.",
        ear: Ear::Over,
        data: include_str!("targets/oratory1990-over-ear.csv"),
    },
    Target {
        id: "diffuse-field-iso-11904-1",
        name: "Neutral (diffuse field)",
        does: "flat, no preference",
        character: "Neutral: the ear's response to sound arriving evenly from every direction (ISO 11904-1), with no bass or treble preference.",
        ear: Ear::In,
        data: include_str!("targets/diffuse-field-iso-11904-1.csv"),
    },
    Target {
        id: "harman-in-ear-2019",
        name: "Harman in-ear 2019",
        does: "neutral plus Harman's bass shelf and brighter treble",
        character: "Harman's in-ear preference target: a bigger bass shelf than over-ear, and more treble.",
        ear: Ear::In,
        data: include_str!("targets/harman-in-ear-2019.csv"),
    },
    Target {
        id: "harman-in-ear-2019-without-bass",
        name: "Harman in-ear 2019, no bass shelf",
        does: "Harman's treble, no extra bass",
        character: "Harman's in-ear curve with a flat low end.",
        ear: Ear::In,
        data: include_str!("targets/harman-in-ear-2019-without-bass.csv"),
    },
    Target {
        id: "autoeq-in-ear",
        name: "AutoEQ in-ear",
        does: "AutoEQ's own in-ear neutral",
        character: "AutoEQ's own in-ear target.",
        ear: Ear::In,
        data: include_str!("targets/autoeq-in-ear.csv"),
    },
    Target {
        id: "oratory1990-in-ear",
        name: "oratory1990 in-ear",
        does: "oratory1990's preference",
        character: "oratory1990's target for in-ears.",
        ear: Ear::In,
        data: include_str!("targets/oratory1990-in-ear.csv"),
    },
];

/// A result's own target is taken for one of `TARGETS` when it is within this
/// much of it, RMS over the whole band, both levelled at 1 kHz. A result made
/// for one of them on another rig stays within a decibel or so; one made for
/// something else is well past this.
const SAME_TARGET_DB: f64 = 1.5;

/// The furthest a target difference moves any frequency.
const MAX_DB: f64 = 12.0;

/// How much nearer the best match must be than the next for a result to be
/// taken for it. Shipped targets differ by less than a decibel in places,
/// and a guess between two would apply the wrong difference.
const MARGIN_DB: f64 = 0.3;

/// The level a curve's point may have. Targets sit within about ±15 dB; a
/// value past this is a file that is not one, and is refused rather than
/// carried into a filter.
const LEVEL_LIMIT_DB: f64 = 40.0;

/// The largest file read as a target.
const FILE_CAP: u64 = 1 << 20;

impl Ear {
    /// The kind of headphone an AutoEQ result is for, from its path in the
    /// results: `…/over-ear/…`, `…/711 in-ear/…`, `…/earbud/…`.
    pub fn of_result_path(path: &str) -> Option<Self> {
        let lower = path.to_ascii_lowercase();
        if lower.contains("in-ear") || lower.contains("earbud") {
            Some(Ear::In)
        } else if lower.contains("over-ear") || lower.contains("on-ear") {
            Some(Ear::Over)
        } else {
            None
        }
    }
}

/// A curve: (Hz, dB), rising in frequency.
pub type Curve = Vec<(f64, f64)>;

/// Read a curve: two numbers per line, frequency and level, separated by a
/// comma, a tab or spaces, as AutoEQ's CSVs, REW exports and squig.link's
/// text have them. Header and comment lines are skipped, and so is a point
/// that is not a frequency in hertz with a level within ±40 dB.
pub fn parse(text: &str) -> Curve {
    let mut curve = points(text);
    curve.retain(|(_, db)| db.abs() <= LEVEL_LIMIT_DB);
    curve
}

/// Every frequency and level pair in `text`, at whatever level: a
/// measurement in dB SPL sits around 90.
pub(crate) fn points(text: &str) -> Curve {
    let mut curve: Curve = text
        .lines()
        .filter_map(|line| {
            let mut fields = line
                .split(|c: char| c == ',' || c == ';' || c.is_whitespace())
                .filter(|f| !f.is_empty());
            let hz: f64 = fields.next()?.parse().ok()?;
            let db: f64 = fields.next()?.parse().ok()?;
            (hz > 0.0 && hz.is_finite() && db.is_finite()).then_some((hz, db))
        })
        .collect();
    curve.sort_by(|a, b| a.0.total_cmp(&b.0));
    curve.dedup_by(|a, b| a.0 == b.0);
    curve
}

impl Target {
    pub fn curve(&self) -> Curve {
        parse(self.data)
    }
}

pub fn shipped(id: &str) -> Option<&'static Target> {
    TARGETS.iter().find(|t| t.id == id)
}

/// The level of `curve` at `hz`, interpolated against log frequency and held
/// past either end.
pub(crate) fn at(curve: &[(f64, f64)], hz: f64) -> f64 {
    let (Some(first), Some(last)) = (curve.first(), curve.last()) else {
        return 0.0;
    };
    if hz <= first.0 {
        return first.1;
    }
    if hz >= last.0 {
        return last.1;
    }
    let i = curve.partition_point(|p| p.0 <= hz);
    let ((f0, g0), (f1, g1)) = (curve[i - 1], curve[i]);
    g0 + (g1 - g0) * (hz / f0).ln() / (f1 / f0).ln()
}

/// AutoEQ's grid: what the shipped targets and every result use, and what
/// responses are drawn on.
pub fn grid() -> Vec<f64> {
    TARGETS[0].curve().into_iter().map(|(hz, _)| hz).collect()
}

/// `curve` on `grid`, levelled to 0 dB at 1 kHz.
fn levelled(curve: &[(f64, f64)], grid: &[f64]) -> Vec<f64> {
    let k = at(curve, 1000.0);
    grid.iter().map(|&hz| at(curve, hz) - k).collect()
}

/// Which of `TARGETS` for `ear` a result was made for, from the target its
/// CSV carries: the nearest, when it is close and clearly nearer than the
/// next. Targets for the other kind of headphone are not compared, since
/// some lie within a decibel of each other.
pub fn identify(result_target: &[(f64, f64)], ear: Ear) -> Option<&'static Target> {
    if result_target.len() < 2
        || result_target
            .iter()
            .any(|(f, d)| !f.is_finite() || !d.is_finite())
    {
        return None;
    }
    let grid = grid();
    let theirs = levelled(result_target, &grid);
    let rms = |t: &Target| {
        let ours = levelled(&t.curve(), &grid);
        let sum: f64 = theirs.iter().zip(&ours).map(|(a, b)| (a - b).powi(2)).sum();
        (sum / grid.len() as f64).sqrt()
    };
    let mut near: Vec<(f64, &'static Target)> = TARGETS
        .iter()
        .filter(|t| t.ear == ear)
        .map(|t| (rms(t), t))
        .collect();
    near.sort_by(|a, b| a.0.total_cmp(&b.0));
    match near.as_slice() {
        [(best, t), rest @ ..]
            if *best <= SAME_TARGET_DB
                && rest
                    .first()
                    .is_none_or(|(next, _)| next - best >= MARGIN_DB) =>
        {
            Some(t)
        }
        _ => None,
    }
}

/// The curve that moves a correction made for `from` to `to`: their
/// difference, levelled at 1 kHz, smoothed over a twelfth of an octave so the
/// FIR follows the shape rather than each wiggle, held within ±12 dB, and
/// sampled every twelfth of an octave, which the graphic filter interpolates
/// between.
pub fn difference(from: &[(f64, f64)], to: &[(f64, f64)]) -> GraphicEq {
    let grid = grid();
    let (a, b) = (levelled(from, &grid), levelled(to, &grid));
    // A curve that is not a target's makes no difference at all, rather than
    // one that is not a number.
    let delta: Vec<f64> = b
        .iter()
        .zip(&a)
        .map(|(t, f)| t - f)
        .map(|d| if d.is_finite() { d } else { 0.0 })
        .collect();
    let smoothed: Vec<f64> = grid
        .iter()
        .map(|&hz| {
            let (lo, hi) = (hz / 2f64.powf(1.0 / 24.0), hz * 2f64.powf(1.0 / 24.0));
            let near: Vec<f64> = grid
                .iter()
                .zip(&delta)
                .filter(|(f, _)| (lo..=hi).contains(*f))
                .map(|(_, d)| *d)
                .collect();
            near.iter().sum::<f64>() / near.len().max(1) as f64
        })
        .collect();
    let mut points = Vec::new();
    let mut next = 20.0;
    for (&hz, &db) in grid.iter().zip(&smoothed) {
        if hz >= next || Some(&hz) == grid.last() {
            let db = if db.is_nan() { 0.0 } else { db };
            points.push((hz, db.clamp(-MAX_DB, MAX_DB)));
            next = hz * 2f64.powf(1.0 / 12.0);
        }
    }
    GraphicEq {
        points,
        channels: Vec::new(),
    }
}

// --- Targets a person adds ---------------------------------------------------

/// Where added targets are kept, one CSV each.
fn added_dir() -> PathBuf {
    config::config_dir().join("dsp").join("targets")
}

/// An id for an added target, which `choice_curve` reads back.
const ADDED: &str = "added:";

/// A target a person added: its id and name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Added {
    pub id: String,
    pub name: String,
}

/// The targets a person has added, by name.
pub fn added() -> Vec<Added> {
    let Ok(dir) = std::fs::read_dir(added_dir()) else {
        return Vec::new();
    };
    let mut out: Vec<Added> = dir
        .filter_map(|e| e.ok())
        .filter_map(|e| {
            let name = e.path().file_stem()?.to_string_lossy().into_owned();
            (e.path().extension()? == "csv").then(|| Added {
                id: format!("{ADDED}{name}"),
                name,
            })
        })
        .collect();
    out.sort_by_key(|a| a.name.to_lowercase());
    out
}

/// Add a target from a file: a CSV of frequency and level, or a squig.link
/// export. Kept on AutoEQ's grid, named after the file. Refused unless it
/// covers the audible band well enough to take a difference from: points
/// from below 100 Hz to above 10 kHz, at least twenty of them.
pub fn add(path: &Path) -> Result<Added, String> {
    use std::io::Read as _;
    let file = std::fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?;
    let mut bytes = Vec::new();
    file.take(FILE_CAP + 1)
        .read_to_end(&mut bytes)
        .map_err(|e| format!("{}: {e}", path.display()))?;
    if bytes.len() as u64 > FILE_CAP {
        return Err("A target file is a few kilobytes; this one is over a megabyte".into());
    }
    let text = String::from_utf8_lossy(&bytes);
    let curve = covering(&text, "A target")?;
    let name: String = path
        .file_stem()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_else(|| "Target".into())
        .chars()
        .filter(|c| !matches!(c, '/' | '\\' | ':'))
        .collect();
    let name = name.trim().to_owned();
    if name.is_empty() || name.starts_with('.') {
        return Err("The file needs a name to call the target by".into());
    }
    let dir = added_dir();
    std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
    std::fs::write(dir.join(format!("{name}.csv")), on_grid(&curve)).map_err(|e| e.to_string())?;
    Ok(Added {
        id: format!("{ADDED}{name}"),
        name,
    })
}

/// The curve in `text`, if it covers the audible band well enough to correct
/// from or to: points from below 100 Hz to above 10 kHz, at least twenty.
/// `what` names it in the refusal.
pub fn covering(text: &str, what: &str) -> Result<Curve, String> {
    if text.len() as u64 > FILE_CAP {
        return Err(format!(
            "{what} is a few kilobytes; this is over a megabyte"
        ));
    }
    // Levelled at 1 kHz before anything is checked: REW and squig.link
    // export absolute levels, which only the shape of matters here.
    let mut curve = points(text);
    let k = at(&curve, 1000.0);
    for (_, db) in &mut curve {
        *db -= k;
    }
    curve.retain(|(_, db)| db.abs() <= LEVEL_LIMIT_DB);
    let (Some(first), Some(last)) = (curve.first(), curve.last()) else {
        return Err("No frequency and level pairs in it".into());
    };
    if curve.len() < 20 || first.0 > 100.0 || last.0 < 10_000.0 {
        return Err(format!(
            "{what} needs points from below 100 Hz to above 10 kHz, at least twenty of them"
        ));
    }
    Ok(curve)
}

/// `curve` on AutoEQ's grid, as a CSV of frequency and level.
pub fn on_grid(curve: &[(f64, f64)]) -> String {
    let mut out = String::from("frequency,raw\n");
    for hz in grid() {
        out.push_str(&format!("{hz:.2},{:.2}\n", at(curve, hz)));
    }
    out
}

/// Where a correction built from a measurement keeps it.
pub fn measurement_path(dsp_dir: &Path) -> PathBuf {
    dsp_dir.join("measurement.csv")
}

/// The headphone's measurement kept in `dsp_dir`, if there is one.
pub fn measurement(dsp_dir: &Path) -> Option<Curve> {
    let text = std::fs::read_to_string(measurement_path(dsp_dir)).ok()?;
    Some(parse(&text)).filter(|c| !c.is_empty())
}

/// Above here, measurements of one headphone on different rigs disagree
/// most, and a correction is held to `TREBLE_DB`; it narrows to that from
/// `TREBLE_FROM` on.
const TREBLE_FROM: f64 = 6_000.0;
const TREBLE_AT: f64 = 10_000.0;
const TREBLE_DB: f64 = 3.0;

/// What brings a headphone measured as `measurement` to `target`: their
/// difference, made as a target swap's is (levelled at 1 kHz, smoothed,
/// within ±12 dB), and held to ±3 dB in the treble, where a measurement
/// says least about the headphone and most about the rig.
pub fn correction(measurement: &[(f64, f64)], target: &[(f64, f64)]) -> GraphicEq {
    let mut g = difference(measurement, target);
    for (hz, db) in &mut g.points {
        let limit = if *hz <= TREBLE_FROM {
            MAX_DB
        } else if *hz >= TREBLE_AT {
            TREBLE_DB
        } else {
            let t = (*hz / TREBLE_FROM).ln() / (TREBLE_AT / TREBLE_FROM).ln();
            MAX_DB + (TREBLE_DB - MAX_DB) * t
        };
        *db = db.clamp(-limit, limit);
    }
    g
}

/// What an AutoEQ install in `dsp_dir` kept of its result: the headphone as
/// measured, and the target it was corrected to, on the rig it was measured
/// on.
pub fn autoeq_measurement(dsp_dir: &Path) -> Option<(Curve, Curve)> {
    let text = std::fs::read_to_string(result_path(dsp_dir)).ok()?;
    let (raw, target) = (result_column(&text, "raw"), result_column(&text, "target"));
    (!raw.is_empty() && !target.is_empty()).then_some((raw, target))
}

/// `target` moved by `step`: a result's own target, rig and all, taken to
/// another target by the two targets' difference, which is what a rebuilt
/// correction aims at.
pub fn moved(target: &[(f64, f64)], step: &GraphicEq) -> Curve {
    target
        .iter()
        .map(|&(hz, db)| (hz, db + at(&step.points, hz)))
        .collect()
}

/// Whether two targets are for the same kind of headphone. A target added
/// by hand says nothing of it, and is taken at its word.
pub fn same_ear(a: &str, b: &str) -> bool {
    match (shipped(a), shipped(b)) {
        (Some(a), Some(b)) => a.ear == b.ear,
        _ => true,
    }
}

/// The curve a chosen target id names: one that ships, or one added.
pub fn choice_curve(id: &str) -> Option<Curve> {
    if let Some(t) = shipped(id) {
        return Some(t.curve());
    }
    let name = id.strip_prefix(ADDED)?;
    if name.contains(['/', '\\']) || name.starts_with('.') {
        return None;
    }
    let text = std::fs::read_to_string(added_dir().join(format!("{name}.csv"))).ok()?;
    Some(parse(&text)).filter(|c| !c.is_empty())
}

/// Where a profile installed from AutoEQ keeps its result's CSV.
pub fn result_path(dsp_dir: &Path) -> PathBuf {
    dsp_dir.join("autoeq.csv")
}

/// A column of an AutoEQ result CSV, as a curve.
pub fn result_column(text: &str, column: &str) -> Curve {
    let mut lines = text.lines();
    let Some(header) = lines.next() else {
        return Vec::new();
    };
    let names: Vec<&str> = header.split(',').map(str::trim).collect();
    let (Some(f), Some(c)) = (
        names.iter().position(|n| *n == "frequency"),
        names.iter().position(|n| *n == column),
    ) else {
        return Vec::new();
    };
    lines
        .filter_map(|line| {
            let fields: Vec<&str> = line.split(',').collect();
            let hz: f64 = fields.get(f)?.trim().parse().ok()?;
            let db: f64 = fields.get(c)?.trim().parse().ok()?;
            Some((hz, db))
        })
        .collect()
}

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

    #[test]
    fn every_target_is_on_autoeqs_grid() {
        let grid = grid();
        assert_eq!(grid.len(), 695);
        assert_eq!(grid[0], 20.0);
        assert!(*grid.last().unwrap() > 19_900.0);
        for t in TARGETS {
            let c = t.curve();
            assert_eq!(c.len(), 695, "{}", t.id);
            assert!(c.iter().zip(&grid).all(|((f, _), g)| f == g), "{}", t.id);
        }
    }

    /// REW and squig.link export dB SPL: read for its shape, levelled at
    /// 1 kHz, as the measurement flow's own example is written.
    #[test]
    fn a_measurement_in_db_spl_is_read() {
        let mut text = String::from("Frequency(Hz), SPL(dB)\n");
        for hz in grid() {
            let db = 92.0 + if hz < 100.0 { 6.0 } else { 0.0 };
            text.push_str(&format!("{hz:.2}, {db:.1}\n"));
        }
        let curve = covering(&text, "A measurement").unwrap();
        assert_eq!(curve.len(), 695);
        assert!(at(&curve, 1000.0).abs() < 1e-9);
        assert!((at(&curve, 50.0) - 6.0).abs() < 1e-9);
        assert!(
            covering("20, 92.4\n21, 92.6\n", "A measurement").is_err(),
            "too few"
        );
    }

    #[test]
    fn a_result_is_known_by_its_target_and_its_rigs_offset_is_tolerated() {
        let harman = shipped("harman-over-ear-2018").unwrap();
        assert_eq!(
            identify(&harman.curve(), Ear::Over).map(|t| t.id),
            Some(harman.id)
        );
        // A rig's compensation: a dip of a few dB around 6 kHz, as AutoEQ's
        // Rtings and Innerfidelity results carry.
        let rig: Curve = harman
            .curve()
            .into_iter()
            .map(|(hz, db)| {
                let x = (hz / 6000.0).log2();
                (hz, db - 3.0 * (-x * x * 4.0).exp())
            })
            .collect();
        assert_eq!(identify(&rig, Ear::Over).map(|t| t.id), Some(harman.id));
        let no_bass = shipped("harman-over-ear-2018-without-bass").unwrap();
        assert_eq!(
            identify(&no_bass.curve(), Ear::Over).map(|t| t.id),
            Some(no_bass.id)
        );
        // Something none of them is near.
        let tilted: Curve = harman
            .curve()
            .into_iter()
            .map(|(hz, db)| (hz, db + 6.0 * (hz / 1000.0).log2()))
            .collect();
        assert_eq!(identify(&tilted, Ear::Over), None);
    }

    /// An in-ear result is never taken for an over-ear target however near
    /// one lies, and a result between two targets is taken for neither.
    #[test]
    fn identification_keeps_to_the_ear_and_refuses_to_guess() {
        let in_ear = shipped("harman-in-ear-2019").unwrap();
        assert_eq!(
            identify(&in_ear.curve(), Ear::In).map(|t| t.id),
            Some(in_ear.id)
        );
        // AutoEQ's in-ear target lies within a decibel of Harman over-ear
        // 2018 without its bass shelf; as an in-ear result it is itself.
        let autoeq = shipped("autoeq-in-ear").unwrap();
        assert_eq!(
            identify(&autoeq.curve(), Ear::In).map(|t| t.id),
            Some(autoeq.id)
        );
        // Halfway between Harman 2018 with and without its bass shelf.
        let with = shipped("harman-over-ear-2018").unwrap().curve();
        let without = shipped("harman-over-ear-2018-without-bass")
            .unwrap()
            .curve();
        let between: Curve = with
            .iter()
            .zip(&without)
            .map(|((f, a), (_, b))| (*f, (a + b) / 2.0))
            .collect();
        assert_eq!(identify(&between, Ear::Over), None);
        assert_eq!(
            Ear::of_result_path("crinacle/711 in-ear/1Custom SA02"),
            Some(Ear::In)
        );
        assert_eq!(
            Ear::of_result_path("oratory1990/over-ear/Sennheiser HD 650"),
            Some(Ear::Over)
        );
        assert_eq!(
            Ear::of_result_path("Rtings/HMS II.3 over-ear/X"),
            Some(Ear::Over)
        );
        assert_eq!(Ear::of_result_path("someone/elsewhere/X"), None);
    }

    /// A file of garbage cannot put anything but numbers into a filter.
    #[test]
    fn a_hostile_curve_cannot_make_a_filter_that_is_not_a_number() {
        let hostile = "20000,-3\n20,nan\n100,inf\n200,-inf\n300,1.7e308\n400,-1.7e308\n500,-50\n1000,0\n50,2\n";
        let curve = parse(hostile);
        assert_eq!(curve, vec![(50.0, 2.0), (1000.0, 0.0), (20000.0, -3.0)]);
        let from = shipped("harman-over-ear-2018").unwrap().curve();
        let g = difference(
            &from,
            &[(20.0, f64::MAX), (21.0, -f64::MAX), (20000.0, 0.0)],
        );
        assert!(g.points.iter().all(|(f, d)| f.is_finite() && d.is_finite()));
    }

    #[test]
    fn the_difference_is_the_targets_apart_and_keeps_the_rigs_part() {
        let from = shipped("harman-over-ear-2018").unwrap().curve();
        let to = shipped("harman-over-ear-2018-without-bass")
            .unwrap()
            .curve();
        let g = difference(&from, &to);
        let level = |hz: f64| at(&g.points, hz);
        assert!(level(1000.0).abs() < 0.2, "level at 1 kHz");
        assert!(
            level(30.0) < -3.0,
            "the bass shelf comes off: {}",
            level(30.0)
        );
        assert!(level(4000.0).abs() < 0.5, "the rest is alike");
        // Points every twelfth of an octave from 20 Hz: about 120.
        assert!((110..=125).contains(&g.points.len()), "{}", g.points.len());
        assert!(g.points.iter().all(|(_, db)| db.abs() <= MAX_DB));
        // The same target: no difference.
        assert!(
            difference(&from, &from)
                .points
                .iter()
                .all(|(_, db)| db.abs() < 1e-9)
        );
    }

    #[test]
    fn curves_are_read_from_csv_or_squiglink_text() {
        let csv = "frequency,raw\n20,1.5\n1000,0\n20000,-3\n";
        assert_eq!(
            parse(csv),
            vec![(20.0, 1.5), (1000.0, 0.0), (20000.0, -3.0)]
        );
        let squig = "* squig.link target\n20\t1.5\n1000\t0\n20000 -3\n";
        assert_eq!(parse(squig), parse(csv));
        let result = "frequency,raw,target\n20,-6,3.3\n1000,0,0\n";
        assert_eq!(
            result_column(result, "target"),
            vec![(20.0, 3.3), (1000.0, 0.0)]
        );
    }

    #[test]
    fn an_added_target_is_kept_on_the_grid_and_read_back() {
        let _guard = crate::config::tests::PERSIST_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let dir = tempfile::tempdir().unwrap();
        config::set_config_dir(dir.path());
        let file = dir.path().join("Super Warm.txt");
        let mut text = String::from("Frequency\tdB\n");
        for i in 0..40 {
            let hz = 20.0 * 2f64.powf(i as f64 / 4.0);
            text.push_str(&format!("{hz}\t{}\n", 10.0 - i as f64 / 4.0));
        }
        std::fs::write(&file, text).unwrap();
        let added = add(&file).unwrap();
        assert_eq!(added.name, "Super Warm");
        assert_eq!(super::added(), vec![added.clone()]);
        let curve = choice_curve(&added.id).unwrap();
        assert_eq!(curve.len(), 695);
        assert!(choice_curve("added:../escape").is_none());

        let short = dir.path().join("short.csv");
        std::fs::write(&short, "100,0\n1000,0\n").unwrap();
        assert!(add(&short).is_err());
    }

    /// A correction moved to another target plays their difference after
    /// its own filters; moved back, or never moved, nothing is added.
    #[test]
    fn a_chosen_target_adds_one_curve_to_the_chain() {
        use crate::audio::dsp::profiles;
        use crate::config::{Config, DspFilter, DspProfile, DspTarget, EqFilter, EqFilterKind};
        let _guard = crate::config::tests::PERSIST_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let dir = tempfile::tempdir().unwrap();
        config::set_config_dir(dir.path());
        let band = DspFilter::Band(EqFilter {
            kind: EqFilterKind::Peaking,
            freq: 100.0,
            gain_db: 3.0,
            q: 1.0,
            channels: vec![],
        });
        Config::persist(|c| {
            c.dsp.profiles.push(DspProfile {
                name: "HD 650 (AutoEQ, oratory1990)".into(),
                filters: vec![band.clone()],
                target: Some(DspTarget {
                    made_for: "harman-over-ear-2018".into(),
                    chosen: None,
                }),
                ..Default::default()
            })
        })
        .unwrap();
        let name = "HD 650 (AutoEQ, oratory1990)";
        let loaded = || {
            let cfg = Config::cached();
            let p = cfg
                .dsp
                .profiles
                .iter()
                .find(|p| p.name == name)
                .unwrap()
                .clone();
            super::super::Setup::load(&p, &cfg.dsp.profiles, dir.path())
                .unwrap()
                .unwrap()
                .filters
        };
        assert_eq!(loaded(), vec![band.clone()]);

        let choices = profiles::target_choices(name).unwrap();
        assert_eq!(choices.made_for.unwrap().id, "harman-over-ear-2018");
        assert!(
            choices
                .choices
                .iter()
                .all(|c| shipped(&c.id).is_none_or(|t| t.ear == Ear::Over))
        );
        assert!(
            profiles::choose_target(name, Some("harman-in-ear-2019")).is_err(),
            "in-ear"
        );
        profiles::choose_target(name, Some("harman-over-ear-2018-without-bass")).unwrap();
        let filters = loaded();
        assert_eq!(filters.len(), 2);
        assert_eq!(filters[0], band);
        assert!(matches!(filters[1], DspFilter::Graphic(_)));

        // Its own target is no move at all.
        profiles::choose_target(name, Some("harman-over-ear-2018")).unwrap();
        assert_eq!(loaded(), vec![band.clone()]);
        profiles::choose_target(name, Some("diffuse-field-gras-kemar")).unwrap();
        profiles::choose_target(name, None).unwrap();
        assert_eq!(loaded(), vec![band]);
    }
}