drawbar 0.6.0

Your Nord's sounds, in a window: browse, edit and send programs, samples and pianos, in the browser or on the desktop
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
//! New → a Sample Editor project, a sample instrument or a piano library: some WAVs,
//! what each one was recorded at, and the `.nsmpproj`, `.nsmp` or `.npno` that comes
//! out of them.
//!
//! The editor's own *Import Auto…* is what the first two imitate — one zone per file,
//! ordered by root key, key ranges derived from the roots. Everything either format
//! needs beyond the audio is the root key, and a filename is the only place a guess at
//! one can come from, so the dialog exists to let that guess be corrected before
//! anything is made.
//!
//! A piano library asks for more per file — a bank and a velocity layer as well as a
//! root — and takes long enough to code that the build runs off the frame.
//!
//! ⚠️ A project holds **paths, not audio**. What lands in the list references the WAVs
//! by the names they were picked under, and the editor looks for them beside the
//! project file. An instrument and a library hold the audio itself, which is why they
//! take the encoder's limits on what a WAV may be.

use eframe::egui;
use nord_format::formats::npno::encode::{
    build, parse_stroke_name, resample, Clash, Donor, Kind, LayerTag, Options, Recording, Rules,
    Stem, HIGHEST_PLAYED_LAYER,
};
use nord_format::formats::npno::{Bank, Library};
use nord_format::formats::nsmp::codec::Layout;
use nord_format::formats::nsmp::zone::derive_top_notes;
use nord_format::formats::nsmp::{encode, MAX_NAME_LEN};
use nord_format::formats::nsmpproj::{
    project_frames, NewZone, Project, HIGHEST_NOTE, LOWEST_NOTE, PROJECT_RATE,
};
use nord_format::note;
use nord_format::wav::Pcm16;
use nord_format::Entity;

use crate::document::controls::fits;
use crate::document::encode::{refusal as encodable, Source};
use crate::document::note_picker;
use crate::log::Log;
use crate::work::{self, Job, Progress};
use crate::workspace::{Origin, Workspace};

/// Zones one draft can hold: every key the dialog lays a root on.
const MOST_ZONES: usize = (HIGHEST_NOTE - LOWEST_NOTE) as usize + 1;

/// The root a file that names none is taken to have been recorded at.
const MIDDLE_C: u8 = 60;

/// What a pick of WAVs is turned into.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Making {
    /// A `.nsmpproj`: the file names, and where each sits on the keyboard.
    Project,
    /// A `.nsmp`: the audio itself, one zone per file, at the generation that has been
    /// played on hardware. The document panel over a WAV is where a generation is
    /// chosen; this makes the one that plays.
    Instrument,
    /// A `.npno`: one stroke per file, resampled onto the lattice the piano section
    /// plays at and coded into the library.
    Piano,
}

impl Making {
    /// Everything a pick of WAVs makes, in the order the New menu offers them.
    pub const FROM_WAVS: [Making; 3] = [Making::Project, Making::Instrument, Making::Piano];

    /// What it makes, as the dialog, the file picker and the log name it.
    pub fn label(self) -> &'static str {
        match self {
            Making::Project => "Sample Editor project",
            Making::Instrument => "sample instrument",
            Making::Piano => "piano library",
        }
    }

    /// The New menu's own item, and what hovering it says.
    pub fn item(self) -> (&'static str, &'static str) {
        match self {
            Making::Project => (
                "Sample Editor project…",
                "pick the WAVs it plays; the project stores their names and the editor \
                 looks for them beside it",
            ),
            Making::Instrument => (
                "Sample instrument…",
                "pick the WAVs it plays; the audio is encoded into the instrument, so \
                 the files are not needed afterwards",
            ),
            Making::Piano => (
                "Piano library…",
                "pick one WAV per stroke; the audio is coded into the library, so the \
                 files are not needed afterwards",
            ),
        }
    }

    fn extension(self) -> &'static str {
        match self {
            Making::Project => nord_format::formats::nsmpproj::FORMAT,
            Making::Instrument => Layout::V2.extension(),
            Making::Piano => nord_format::formats::npno::FORMAT,
        }
    }

    fn caption(self) -> &'static str {
        match self {
            Making::Project => {
                "One zone per file, ordered by root key. The project stores the file \
                 names and the editor looks for them beside it, so keep them together."
            }
            Making::Instrument => {
                "One zone per file, ordered by root key. The audio is encoded into the \
                 instrument, so the WAVs are not needed afterwards."
            }
            Making::Piano => {
                "One stroke per WAV. A name like 060-b0-l00.wav sets the root, bank and \
                 layer; set them here otherwise. Kind, gain and the damper limit are \
                 edited in the document afterwards."
            }
        }
    }
}

/// One picked WAV, as the dialog shows it.
pub struct Take {
    /// The name a project would reference it by, resolved beside the project file.
    pub path: String,
    /// The file as it read, or the reader's own complaint.
    pub source: Source,
    /// Frames as a project counts them — see [`project_frames`]. Zero where the file
    /// did not read, or holds no audio.
    pub frames: u64,
    pub root_key: u8,
    /// Which of a piano library's three banks the stroke belongs to. A zone has no
    /// such thing.
    pub bank: Bank,
    /// Where the stroke sits among its root and bank's layers. A zone has no such
    /// thing.
    pub layer: LayerTag,
}

impl Take {
    fn new(path: String, bytes: &[u8], root_key: u8, bank: Bank, layer: LayerTag) -> Take {
        let source = Source::read(bytes);
        let frames = match &source {
            Source::Read(pcm) => project_frames(pcm.frames() as u64, pcm.rate).unwrap_or_default(),
            Source::Unreadable(_) => 0,
        };
        Take {
            path,
            source,
            frames,
            root_key,
            bank,
            layer,
        }
    }

    fn pcm(&self) -> Option<&Pcm16> {
        match &self.source {
            Source::Read(pcm) => Some(pcm),
            Source::Unreadable(_) => None,
        }
    }

    /// Why this file cannot be part of what is being made.
    ///
    /// A project references audio it never reads, so anything that holds some will do.
    /// An instrument carries it, and so takes the encoder's own limits. A library takes
    /// any rate — it resamples — and states the rest of its limits when it is coded.
    pub fn refusal(&self, making: Making) -> Option<String> {
        match making {
            Making::Instrument => encodable(&self.source),
            Making::Project => match &self.source {
                Source::Unreadable(why) => Some(why.clone()),
                Source::Read(_) => (self.frames == 0).then(|| "it holds no audio".to_string()),
            },
            Making::Piano => match &self.source {
                Source::Unreadable(why) => Some(why.clone()),
                Source::Read(_) => None,
            },
        }
    }
}

/// The picked files, waiting on their root keys.
pub struct Draft {
    pub making: Making,
    pub name: String,
    pub takes: Vec<Take>,
    /// The piano document a build donates its playback fields from, where one is
    /// chosen.
    pub template: Option<u64>,
    /// The build in flight, once Create has been pressed.
    job: Option<Job<Result<Built, String>>>,
    /// What the coder last refused, kept beside the takes so they can be fixed.
    refused: Option<String>,
}

/// The key each file is taken to have been recorded at.
///
/// A note name on the end of a filename, where **every** file carries a distinct one —
/// a run where one file disagrees is a guess worth not making. Otherwise a chromatic run
/// from middle C, pulled down where it would not fit under the highest key a project
/// maps.
pub fn default_roots(paths: &[String]) -> Vec<u8> {
    let named: Option<Vec<u8>> = paths.iter().map(|path| trailing_note(path)).collect();
    if let Some(named) = named {
        let mut sorted = named.clone();
        sorted.sort_unstable();
        sorted.dedup();
        if sorted.len() == named.len() {
            return named;
        }
    }
    // ⚠️ Counted in usize: a pick holds as many files as were picked, which is more than
    // a count of keys, and the run has to walk up from one end of the keyboard whatever
    // that count is.
    let after_first = paths.len().min(MOST_ZONES).saturating_sub(1);
    let start = u8::try_from(usize::from(HIGHEST_NOTE).saturating_sub(after_first))
        .unwrap_or(LOWEST_NOTE)
        .clamp(LOWEST_NOTE, MIDDLE_C);
    (start..=HIGHEST_NOTE)
        .chain(std::iter::repeat(HIGHEST_NOTE))
        .take(paths.len())
        .collect()
}

/// A note name on the end of a file's stem: `Marimba-C3.wav` is C3.
///
/// The token has to start with a letter, so a trailing `1` is a take number rather
/// than MIDI note 1.
fn trailing_note(path: &str) -> Option<u8> {
    let stem = path.rsplit_once('.').map_or(path, |(stem, _)| stem);
    let token = stem.rsplit(['-', '_', ' ']).next()?;
    token
        .chars()
        .next()
        .filter(char::is_ascii_alphabetic)
        .and_then(|_| note::parse(token).ok())
        .filter(|note| (LOWEST_NOTE..=HIGHEST_NOTE).contains(note))
}

/// The stroke a WAV's name states: `060-b0-l00`, as `nord piano build` reads it, and
/// `<stem>-060-b0-l00` as [`crate::workspace::stroke_wav_name`] writes it.
fn stroke_name(path: &str) -> Option<(u8, Bank, LayerTag)> {
    let stem = path.rsplit_once('.').map_or(path, |(stem, _)| stem);
    parse_stroke_name(stem, Stem::Any)
}

/// Whether a dropped file is one an open draft takes rather than a document to open.
/// The extension alone: a file that will not read is listed with the reader's own
/// complaint beside it.
pub fn is_wav_name(name: &str) -> bool {
    std::path::Path::new(name)
        .extension()
        .is_some_and(|e| e.eq_ignore_ascii_case("wav"))
}

/// What a picked WAV is taken to be a recording of: the stroke its name states, or a
/// root read off the end of the name and the loudest layer of the attack bank.
fn stroke_defaults(path: &str) -> (u8, Bank, LayerTag) {
    stroke_name(path).unwrap_or((
        trailing_note(path).unwrap_or(MIDDLE_C),
        Bank::Attack,
        LayerTag::Index(0),
    ))
}

/// The name a draft over these files starts under: the first file's stem, cut to what
/// an instrument's own name field holds.
fn draft_name(making: Making, paths: &[String]) -> String {
    let first = paths.first().map(String::as_str).unwrap_or_default();
    let stem = first.rsplit_once('.').map_or(first, |(stem, _)| stem);
    // A stroke's own name is the stroke, not the library: `Grand-060-b0-l00` opens on
    // `Grand`.
    let stem = match making == Making::Piano && stroke_name(stem).is_some() {
        true => stem.rsplitn(4, '-').nth(3).unwrap_or_default(),
        false => stem,
    };
    let stem = match stem.trim() {
        "" => "Untitled",
        stem => stem,
    };
    let mut name = stem.to_string();
    match making {
        Making::Project | Making::Piano => {}
        Making::Instrument => fits(&mut name, MAX_NAME_LEN),
    }
    name
}

impl Draft {
    /// What was picked, read for what a project needs to know about it.
    ///
    /// Nothing is refused whole here: a file that will not read is still listed, with
    /// the reason beside it, because the operator picked it on purpose.
    pub fn plan(making: Making, files: Vec<(String, Vec<u8>)>) -> Option<Draft> {
        if files.is_empty() {
            return None;
        }
        let paths: Vec<String> = files.iter().map(|(name, _)| name.clone()).collect();
        let takes = match making {
            Making::Piano => files
                .iter()
                .map(|(path, bytes)| {
                    let (root, bank, layer) = stroke_defaults(path);
                    Take::new(path.clone(), bytes, root, bank, layer)
                })
                .collect(),
            Making::Project | Making::Instrument => files
                .iter()
                .zip(default_roots(&paths))
                .map(|((path, bytes), root_key)| {
                    Take::new(
                        path.clone(),
                        bytes,
                        root_key,
                        Bank::Attack,
                        LayerTag::Index(0),
                    )
                })
                .collect(),
        };
        Some(Draft {
            making,
            name: draft_name(making, &paths),
            takes,
            template: None,
            job: None,
            refused: None,
        })
    }

    /// Take on more files, as a drop onto the open dialog does.
    pub fn add(&mut self, files: Vec<(String, Vec<u8>)>) {
        for (path, bytes) in files {
            let take = match self.making {
                Making::Piano => {
                    let (root, bank, layer) = stroke_defaults(&path);
                    Take::new(path, bytes.as_slice(), root, bank, layer)
                }
                Making::Project | Making::Instrument => {
                    let root = trailing_note(&path).unwrap_or_else(|| self.free_key());
                    Take::new(
                        path,
                        bytes.as_slice(),
                        root,
                        Bank::Attack,
                        LayerTag::Index(0),
                    )
                }
            };
            self.takes.push(take);
        }
    }

    /// The lowest key a project maps that no take is on: where a dropped file naming
    /// no key goes, since each zone needs a key of its own.
    fn free_key(&self) -> u8 {
        (LOWEST_NOTE..=HIGHEST_NOTE)
            .find(|key| self.takes.iter().all(|take| take.root_key != *key))
            .unwrap_or(HIGHEST_NOTE)
    }

    /// Why this draft cannot be made into anything yet, in the operator's words.
    pub fn refusal(&self) -> Option<String> {
        if let Some((take, why)) = self
            .takes
            .iter()
            .find_map(|take| Some((take, take.refusal(self.making)?)))
        {
            return Some(format!("{}: {why}", take.path));
        }
        // ⚠️ A library states every other rule about its strokes when it is coded, and
        // says so in its own words with the takes still here to be fixed.
        if self.making == Making::Piano {
            return layer_values(&self.takes).err();
        }
        if self.making == Making::Instrument && self.name.len() > MAX_NAME_LEN {
            return Some(format!(
                "the name is {} bytes — an instrument's own name field holds \
                 {MAX_NAME_LEN}",
                self.name.len()
            ));
        }
        if self.takes.len() > MOST_ZONES {
            return Some(format!(
                "{} files — one zone per file, and the dialog lays roots on the \
                 {MOST_ZONES} keys a project maps",
                self.takes.len()
            ));
        }
        if let Some(take) = self
            .takes
            .iter()
            .find(|take| !(LOWEST_NOTE..=HIGHEST_NOTE).contains(&take.root_key))
        {
            return Some(format!(
                "{} is set to {} — the keys run {} to {}",
                take.path,
                note::name(take.root_key),
                note::name(LOWEST_NOTE),
                note::name(HIGHEST_NOTE),
            ));
        }
        let mut roots: Vec<u8> = self.takes.iter().map(|take| take.root_key).collect();
        roots.sort_unstable();
        roots
            .windows(2)
            .find(|pair| pair[0] == pair[1])
            .map(|pair| {
                format!(
                    "two files are set to {} — each zone needs a key of its own",
                    note::name(pair[0])
                )
            })
    }

    /// The file the picked WAVs make, in the frame that asks for it.
    ///
    /// ⚠️ The two kinds a frame can make. Coding a library takes longer than one, so the
    /// dialog starts that build instead — [`Draft::begin`] — and nothing here can hand
    /// back a library.
    fn bytes(&self) -> Result<Vec<u8>, String> {
        match self.making {
            Making::Project => self.project(),
            Making::Instrument => self.instrument(),
            Making::Piano => Err("a piano library is coded off the frame".to_string()),
        }
    }

    /// Everything a build needs, owned, so it can run away from the dialog.
    fn coding(&self, donor: Option<Library<'static>>) -> Result<Coding, String> {
        let values = layer_values(&self.takes)?;
        let mut takes = Vec::with_capacity(self.takes.len());
        for (take, layer) in self.takes.iter().zip(values) {
            let pcm = take
                .pcm()
                .ok_or_else(|| format!("{}: it did not read as a WAV", take.path))?;
            takes.push(Recorded {
                path: take.path.clone(),
                samples: pcm.samples.clone(),
                channels: pcm.channels,
                rate: pcm.rate,
                root: take.root_key,
                bank: take.bank,
                layer,
            });
        }
        Ok(Coding {
            name: self.name.clone(),
            donor,
            takes,
        })
    }

    /// Start coding the library, donating from `donor` where a template was chosen.
    fn begin(
        &mut self,
        ctx: &egui::Context,
        donor: Option<Library<'static>>,
    ) -> Result<(), String> {
        let coding = self.coding(donor)?;
        self.refused = None;
        self.job = Some(work::run(ctx, move |progress| coding.run(progress)));
        Ok(())
    }

    /// The answer the build has ready, taken once. A refusal stays behind as the line
    /// the dialog paints.
    fn settle(&mut self) -> Option<Result<Built, String>> {
        let answer = match self.job.as_ref()?.poll() {
            work::Answer::Running => return None,
            work::Answer::Answered(answer) => answer,
            work::Answer::Died => Err("coding the library stopped without an answer".to_string()),
        };
        self.job = None;
        if let Err(why) = &answer {
            self.refused = Some(why.clone());
        }
        Some(answer)
    }

    fn project(&self) -> Result<Vec<u8>, String> {
        let zones: Vec<NewZone> = self
            .takes
            .iter()
            .map(|take| NewZone {
                path: take.path.clone(),
                sample_rate: take.pcm().map_or(0, |pcm| pcm.rate),
                frames: take.frames,
                root_key: take.root_key,
            })
            .collect();
        let project = Project::new(&self.name, &zones, now()).map_err(|e| e.to_string())?;
        nord_format::to_bytes(&Entity::SampleProject(project)).map_err(|e| e.to_string())
    }

    /// One `stk` per file, highest root first, each zone reaching up to where
    /// [`derive_top_notes`] puts it — the layout `Project::new` writes, encoded rather
    /// than referenced.
    fn instrument(&self) -> Result<Vec<u8>, String> {
        let mut order: Vec<&Take> = self.takes.iter().collect();
        order.sort_by_key(|take| std::cmp::Reverse(take.root_key));
        let roots: Vec<u8> = order.iter().map(|take| take.root_key).collect();
        let tops = derive_top_notes(&roots);
        let mut audio = Vec::with_capacity(order.len());
        for take in &order {
            audio.push(
                take.pcm()
                    .ok_or_else(|| format!("{}: it did not read as a WAV", take.path))?,
            );
        }
        let zones: Vec<encode::NewZone> = order
            .iter()
            .zip(&audio)
            .zip(&tops)
            .enumerate()
            .map(|(i, ((take, pcm), top_note))| encode::NewZone {
                source: &pcm.samples,
                channels: pcm.channels,
                root_key: take.root_key,
                top_note: *top_note,
                global_id: i as u32 + 1,
                loops: None,
                secondary_start: encode::default_secondary_start(pcm.frames(), None),
                shift: None,
                gain: 1.0,
                loop_decay: encode::DEFAULT_LOOP_DECAY,
            })
            .collect();
        let instrument = encode::multi_zone(
            encode::Instrument {
                name: &self.name,
                map_gain: 1.0,
                predictor: encode::Predictor::Minimising,
                layout: Layout::V2,
                preset: encode::Preset::default(),
            },
            &zones,
        )
        .map_err(|e| e.to_string())?;
        instrument.to_bytes().map_err(|e| e.to_string())
    }
}

/// One take as a build reads it: the audio as the file holds it, and the stroke it is
/// to become.
struct Recorded {
    path: String,
    samples: Vec<i16>,
    channels: u16,
    rate: u32,
    root: u8,
    bank: Bank,
    layer: u8,
}

/// A piano library's build, away from the dialog that stated it.
struct Coding {
    name: String,
    donor: Option<Library<'static>>,
    takes: Vec<Recorded>,
}

/// A library coded, and what the coding has to say about it.
#[derive(Debug)]
struct Built {
    bytes: Vec<u8>,
    strokes: usize,
    roots: usize,
    /// Samples that saturated at int16 while resampling.
    clipped: usize,
}

impl Coding {
    fn run(self, progress: &Progress) -> Result<Built, String> {
        let count = self.takes.len();
        let mut recordings = Vec::with_capacity(count);
        let mut clipped = 0usize;
        for (done, take) in self.takes.iter().enumerate() {
            progress.say(format!("resampling {} of {count}", done + 1));
            let audio = resample(&take.samples, usize::from(take.channels), take.rate)
                .map_err(|e| format!("{}: {e}", take.path))?;
            clipped += audio.clipped;
            recordings.push(Recording {
                root: take.root,
                bank: take.bank,
                layer: take.layer,
                channels: audio.channels,
            });
        }

        progress.say(format!("coding {count} strokes…"));
        let donor = match &self.donor {
            Some(template) => Donor::Template(template),
            None => Donor::Rules(Rules::new(Kind::Grand)),
        };
        let library =
            build(&donor, &Options::new(&self.name), &recordings).map_err(|e| e.to_string())?;
        let (strokes, roots) = (library.strokes().len(), library.roots().len());
        let piano = library.to_piano().map_err(|e| e.to_string())?;
        Ok(Built {
            bytes: nord_format::to_bytes(&Entity::Piano(piano)).map_err(|e| e.to_string())?,
            strokes,
            roots,
            clipped,
        })
    }
}

/// The layer value each take's stroke states, in the order the takes are listed, as
/// [`nord_format::formats::npno::encode::layer_values`] reads what their names claim.
fn layer_values(takes: &[Take]) -> Result<Vec<u8>, String> {
    let named: Vec<(u8, Bank, LayerTag)> = takes
        .iter()
        .map(|take| (take.root_key, take.bank, take.layer))
        .collect();
    nord_format::formats::npno::encode::layer_values(&named).map_err(|clash| {
        let what = format!("root {} {}", note::name(clash.root), clash.bank.name());
        match clash.how {
            Clash::BothForms => format!(
                "{what} names some of its layers by index and some by value; one root's \
                 bank names them one way"
            ),
            Clash::Twice => format!("{what} names one of its layers twice"),
        }
    })
}

/// Unix seconds, for the `m_modifyDate` every block in a project carries.
#[cfg(not(target_arch = "wasm32"))]
fn now() -> u32 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |since| since.as_secs() as u32)
}

/// ⚠️ `SystemTime::now` panics in a wasm module; the page's own clock is the only one.
#[cfg(target_arch = "wasm32")]
fn now() -> u32 {
    (js_sys::Date::now() / 1000.0) as u32
}

/// The dialog between picking WAVs and having what they make, if a draft is waiting.
///
/// Returns the new asset once it is made, for the caller to open a tab on.
pub fn dialog(ctx: &egui::Context, workspace: &mut Workspace, log: &mut Log) -> Option<u64> {
    if let Some(made) = answered(workspace, log) {
        return Some(made);
    }
    let making = workspace.draft_mut()?.making;
    let templates = match making {
        Making::Piano => templates(workspace),
        Making::Project | Making::Instrument => Vec::new(),
    };

    let mut make = false;
    let mut cancel = false;
    let draft = workspace.draft_mut()?;
    let coding = draft.job.is_some();
    let progress = draft.job.as_ref().map(Job::progress).unwrap_or_default();
    egui::Modal::new(egui::Id::new("new_project")).show(ctx, |ui| {
        ui.set_width(match making {
            Making::Piano => 660.0,
            Making::Project | Making::Instrument => 520.0,
        });
        ui.heading(format!("New {}", making.label()));
        ui.label(egui::RichText::new(making.caption()).small().weak());
        ui.add_space(8.0);
        ui.horizontal(|ui| {
            ui.label("Name");
            ui.add(egui::TextEdit::singleline(&mut draft.name).desired_width(240.0));
        });
        ui.add_space(4.0);
        let column = match making {
            Making::Piano => 170.0,
            Making::Project | Making::Instrument => 260.0,
        };
        egui::ScrollArea::vertical()
            .max_height(260.0)
            .show(ui, |ui| {
                for (i, take) in draft.takes.iter_mut().enumerate() {
                    ui.horizontal(|ui| {
                        ui.add_sized(
                            [column, ui.spacing().interact_size.y],
                            egui::Label::new(&take.path)
                                .halign(egui::Align::LEFT)
                                .truncate(),
                        );
                        ui.label(match making {
                            Making::Piano => "root",
                            Making::Project | Making::Instrument => "root key",
                        });
                        if let Some(note) = note_picker(ui, ("draft_root", i), take.root_key) {
                            take.root_key = note;
                        }
                        if making == Making::Piano {
                            stroke_controls(ui, i, take);
                        }
                        match (take.refusal(making), take.pcm()) {
                            (Some(why), _) => {
                                ui.label(
                                    egui::RichText::new(why)
                                        .small()
                                        .color(crate::app::bad(ui.visuals())),
                                );
                            }
                            (None, Some(pcm)) => {
                                ui.label(
                                    egui::RichText::new(format!(
                                        "{} Hz, {:.2} s",
                                        pcm.rate,
                                        take.frames as f64 / PROJECT_RATE as f64
                                    ))
                                    .small()
                                    .weak(),
                                );
                            }
                            (None, None) => {}
                        }
                    });
                }
            });
        if making == Making::Piano {
            ui.add_space(4.0);
            egui::CollapsingHeader::new("Advanced").show(ui, |ui| {
                template_picker(ui, draft, &templates);
            });
        }
        let refusal = draft.refusal();
        if let Some(why) = refusal.as_deref().or(draft.refused.as_deref()) {
            ui.add_space(4.0);
            ui.label(egui::RichText::new(why).color(crate::app::bad(ui.visuals())));
        }
        ui.add_space(8.0);
        ui.separator();
        ui.horizontal(|ui| {
            cancel = ui.button("Cancel").clicked();
            make = ui
                .add_enabled(
                    refusal.is_none() && !coding,
                    egui::Button::new(egui::RichText::new("Create").strong()),
                )
                .clicked();
            if coding {
                ui.spinner();
                ui.label(egui::RichText::new(&progress).small().weak());
            }
        });
    });

    if cancel {
        workspace.take_draft();
        return None;
    }
    if !make {
        return None;
    }
    if making == Making::Piano {
        start(ctx, workspace, log);
        // ⚠️ wasm runs the build where it is started, so the answer is already here.
        return answered(workspace, log);
    }
    let draft = workspace.take_draft()?;
    match draft.bytes() {
        Ok(bytes) => Some(workspace.ingest(
            format!("{}.{}", draft.name, making.extension()),
            Origin::Fresh,
            bytes,
            log,
        )),
        Err(why) => {
            log.error(format!("new {}: {why}", making.label()));
            log.trouble(format!(
                "Could not make a {} out of those files.",
                making.label()
            ));
            None
        }
    }
}

/// The bank and layer a piano take carries beyond a zone's root key.
fn stroke_controls(ui: &mut egui::Ui, i: usize, take: &mut Take) {
    egui::ComboBox::from_id_salt(("draft_bank", i))
        .width(92.0)
        .selected_text(take.bank.name())
        .show_ui(ui, |ui| {
            for bank in Bank::ALL {
                ui.selectable_value(&mut take.bank, bank, bank.name());
            }
        });
    let (number, word) = match take.layer {
        LayerTag::Index(n) => (n, "index"),
        LayerTag::Value(n) => (n, "value"),
    };
    egui::ComboBox::from_id_salt(("draft_layer", i))
        .width(64.0)
        .selected_text(word)
        .show_ui(ui, |ui| {
            ui.selectable_value(&mut take.layer, LayerTag::Index(number), "index");
            ui.selectable_value(&mut take.layer, LayerTag::Value(number), "value");
        });
    let mut set = number;
    // The value scale is the format's: past HIGHEST_PLAYED_LAYER is a stroke no velocity
    // selects, which `build` refuses. An index is a rank among the takes sharing a root
    // and bank, and no root is played at more layers than that either.
    if ui
        .add(egui::DragValue::new(&mut set).range(0..=HIGHEST_PLAYED_LAYER))
        .changed()
    {
        take.layer = match take.layer {
            LayerTag::Index(_) => LayerTag::Index(set),
            LayerTag::Value(_) => LayerTag::Value(set),
        };
    }
}

/// What "none" is called in the template picker, and the reading of an empty list.
const NO_TEMPLATE: &str = "none — the rules";

fn template_picker(ui: &mut egui::Ui, draft: &mut Draft, templates: &[(u64, String)]) {
    ui.horizontal(|ui| {
        ui.label("Template");
        let shown = draft
            .template
            .and_then(|id| templates.iter().find(|(open, _)| *open == id))
            .map_or(NO_TEMPLATE, |(_, name)| name.as_str());
        egui::ComboBox::from_id_salt("draft_template")
            .width(300.0)
            .selected_text(shown)
            .show_ui(ui, |ui| {
                ui.selectable_value(&mut draft.template, None, NO_TEMPLATE);
                for (id, name) in templates {
                    ui.selectable_value(&mut draft.template, Some(*id), name);
                }
            });
    });
}

/// The piano documents on this computer, by name, for a build to donate from.
fn templates(workspace: &Workspace) -> Vec<(u64, String)> {
    let mut open: Vec<(u64, String)> = workspace
        .entities()
        .iter()
        .filter(|entity| matches!(entity.entity, Some(Entity::Piano(_))))
        .map(|entity| (entity.id, entity.name.clone()))
        .collect();
    open.sort_by(|a, b| a.1.cmp(&b.1).then(a.0.cmp(&b.0)));
    open
}

/// A template's prefix and stroke records, read here because a build cannot borrow the
/// document's bytes.
fn skeleton(workspace: &Workspace, id: u64) -> Result<Library<'static>, String> {
    let entity = workspace
        .get(id)
        .ok_or_else(|| "the template is no longer open".to_string())?;
    Library::borrow(&entity.bytes)
        .map(|library| library.without_audio())
        .map_err(|e| format!("{}: {e}", entity.name))
}

/// Set the build going, with whatever template the Advanced row names.
fn start(ctx: &egui::Context, workspace: &mut Workspace, log: &mut Log) {
    let chosen = workspace.draft_mut().and_then(|draft| draft.template);
    let donor = match chosen {
        Some(id) => skeleton(workspace, id).map(Some),
        None => Ok(None),
    };
    let Some(draft) = workspace.draft_mut() else {
        return;
    };
    if let Err(why) = donor.and_then(|donor| draft.begin(ctx, donor)) {
        draft.refused = Some(why.clone());
        log.error(format!("new piano library: {why}"));
    }
}

/// What the build has to say for itself, once it has something to say.
fn answered(workspace: &mut Workspace, log: &mut Log) -> Option<u64> {
    let draft = workspace.draft_mut()?;
    let built = match draft.settle()? {
        Ok(built) => built,
        Err(why) => {
            log.error(format!("new piano library: {why}"));
            return None;
        }
    };
    let name = format!("{}.{}", draft.name, Making::Piano.extension());
    workspace.take_draft();
    let id = workspace.ingest(name, Origin::Fresh, built.bytes, log);
    if built.clipped > 0 {
        log.warn(format!(
            "{} resampled sample(s) saturated at int16",
            built.clipped
        ));
    }
    log.say(format!(
        "Built {} stroke(s) over {} root(s).",
        built.strokes, built.roots
    ));
    Some(id)
}

#[cfg(test)]
mod tests {
    use super::*;
    use nord_format::formats::npno::encode::SOFTEST_LAYER;
    use nord_format::formats::nsmp::codec::SOURCE_RATE;

    #[test]
    fn root_keys_are_read_off_the_names_or_counted_from_middle_c() {
        let named = ["Marimba-C3.wav", "Marimba-C4.wav", "Marimba_F#4.wav"].map(String::from);
        assert_eq!(default_roots(&named), vec![48, 60, 66]);

        // One file that names no key, and the whole run is a guess again.
        let mixed = ["Marimba-C3.wav", "Marimba-take2.wav"].map(String::from);
        assert_eq!(default_roots(&mixed), vec![60, 61]);

        // Two files naming one key would make a project that cannot be built.
        let clashing = ["a-C3.wav", "b-C3.wav"].map(String::from);
        assert_eq!(default_roots(&clashing), vec![60, 61]);

        // A trailing number is a take, not MIDI note 1.
        assert_eq!(trailing_note("hit-1.wav"), None);
        assert_eq!(trailing_note("hit-Bb2.wav"), Some(46));
    }

    #[test]
    fn the_default_run_fits_under_the_highest_key_a_project_maps() {
        let many: Vec<String> = (0..MOST_ZONES).map(|i| format!("{i}.wav")).collect();
        let roots = default_roots(&many);
        assert_eq!(roots.first(), Some(&LOWEST_NOTE));
        assert_eq!(roots.last(), Some(&HIGHEST_NOTE));
        let mut unique = roots.clone();
        unique.dedup();
        assert_eq!(unique.len(), roots.len(), "one key each");
    }

    /// ⚠️ A pick this long is refused for its count, but the run laid under that refusal
    /// still walks up from the lowest key, one file per key while there are keys.
    #[test]
    fn a_pick_longer_than_a_u8_counts_still_walks_up_from_the_lowest_key() {
        let many: Vec<String> = (0..MOST_ZONES + 200).map(|i| format!("{i}.wav")).collect();
        let roots = default_roots(&many);
        assert_eq!(roots.len(), many.len(), "a key per file");
        assert_eq!(roots.first(), Some(&LOWEST_NOTE));
        assert_eq!(roots[MOST_ZONES - 1], HIGHEST_NOTE);
        let mut laid = roots[..MOST_ZONES].to_vec();
        laid.dedup();
        assert_eq!(laid.len(), MOST_ZONES, "one key each, while there are keys");
    }

    fn wav(rate: u32, frames: usize) -> Vec<u8> {
        nord_format::wav::mono_pcm16(&vec![0i16; frames], rate).unwrap()
    }

    #[test]
    fn a_draft_becomes_a_project_over_the_picked_files() {
        let draft = Draft::plan(
            Making::Project,
            vec![
                ("Low-C3.wav".into(), wav(44_100, 4_410)),
                ("High-C5.wav".into(), wav(22_050, 2_205)),
            ],
        )
        .expect("two files were picked");
        assert_eq!(draft.name, "Low-C3");
        assert_eq!(
            draft.takes.iter().map(|t| t.root_key).collect::<Vec<_>>(),
            vec![48, 72]
        );
        // Both are a tenth of a second, so both count 4410 frames.
        assert_eq!(draft.takes[0].frames, 4_410);
        assert_eq!(draft.takes[1].frames, 4_410);
        assert_eq!(
            draft.takes[1].pcm().unwrap().rate,
            22_050,
            "the file's own rate is kept"
        );
        assert!(draft.refusal().is_none());

        let bytes = draft.bytes().expect("a project");
        let entity = nord_format::from_stream(&mut std::io::Cursor::new(&bytes)).unwrap();
        let Entity::SampleProject(project) = &entity else {
            panic!("a Sample Editor project");
        };
        assert_eq!(project.name().unwrap(), "Low-C3");
        let paths: Vec<String> = project
            .audio_files()
            .unwrap()
            .into_iter()
            .map(|file| file.path)
            .collect();
        assert_eq!(paths, ["Low-C3.wav", "High-C5.wav"]);
        let roots: Vec<u8> = project
            .zones()
            .unwrap()
            .iter()
            .map(|zone| zone.root_key)
            .collect();
        assert_eq!(roots, [72, 48], "zones are stored high to low");
        assert_eq!(nord_format::to_bytes(&entity).unwrap(), bytes);
    }

    /// The same pick, made into the instrument instead: the audio itself, one zone per
    /// file, and bytes that come back the way they went out.
    #[test]
    fn a_draft_becomes_an_instrument_over_the_same_files() {
        use nord_format::formats::nsmp::encode::MIN_FRAMES;

        let draft = Draft::plan(
            Making::Instrument,
            vec![
                ("Marimba-C3.wav".into(), wav(SOURCE_RATE, MIN_FRAMES)),
                ("Marimba-C5.wav".into(), wav(SOURCE_RATE, MIN_FRAMES * 2)),
            ],
        )
        .expect("two files were picked");
        assert_eq!(draft.name, "Marimba-C3");
        assert!(draft.refusal().is_none());

        let bytes = draft.bytes().expect("an instrument");
        let entity = nord_format::from_stream(&mut std::io::Cursor::new(&bytes)).unwrap();
        assert!(matches!(entity, Entity::Sample(_)), "{entity:?}");
        assert_eq!(nord_format::to_bytes(&entity).unwrap(), bytes);

        let snapshot = crate::document::sample::snapshot(&entity)
            .expect("a sample instrument")
            .expect("it reads");
        assert_eq!(snapshot.name, "Marimba-C3");
        assert_eq!(snapshot.generation, "v2");
        let zones: Vec<(u8, u8)> = snapshot
            .zones
            .iter()
            .map(|zone| (zone.root_key, zone.top_note))
            .collect();
        // High to low, and the top notes are the ones a project derives for these roots.
        assert_eq!(zones, [(72, 96), (48, 59)]);
    }

    /// ⚠️ The instrument carries the audio, so the encoder's own limits are refusals in
    /// the dialog rather than a failure after Create. A project references the file and
    /// takes it as it is.
    #[test]
    fn a_wav_the_encoder_will_not_take_is_refused_before_create() {
        let slow = vec![("Marimba-C3.wav".into(), wav(22_050, 4_410))];
        let project = Draft::plan(Making::Project, slow.clone()).unwrap();
        assert!(project.refusal().is_none(), "a project keeps the rate");

        let instrument = Draft::plan(Making::Instrument, slow).unwrap();
        let why = instrument.refusal().expect("22 050 Hz is not encodable");
        assert!(why.contains("22050 Hz"), "{why}");
        assert!(why.starts_with("Marimba-C3.wav: "), "{why}");
    }

    /// The name field an instrument carries is 31 bytes, and a filename is not.
    #[test]
    fn an_instrument_opens_on_a_name_its_own_field_holds() {
        let long = ["an extremely long marimba sample name.wav".to_string()];
        assert_eq!(draft_name(Making::Instrument, &long).len(), MAX_NAME_LEN);
        assert!(draft_name(Making::Project, &long).len() > MAX_NAME_LEN);

        let mut draft = Draft::plan(
            Making::Instrument,
            vec![(
                "Marimba.wav".into(),
                wav(SOURCE_RATE, nord_format::formats::nsmp::encode::MIN_FRAMES),
            )],
        )
        .unwrap();
        draft.name = "x".repeat(MAX_NAME_LEN + 1);
        let why = draft.refusal().expect("a name the field cannot hold");
        assert!(why.contains(&MAX_NAME_LEN.to_string()), "{why}");
    }

    #[test]
    fn a_cancelled_pick_raises_no_dialog() {
        assert!(Draft::plan(Making::Project, Vec::new()).is_none());
        assert!(Draft::plan(Making::Instrument, Vec::new()).is_none());
    }

    // ---- a piano library ----------------------------------------------------------

    /// A short mono take at the rate the piano section plays, so a build resamples
    /// nothing and the coder has something other than silence to fit.
    fn stroke_wav(frames: usize) -> Vec<u8> {
        let samples: Vec<i16> = (0..frames)
            .map(|n| ((n as f64 * 0.05).sin() * 8_000.0) as i16)
            .collect();
        nord_format::wav::mono_pcm16(&samples, nord_format::formats::npno::codec::RATE).unwrap()
    }

    fn piano_draft(stems: &[&str]) -> Draft {
        let files = stems
            .iter()
            .map(|stem| (format!("{stem}.wav"), stroke_wav(3_000)))
            .collect();
        Draft::plan(Making::Piano, files).expect("files were picked")
    }

    fn finish(draft: &mut Draft) -> Result<Built, String> {
        loop {
            if let Some(answer) = draft.settle() {
                return answer;
            }
            std::thread::yield_now();
        }
    }

    #[test]
    fn a_wavs_name_states_the_stroke_it_holds() {
        assert_eq!(
            stroke_name("060-b0-l00.wav"),
            Some((60, Bank::Attack, LayerTag::Index(0))),
            "the spelling `nord piano build` reads"
        );
        assert_eq!(
            stroke_name("Grand-060-b0-l00.wav"),
            Some((60, Bank::Attack, LayerTag::Index(0))),
            "and the one a stroke exported from here is written under"
        );
        assert_eq!(stroke_name("060-b0.wav"), None, "no stroke named at all");

        assert_eq!(
            stroke_defaults("Marimba-C3.wav"),
            (48, Bank::Attack, LayerTag::Index(0)),
            "a plain name states a root and nothing else"
        );
        assert_eq!(
            stroke_defaults("hit.wav"),
            (MIDDLE_C, Bank::Attack, LayerTag::Index(0)),
            "and a name stating none opens on middle C"
        );
    }

    /// The value a layer takes is what selects it, so the layers of one root are spread
    /// over the velocity range rather than packed at the loud end.
    #[test]
    fn a_roots_layers_are_spread_over_the_velocity_range_loudest_first() {
        let draft = piano_draft(&["060-b0-l02", "060-b0-l00", "060-b0-l01", "072-b0-l00"]);
        assert_eq!(
            layer_values(&draft.takes).unwrap(),
            vec![27, 0, 14, 0],
            "the list order is kept; the rank is the layer's own"
        );
    }

    #[test]
    fn one_roots_bank_names_its_layers_one_way() {
        let draft = piano_draft(&["060-b0-l00", "060-b0-v12"]);
        let why = draft.refusal().expect("an index and a value in one group");
        assert!(why.contains("C4"), "{why}");
        assert!(why.contains("attack"), "{why}");

        let apart = piano_draft(&["060-b0-l00", "060-b2-v12"]);
        assert!(
            apart.refusal().is_none(),
            "a bank of its own names its layers its own way"
        );
    }

    /// Two takes claiming one layer of a root and bank would be spread to two different
    /// values, which is neither of the things their names said.
    #[test]
    fn one_roots_bank_names_each_of_its_layers_once() {
        let twice = piano_draft(&["a-060-b0-l00", "b-060-b0-l00"]);
        let why = twice.refusal().expect("one layer named twice");
        assert!(why.contains("C4"), "{why}");
        assert!(why.contains("names one of its layers twice"), "{why}");

        let apart = piano_draft(&["a-060-b0-l00", "b-060-b1-l00"]);
        assert!(
            apart.refusal().is_none(),
            "a bank of its own numbers its own layers"
        );
    }

    #[test]
    fn a_piano_draft_codes_the_takes_into_a_library() {
        let mut draft = piano_draft(&["Grand-060-b0-l00", "Grand-060-b0-l01", "Grand-072-b2-l00"]);
        assert_eq!(draft.name, "Grand", "the stroke group is not the name");
        assert!(draft.refusal().is_none());

        draft
            .begin(&egui::Context::default(), None)
            .expect("the takes state a library");
        let built = finish(&mut draft).expect("a library");
        assert_eq!((built.strokes, built.roots), (3, 2));
        assert_eq!(built.clipped, 0);

        let entity = nord_format::from_stream(&mut std::io::Cursor::new(&built.bytes)).unwrap();
        assert!(matches!(entity, Entity::Piano(_)), "{entity:?}");
        assert_eq!(nord_format::to_bytes(&entity).unwrap(), built.bytes);
        let library = Library::borrow(&built.bytes).unwrap();
        assert_eq!(library.name(), ("Grand".to_string(), String::new()));
        let strokes: Vec<(u8, Option<Bank>, u8)> = library
            .strokes()
            .iter()
            .map(|stroke| (stroke.root, stroke.bank(), stroke.layer()))
            .collect();
        assert_eq!(
            strokes,
            [
                (60, Some(Bank::Attack), 0),
                (60, Some(Bank::Attack), SOFTEST_LAYER),
                (72, Some(Bank::Release), 0),
            ],
            "the root's two layers spread from the loudest to the softest"
        );
    }

    /// A template donates the playback a recording cannot carry, and is read without
    /// its audio because a build cannot borrow the document's bytes.
    #[test]
    fn a_template_donates_what_the_recordings_do_not_state() {
        let mut plain = piano_draft(&["Grand-060-b0-l00"]);
        plain.begin(&egui::Context::default(), None).unwrap();
        let rules = finish(&mut plain).expect("a library").bytes;
        assert_eq!(
            Library::borrow(&rules).unwrap().damper_top(),
            Kind::Grand.damper_top()
        );

        let mut edited = Library::borrow(&rules).unwrap();
        edited.set_damper_top(100).unwrap();
        let template = nord_format::to_bytes(&Entity::Piano(edited.to_piano().unwrap())).unwrap();

        let mut again = piano_draft(&["Grand-060-b0-l00"]);
        let donor = Library::borrow(&template).unwrap().without_audio();
        again.begin(&egui::Context::default(), Some(donor)).unwrap();
        let built = finish(&mut again).expect("a library");
        assert_eq!(Library::borrow(&built.bytes).unwrap().damper_top(), 100);
    }

    /// ⚠️ Everything a library states about its strokes beyond the rules the dialog holds
    /// is stated by the coder, in its own words, with the takes still there.
    #[test]
    fn what_the_coder_refuses_comes_back_as_the_dialogs_own_line() {
        let mut draft = piano_draft(&["a-060-b0-v31", "b-072-b0-v31"]);
        assert!(
            draft.refusal().is_none(),
            "the dialog states no rule about this"
        );

        draft.begin(&egui::Context::default(), None).unwrap();
        let why = finish(&mut draft).expect_err("a layer no velocity selects");
        assert!(why.contains("no velocity selects"), "{why}");
        assert_eq!(draft.refused.as_deref(), Some(why.as_str()));
        assert_eq!(draft.takes.len(), 2, "the takes stay, to be fixed");
    }

    #[test]
    fn a_draft_says_why_it_cannot_be_made() {
        let unreadable = Draft::plan(
            Making::Project,
            vec![("notes.txt".into(), b"not a wav".to_vec())],
        )
        .unwrap();
        let why = unreadable.refusal().expect("it will not read");
        assert!(why.starts_with("notes.txt: "), "{why}");

        let mut clashing = Draft::plan(
            Making::Project,
            vec![
                ("a.wav".into(), wav(44_100, 4_410)),
                ("b.wav".into(), wav(44_100, 4_410)),
            ],
        )
        .unwrap();
        assert!(clashing.refusal().is_none());
        clashing.takes[1].root_key = clashing.takes[0].root_key;
        let why = clashing.refusal().expect("two zones on one key");
        assert!(why.contains("C4"), "{why}");
        assert!(clashing.bytes().is_err());
    }
}