drawbar 0.7.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
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
//! The sample-instrument document.
//!
//! A sample is mostly encoded audio, so what is settable is what the format can patch in
//! place without touching a stroke: the name, each zone's root key and boundaries, and
//! the v2 keyboard map's per-key gain and detune. Every generation edits. Where a
//! container also describes the keyboard note by note, that description is recomputed
//! from the zones as they move.
//!
//! ⚠️ Decoding a stroke is expensive and a library instrument is hundreds of megabytes,
//! so **nothing here decodes to draw a frame**. A zone's audio is decoded once, when the
//! operator asks for it, and kept in a [`Cache`] until the bytes under it change.
//!
//! The chrome the project editor shares — the key map, the zone rows, the cells of an
//! open row — lives here rather than being written twice: an `.nsmpproj` is the same
//! object seen from the source side.

use std::collections::HashMap;
use std::io::Cursor;

use eframe::egui;
use nord_format::formats::nsmp::codec::{self, Audio};
use nord_format::formats::nsmp::{keymap, zone, KeyTable, Level, Sty};
use nord_format::note;
use nord_format::{Entity, Sample};

use super::capability::{Fact, Offset, Row, State as Cap};
use super::controls::{self, Sets};
use super::header::{Body, Cell};
use super::keys;
use super::table::{self, Width, NAME_TEXT, PAD};
use crate::app;
use crate::icon::{icon, Glyph};
use crate::room;
use crate::workspace::Baseline;

fn sample(entity: &Entity) -> Option<&Sample> {
    match entity {
        Entity::Sample(sample) => Some(sample),
        _ => None,
    }
}

fn sample_mut(entity: &mut Entity) -> Option<&mut Sample> {
    match entity {
        Entity::Sample(sample) => Some(sample),
        _ => None,
    }
}

/// One zone, numbered the way the panel numbers them: from 1, in stored order.
#[derive(Clone, PartialEq, Eq)]
pub struct Zone {
    pub root_key: u8,
    pub top_note: u8,
    /// The bottom of the range where the file states one outright. The v2 table does
    /// not, so there it is worked out from the zone below.
    pub low_note: Option<u8>,
    /// The zone record's own linear gain, where the generation carries one —
    /// [`zone::GAIN_UNITY`] is 1.0. Nothing here writes it.
    pub gain: Option<u32>,
    /// The velocity window the record states, where the layout has one. Nothing here
    /// writes it either.
    pub velocity: Option<(u8, u8)>,
    /// The stroke stream this zone plays, in bytes.
    pub bytes: usize,
}

/// Everything the document shows about an instrument, in one read.
#[derive(Clone, PartialEq)]
pub struct Snapshot {
    pub name: String,
    pub max_name_len: usize,
    /// The v3/v4 second name — what follows the `_` in the vendor's filenames. Empty
    /// on a v2 instrument, which has one name.
    pub sub_name: String,
    /// `v2`, `v3` or `v4`, taken from the content version rather than the filename.
    pub generation: &'static str,
    pub categories: Vec<String>,
    pub zones: Vec<Zone>,
    /// Whether the zone controls do anything. The name is always settable.
    pub zones_editable: bool,
    /// The keyboard map ahead of the v2 zone table, where the section holds one.
    pub key_table: Option<KeyTable>,
    /// Bytes per zone record on this body's own chain, for the Advanced face.
    pub record_len: usize,
    /// What the `sty` preset states. Read only: it is the preset the loader installs
    /// for the instrument's category.
    pub sound: Vec<(&'static str, String)>,
    /// The content version, which is what decides the generation.
    pub version: u32,
}

pub fn snapshot(entity: &Entity) -> Option<Result<Snapshot, String>> {
    Some(read(sample(entity)?))
}

fn read(sample: &Sample) -> Result<Snapshot, String> {
    let told = told(sample)?;
    Ok(Snapshot {
        name: sample.name().map_err(|e| e.to_string())?,
        max_name_len: sample.max_name_len(),
        sub_name: told.sub_name,
        generation: sample.generation(),
        categories: told.categories,
        zones: sample
            .zones()
            .map_err(|e| e.to_string())?
            .iter()
            .enumerate()
            .map(|(index, zone)| {
                let stated = told.records.get(index).copied().unwrap_or_default();
                Zone {
                    root_key: zone.root_key,
                    top_note: zone.top_note,
                    low_note: zone.low_note,
                    gain: stated.gain,
                    velocity: stated.velocity,
                    bytes: zone.stream.len(),
                }
            })
            .collect(),
        zones_editable: sample.zones_are_editable(),
        key_table: told.key_table,
        record_len: told.record_len,
        sound: sound(sample),
        version: told.version,
    })
}

/// What one generation's own sections state, in one read of them.
struct Told {
    /// Empty on the narrow chain, which has one name.
    sub_name: String,
    /// The `cat` section, which only the narrow chain carries.
    categories: Vec<String>,
    key_table: Option<KeyTable>,
    record_len: usize,
    /// One per zone, in stored order.
    records: Vec<Stated>,
    version: u32,
}

/// What a zone record states beyond its notes, where its generation holds it: the
/// record's own gain on the narrow chain, the velocity window it answers on the wide one.
#[derive(Clone, Copy, Default)]
struct Stated {
    gain: Option<u32>,
    velocity: Option<(u8, u8)>,
}

fn told(sample: &Sample) -> Result<Told, String> {
    Ok(match sample {
        Sample::V2(body) => Told {
            sub_name: String::new(),
            categories: body.categories(),
            key_table: body.key_table().ok(),
            // A `map` version with no zone layout is a body whose zones did not read at
            // all, and the error the zone read gives is the one worth showing.
            record_len: body
                .chain()
                .map_or(zone::RECORD_LEN, |chain| chain.zone_record_len()),
            records: body
                .zones()
                .map_err(|e| e.to_string())?
                .iter()
                .map(|zone| Stated {
                    gain: Some(zone.gain),
                    velocity: None,
                })
                .collect(),
            version: body.header.version,
        },
        Sample::V3(body) => Told {
            sub_name: body.sub_name().map_err(|e| e.to_string())?,
            categories: Vec::new(),
            key_table: None,
            record_len: body
                .zone_table()
                .map_or(zone::RECORD_LEN, |table| table.wide.record_len()),
            records: body
                .zones()
                .map_err(|e| e.to_string())?
                .iter()
                .map(|zone| Stated {
                    gain: None,
                    velocity: zone.velocity.map(|window| (window.low, window.high)),
                })
                .collect(),
            version: body.header.version,
        },
    })
}

/// What the `sty` section states, in the words the read cells print.
///
/// The v2 preset carries the two velocity depths on a three-step scale; the wide ones
/// carry the dynamics curve and its response instead.
fn sound(sample: &Sample) -> Vec<(&'static str, String)> {
    let on = |flag: bool| match flag {
        true => "on".to_string(),
        false => "off".to_string(),
    };
    match sample {
        Sample::V2(body) => match body.sty() {
            Ok(sty) => vec![
                ("Dynamics", on(sty.dynamics_enabled())),
                (
                    "Velocity → amplitude",
                    sty.velocity_to_amplitude().to_string(),
                ),
                ("Velocity → timbre", sty.velocity_to_timbre().to_string()),
            ],
            Err(_) => Vec::new(),
        },
        Sample::V3(body) => match body.sty() {
            Ok(Sty::V3(sty)) => vec![
                ("Dynamics", on(sty.dynamics_enabled())),
                ("Dynamics curve", sty.dynamics_curve().to_string()),
                ("Dynamics response", sty.dynamics_response().to_string()),
            ],
            Ok(Sty::V4(sty)) => vec![
                ("Dynamics", on(sty.dynamics_enabled())),
                (
                    "Dynamics curve",
                    match sty.dynamics_curve() {
                        Some(curve) => curve.to_string(),
                        None => "none".to_string(),
                    },
                ),
            ],
            Ok(Sty::V2(_)) | Err(_) => Vec::new(),
        },
    }
}

/// Apply one `path = value`. Paths are the CLI's: `name`, `zone1.root_key`,
/// `zone1.top_note`, `zone1.low_note`.
fn set(sample: &mut Sample, path: &str, value: &str) -> Result<(), String> {
    if path == "name" {
        return sample.set_name(value).map_err(|e| e.to_string());
    }
    let unknown = || format!("unknown field {path:?}");
    let (zone, field) = path.split_once('.').ok_or_else(unknown)?;
    let index = zone
        .strip_prefix("zone")
        .and_then(|n| n.parse::<usize>().ok())
        .filter(|&n| n >= 1)
        .ok_or_else(unknown)?;
    // Checked here so the message speaks the panel's 1-based numbering, not the format
    // crate's 0-based one.
    let zones = sample.zones().map_err(|e| e.to_string())?.len();
    if index > zones {
        return Err(format!("there is no zone {index}: this sample has {zones}"));
    }
    let note = note::parse(value)?;
    match field {
        "root_key" => sample.set_root_key(index - 1, note),
        "top_note" => sample.set_zone_top_note(index - 1, note),
        "low_note" => sample.set_zone_low_note(index - 1, note),
        _ => return Err(unknown()),
    }
    .map_err(|e| e.to_string())
}

/// The note and field of a `key60.gain` path.
fn key_path(path: &str) -> Option<(u8, &str)> {
    let (key, field) = path.split_once('.')?;
    let note = key.strip_prefix("key")?.parse::<u8>().ok()?;
    (usize::from(note) < keymap::KEYS).then_some((note, field))
}

/// A per-key value as the lane writes it: a figure, and the unit it is measured in.
fn measured(value: &str, unit: &str) -> Result<f64, String> {
    let text = value.trim();
    text.strip_suffix(unit)
        .unwrap_or(text)
        .trim()
        .parse::<f64>()
        .map_err(|_| format!("{value:?} is not a reading in {unit}"))
}

/// A gain field as decibels. The field is a linear ratio with unity at
/// [`keymap::GAIN_UNITY`], so the reading is the usual 20·log10 of it.
pub fn gain_db(gain: u32, unity: u32) -> f64 {
    match gain {
        0 => f64::NEG_INFINITY,
        held => 20.0 * (f64::from(held) / f64::from(unity)).log10(),
    }
}

/// The gain field that reads as `db`, refused where it does not fit the 24 bits.
fn gain_units(db: f64) -> Result<u32, String> {
    let units = f64::from(keymap::GAIN_UNITY) * 10.0_f64.powf(db / 20.0);
    let rounded = units.round();
    match rounded.is_finite() && rounded >= 0.0 && rounded <= f64::from(keymap::GAIN_MAX) {
        true => Ok(rounded as u32),
        false => Err(format!(
            "{db} dB is outside what the gain field holds (up to {:.1} dB)",
            gain_db(keymap::GAIN_MAX, keymap::GAIN_UNITY)
        )),
    }
}

/// A detune field as cents, and the field that reads as a number of them.
pub fn detune_cents(detune: i32) -> f64 {
    f64::from(detune) / f64::from(keymap::DETUNE_PER_SEMITONE) * 100.0
}

fn detune_units(cents: f64) -> i32 {
    (cents / 100.0 * f64::from(keymap::DETUNE_PER_SEMITONE)).round() as i32
}

/// Every `key{note}.{gain|detune}` set, applied to one read of the keyboard map and
/// written back once.
///
/// The map is one field: two key edits are one write, and a key's other half is carried
/// over rather than reset to neutral.
fn apply_keys(sample: &mut Sample, sets: &[(String, String)]) -> Result<(), String> {
    let Sample::V2(body) = sample else {
        return Err("only a v2 instrument carries a keyboard map this editor writes".into());
    };
    let mut table = body.key_table().map_err(|e| e.to_string())?;
    for (path, value) in sets {
        let (note, field) = key_path(path).ok_or_else(|| format!("unknown field {path:?}"))?;
        let held = table.key(note).map_err(|e| e.to_string())?;
        let level = match field {
            "gain" => Level::new(gain_units(measured(value, "dB")?)?, held.detune()),
            "detune" => Level::new(held.gain(), detune_units(measured(value, "c")?)),
            _ => return Err(format!("unknown field {path:?}")),
        }
        .map_err(|e| e.to_string())?;
        table.set_key(note, level).map_err(|e| e.to_string())?;
    }
    body.set_key_table(&table).map_err(|e| e.to_string())
}

/// Apply every set to a fresh decode and re-encode, the same all-or-nothing rule the
/// registry bodies follow.
pub fn apply(bytes: &[u8], sets: &[(String, String)]) -> Result<Vec<u8>, String> {
    let mut entity =
        nord_format::from_stream(&mut Cursor::new(bytes)).map_err(|e| e.to_string())?;
    let sample = sample_mut(&mut entity).ok_or("not a sample instrument")?;
    let (table, fields): (Sets, Sets) = sets
        .iter()
        .cloned()
        .partition(|(path, _)| key_path(path).is_some());
    for (path, value) in &fields {
        set(sample, path, value)?;
    }
    if !table.is_empty() {
        apply_keys(sample, &table)?;
    }
    nord_format::to_bytes(&entity).map_err(|e| e.to_string())
}

/// The range a zone covers, in plain words.
///
/// Zones are stored high to low and the panel numbers them from 1 at the top of the
/// keyboard; a zone's bottom is one note above the next record's top, except where the
/// file states the bottom itself.
pub fn range(zones: &[Zone], index: usize) -> String {
    let top = note::name(zones[index].top_note);
    match bottom(zones, index) {
        Some(low) => format!("{} up to {top}", note::name(low)),
        None => format!("up to {top}"),
    }
}

/// Where a zone's range starts: what the record states, or one above the zone below.
fn bottom(zones: &[Zone], index: usize) -> Option<u8> {
    zones[index].low_note.or_else(|| {
        zones
            .get(index + 1)
            .map(|below| below.top_note.saturating_add(1))
    })
}

/// What the sample view asked the document to do about one zone's audio.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Ask {
    /// Decode this zone, because the operator opened it.
    Decode(usize),
    /// Start it, or stop it if it is the one sounding.
    Play(usize),
    Save(usize),
    /// Sound this zone at the pitch a struck key asks of it.
    Strike {
        zone: usize,
        semitones: i16,
    },
}

/// Decoded zone audio, kept only while the bytes it came from are the current ones.
///
/// ⚠️ Keyed by the asset's [`stamp`](crate::workspace::LocalEntity::stamp) as well as
/// its id: an edit re-encodes the whole file, and audio decoded from what it held
/// before is audio from another instrument.
#[derive(Default)]
pub struct Cache {
    of: Option<(u64, u64)>,
    zones: HashMap<usize, Result<Decoded, String>>,
}

/// One zone's audio, and the envelope drawn from it.
pub struct Decoded {
    pub audio: Audio,
    /// Min and max per column, in `-1.0..=1.0`. Drawn once and stretched to whatever
    /// width the panel has, because rebuilding it on a window drag is another pass over
    /// every sample.
    pub envelope: Vec<(f32, f32)>,
}

/// Columns an envelope is reduced to. Wide enough that a wide panel has no gaps in it,
/// small enough that the whole thing is a few kilobytes whatever the zone holds.
const COLUMNS: usize = 512;

impl Cache {
    /// Drop everything decoded from bytes that are no longer what `id` holds.
    pub fn follow(&mut self, id: u64, stamp: u64) {
        if self.of != Some((id, stamp)) {
            self.of = Some((id, stamp));
            self.zones.clear();
        }
    }

    pub fn get(&self, zone: usize) -> Option<&Result<Decoded, String>> {
        self.zones.get(&zone)
    }

    /// Decode one zone, once. A refusal is remembered like a success: the operator gets
    /// the codec's own reason, and clicking again would only produce it a second time.
    pub fn decode(&mut self, entity: &Entity, zone: usize) {
        if self.zones.contains_key(&zone) {
            return;
        }
        self.zones.insert(zone, decode(entity, zone));
    }
}

fn decode(entity: &Entity, index: usize) -> Result<Decoded, String> {
    let sample = sample(entity).ok_or("this is not a sample instrument")?;
    let layout = sample.layout().map_err(|e| e.to_string())?;
    let zones = sample.zones().map_err(|e| e.to_string())?;
    let zone = zones
        .get(index)
        .ok_or_else(|| format!("there is no zone {}", index + 1))?;
    let audio = codec::decode(zone.stream, zone.at, layout).map_err(|e| e.to_string())?;
    let envelope = envelope(&audio.samples, audio.channels, COLUMNS);
    Ok(Decoded { audio, envelope })
}

/// The min and max of each of `columns` equal slices of the audio, scaled to
/// `-1.0..=1.0`.
///
/// Frames rather than samples, so a stereo zone draws one envelope over both channels
/// instead of two half-width ones. A column with no frames in it — more columns than
/// frames — is flat, which is what a zone shorter than the widget should look like.
pub fn envelope(samples: &[i16], channels: u16, columns: usize) -> Vec<(f32, f32)> {
    let channels = usize::from(channels).max(1);
    let frames = samples.len() / channels;
    if columns == 0 || frames == 0 {
        return Vec::new();
    }
    let scale = |v: i16| f32::from(v) / 32768.0;
    // ⚠️ In 64-bit: a long zone times the column count overflows a 32-bit `usize`, and
    // wasm is a 32-bit target. Every result is at most `frames`, so the cast back is safe.
    let edge = |column: usize| (column as u64 * frames as u64 / columns as u64) as usize;
    (0..columns)
        .map(|column| {
            let from = edge(column);
            let to = edge(column + 1).max(from + 1).min(frames);
            let span = &samples[from * channels..to * channels];
            let low = span.iter().copied().min().unwrap_or(0);
            let high = span.iter().copied().max().unwrap_or(0);
            (scale(low), scale(high))
        })
        .collect()
}

/// Which zone is open, what was last struck, and the map the paints are measured
/// against. Nothing here is an edit: an edit is on the working copy the moment it is
/// made.
///
/// ⚠️ Reset when the document changes: a row index belongs to the instrument it was
/// opened on.
#[derive(Default)]
pub struct State {
    selected: Option<usize>,
    /// Whether the selected row's own fields are unfolded. Only the selected row has a
    /// body, so there is never a second one to remember.
    open: bool,
    /// A row to bring up under the pinned map, once the body draws it.
    reveal: Option<usize>,
    /// Whether the 128-key table is unfolded.
    table: bool,
    audition: Option<keys::Audition>,
    /// What the struck key did, so the sentence outlives the click that made it.
    answer: Option<Answer>,
    /// The saved bytes a paint mark is measured against.
    baseline: Option<Saved>,
}

/// The saved bytes a painted key is told apart from, and the keyboard map they hold.
///
/// ⚠️ Keyed by what was saved as well as by which asset: saving moves the baseline
/// without giving the asset new bytes, and a map read before that is one every stored
/// value now reads as painted against.
struct Saved {
    of: (u64, Option<u32>, usize),
    /// `None` is a body that carries no map.
    table: Option<KeyTable>,
}

/// What a struck key found.
struct Answer {
    /// The row of the zone that answered it, where one did.
    zone: Option<usize>,
    /// How far the key is from that zone's root, which is what resamples the stroke.
    /// Zero where no zone answers.
    semitones: i16,
    words: String,
    sounded: bool,
}

impl State {
    /// Select a zone, and open its row.
    ///
    /// Clicking the row that is already open and selected closes it; picking from the
    /// map always opens, and asks for the row to be brought into view.
    fn pick(&mut self, zone: usize, reveal: bool) {
        let close = !reveal && self.selected == Some(zone) && self.open;
        self.selected = Some(zone);
        self.open = !close;
        if reveal {
            self.reveal = Some(zone);
        }
    }

    /// The zone a live audition is sounding.
    fn lit(&self) -> Option<usize> {
        self.answer.as_ref().and_then(|answer| answer.zone)
    }
}

/// The row the editor has selected — for a widget whose own order is not the row order.
pub fn selected(state: &State) -> Option<usize> {
    state.selected
}

/// Select a row and bring it up under the map, which is what a pick outside the row
/// list means.
pub fn pick_row(state: &mut State, row: usize) {
    state.pick(row, true);
}

/// One zone as the key map draws it, whichever format states it.
pub struct MapZone {
    /// The row of the zone list this band stands for.
    ///
    /// ⚠️ Not the band's own index: a project draws only the zones that answer a key, so
    /// the third band there may be the fourth row.
    pub row: usize,
    pub low: u8,
    pub top: u8,
    pub root: u8,
    pub name: String,
    /// The velocity window it answers, where the format states one.
    pub velocity: Option<(u8, u8)>,
}

/// Whether a struck key can be heard here.
///
/// A project's audio is not decoded: it is built into an instrument first, and the codec
/// that would do it is not understood — so the map says what answers the key and that
/// nothing will come out.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Sounds {
    Now,
    NotUntilBuilt,
}

/// What the key map was asked for this frame.
pub enum MapAct {
    /// A handle moved: every band's `(low, top)` after the clamp, in the order given.
    Bounds(Vec<(u8, u8)>),
    /// A key was struck and a zone answers it, `semitones` from its root. `zone` is the
    /// row it stands on — see [`MapZone::row`].
    Struck { zone: usize, semitones: i16 },
}

/// The keyboard an instrument's zones are laid out over.
///
/// The format states no limit — a note is a byte — so this is the six octaves the
/// editor's own map covers, widened where a zone reaches past it.
const NSMP_SPAN: keys::Span = keys::Span { low: 24, high: 96 };

/// The stretch of keyboard the map must show: the default, and any zone outside it.
fn span(zones: &[MapZone], default: keys::Span) -> keys::Span {
    zones.iter().fold(default, |span, zone| keys::Span {
        low: span.low.min(zone.low),
        high: span.high.max(zone.top),
    })
}

/// The pinned key map: the zone bands, the keyboard under them, and what the last
/// struck key did.
///
/// Both instrument kinds draw this one map. `edges` is what the format lets a pointer
/// move — [`keys::Edges::TopOnly`] where a zone's low is derived from the zone below.
pub fn key_map(
    ui: &mut egui::Ui,
    state: &mut State,
    zones: &[MapZone],
    default: keys::Span,
    edges: keys::Edges,
    sounds: Sounds,
) -> Option<MapAct> {
    let now = ui.input(|input| input.time);
    if state.audition.as_ref().is_some_and(|held| !held.live(now)) {
        state.audition = None;
        state.answer = None;
    }
    let span = span(zones, default);
    let bounds: Vec<(u8, u8)> = zones.iter().map(|zone| (zone.low, zone.top)).collect();
    let silent = keys::gaps(&bounds, span);
    let visuals = ui.visuals().clone();
    let (cover, ink) = match silent.len() {
        0 => ("every key answered".to_string(), app::good(&visuals)),
        1 => ("1 silent range".to_string(), app::warn(&visuals)),
        n => (format!("{n} silent ranges"), app::warn(&visuals)),
    };
    let reading = format!(
        "{cover} · {}{}",
        note::name(span.low),
        note::name(span.high)
    );
    controls::heading(
        ui,
        "Key map",
        match edges {
            keys::Edges::Fixed => "these zones cannot be written; click a key to hear one",
            keys::Edges::TopOnly => {
                "v2 derives each low from the zone below — drag a top; click a key to hear it"
            }
            keys::Edges::Both => {
                "drag either edge; pull zones apart to leave keys silent, never to \
                 overlap; click a key to hear it"
            }
        },
        Some((&reading, ink)),
    );

    let mut act = None;
    let lane: Vec<keys::Band> = zones
        .iter()
        .map(|zone| keys::Band {
            low: zone.low,
            top: zone.top,
            name: zone.name.clone(),
            range_text: format!("{}{}", note::name(zone.low), note::name(zone.top)),
            hint: format!(
                "{} · root {} · answers {}{}",
                zone.name,
                note::name(zone.root),
                note::name(zone.low),
                note::name(zone.top)
            ),
        })
        .collect();
    // A band is not a row, so both the highlight going in and the pick coming out are
    // translated — see [`MapZone::row`].
    let band_of =
        |row: Option<usize>| row.and_then(|row| zones.iter().position(|zone| zone.row == row));
    match keys::bands(
        ui,
        span,
        &lane,
        band_of(state.selected),
        band_of(state.lit()),
        edges,
    ) {
        Some(keys::BandAct::Pick(band)) => state.pick(zones[band].row, true),
        Some(keys::BandAct::Drag { bounds, .. }) => act = Some(MapAct::Bounds(bounds)),
        None => {}
    }

    let marks: Vec<keys::Mark> = zones
        .iter()
        .map(|zone| keys::Mark {
            note: zone.root,
            label: Some(note::name(zone.root)),
        })
        .collect();
    let struck = keys::keyboard(
        ui,
        span,
        state.audition.as_ref().map(|held| held.note),
        &marks,
    );
    if let Some(note) = struck {
        state.audition = Some(keys::Audition::new(note, now));
        let answer = answered(zones, note, sounds);
        if let (true, Some(row)) = (answer.sounded, answer.zone) {
            act = Some(MapAct::Struck {
                zone: row,
                semitones: answer.semitones,
            });
        }
        state.answer = Some(answer);
    }
    if let Some(held) = &state.audition {
        let left = keys::Audition::HOLD - (now - held.started);
        ui.ctx()
            .request_repaint_after(std::time::Duration::from_secs_f64(left.max(0.0)));
    }
    if let Some(answer) = &state.answer {
        status(ui, &answer.words, answer.sounded);
    }
    ui.add_space(8.0);
    act
}

/// What striking `note` at [`keys::AUDITION_VELOCITY`] does, and the sentence that says
/// so.
///
/// A key outside every zone is silent, and so is one inside a zone whose velocity
/// window the struck velocity is outside.
fn answered(zones: &[MapZone], note: u8, sounds: Sounds) -> Answer {
    let velocity = keys::AUDITION_VELOCITY;
    let Some(zone) = zones
        .iter()
        .find(|zone| note >= zone.low && note <= zone.top)
    else {
        return Answer {
            zone: None,
            semitones: 0,
            words: format!("{} — no zone answers this key; silence.", note::name(note)),
            sounded: false,
        };
    };
    let found = format!(
        "{} at vel {velocity}{} · root {} · {}",
        note::name(note),
        zone.name,
        note::name(zone.root),
        keys::shifted(note, zone.root)
    );
    let (words, sounded) = match (zone.velocity, sounds) {
        (Some((low, high)), _) if !(low..=high).contains(&velocity) => (
            format!("{found} — outside its velocity window {low}{high}; silence"),
            false,
        ),
        (_, Sounds::NotUntilBuilt) => (
            format!("{found} — a project is built into an instrument before it plays"),
            false,
        ),
        (_, Sounds::Now) => (found, true),
    };
    Answer {
        zone: Some(zone.row),
        semitones: i16::from(note) - i16::from(zone.root),
        words,
        sounded,
    }
}

/// The one sentence under the keyboard: what the struck key did.
fn status(ui: &mut egui::Ui, words: &str, sounded: bool) {
    const TEXT: f32 = 11.0;
    const GLYPH: f32 = 12.0;
    ui.horizontal(|ui| {
        ui.add_space(PAD);
        ui.spacing_mut().item_spacing.x = 7.0;
        let (glyph, ink) = match sounded {
            true => (Glyph::AudioLines, app::good(ui.visuals())),
            false => (Glyph::CircleAlert, app::warn(ui.visuals())),
        };
        icon(ui, glyph, GLYPH, ink);
        ui.label(
            egui::RichText::new(words)
                .size(TEXT)
                .color(ui.visuals().weak_text_color()),
        );
    });
}

/// The map over a sample instrument. The sets are the bands a drag moved.
pub fn map(
    ui: &mut egui::Ui,
    state: &mut State,
    snapshot: &Snapshot,
    sets: &mut Sets,
) -> Option<Ask> {
    let zones = map_zones(snapshot);
    let stated = snapshot.zones.iter().all(|zone| zone.low_note.is_some());
    let edges = match (snapshot.zones_editable, stated) {
        (false, _) => keys::Edges::Fixed,
        (true, true) => keys::Edges::Both,
        (true, false) => keys::Edges::TopOnly,
    };
    match key_map(ui, state, &zones, NSMP_SPAN, edges, Sounds::Now)? {
        MapAct::Struck { zone, semitones } => Some(Ask::Strike { zone, semitones }),
        MapAct::Bounds(bounds) => {
            if snapshot.zones_editable {
                sets.extend(moved(&snapshot.zones, &bounds));
            }
            None
        }
    }
}

/// What a moved band writes: the ends that changed, and only the ones the record
/// states — a v2 low is derived from the zone below and follows the top that moved it.
fn moved(zones: &[Zone], bounds: &[(u8, u8)]) -> Sets {
    let mut sets = Sets::new();
    for (index, (low, top)) in bounds.iter().enumerate() {
        let Some(zone) = zones.get(index) else {
            continue;
        };
        let n = index + 1;
        if *top != zone.top_note {
            sets.push((format!("zone{n}.top_note"), note::name(*top)));
        }
        if zone.low_note.is_some_and(|held| held != *low) {
            sets.push((format!("zone{n}.low_note"), note::name(*low)));
        }
    }
    sets
}

/// The instrument's zones as the map states them, with each v2 low derived.
fn map_zones(snapshot: &Snapshot) -> Vec<MapZone> {
    snapshot
        .zones
        .iter()
        .enumerate()
        .map(|(index, zone)| MapZone {
            row: index,
            low: bottom(&snapshot.zones, index).unwrap_or(NSMP_SPAN.low),
            top: zone.top_note,
            root: zone.root_key,
            name: format!("Zone {}", index + 1),
            velocity: zone.velocity,
        })
        .collect()
}

/// One row of the zone list, in the words it prints.
pub struct RowSpec {
    pub name: String,
    pub answers: String,
    /// The middle column: what this zone is made of.
    pub facts: String,
    pub size: String,
    pub hint: String,
}

const ROW_H: f32 = 26.0;
const MARK: f32 = 6.0;
/// How far an open row's body is indented, measured from the page's edge.
const INDENT: f32 = 68.0;
/// Which of the five columns holds the size, which is the one set right to left.
const SIZE_COLUMN: usize = 3;
const ROW_MONO: f32 = 11.0;
const FACTS_TEXT: f32 = 11.0;
const SIZE_TEXT: f32 = 10.5;
const CHEVRON: f32 = 12.0;

/// The five columns of the row grid: the name, what the zone answers, what it is made
/// of, its size, and the chevron's own.
const GRID: [Width; 5] = [
    Width::Fixed(56.0),
    Width::Share(1.1),
    Width::Share(1.5),
    Width::Fixed(74.0),
    Width::Fixed(20.0),
];

/// The zone list: the column heads, one row per zone, and the body of each open row
/// drawn by `open`.
///
/// `heads` names the first and third columns, which are the two that differ between an
/// instrument and the project it was built from.
pub fn rows(
    ui: &mut egui::Ui,
    state: &mut State,
    heads: (&str, &str),
    specs: &[RowSpec],
    mut open: impl FnMut(&mut egui::Ui, usize),
) {
    let visuals = ui.visuals().clone();
    let hairline = egui::Stroke::new(1.0_f32, visuals.widgets.noninteractive.bg_stroke.color);
    table::heads(
        ui,
        GRID,
        [heads.0, "Answers", heads.1, "Size", ""],
        &[SIZE_COLUMN],
    );

    let lit = state.lit();
    for (index, spec) in specs.iter().enumerate() {
        let picked = state.selected == Some(index);
        let (rect, response) = ui.allocate_exact_size(
            egui::vec2(ui.available_width(), ROW_H),
            egui::Sense::click(),
        );
        if state.reveal == Some(index) {
            state.reveal = None;
            response.scroll_to_me(Some(egui::Align::TOP));
        }
        if response.clicked() {
            state.pick(index, false);
        }
        let painter = ui.painter();
        if picked {
            painter.rect_filled(rect, 0.0, visuals.selection.bg_fill);
        } else if response.hovered() {
            painter.rect_filled(rect, 0.0, visuals.widgets.hovered.weak_bg_fill);
        }
        painter.hline(rect.x_range(), rect.bottom() - 0.5, hairline);
        // ⚠️ Every cell of a selected row takes the selection's own ink: the track
        // colour behind it is not readable text.
        let ink = match picked {
            true => visuals.selection.stroke.color,
            false => visuals.weak_text_color(),
        };
        let cells = table::columns(rect, GRID);
        let dot = match (picked, lit == Some(index)) {
            (_, true) => app::good(&visuals),
            (true, false) => app::accent(&visuals),
            (false, false) => app::caption(&visuals),
        };
        painter.circle_filled(
            egui::pos2(cells[0].0 + MARK / 2.0, rect.center().y),
            MARK / 2.0,
            dot,
        );
        let written = [
            (
                spec.name.as_str(),
                egui::FontId::new(NAME_TEXT, app::bold()),
                cells[0].0 + MARK + 6.0,
                cells[0].1 - MARK - 6.0,
                false,
            ),
            (
                spec.answers.as_str(),
                egui::FontId::monospace(ROW_MONO),
                cells[1].0,
                cells[1].1,
                false,
            ),
            (
                spec.facts.as_str(),
                egui::FontId::proportional(FACTS_TEXT),
                cells[2].0,
                cells[2].1,
                false,
            ),
            (
                spec.size.as_str(),
                egui::FontId::monospace(SIZE_TEXT),
                cells[3].0,
                cells[3].1,
                true,
            ),
        ];
        for (text, font, left, width, right) in written {
            let mut job = egui::text::LayoutJob::default();
            job.append(text, 0.0, egui::TextFormat::simple(font, ink));
            job.wrap = egui::text::TextWrapping::truncate_at_width(width.max(0.0));
            let galley = painter.layout_job(job);
            let at = match right {
                true => left + width - galley.size().x,
                false => left,
            };
            painter.galley(
                egui::pos2(at, rect.center().y - galley.size().y / 2.0),
                galley,
                ink,
            );
        }
        let glyph = match state.open && picked {
            true => Glyph::ChevronDown,
            false => Glyph::ChevronRight,
        };
        crate::icon::painted(
            ui,
            glyph,
            egui::Rect::from_center_size(
                egui::pos2(cells[4].0 + cells[4].1 - CHEVRON / 2.0, rect.center().y),
                egui::Vec2::splat(CHEVRON),
            ),
            ink,
        );
        if !spec.hint.is_empty() {
            response.on_hover_text(&spec.hint);
        }

        if state.open && picked {
            let body = egui::Frame::new()
                .fill(visuals.window_fill)
                .inner_margin(egui::Margin {
                    left: INDENT as i8,
                    right: PAD as i8,
                    top: 10,
                    bottom: 12,
                });
            body.show(ui, |ui| {
                ui.set_width(ui.available_width());
                open(ui, index);
            });
            ui.painter()
                .hline(rect.x_range(), ui.min_rect().bottom() - 0.5, hairline);
        }
    }
}

const LABEL_TEXT: f32 = 9.5;
const VALUE_TEXT: f32 = 11.5;
const ACTION_H: f32 = 20.0;
const ACTION_TEXT: f32 = 11.0;
const ACTION_GLYPH: f32 = 12.0;
const RADIUS: f32 = 2.0;

/// A run of cells across an open row, wrapping where the window is narrow.
pub fn strip(ui: &mut egui::Ui, body: impl FnOnce(&mut egui::Ui)) {
    let row = egui::Layout::left_to_right(egui::Align::TOP).with_main_wrap(true);
    ui.with_layout(row, |ui| {
        ui.spacing_mut().item_spacing = egui::vec2(20.0, 10.0);
        body(ui);
    });
}

/// One cell: a MICRO-caps label with what it names under it.
pub fn cell(ui: &mut egui::Ui, label: &str, width: f32, body: impl FnOnce(&mut egui::Ui)) {
    ui.allocate_ui(egui::vec2(width, 0.0), |ui| {
        ui.vertical(|ui| {
            ui.spacing_mut().item_spacing.y = 4.0;
            ui.label(
                egui::RichText::new(label.to_uppercase())
                    .size(LABEL_TEXT)
                    .color(app::caption(ui.visuals())),
            );
            body(ui);
        });
    });
}

/// A cell whose value the file states and nothing here writes.
///
/// It asks for the room its own words need: a read cell has no control to size it by.
pub fn read_cell(ui: &mut egui::Ui, label: &str, value: &str, note: &str) {
    let laid = |text: &str, font: egui::FontId| {
        ui.fonts(|fonts| {
            fonts
                .layout_no_wrap(text.to_string(), font, egui::Color32::PLACEHOLDER)
                .size()
                .x
        })
    };
    let width = laid(value, egui::FontId::monospace(VALUE_TEXT)) + 20.0;
    let width = width
        .max(laid(
            &label.to_uppercase(),
            egui::FontId::proportional(LABEL_TEXT),
        ))
        .max(laid(note, egui::FontId::proportional(10.0)));
    cell(ui, label, width, |ui| {
        ui.horizontal(|ui| {
            ui.spacing_mut().item_spacing.x = 5.0;
            icon(ui, Glyph::Eye, 11.0, app::caption(ui.visuals()));
            ui.label(
                egui::RichText::new(value)
                    .font(egui::FontId::monospace(VALUE_TEXT))
                    .color(ui.visuals().weak_text_color()),
            );
        });
        if !note.is_empty() {
            ui.label(
                egui::RichText::new(note)
                    .size(10.0)
                    .color(app::caption(ui.visuals())),
            );
        }
    });
}

/// One outlined action of an open row. `accent` is whether its glyph is the loud one.
pub fn action(ui: &mut egui::Ui, label: &str, glyph: Glyph, accent: bool) -> bool {
    let visuals = ui.visuals().clone();
    let painter = ui.painter().clone();
    let ink = visuals.weak_text_color();
    let word = painter.layout_no_wrap(
        label.to_string(),
        egui::FontId::proportional(ACTION_TEXT),
        ink,
    );
    let (rect, response) = ui.allocate_exact_size(
        egui::vec2(ACTION_GLYPH + word.size().x + 21.0, ACTION_H),
        egui::Sense::click(),
    );
    if response.hovered() {
        painter.rect_filled(rect, RADIUS, visuals.widgets.hovered.weak_bg_fill);
    }
    painter.rect_stroke(
        rect,
        RADIUS,
        egui::Stroke::new(1.0_f32, visuals.widgets.noninteractive.bg_stroke.color),
        egui::StrokeKind::Inside,
    );
    crate::icon::painted(
        ui,
        glyph,
        egui::Rect::from_center_size(
            egui::pos2(rect.left() + 8.0 + ACTION_GLYPH / 2.0, rect.center().y),
            egui::Vec2::splat(ACTION_GLYPH),
        ),
        match accent {
            true => app::accent(&visuals),
            false => app::caption(&visuals),
        },
    );
    painter.galley(
        egui::pos2(
            rect.left() + 8.0 + ACTION_GLYPH + 5.0,
            rect.center().y - word.size().y / 2.0,
        ),
        word,
        ink,
    );
    response.clicked()
}

/// What the document knows about one zone's audio while it draws the zone.
pub struct Sound<'a> {
    pub decoded: Option<&'a Result<Decoded, String>>,
    pub playing: bool,
}

/// The zones, the per-key map, and the preset the loader installs.
///
/// The key map is pinned above this by the document; the name is the header's.
pub fn ui(
    ui: &mut egui::Ui,
    state: &mut State,
    snapshot: &Snapshot,
    sounds: &[Sound],
    sets: &mut Sets,
) -> Option<Ask> {
    let mut ask = None;
    if !snapshot.zones.is_empty() && !snapshot.zones_editable {
        ui.horizontal_wrapped(|ui| {
            ui.add_space(PAD);
            ui.label(
                egui::RichText::new(
                    "This instrument's keyboard map cannot be read, so its zones are \
                     shown rather than changed. The name is still yours to set.",
                )
                .size(FACTS_TEXT)
                .color(app::caption(ui.visuals())),
            );
        });
    }

    velocity(ui, state, snapshot);

    let quiet = app::caption(ui.visuals());
    controls::heading(
        ui,
        "Zones",
        "click a row for its fields",
        Some((&format!("{} zones", snapshot.zones.len()), quiet)),
    );
    let specs: Vec<RowSpec> = snapshot
        .zones
        .iter()
        .enumerate()
        .map(|(index, zone)| RowSpec {
            name: format!("Zone {}", index + 1),
            answers: range(&snapshot.zones, index),
            facts: facts_of(zone, sounds.get(index)),
            size: room::measure(zone.bytes as u64),
            hint: format!("root {}", note::name(zone.root_key)),
        })
        .collect();
    rows(ui, state, ("Zone", "Stroke"), &specs, |ui, index| {
        let zone = &snapshot.zones[index];
        let n = index + 1;
        strip(ui, |ui| {
            ui.add_enabled_ui(snapshot.zones_editable, |ui| {
                cell(ui, "Root key", 72.0, |ui| {
                    if let Some(note) = note_picker(ui, ("root", n), zone.root_key) {
                        sets.push((format!("zone{n}.root_key"), note::name(note)));
                    }
                });
                cell(ui, "Top note", 72.0, |ui| {
                    if let Some(note) = note_picker(ui, ("top", n), zone.top_note) {
                        sets.push((format!("zone{n}.top_note"), note::name(note)));
                    }
                });
            });
            match zone.low_note {
                Some(low) => {
                    ui.add_enabled_ui(snapshot.zones_editable, |ui| {
                        cell(ui, "Low note", 72.0, |ui| {
                            if let Some(note) = note_picker(ui, ("low", n), low) {
                                sets.push((format!("zone{n}.low_note"), note::name(note)));
                            }
                        });
                    });
                }
                None => read_cell(
                    ui,
                    "Low note",
                    &note::name(bottom(&snapshot.zones, index).unwrap_or(NSMP_SPAN.low)),
                    "derived from the zone below",
                ),
            }
            if let Some((low, high)) = zone.velocity {
                read_cell(
                    ui,
                    "Velocity window",
                    &format!("{low}{high}"),
                    "stated by the record",
                );
            }
            if let Some(gain) = zone.gain {
                read_cell(
                    ui,
                    "Gain",
                    &decibels(gain_db(gain, zone::GAIN_UNITY)),
                    "the zone record's own",
                );
            }
        });
        ui.add_space(10.0);
        if let Some(sound) = sounds.get(index) {
            if let Some(asked) = zone_audio(ui, index, sound) {
                ask = Some(asked);
            }
        }
    });

    if let Some(table) = &snapshot.key_table {
        per_key(ui, state, snapshot, table, sets);
    }
    if !snapshot.sound.is_empty() {
        controls::heading(
            ui,
            "Sound parameters",
            "the loader's preset for this category, read only",
            None,
        );
        ui.horizontal_wrapped(|ui| {
            ui.add_space(PAD);
            strip(ui, |ui| {
                for (label, value) in &snapshot.sound {
                    read_cell(ui, label, value, "");
                }
            });
        });
        ui.add_space(8.0);
    }
    ask
}

/// The key × velocity field, on the generations whose records state a window.
///
/// Read-only: `nord-format` has no setter for a wide zone's window, and every shipped
/// instrument answers the whole of it. Clicking a block still opens its row.
fn velocity(ui: &mut egui::Ui, state: &mut State, snapshot: &Snapshot) {
    let stated: Vec<(usize, &Zone)> = snapshot
        .zones
        .iter()
        .enumerate()
        .filter(|(_, zone)| zone.velocity.is_some())
        .collect();
    if stated.is_empty() {
        return;
    }
    let blocks: Vec<keys::VelBlock> = stated
        .iter()
        .map(|(row, zone)| {
            let window = zone
                .velocity
                .unwrap_or((keys::VELOCITY_LOW, keys::VELOCITY_HIGH));
            keys::VelBlock {
                low: bottom(&snapshot.zones, *row).unwrap_or(NSMP_SPAN.low),
                top: zone.top_note,
                window,
                name: format!("Zone {}", row + 1),
                hint: format!(
                    "Zone {} answers at velocity {}{} · the record states it and nothing \
                     here writes it",
                    row + 1,
                    window.0,
                    window.1
                ),
            }
        })
        .collect();
    let rows: Vec<usize> = stated.iter().map(|(row, _)| *row).collect();
    let asked = velocity_field(
        ui,
        "every stroke answers the full window",
        span(&map_zones(snapshot), NSMP_SPAN),
        &blocks,
        &rows,
        keys::Handles::Fixed,
        state.selected,
    );
    match asked {
        Some(VelocityAsk::Open(row)) => state.pick(row, true),
        // Fixed handles never move: nothing here writes a wide zone's window.
        Some(VelocityAsk::Window { .. }) | None => {}
    }
}

/// What the velocity field was asked for, in the rows the caller knows.
pub(super) enum VelocityAsk {
    /// Open this row.
    Open(usize),
    /// A handle moved: which block, and the window the drag left it with.
    Window { block: usize, window: (u8, u8) },
}

/// The key × velocity field both documents draw: the heading with what the windows
/// cover, and the field itself.
///
/// `rows` names the row each block stands for, one per block, and `selected` is the row
/// to highlight — the blocks are the zones that state a window, which on neither
/// document is every row.
pub(super) fn velocity_field(
    ui: &mut egui::Ui,
    note: &str,
    span: keys::Span,
    blocks: &[keys::VelBlock],
    rows: &[usize],
    handles: keys::Handles,
    selected: Option<usize>,
) -> Option<VelocityAsk> {
    let visuals = ui.visuals().clone();
    let (cover, ink) = match keys::velocity_holes(blocks).len() {
        0 => ("fully covered".to_string(), app::good(&visuals)),
        1 => ("1 hole".to_string(), app::warn(&visuals)),
        n => (format!("{n} holes"), app::warn(&visuals)),
    };
    controls::heading(ui, "Velocity", note, Some((&cover, ink)));
    let picked = selected.and_then(|row| rows.iter().position(|held| *held == row));
    let acted = ui
        .horizontal(|ui| {
            ui.add_space(PAD);
            let room = (ui.available_width() - PAD).max(64.0);
            ui.allocate_ui(egui::vec2(room, 0.0), |ui| {
                keys::velocity(ui, span, blocks, picked, handles)
            })
            .inner
        })
        .inner;
    ui.add_space(8.0);
    match acted? {
        keys::VelocityAct::Pick(block) => Some(VelocityAsk::Open(rows[block])),
        keys::VelocityAct::Drag { zone, window, .. } => Some(VelocityAsk::Window {
            block: zone,
            window,
        }),
    }
}

/// The middle column of a zone's row: what the record states, and what a decode found.
fn facts_of(zone: &Zone, sound: Option<&Sound>) -> String {
    let mut parts = Vec::new();
    if let Some(gain) = zone.gain {
        parts.push(decibels(gain_db(gain, zone::GAIN_UNITY)));
    }
    if let Some((low, high)) = zone.velocity {
        parts.push(format!("vel {low}{high}"));
    }
    if let Some(Ok(decoded)) = sound.and_then(|sound| sound.decoded) {
        parts.push(format!("{:.3} s", decoded.audio.seconds()));
        parts.push(match decoded.audio.channels {
            1 => "mono".to_string(),
            2 => "stereo".to_string(),
            n => format!("{n} channels"),
        });
    }
    parts.join(" · ")
}

/// A gain reading, with the silence a zero field means spelled out rather than as an
/// infinity.
pub(super) fn decibels(db: f64) -> String {
    match db.is_finite() {
        true => format!("{db:+.1} dB"),
        false => "silent".to_string(),
    }
}

/// The actions of an open zone, and the envelope once it is decoded.
fn zone_audio(ui: &mut egui::Ui, index: usize, sound: &Sound) -> Option<Ask> {
    let mut ask = None;
    ui.horizontal_wrapped(|ui| {
        ui.spacing_mut().item_spacing = egui::vec2(6.0, 6.0);
        match (sound.playing, sound.decoded) {
            // ⚠️ Stopping cannot wait on a decode: an edit drops the audio of a zone
            // that goes on sounding, and this is the only control that stops it.
            (true, _) => {
                if action(ui, "Stop", Glyph::X, true) {
                    ask = Some(Ask::Play(index));
                }
            }
            (false, None) => {
                if action(ui, "Show audio", Glyph::AudioLines, true) {
                    ask = Some(Ask::Decode(index));
                }
            }
            (false, Some(Err(why))) => {
                ui.label(
                    egui::RichText::new(format!("not decoded: {why}"))
                        .size(FACTS_TEXT)
                        .color(crate::app::bad(ui.visuals())),
                );
            }
            (false, Some(Ok(_))) => {
                if action(ui, "Play", Glyph::AudioLines, true) {
                    ask = Some(Ask::Play(index));
                }
            }
        }
        if matches!(sound.decoded, Some(Ok(_))) && action(ui, "Save WAV…", Glyph::Waves, false) {
            ask = Some(Ask::Save(index));
        }
    });
    if let Some(Ok(decoded)) = sound.decoded {
        ui.add_space(8.0);
        waveform(ui, &decoded.envelope, sound.playing);
    }
    ask
}

const LANE_LABEL_W: f32 = 80.0;
const LANE_AXIS_W: f32 = 34.0;
const LANE_SUMMARY_W: f32 = 150.0;
const LANE_H: f32 = 38.0;
const AXIS_TEXT: f32 = 9.0;
const SUMMARY_TEXT: f32 = 10.0;
/// What a full-deflection per-key gain and detune read as.
const GAIN_FULL: f32 = 3.0;
const DETUNE_FULL: i32 = 25;

/// The two per-key lanes and the table under them.
///
/// A drag across a lane paints values over the ones the file holds; a key reads as
/// painted when its record differs from the one the asset was last saved with, so the
/// count and the accent bars survive a frame.
fn per_key(
    ui: &mut egui::Ui,
    state: &mut State,
    snapshot: &Snapshot,
    table: &KeyTable,
    sets: &mut Sets,
) {
    let span = span(&map_zones(snapshot), NSMP_SPAN);
    let quiet = app::caption(ui.visuals());
    controls::heading(
        ui,
        "Per key",
        "drag across a lane to draw values; the table below edits one key at a time",
        Some((
            &format!(
                "instrument {}",
                decibels(gain_db(table.instrument.gain(), keymap::GAIN_UNITY))
            ),
            quiet,
        )),
    );
    let baseline = state
        .baseline
        .as_ref()
        .and_then(|saved| saved.table.as_ref());
    for (label, field, scale) in [
        ("Gain", "gain", keys::Scale::Db(GAIN_FULL)),
        ("Detune", "detune", keys::Scale::Cents(DETUNE_FULL)),
    ] {
        let values: Vec<f32> = (span.low..=span.high)
            .map(|note| held(table, note, scale))
            .collect();
        let painted: Vec<bool> = (span.low..=span.high)
            .map(|note| match baseline {
                Some(saved) => table.key(note).ok() != saved.key(note).ok(),
                None => false,
            })
            .collect();
        let edited = painted.iter().filter(|edited| **edited).count();
        ui.horizontal(|ui| {
            ui.add_space(PAD);
            ui.spacing_mut().item_spacing.x = 8.0;
            ui.allocate_ui(egui::vec2(LANE_LABEL_W, LANE_H), |ui| {
                ui.label(
                    egui::RichText::new(label)
                        .size(FACTS_TEXT)
                        .color(ui.visuals().weak_text_color()),
                );
            });
            axis(ui, scale);
            let room = (ui.available_width() - LANE_SUMMARY_W - 8.0).max(64.0);
            let drawn = ui
                .allocate_ui(egui::vec2(room, LANE_H), |ui| {
                    ui.push_id(field, |ui| keys::lane(ui, span, &values, &painted, scale))
                        .inner
                })
                .inner;
            for (note, value) in drawn {
                sets.push((format!("key{note}.{field}"), scale.format(value)));
            }
            let summary = format!(
                "{} keys · ±{}{}",
                span.keys(),
                match scale {
                    keys::Scale::Db(full) => format!("{full:.1} dB"),
                    keys::Scale::Cents(full) => format!("{full} c"),
                },
                match edited {
                    0 => String::new(),
                    n => format!(" · {n} edited"),
                }
            );
            let ink = match edited {
                0 => app::caption(ui.visuals()),
                _ => app::warn(ui.visuals()),
            };
            ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                ui.label(
                    egui::RichText::new(summary)
                        .font(egui::FontId::monospace(SUMMARY_TEXT))
                        .color(ink),
                );
            });
        });
        ui.add_space(8.0);
    }
    key_table(ui, state, table);
    ui.add_space(8.0);
}

/// One key's stored value on the lane's own scale, clamped to what it can draw.
fn held(table: &KeyTable, note: u8, scale: keys::Scale) -> f32 {
    let Ok(level) = table.key(note) else {
        return 0.0;
    };
    let value = match scale {
        keys::Scale::Db(full) => gain_db(level.gain(), keymap::GAIN_UNITY) / f64::from(full),
        keys::Scale::Cents(full) => detune_cents(level.detune()) / f64::from(full),
    };
    match value.is_finite() {
        true => value.clamp(-1.0, 1.0) as f32,
        false => -1.0,
    }
}

/// The lane's own axis column: the two ends of its scale, and the zero between them.
fn axis(ui: &mut egui::Ui, scale: keys::Scale) {
    let (top, bottom) = scale.axis_labels();
    ui.allocate_ui(egui::vec2(LANE_AXIS_W, LANE_H), |ui| {
        ui.vertical(|ui| {
            ui.spacing_mut().item_spacing.y = 4.0;
            for text in [top, "0".to_string(), bottom] {
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    ui.label(
                        egui::RichText::new(text)
                            .font(egui::FontId::monospace(AXIS_TEXT))
                            .color(app::caption(ui.visuals())),
                    );
                });
            }
        });
    });
}

const TABLE_ROW: f32 = 20.0;
const TABLE_TEXT: f32 = 10.5;
const TABLE_COLUMNS: usize = 4;
const TABLE_KEY_W: f32 = 34.0;

/// Every key's record, four columns across, behind a fold.
fn key_table(ui: &mut egui::Ui, state: &mut State, table: &KeyTable) {
    ui.horizontal(|ui| {
        ui.add_space(PAD);
        ui.spacing_mut().item_spacing.x = 6.0;
        let glyph = match state.table {
            true => Glyph::ChevronDown,
            false => Glyph::ChevronRight,
        };
        let quiet = app::caption(ui.visuals());
        icon(ui, glyph, CHEVRON, quiet);
        let label = match state.table {
            true => "Hide the per-key table",
            false => "Show the 128-key gain and detune table",
        };
        if ui
            .add(
                egui::Label::new(
                    egui::RichText::new(label)
                        .size(FACTS_TEXT)
                        .color(ui.visuals().weak_text_color()),
                )
                .sense(egui::Sense::click()),
            )
            .clicked()
        {
            state.table = !state.table;
        }
    });
    if !state.table {
        return;
    }
    let per_column = keymap::KEYS.div_ceil(TABLE_COLUMNS);
    let visuals = ui.visuals().clone();
    ui.add_space(6.0);
    let (rect, _) = ui.allocate_exact_size(
        egui::vec2(ui.available_width(), per_column as f32 * TABLE_ROW),
        egui::Sense::hover(),
    );
    let painter = ui.painter();
    let inner = rect.shrink2(egui::vec2(PAD, 0.0));
    let column_w = inner.width() / TABLE_COLUMNS as f32;
    for note in 0..keymap::KEYS {
        let cell = egui::Rect::from_min_size(
            egui::pos2(
                inner.left() + (note / per_column) as f32 * column_w,
                inner.top() + (note % per_column) as f32 * TABLE_ROW,
            ),
            egui::vec2(column_w - 18.0, TABLE_ROW),
        );
        painter.hline(
            cell.x_range(),
            cell.bottom() - 0.5,
            egui::Stroke::new(1.0_f32, visuals.widgets.noninteractive.bg_stroke.color),
        );
        let Ok(level) = table.key(note as u8) else {
            continue;
        };
        let loud = |quiet: bool| match quiet {
            true => app::caption(&visuals),
            false => visuals.text_color(),
        };
        let written = [
            (
                note::name(note as u8),
                visuals.weak_text_color(),
                cell.left(),
                false,
            ),
            (
                decibels(gain_db(level.gain(), keymap::GAIN_UNITY)),
                loud(level.gain() == keymap::GAIN_UNITY),
                cell.left() + TABLE_KEY_W + (cell.width() - TABLE_KEY_W) / 2.0,
                true,
            ),
            (
                format!("{:+.0} c", detune_cents(level.detune())),
                loud(level.detune() == 0),
                cell.right(),
                true,
            ),
        ];
        for (text, ink, at, right) in written {
            let galley = painter.layout_no_wrap(text, egui::FontId::monospace(TABLE_TEXT), ink);
            let left = match right {
                true => at - galley.size().x,
                false => at,
            };
            painter.galley(
                egui::pos2(left, cell.center().y - galley.size().y / 2.0),
                galley,
                ink,
            );
        }
    }
}

/// Read the map the asset was last saved with, once per set of saved bytes.
///
/// ⚠️ Paint marks are the difference between what is held and what was saved, so the
/// baseline has to be the saved bytes rather than the working copy: measured against
/// itself, nothing is ever painted.
pub fn follow(state: &mut State, id: u64, saved: &Baseline) {
    let of = (id, saved.crc32, saved.bytes.len());
    if state.baseline.as_ref().is_some_and(|held| held.of == of) {
        return;
    }
    let table = nord_format::from_stream(&mut Cursor::new(&saved.bytes))
        .ok()
        .as_ref()
        .and_then(sample)
        .and_then(|sample| match sample {
            Sample::V2(body) => body.key_table().ok(),
            Sample::V3(_) => None,
        });
    state.baseline = Some(Saved { of, table });
}

/// The identity cell an instrument puts on the header: what the file states about
/// itself that the strip cannot carry.
///
/// Both are read-only because neither has a setter: the `cat` section and the wide
/// chain's second name are shown as the file holds them.
pub fn stated(entity: &Entity) -> Option<Cell> {
    match sample(entity)? {
        Sample::V2(body) => {
            let categories = body.categories();
            match categories.is_empty() {
                true => None,
                false => Some(Cell {
                    label: "Category",
                    body: Body::Read(categories.join(", ")),
                    note: None,
                    hint: "the cat section, as the file holds it",
                }),
            }
        }
        Sample::V3(body) => {
            let sub = body.sub_name().unwrap_or_default();
            match sub.trim().is_empty() {
                true => None,
                false => Some(Cell {
                    label: "Sub name",
                    body: Body::Read(sub),
                    note: None,
                    hint: "the vendor's second name",
                }),
            }
        }
    }
}

/// What the file says about itself.
pub fn metadata(ui: &mut egui::Ui, snapshot: &Snapshot) {
    controls::heading(
        ui,
        "Metadata",
        "what the file says about itself — read here, never written differently",
        None,
    );
    let mut rows = vec![
        Fact {
            key: "Format",
            value: format!("nsmp {}", snapshot.generation),
            note: "a sample instrument",
        },
        Fact {
            key: "Content version",
            value: snapshot.version.to_string(),
            note: "what decides which fields this format has",
        },
        Fact {
            key: "Name in file",
            value: format!(
                "{} B, {} used",
                snapshot.max_name_len,
                snapshot.name.trim_end().len()
            ),
            note: "the whole field is written; the rest is padding",
        },
        Fact {
            key: "Zones",
            value: format!("{} × {} B", snapshot.zones.len(), snapshot.record_len),
            note: "one record per zone, in the map section",
        },
        Fact {
            key: "Channels",
            value: "per stroke".to_string(),
            note: "shown on each zone once it is decoded",
        },
    ];
    if !snapshot.sub_name.trim().is_empty() {
        rows.push(Fact {
            key: "Sub name",
            value: snapshot.sub_name.clone(),
            note: "the vendor's second name",
        });
    }
    if snapshot.key_table.is_some() {
        rows.push(Fact {
            key: "Keyboard map",
            value: format!("{} × {} B", keymap::KEYS, keymap::RECORD_LEN),
            note: "gain and detune per key, ahead of the zone table",
        });
    }
    super::capability::facts(ui, &rows);
}

/// The nineteen capabilities of the instrument editor, as this generation stands in
/// them.
///
/// `Editable` is a field a control on the Edit face writes or an act it performs,
/// `ReadOnly` a field the format states and nothing here writes, `Absent` a field the
/// format does not have at all. The table is checked against the paths [`set`] accepts —
/// see the tests.
pub fn capabilities(generation: &str) -> Vec<Row> {
    let v2 = generation == "v2";
    let row = |name: &'static str, state: Cap, note: &'static str| Row { name, state, note };
    vec![
        row(
            "name",
            Cap::Editable,
            match v2 {
                true => "32 B in the hdr section",
                false => "66 B in the hdr section",
            },
        ),
        row(
            "category / sub",
            Cap::ReadOnly,
            match v2 {
                true => "the cat section, on the identity row",
                false => "the sub name, on the identity row",
            },
        ),
        row(
            "key zones: root / top / low",
            Cap::Editable,
            match v2 {
                true => "the zone table; each low is derived from the zone below",
                false => "the wide zone record states all three",
            },
        ),
        row(
            "velocity layers",
            match v2 {
                true => Cap::Absent,
                false => Cap::ReadOnly,
            },
            match v2 {
                true => "dropped by v2",
                false => "one wide window per zone, and no setter for it",
            },
        ),
        row(
            "per-zone gain / detune",
            match v2 {
                true => Cap::ReadOnly,
                false => Cap::Absent,
            },
            match v2 {
                true => "a u24 gain in the record, and no detune beside it",
                false => "a dB float in the stroke header, which is not decoded",
            },
        ),
        row(
            "per-key table",
            match v2 {
                true => Cap::Editable,
                false => Cap::Absent,
            },
            match v2 {
                true => "128 records of gain and detune",
                false => "no keyboard map the wide layouts expose",
            },
        ),
        row(
            "instrument gain",
            match v2 {
                true => Cap::ReadOnly,
                false => Cap::Absent,
            },
            match v2 {
                true => "the map's own record, read on the Per key heading",
                false => "not in the wide chain's map",
            },
        ),
        row(
            "loop points / crossfade",
            Cap::Absent,
            "baked into the stroke, with no mark this editor reads",
        ),
        row(
            "loop decay / detune",
            Cap::Absent,
            match v2 {
                true => "dropped by v2",
                false => "in the stroke header, which is written back verbatim",
            },
        ),
        row("release samples", Cap::Absent, "a piano library's bank 2"),
        row(
            "pedal resonance samples",
            Cap::Absent,
            "a piano library's bank 1",
        ),
        row(
            "sound parameters",
            Cap::ReadOnly,
            "the sty preset the loader installs for this category",
        ),
        row(
            "stereo / channels",
            Cap::ReadOnly,
            "per stroke, and known once the zone is decoded",
        ),
        row(
            "replace / add a stroke",
            Cap::NeedsEncode,
            match v2 {
                true => "encode a WAV; played on hardware",
                false => "encode a WAV; byte-exact, never played here",
            },
        ),
        row(
            "cut / move / drop strokes",
            Cap::ReadOnly,
            "the zone table is read; nothing here removes a zone",
        ),
        row("decode / audition", Cap::Editable, "click a key, or a zone"),
        row(
            "size trim",
            Cap::ReadOnly,
            "each zone's bytes are read; nothing here drops one",
        ),
        row(
            "write to the instrument",
            Cap::Editable,
            match v2 {
                true => "the header's Queue send, a class 3 write",
                false => "the header's Queue send",
            },
        ),
        row(
            "byte-exact round trip",
            Cap::Verified,
            "every stroke is written back byte for byte",
        ),
    ]
}

/// Where each field the Edit face reads or writes lands in the file.
///
/// Every figure is one of `nord_format`'s own declarations rather than a measurement.
pub fn offsets(snapshot: &Snapshot) -> Vec<Offset> {
    let mut rows = vec![Offset {
        at: format!("map+{}", zone::COUNT_AT),
        holds: "u8".to_string(),
        note: "how many zone records follow",
    }];
    if snapshot.key_table.is_some() {
        rows.insert(
            0,
            Offset {
                at: "map+0".to_string(),
                holds: format!("{} B", keymap::RECORD_LEN),
                note: "the instrument's own gain and detune",
            },
        );
        rows.insert(
            1,
            Offset {
                at: format!("map+{}", keymap::KEY_TABLE_AT),
                holds: format!("{} × {} B", keymap::KEYS, keymap::RECORD_LEN),
                note: "a u24 gain and an s24 detune per MIDI note",
            },
        );
    }
    rows.push(Offset {
        at: format!("map+{}", zone::RECORDS_AT),
        holds: format!("{} × {} B", snapshot.zones.len(), snapshot.record_len),
        note: "root, top note, stroke id, gain, relative strength",
    });
    rows
}

/// Height of the drawn envelope.
const WAVE_HEIGHT: f32 = 44.0;

/// Draw an envelope across whatever width is left.
///
/// Painted from the theme's own colours rather than fixed ones: the trough is the panel's
/// extreme fill, the wave is the instrument's red while it is sounding and the body text
/// colour when it is not, so both themes stay legible.
pub fn waveform(ui: &mut egui::Ui, envelope: &[(f32, f32)], playing: bool) {
    let width = ui.available_width().max(64.0);
    let (rect, _) = ui.allocate_exact_size(egui::vec2(width, WAVE_HEIGHT), egui::Sense::hover());
    let visuals = ui.visuals();
    let painter = ui.painter();
    painter.rect_filled(rect, 2.0, visuals.extreme_bg_color);
    let middle = rect.center().y;
    painter.hline(
        rect.x_range(),
        middle,
        egui::Stroke::new(1.0_f32, crate::app::unlit(visuals)),
    );
    if envelope.is_empty() {
        return;
    }
    let ink = match playing {
        true => crate::app::accent(visuals),
        false => visuals.text_color(),
    };
    // The envelope has a fixed column count and the panel does not, so a column is as
    // wide as its share of the rect — never thinner than the pixel it has to cover.
    let column = (rect.width() / envelope.len() as f32).max(1.0);
    let half = rect.height() / 2.0 - 1.0;
    for (i, (low, high)) in envelope.iter().enumerate() {
        let x = rect.left() + rect.width() * i as f32 / envelope.len() as f32;
        let top = middle - high.clamp(-1.0, 1.0) * half;
        let bottom = middle - low.clamp(-1.0, 1.0) * half;
        painter.rect_filled(
            egui::Rect::from_min_max(
                egui::pos2(x, top),
                egui::pos2(x + column, bottom.max(top + 1.0)),
            ),
            0.0,
            ink,
        );
    }
}

/// A MIDI note as a name: `C4` is middle C. Typing a number works too.
pub fn note_picker(ui: &mut egui::Ui, id: (&str, usize), note: u8) -> Option<u8> {
    let mut value = note as f64;
    let response = ui.push_id(id, |ui| {
        ui.add(
            egui::DragValue::new(&mut value)
                .range(0.0..=127.0)
                .speed(0.2)
                .custom_formatter(|n, _| note::name(n as u8))
                .custom_parser(|text| note::parse(text).ok().map(|n| n as f64)),
        )
    });
    let picked = value.round() as u8;
    (response.inner.changed() && picked != note).then_some(picked)
}

#[cfg(test)]
mod tests {
    use nord_format::cbin::Cbin;
    use nord_format::formats::nsmp;

    use super::*;

    fn zone(root_key: u8, top_note: u8, low_note: Option<u8>) -> Zone {
        Zone {
            root_key,
            top_note,
            low_note,
            gain: None,
            velocity: None,
            bytes: 0,
        }
    }

    /// A zone reads as the stretch of keyboard it covers, and the last one runs to the
    /// bottom.
    #[test]
    fn a_zone_reads_as_the_keys_it_covers() {
        let zones = vec![zone(72, 96, None), zone(60, 71, None)];
        // Zone 2 tops out at B4, so zone 1 starts one key above it.
        assert_eq!(range(&zones, 0), "C5 up to C7");
        assert_eq!(range(&zones, 1), "up to B4");

        // A file that states its own bottom is believed rather than derived.
        let stated = vec![zone(60, 71, Some(48))];
        assert_eq!(range(&stated, 0), "C3 up to B4");
    }

    #[test]
    fn an_envelope_reduces_the_audio_to_one_pair_per_column() {
        // Four frames of mono, two columns: each column covers two frames.
        let mono = [i16::MAX, 0, -8192, 8192];
        let pairs = envelope(&mono, 1, 2);
        assert_eq!(pairs.len(), 2);
        assert!((pairs[0].1 - 0.999_97).abs() < 1e-4, "{:?}", pairs[0]);
        assert_eq!(pairs[0].0, 0.0);
        assert_eq!(pairs[1], (-0.25, 0.25));

        // Stereo: one envelope over both channels, not two half-width ones.
        let stereo = [0, i16::MIN, 0, 0];
        assert_eq!(envelope(&stereo, 2, 1), vec![(-1.0, 0.0)]);

        // Fewer frames than columns: every column still gets a pair, and none is empty.
        let short = [1000i16, -1000];
        assert_eq!(envelope(&short, 1, 8).len(), 8);
        // Degenerate asks answer with nothing rather than an empty span or a divide.
        assert!(envelope(&mono, 1, 0).is_empty());
        assert!(envelope(&[], 1, 4).is_empty());
    }

    /// A wide-chain instrument reads its own name, sub-name and generation, and its
    /// zones are editable where the `map` layout does not name every key.
    #[test]
    fn a_later_generation_reads_and_edits() {
        let entity = Entity::Sample(Sample::V3(v3_sample(300)));

        let snapshot = snapshot(&entity).expect("a sample").expect("it reads");
        assert_eq!(snapshot.name, "Bass Clarinet");
        assert_eq!(snapshot.sub_name, "KG  mono");
        assert_eq!(snapshot.generation, "v3");
        assert!(snapshot.zones_editable);
        let zones: Vec<(u8, u8, Option<u8>)> = snapshot
            .zones
            .iter()
            .map(|zone| (zone.root_key, zone.top_note, zone.low_note))
            .collect();
        assert_eq!(zones, [(72, 96, Some(61)), (60, 60, Some(17))]);
        assert_eq!(range(&snapshot.zones, 0), "C#4 up to C7");
        assert!(
            snapshot.key_table.is_none(),
            "the wide layouts expose no keyboard map"
        );
    }

    /// Each generation's zone record states its own thing, and one read of the body
    /// brings back whichever it is: the narrow record's gain, the wide record's
    /// velocity window.
    #[test]
    fn a_zone_carries_what_its_own_generation_states() {
        let narrow = v2_snapshot();
        assert!(!narrow.zones.is_empty());
        assert!(narrow
            .zones
            .iter()
            .all(|zone| zone.gain.is_some() && zone.velocity.is_none()));

        let entity = Entity::Sample(Sample::V3(v3_sample(300)));
        let wide = snapshot(&entity).unwrap().unwrap();
        assert!(!wide.zones.is_empty());
        assert!(wide
            .zones
            .iter()
            .all(|zone| zone.gain.is_none() && zone.velocity.is_some()));
    }

    #[test]
    fn a_v4_instrument_says_so() {
        let entity = Entity::Sample(Sample::V3(v3_sample(400)));
        let snapshot = snapshot(&entity).unwrap().unwrap();
        assert_eq!(snapshot.generation, "v4");
    }

    /// One second of 44.1 kHz mono, and the one-zone v2 instrument the encoder makes
    /// of it — the only instrument this app can build from nothing.
    fn v2_bytes() -> Vec<u8> {
        let samples: Vec<i16> = (0..codec::SOURCE_RATE as usize)
            .map(|i| ((i as f64 / 40.0).sin() * 12_000.0) as i16)
            .collect();
        let options = nsmp::encode::Options::new("Marimba").root_key(60);
        nsmp::encode::instrument(&samples, &options)
            .unwrap()
            .to_bytes()
            .unwrap()
    }

    fn table_of(bytes: &[u8]) -> KeyTable {
        let entity = nord_format::from_stream(&mut Cursor::new(bytes)).unwrap();
        snapshot(&entity).unwrap().unwrap().key_table.unwrap()
    }

    /// Where two sets of bytes differ, as `(start, length)` runs.
    fn runs(was: &[u8], is: &[u8]) -> Vec<(usize, usize)> {
        let mut out: Vec<(usize, usize)> = Vec::new();
        for (at, _) in was
            .iter()
            .zip(is)
            .enumerate()
            .filter(|(_, (was, is))| was != is)
        {
            match out.last_mut() {
                Some(run) if run.0 + run.1 == at => run.1 += 1,
                _ => out.push((at, 1)),
            }
        }
        out
    }

    /// The two per-key units are the record's own: unity gain reads as no change at
    /// all, and a semitone of detune is a hundred cents.
    #[test]
    fn a_per_key_reading_is_the_record_in_its_own_unit() {
        assert_eq!(gain_db(keymap::GAIN_UNITY, keymap::GAIN_UNITY), 0.0);
        assert_eq!(gain_units(0.0).unwrap(), keymap::GAIN_UNITY);
        assert_eq!(detune_cents(keymap::DETUNE_PER_SEMITONE), 100.0);
        assert_eq!(detune_units(100.0), keymap::DETUNE_PER_SEMITONE);
        assert_eq!(detune_units(-100.0), -keymap::DETUNE_PER_SEMITONE);

        // Halving the ratio is six decibels down, and the reading goes back the way it
        // came.
        let half = gain_units(-6.020_6).unwrap();
        assert_eq!(half, keymap::GAIN_UNITY / 2);
        assert!(gain_db(half, keymap::GAIN_UNITY) < -6.0);
        // A zero field is silence, not a reading.
        assert!(!gain_db(0, keymap::GAIN_UNITY).is_finite());
        assert_eq!(decibels(gain_db(0, keymap::GAIN_UNITY)), "silent");
        // More gain than the 24-bit field holds is refused rather than wrapped.
        assert!(gain_units(48.0).is_err());
    }

    /// What the lane writes is what the lane reads: the value it paints is spelled in
    /// the unit the setter parses.
    #[test]
    fn a_painted_value_round_trips_through_the_path_it_is_written_on() {
        assert_eq!(keys::Scale::Db(GAIN_FULL).format(0.5), "+1.5 dB");
        assert_eq!(measured("+1.5 dB", "dB").unwrap(), 1.5);
        assert_eq!(measured("-12 c", "c").unwrap(), -12.0);
        assert_eq!(key_path("key60.gain"), Some((60, "gain")));
        assert_eq!(key_path("key127.detune"), Some((127, "detune")));
        assert_eq!(key_path("key128.gain"), None, "not a MIDI note");
        assert_eq!(key_path("zone1.top_note"), None);
        assert!(measured("loud", "dB").is_err());
    }

    /// ⚠️ The keyboard map is one field: a painted key must leave every other key's
    /// record alone, and the key's own other half with it.
    #[test]
    fn a_painted_key_moves_its_own_record_and_nothing_else() {
        let bytes = v2_bytes();
        let before = table_of(&bytes);
        assert_eq!(before.key(60).unwrap(), Level::NEUTRAL);

        let edited = apply(
            &bytes,
            &[
                ("key60.gain".into(), "+1.5 dB".into()),
                ("key61.detune".into(), "-12 c".into()),
            ],
        )
        .unwrap();
        let after = table_of(&edited);

        assert_eq!(after.key(60).unwrap().gain(), gain_units(1.5).unwrap());
        assert_eq!(after.key(60).unwrap().detune(), 0, "the other half stands");
        assert_eq!(after.key(61).unwrap().detune(), detune_units(-12.0));
        assert_eq!(
            after.key(61).unwrap().gain(),
            keymap::GAIN_UNITY,
            "the other half stands"
        );
        assert_eq!(after.instrument, before.instrument);
        assert_eq!(after.adjusted().collect::<Vec<_>>(), [60, 61]);

        // Byte isolation: the container's own checksum word, and one three-byte half of
        // each record — the gain of key 60 and the detune of key 61. Nothing else in
        // the file moved, the other half of each record included.
        assert_eq!(bytes.len(), edited.len());
        let moved = runs(&bytes, &edited);
        assert_eq!(moved.len(), 3, "{moved:?}");
        assert_eq!(moved[0].1, 4, "the container's checksum word: {moved:?}");
        assert_eq!((moved[1].1, moved[2].1), (3, 3), "{moved:?}");
        assert_eq!(
            moved[2].0 - moved[1].0,
            keymap::RECORD_LEN + 3,
            "the next key's other half: {moved:?}"
        );
    }

    /// A key edit and a zone edit in one frame are one apply, and neither is lost.
    #[test]
    fn a_key_and_a_zone_edit_land_together() {
        let bytes = v2_bytes();
        let edited = apply(
            &bytes,
            &[
                ("key60.gain".into(), "+1.5 dB".into()),
                ("zone1.top_note".into(), "C6".into()),
                ("key60.detune".into(), "+50 c".into()),
            ],
        )
        .unwrap();
        let entity = nord_format::from_stream(&mut Cursor::new(&edited)).unwrap();
        let snapshot = snapshot(&entity).unwrap().unwrap();
        assert_eq!(snapshot.zones[0].top_note, 84);
        let key = snapshot.key_table.unwrap().key(60).unwrap();
        assert_eq!(key.gain(), gain_units(1.5).unwrap());
        assert_eq!(key.detune(), detune_units(50.0));
    }

    /// ⚠️ Zones are numbered from 1, the way the panel numbers them: a path outside
    /// what the file holds is refused, and the refusal speaks that numbering rather
    /// than the format crate's own.
    #[test]
    fn unknown_zone_paths_are_refused() {
        let bytes = v2_bytes();
        assert_eq!(v2_snapshot().zones.len(), 1);
        let refused = |path: &str| {
            apply(&bytes, &[(path.into(), "C4".into())]).expect_err(&format!("{path} was accepted"))
        };
        assert_eq!(
            refused("zone2.root_key"),
            "there is no zone 2: this sample has 1"
        );
        for path in [
            "zone0.root_key",
            "zone1.bogus",
            "zone1.",
            ".root_key",
            "zone1",
        ] {
            assert_eq!(refused(path), format!("unknown field {path:?}"));
        }

        // Bytes that decode as something else are refused before any path is read.
        let song = crate::workspace::Fresh::SetList.bytes().unwrap();
        assert_eq!(
            apply(&song, &[("name".into(), "Vibes".into())]).unwrap_err(),
            "not a sample instrument"
        );
    }

    /// A path that names no field is refused before anything is written.
    #[test]
    fn unknown_key_paths_are_refused() {
        let bytes = v2_bytes();
        for (path, value) in [
            ("key60.loudness", "+1.5 dB"),
            ("key60.gain", "loud"),
            ("key60.gain", "+48.0 dB"),
        ] {
            assert!(
                apply(&bytes, &[(path.into(), value.into())]).is_err(),
                "{path} = {value}"
            );
        }
        // The wide chain has no map to write, and says so rather than writing one.
        let wide = nord_format::to_bytes(&Entity::Sample(Sample::V3(v3_sample(300)))).unwrap();
        assert!(apply(&wide, &[("key60.gain".into(), "+1.5 dB".into())]).is_err());
    }

    /// Every path in [`EDITS`], which is what the capability table's editable rows are
    /// checked against.
    const EDITS: [(&str, Option<&str>); 6] = [
        ("name", Some("name")),
        ("key zones: root / top / low", Some("zone1.top_note")),
        ("per-key table", Some("key60.gain")),
        // The two the Edit face performs as acts rather than field writes.
        ("decode / audition", None),
        ("write to the instrument", None),
        // Named here so the wide generations' table is covered by the same check.
        ("velocity layers", None),
    ];

    /// ⚠️ The capability table is a claim about this editor, not a wish list. Every row
    /// it calls editable has to name something the editor can actually write, or be one
    /// of the acts named in [`EDITS`].
    #[test]
    fn every_editable_capability_names_a_path_the_editor_accepts() {
        let bytes = v2_bytes();
        for generation in ["v2", "v3", "v4"] {
            for row in capabilities(generation) {
                if row.state != Cap::Editable {
                    continue;
                }
                let named = EDITS
                    .iter()
                    .find(|(name, _)| *name == row.name)
                    .unwrap_or_else(|| {
                        panic!("{generation}: {} is editable and unlisted", row.name)
                    });
                let Some(path) = named.1 else { continue };
                if generation != "v2" {
                    continue;
                }
                assert!(
                    apply(&bytes, &[(path.into(), example(path))]).is_ok(),
                    "{}: {path} is not a path the editor accepts",
                    row.name
                );
            }
        }
        // A row the table calls editable on the wide generations but not on v2 is only
        // ever listed as an act: nothing here writes a velocity window.
        assert!(capabilities("v3")
            .iter()
            .all(|row| row.name != "velocity layers" || row.state == Cap::ReadOnly));
    }

    /// A value each example path takes.
    fn example(path: &str) -> String {
        match path {
            "name" => "Vibes".to_string(),
            "zone1.top_note" => "C6".to_string(),
            _ => "+1.5 dB".to_string(),
        }
    }

    /// The identity row carries what the strip cannot: a narrow instrument's category
    /// and a wide one's second name, both read-only.
    #[test]
    fn the_identity_cell_is_what_the_file_states_about_itself() {
        let wide = Entity::Sample(Sample::V3(v3_sample(300)));
        let cell = stated(&wide).expect("a sub name");
        assert_eq!(cell.label, "Sub name");
        match cell.body {
            Body::Read(value) => assert_eq!(value, "KG  mono"),
            Body::Chips(_) => panic!("a sub name is not a chip"),
        }

        let narrow = nord_format::from_stream(&mut Cursor::new(&v2_bytes())).unwrap();
        let cell = stated(&narrow).expect("the encoder writes a cat section");
        assert_eq!(cell.label, "Category");
        match cell.body {
            Body::Read(value) => assert!(!value.is_empty(), "the cat section is read"),
            Body::Chips(_) => panic!("a category is not a chip"),
        }
    }

    /// Every offset the Advanced face prints is one of the format's own declarations.
    #[test]
    fn the_offsets_are_the_formats_own_declarations() {
        let entity = nord_format::from_stream(&mut Cursor::new(&v2_bytes())).unwrap();
        let narrow = snapshot(&entity).unwrap().unwrap();
        let rows = offsets(&narrow);
        let at: Vec<&str> = rows.iter().map(|row| row.at.as_str()).collect();
        assert_eq!(
            at,
            [
                "map+0",
                &format!("map+{}", keymap::KEY_TABLE_AT),
                &format!("map+{}", zone::COUNT_AT),
                &format!("map+{}", zone::RECORDS_AT),
            ]
        );
        assert_eq!(narrow.record_len, 15, "the Library 2 zone record");

        // A body with no keyboard map prints no keyboard map offsets.
        let entity = Entity::Sample(Sample::V3(v3_sample(300)));
        let wide = snapshot(&entity).unwrap().unwrap();
        assert_eq!(offsets(&wide).len(), 2);
    }

    /// Picking from the map opens a row and asks for it; clicking an open, selected row
    /// closes it again. An edit is never what a pick changes.
    #[test]
    fn picking_a_zone_opens_its_row_and_clicking_it_again_closes_it() {
        let mut state = State::default();
        state.pick(1, true);
        assert_eq!(state.selected, Some(1));
        assert!(state.open);
        assert_eq!(state.reveal, Some(1), "the map asked for it to be shown");

        state.reveal = None;
        state.pick(1, false);
        assert!(!state.open, "the same row closes");
        assert_eq!(state.reveal, None);

        // Another row opens rather than toggling the one that was open.
        state.pick(0, false);
        assert_eq!((state.selected, state.open), (Some(0), true));
        state.pick(1, false);
        assert_eq!((state.selected, state.open), (Some(1), true));
    }

    /// What a struck key does: the zone that answers it, or why nothing does.
    #[test]
    fn a_struck_key_says_which_zone_answers_it() {
        let zones = [
            MapZone {
                row: 0,
                low: 61,
                top: 96,
                root: 72,
                name: "Zone 1".into(),
                velocity: None,
            },
            MapZone {
                row: 1,
                low: 24,
                top: 40,
                root: 48,
                name: "Zone 2".into(),
                velocity: Some((1, 64)),
            },
        ];

        let heard = answered(&zones, 84, Sounds::Now);
        assert_eq!(heard.zone, Some(0));
        assert!(heard.sounded);
        assert_eq!(
            heard.words,
            "C6 at vel 90 → Zone 1 · root C5 · shifted +12 st"
        );

        let silent = answered(&zones, 50, Sounds::Now);
        assert_eq!(silent.zone, None);
        assert!(!silent.sounded);
        assert_eq!(silent.words, "D3 — no zone answers this key; silence.");

        // Inside the zone but outside the window it answers at.
        let quiet = answered(&zones, 24, Sounds::Now);
        assert_eq!(quiet.zone, Some(1));
        assert!(!quiet.sounded);
        assert!(
            quiet
                .words
                .ends_with("outside its velocity window 1–64; silence"),
            "{}",
            quiet.words
        );
    }

    /// The map shows every zone, including one that reaches past the six octaves it
    /// opens on — a band off the end of the span is a band over the wrong key.
    #[test]
    fn the_span_widens_to_hold_every_zone() {
        let inside = [MapZone {
            row: 0,
            low: 36,
            top: 84,
            root: 60,
            name: "Zone 1".into(),
            velocity: None,
        }];
        assert_eq!(span(&inside, NSMP_SPAN), NSMP_SPAN);

        let past = [MapZone {
            row: 0,
            low: 17,
            top: 108,
            root: 60,
            name: "Zone 1".into(),
            velocity: None,
        }];
        assert_eq!(span(&past, NSMP_SPAN), keys::Span { low: 17, high: 108 });
    }

    /// A context dressed as the app dresses it: the semibold family a band and a row
    /// are set in is not bound by default, and laying one out without it panics.
    fn dressed() -> egui::Context {
        let ctx = egui::Context::default();
        ctx.set_fonts(crate::app::fonts());
        ctx.set_visuals(egui::Visuals::dark());
        ctx
    }

    /// One frame of the pinned map over `snapshot`: what it painted, what it wrote, and
    /// what it asked for.
    fn mapped(
        ctx: &egui::Context,
        state: &mut State,
        snapshot: &Snapshot,
        events: Vec<egui::Event>,
    ) -> (Vec<(String, egui::Rect)>, Sets, Option<Ask>) {
        let mut sets = Sets::new();
        let mut ask = None;
        let input = egui::RawInput {
            events,
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(900.0, 400.0),
            )),
            ..Default::default()
        };
        let output = ctx.run(input, |ctx| {
            ctx.style_mut(crate::app::metrics);
            egui::CentralPanel::default().show(ctx, |ui| {
                ask = map(ui, state, snapshot, &mut sets);
            });
        });
        let mut said = Vec::new();
        for clipped in &output.shapes {
            walk(&clipped.shape, &mut said);
        }
        (said, sets, ask)
    }

    fn walk(shape: &egui::Shape, into: &mut Vec<(String, egui::Rect)>) {
        match shape {
            egui::Shape::Text(text) => into.push((
                text.galley.text().to_string(),
                egui::Rect::from_min_size(text.pos, text.galley.size()),
            )),
            egui::Shape::Vec(shapes) => shapes.iter().for_each(|shape| walk(shape, into)),
            _ => {}
        }
    }

    /// The lowest place a word was painted — the keyboard's own octave labels sit at
    /// the bottom of the key they name, under everything else that spells a note.
    fn lowest(said: &[(String, egui::Rect)], word: &str) -> egui::Rect {
        said.iter()
            .filter(|(text, _)| text == word)
            .map(|(_, at)| *at)
            .reduce(|a, b| match a.center().y > b.center().y {
                true => a,
                false => b,
            })
            .unwrap_or_else(|| panic!("{word} was never painted: {said:?}"))
    }

    fn press(at: egui::Pos2) -> Vec<egui::Event> {
        vec![
            egui::Event::PointerMoved(at),
            egui::Event::PointerButton {
                pos: at,
                button: egui::PointerButton::Primary,
                pressed: true,
                modifiers: egui::Modifiers::NONE,
            },
            egui::Event::PointerButton {
                pos: at,
                button: egui::PointerButton::Primary,
                pressed: false,
                modifiers: egui::Modifiers::NONE,
            },
        ]
    }

    fn v2_snapshot() -> Snapshot {
        let entity = nord_format::from_stream(&mut Cursor::new(&v2_bytes())).unwrap();
        snapshot(&entity).unwrap().unwrap()
    }

    /// One frame of the body over `snapshot`, with nothing decoded: what it painted,
    /// and what it wrote.
    fn bodied(ctx: &egui::Context, state: &mut State, snapshot: &Snapshot) -> (Vec<String>, Sets) {
        let mut sets = Sets::new();
        let input = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(900.0, 900.0),
            )),
            ..Default::default()
        };
        let output = ctx.run(input, |ctx| {
            ctx.style_mut(crate::app::metrics);
            egui::CentralPanel::default().show(ctx, |page| {
                ui(page, state, snapshot, &[], &mut sets);
            });
        });
        let mut said = Vec::new();
        for clipped in &output.shapes {
            walk(&clipped.shape, &mut said);
        }
        (said.into_iter().map(|(text, _)| text).collect(), sets)
    }

    /// The wide generations state a velocity window per zone, so the field is drawn —
    /// read only, because nothing here writes one. A v2 record holds no window at all,
    /// and gets no section for it.
    #[test]
    fn the_velocity_field_is_drawn_only_where_a_window_is_stated() {
        let ctx = dressed();
        let entity = Entity::Sample(Sample::V3(v3_sample(300)));
        let wide = snapshot(&entity).unwrap().unwrap();
        assert!(wide.zones.iter().all(|zone| zone.velocity.is_some()));
        let (said, sets) = bodied(&ctx, &mut State::default(), &wide);
        assert!(said.iter().any(|text| text == "Velocity"), "{said:?}");
        assert!(
            said.iter()
                .any(|text| text == "every stroke answers the full window"),
            "{said:?}"
        );
        assert!(sets.is_empty(), "a stated window is not an edit");

        let narrow = v2_snapshot();
        assert!(narrow.zones.iter().all(|zone| zone.velocity.is_none()));
        let (said, sets) = bodied(&ctx, &mut State::default(), &narrow);
        assert!(!said.iter().any(|text| text == "Velocity"), "{said:?}");
        assert!(
            said.iter().any(|text| text == "Per key"),
            "the v2 keyboard map is drawn instead: {said:?}"
        );
        assert!(sets.is_empty());
    }

    /// One frame of the actions of an open zone: what they painted and where, and what
    /// a click asked the document for.
    fn actions(
        ctx: &egui::Context,
        sound: &Sound,
        events: Vec<egui::Event>,
    ) -> (Vec<(String, egui::Rect)>, Option<Ask>) {
        let mut ask = None;
        let input = egui::RawInput {
            events,
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(400.0, 200.0),
            )),
            ..Default::default()
        };
        let output = ctx.run(input, |ctx| {
            ctx.style_mut(crate::app::metrics);
            egui::CentralPanel::default().show(ctx, |ui| {
                ask = zone_audio(ui, 0, sound);
            });
        });
        let mut said = Vec::new();
        for clipped in &output.shapes {
            walk(&clipped.shape, &mut said);
        }
        (said, ask)
    }

    /// ⚠️ Every edit drops the decoded audio and the zone that was sounding goes on
    /// sounding: the control that stops it has to stand with nothing decoded, or the
    /// sound has nothing on screen to stop it.
    #[test]
    fn a_sounding_zone_is_stopped_from_the_row_with_nothing_decoded() {
        let ctx = dressed();
        let sounding = Sound {
            decoded: None,
            playing: true,
        };
        let (said, ask) = actions(&ctx, &sounding, Vec::new());
        assert!(ask.is_none(), "nothing was clicked");
        let stop = said
            .iter()
            .find(|(text, _)| text == "Stop")
            .unwrap_or_else(|| panic!("a sounding zone offers no Stop: {said:?}"))
            .1;
        let (_, ask) = actions(&ctx, &sounding, press(stop.center()));
        assert_eq!(ask, Some(Ask::Play(0)));

        // Silent and undecoded, the row offers the decode instead.
        let quiet = Sound {
            decoded: None,
            playing: false,
        };
        let (said, _) = actions(&ctx, &quiet, Vec::new());
        assert!(
            said.iter().any(|(text, _)| text == "Show audio"),
            "{said:?}"
        );
    }

    /// ⚠️ A paint mark is the difference between what is held and what was saved, and
    /// saving moves the baseline without giving the asset new bytes: measured against
    /// the bytes before the save, an edit that is now stored goes on reading as painted.
    #[test]
    fn saving_an_edit_clears_the_paint_marks() {
        let ctx = dressed();
        let bytes = v2_bytes();
        let edited = apply(&bytes, &[("key60.gain".into(), "+1.5 dB".into())]).unwrap();
        let entity = nord_format::from_stream(&mut Cursor::new(&edited)).unwrap();
        let snapshot = snapshot(&entity).unwrap().unwrap();

        let mut state = State::default();
        follow(&mut state, 7, &Baseline::read(bytes, 1));
        let (said, _) = bodied(&ctx, &mut state, &snapshot);
        assert!(
            said.iter().any(|text| text.ends_with("· 1 edited")),
            "the key is painted against the bytes it was saved as: {said:?}"
        );

        follow(&mut state, 7, &Baseline::read(edited, 2));
        let (said, _) = bodied(&ctx, &mut state, &snapshot);
        assert!(
            !said.iter().any(|text| text.contains("edited")),
            "saved, and nothing is painted any more: {said:?}"
        );
    }

    /// Clicking a key a zone answers sounds it and says so; clicking one past every
    /// zone says why nothing came out. Neither is an edit.
    #[test]
    fn a_click_on_the_keyboard_sounds_the_zone_that_answers_it() {
        let ctx = dressed();
        let snapshot = v2_snapshot();
        assert_eq!(snapshot.zones.len(), 1);
        assert_eq!(
            (snapshot.zones[0].root_key, snapshot.zones[0].top_note),
            (60, 84),
            "the encoder's own one-zone instrument"
        );

        let mut state = State::default();
        let (said, sets, ask) = mapped(&ctx, &mut state, &snapshot, Vec::new());
        assert!(ask.is_none(), "nothing sounds unasked");
        assert!(sets.is_empty());
        assert!(
            said.iter()
                .any(|(text, _)| text == "1 silent range · C1–C7"),
            "the keys above the zone answer nothing: {said:?}"
        );

        let middle_c = lowest(&said, "C4").center();
        let (said, sets, ask) = mapped(&ctx, &mut state, &snapshot, press(middle_c));
        assert_eq!(
            ask,
            Some(Ask::Strike {
                zone: 0,
                semitones: 0
            })
        );
        assert!(sets.is_empty(), "a struck key is never an edit");
        assert!(
            said.iter()
                .any(|(text, _)| text == "C4 at vel 90 → Zone 1 · root C4 · shifted +0 st"),
            "{said:?}"
        );

        // A key past the zone's top: the sentence says silence, and nothing is asked.
        let above = lowest(&said, "C7").center();
        let (said, _, ask) = mapped(&ctx, &mut state, &snapshot, press(above));
        assert_eq!(ask, None);
        assert!(
            said.iter()
                .any(|(text, _)| text == "C7 — no zone answers this key; silence."),
            "{said:?}"
        );
    }

    /// A moved band writes the ends the record states, and nothing that did not move.
    ///
    /// ⚠️ A v2 low is derived from the zone below rather than stored, so a drag that
    /// moves one must not write it: the setter refuses, and the whole apply is lost.
    #[test]
    fn a_moved_band_writes_only_the_ends_that_moved_and_are_stored() {
        let tiled = [zone(72, 96, None), zone(60, 60, None)];
        assert!(
            moved(&tiled, &[(61, 96), (24, 60)]).is_empty(),
            "nothing moved"
        );

        // The top of the lower zone moved, and the derived low above it followed.
        let sets = moved(&tiled, &[(56, 96), (24, 55)]);
        assert_eq!(sets, [("zone2.top_note".to_string(), "G3".to_string())]);

        // Where the record states its own low, both ends are written.
        let stated = [zone(72, 96, Some(61)), zone(60, 60, Some(24))];
        let sets = moved(&stated, &[(50, 96), (24, 49)]);
        assert_eq!(
            sets,
            [
                ("zone1.low_note".to_string(), "D3".to_string()),
                ("zone2.top_note".to_string(), "C#3".to_string()),
            ]
        );
    }

    /// Clicking a band picks its zone and asks for the row to be brought up under the
    /// map, which is the one thing the map does to the body.
    #[test]
    fn a_click_on_a_band_opens_its_row() {
        let ctx = dressed();
        let snapshot = v2_snapshot();
        let mut state = State::default();
        let (said, _, _) = mapped(&ctx, &mut state, &snapshot, Vec::new());
        assert_eq!(selected(&state), None);

        let band = lowest(&said, "C1–C6");
        let (_, sets, ask) = mapped(&ctx, &mut state, &snapshot, press(band.center()));
        assert_eq!(selected(&state), Some(0));
        assert_eq!(state.reveal, Some(0));
        assert!(sets.is_empty() && ask.is_none(), "a pick is not an edit");
    }

    /// A two-zone v3 body, hand-built to the layout `map` v14 stores: a per-key table
    /// the reader skips, the zone count at the offset the layout fixes, then one
    /// 16-byte record per zone high to low, each holding root, top and low notes and
    /// naming its stroke by global id at offset 8.
    fn v3_sample(version: u32) -> Cbin<nsmp::SampleV3> {
        use nord_format::formats::nsmp::section::{Section4, HDR4, MAP4, STK4};
        use nord_format::formats::nsmp::zone::Wide;

        // `hdr`: the main name at 10, the sub name from 76.
        let mut hdr = vec![0u8; 140];
        hdr[10..23].copy_from_slice(b"Bass Clarinet");
        hdr[76..84].copy_from_slice(b"KG  mono");

        let mut map = vec![0u8; Wide::V14.count_at().unwrap()];
        map.push(2);
        for (gid, root, top, low) in [(2u32, 72u8, 96u8, 61u8), (1, 60, 60, 17)] {
            let mut record = vec![0u8; 16];
            record[0] = root;
            record[1] = top;
            record[2] = low;
            record[8..12].copy_from_slice(&gid.to_be_bytes());
            map.extend(record);
        }

        let stroke = |gid: u32, root: u8| {
            let mut payload = vec![0u8; 68];
            payload[0..4].copy_from_slice(&gid.to_be_bytes());
            payload[5] = root;
            Section4 {
                tag: *STK4,
                version: 9,
                payload,
            }
        };
        Cbin {
            header: nord_format::cbin::Header::new(nsmp::FORMAT, (0, 0), version),
            body: nsmp::SampleV3 {
                sections: vec![
                    Section4 {
                        tag: *HDR4,
                        version: 9,
                        payload: hdr,
                    },
                    Section4 {
                        tag: *MAP4,
                        version: 14,
                        payload: map,
                    },
                    stroke(2, 72),
                    stroke(1, 60),
                ],
            },
        }
    }
}