rumpl 0.3.0

Sample-free General MIDI player and renderer
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
//! The GM percussion map: which model and spec plays each drum key.
//!
//! Suspended cymbals and hi-hats are `Cymbal`s (`cymbal.rs`); every other
//! drum is a `Perc` (`perc.rs`): body modes, band noise, particle collisions
//! and/or a gliding tone. Specs were fitted to recordings (Logic Pro's Drum
//! Kit Designer kit pieces and percussion library; analysis only) by
//! third-octave energy over time and tracked partials.

use crate::synth::cymbal::{CymbalBand, CymbalSpec};
use crate::synth::perc::{
  Particles, PercMode, PercNoise, PercSpec, PercTone, Resonator,
};

#[derive(Clone, Copy, Debug)]
pub enum DrumSound {
  Perc(&'static PercSpec),
  Cymbal(&'static CymbalSpec),
}

#[derive(Clone, Copy, Debug)]
pub struct DrumPatch {
  pub sound: DrumSound,
  pub gain: f32,
  /// Mute group: voices with the same non-zero group choke each other.
  pub group: u8,
  /// Position in the kit, −1 (left) .. 1 (right), from the drummer's seat;
  /// added to the channel pan.
  pub pan: f32,
}

const fn perc(spec: &'static PercSpec, gain: f32) -> DrumPatch {
  DrumPatch {
    sound: DrumSound::Perc(spec),
    gain,
    group: 0,
    pan: 0.0,
  }
}

const fn cymbal(spec: &'static CymbalSpec, gain: f32) -> DrumPatch {
  DrumPatch {
    sound: DrumSound::Cymbal(spec),
    gain,
    group: 0,
    pan: 0.0,
  }
}

const fn group(mut p: DrumPatch, g: u8) -> DrumPatch {
  p.group = g;
  p
}

const fn pan(mut p: DrumPatch, x: f32) -> DrumPatch {
  p.pan = x;
  p
}

/// Keys of the GM2 percussion map (High Q … Open Surdo): GM's 35–81 plus the
/// GS/GM2 additions 27–34 and 82–87. `drum_patch` has a dedicated sound for each.
pub const DRUM_KEYS: std::ops::RangeInclusive<u8> = 27..=87;

/// GM2 name of the percussion sound on `key`, if it is in `DRUM_KEYS`.
pub fn gm_drum_name(key: u8) -> Option<&'static str> {
  const NAMES: [&str; 61] = [
    "High Q",
    "Slap",
    "Scratch Push",
    "Scratch Pull",
    "Sticks",
    "Square Click",
    "Metronome Click",
    "Metronome Bell",
    "Acoustic Bass Drum",
    "Bass Drum 1",
    "Side Stick",
    "Acoustic Snare",
    "Hand Clap",
    "Electric Snare",
    "Low Floor Tom",
    "Closed Hi-Hat",
    "High Floor Tom",
    "Pedal Hi-Hat",
    "Low Tom",
    "Open Hi-Hat",
    "Low-Mid Tom",
    "Hi-Mid Tom",
    "Crash Cymbal 1",
    "High Tom",
    "Ride Cymbal 1",
    "Chinese Cymbal",
    "Ride Bell",
    "Tambourine",
    "Splash Cymbal",
    "Cowbell",
    "Crash Cymbal 2",
    "Vibraslap",
    "Ride Cymbal 2",
    "Hi Bongo",
    "Low Bongo",
    "Mute Hi Conga",
    "Open Hi Conga",
    "Low Conga",
    "High Timbale",
    "Low Timbale",
    "High Agogo",
    "Low Agogo",
    "Cabasa",
    "Maracas",
    "Short Whistle",
    "Long Whistle",
    "Short Guiro",
    "Long Guiro",
    "Claves",
    "Hi Wood Block",
    "Low Wood Block",
    "Mute Cuica",
    "Open Cuica",
    "Mute Triangle",
    "Open Triangle",
    "Shaker",
    "Jingle Bell",
    "Belltree",
    "Castanets",
    "Mute Surdo",
    "Open Surdo",
  ];
  DRUM_KEYS
    .contains(&key)
    .then(|| NAMES[(key - DRUM_KEYS.start()) as usize])
}

const fn band(hz: f32, db: f32, bloom: f32, tau: f32) -> CymbalBand {
  CymbalBand { hz, db, bloom, tau }
}

// Crash: energy peaked around 3 kHz; the 300–600 Hz modes start hot (the
// strike) and settle, the 2–9 kHz modes swell ~9 dB over the first 50–100 ms.
// T60 ≈ 7 s up to 2 kHz, 5.5 s at 3 kHz, 2.7 s at 8 kHz. The mode floor is
// dense: peaks stand only ~10 dB out. Averaged over three Drum Kit Designer
// crashes.
const CRASH_BANDS: [CymbalBand; 10] = [
  band(150.0, -30.0, 0.0, 0.05),
  band(250.0, -16.0, -1.5, 0.06),
  band(400.0, -4.0, -4.0, 0.05),
  band(600.0, -3.0, -1.0, 0.05),
  band(1_100.0, -11.0, -0.5, 0.1),
  band(2_000.0, -2.0, 0.85, 0.05),
  band(3_150.0, 8.5, 0.9, 0.05),
  band(5_000.0, 3.0, 0.85, 0.07),
  band(9_000.0, -6.0, 0.7, 0.1),
  band(13_000.0, -18.0, 0.0, 0.1),
];

const CRASH_1: CymbalSpec = CymbalSpec {
  seed: 49,
  lo: 140.0,
  hi: 13_000.0,
  density: 3.0,
  scatter_db: 4.0,
  floor_db: -4.0,
  bands: &CRASH_BANDS,
  ring: 7.5,
  ring_corner: 4_500.0,
  brightness: 7_000.0,
  noise_hz: 11_000.0,
  stick: 0.2,
  stick_tau: 0.03,
  sizzle: 0.08,
  sizzle_t60: 1.2,
  drive: 0.0,
  wobble: 1.2,
};

// A larger, darker crash.
const CRASH_2: CymbalSpec = CymbalSpec {
  seed: 57,
  lo: 110.0,
  ring: 6.5,
  ring_corner: 4_600.0,
  brightness: 6_000.0,
  ..CRASH_1
};

// Small and thin: everything an octave or so higher, short.
const SPLASH_BANDS: [CymbalBand; 7] = [
  band(300.0, -15.0, 0.0, 0.05),
  band(500.0, 0.0, -1.5, 0.05),
  band(1_000.0, 2.0, -1.0, 0.05),
  band(2_000.0, 3.0, -0.5, 0.08),
  band(4_000.0, 8.0, 0.5, 0.025),
  band(8_000.0, 8.0, 0.5, 0.04),
  band(13_000.0, -2.0, 0.3, 0.05),
];

const SPLASH: CymbalSpec = CymbalSpec {
  seed: 55,
  lo: 300.0,
  hi: 13_000.0,
  density: 3.0,
  scatter_db: 4.0,
  floor_db: -2.0,
  bands: &SPLASH_BANDS,
  ring: 2.2,
  ring_corner: 7_000.0,
  brightness: 8_000.0,
  noise_hz: 11_000.0,
  stick: 0.25,
  stick_tau: 0.02,
  sizzle: 0.12,
  sizzle_t60: 0.7,
  drive: 0.0,
  wobble: 1.2,
};

// China: mid-heavy, strong swell, short and trashy (the turned-up edge
// clips the vibration).
const CHINA_BANDS: [CymbalBand; 8] = [
  band(150.0, -20.0, 0.0, 0.05),
  band(300.0, 2.0, -1.5, 0.06),
  band(600.0, 6.0, -1.0, 0.08),
  band(1_200.0, 8.0, 0.6, 0.04),
  band(2_400.0, 9.0, 0.7, 0.05),
  band(4_800.0, 5.0, 0.6, 0.06),
  band(9_000.0, -6.0, 0.4, 0.1),
  band(13_000.0, -15.0, 0.0, 0.1),
];

const CHINA: CymbalSpec = CymbalSpec {
  seed: 52,
  lo: 140.0,
  hi: 13_000.0,
  density: 3.5,
  scatter_db: 5.0,
  floor_db: 0.0,
  bands: &CHINA_BANDS,
  ring: 3.0,
  ring_corner: 4_000.0,
  brightness: 6_000.0,
  noise_hz: 11_000.0,
  stick: 0.25,
  stick_tau: 0.03,
  sizzle: 0.06,
  sizzle_t60: 0.8,
  drive: 0.6,
  wobble: 1.2,
};

// Ride: no swell; a bright stick tick (the loudest band for the first
// ~30 ms is above 10 kHz) and a modest 3–8 kHz ping over a long 0.5–3 kHz
// wash (T60 ≈ 12 s at 1–2 kHz, 8.5 s at 3 kHz, 3 s at 8 kHz). Prominent
// partials stand ~15 dB above the floor. Decay and texture averaged over four
// Drum Kit Designer rides; the spectral balance is brighter than their
// average (500 Hz down ~13 dB, 3–8 kHz and the stick tick up), matched by
// ear and spectral centroid (~5 kHz over the first 0.6 s) to the FluidSynth
// GM ride.
const RIDE_BANDS: [CymbalBand; 10] = [
  band(150.0, -30.0, 0.0, 0.05),
  band(200.0, -24.0, -2.0, 0.05),
  band(315.0, -4.0, -1.8, 0.08),
  band(500.0, -13.0, -0.3, 0.1),
  band(800.0, -1.0, 0.0, 0.1),
  band(1_250.0, -8.0, 0.0, 0.1),
  band(2_000.0, -7.0, 0.3, 0.08),
  band(3_150.0, 1.0, -0.4, 0.12),
  band(5_000.0, -3.0, -1.0, 0.12),
  band(8_000.0, -4.0, -1.8, 0.12),
];

const RIDE_1: CymbalSpec = CymbalSpec {
  seed: 51,
  lo: 150.0,
  hi: 13_000.0,
  density: 4.5,
  scatter_db: 12.0,
  floor_db: -22.0,
  bands: &RIDE_BANDS,
  ring: 12.0,
  ring_corner: 4_000.0,
  brightness: 8_000.0,
  noise_hz: 8_000.0,
  stick: 16.0,
  stick_tau: 0.02,
  sizzle: 0.015,
  sizzle_t60: 1.5,
  drive: 0.0,
  wobble: 1.2,
};

// A thinner, brighter ride: less below 1.5 kHz, more 5–12 kHz.
const RIDE_2_BANDS: [CymbalBand; 10] = [
  band(150.0, -30.0, 0.0, 0.05),
  band(200.0, -24.0, -2.0, 0.05),
  band(315.0, -8.0, -1.8, 0.08),
  band(500.0, -14.0, -0.3, 0.1),
  band(800.0, -5.0, 0.0, 0.1),
  band(1_250.0, -9.0, 0.0, 0.1),
  band(2_000.0, -7.0, 0.3, 0.08),
  band(3_150.0, 0.0, -0.4, 0.12),
  band(5_000.0, -1.0, -1.0, 0.12),
  band(8_000.0, -1.0, -1.8, 0.12),
];

const RIDE_2: CymbalSpec = CymbalSpec {
  seed: 59,
  bands: &RIDE_2_BANDS,
  ring: 11.0,
  ..RIDE_1
};

// Bell of the ride: sparse modes with a wide level scatter, so a few
// partials dominate and it clangs instead of washing.
const RIDE_BELL_BANDS: [CymbalBand; 6] = [
  band(400.0, -6.0, 0.0, 0.05),
  band(800.0, 4.0, 0.0, 0.05),
  band(1_600.0, 6.0, 0.0, 0.05),
  band(3_200.0, 3.0, -0.5, 0.08),
  band(6_000.0, -6.0, -1.5, 0.08),
  band(9_000.0, -14.0, -1.5, 0.08),
];

const RIDE_BELL: CymbalSpec = CymbalSpec {
  seed: 53,
  lo: 400.0,
  hi: 9_000.0,
  density: 1.0,
  scatter_db: 9.0,
  floor_db: -15.0,
  bands: &RIDE_BELL_BANDS,
  ring: 7.0,
  ring_corner: 4_000.0,
  brightness: 6_000.0,
  noise_hz: 10_000.0,
  stick: 0.3,
  stick_tau: 0.015,
  sizzle: 0.02,
  sizzle_t60: 1.0,
  drive: 0.0,
  wobble: 0.4,
};

// ---------------------------------------------------------------------------
// Percussion specs.

const fn m(hz: f32, db: f32, t60: f32) -> PercMode {
  PercMode {
    hz,
    db,
    t60,
    delay: 0.0,
  }
}

const fn nz(lo: f32, hi: f32, db: f32, t60: f32) -> PercNoise {
  PercNoise {
    lo,
    hi,
    db,
    delay: 0.0,
    attack: 0.0005,
    hold: 0.0,
    t60,
    bursts: 1,
    spacing: 0.0,
    vel: 0.0,
  }
}

const fn rz(hz: f32, q: f32, db: f32) -> Resonator {
  Resonator { hz, q, db }
}

/// Modes measured on a recording, as (ratio to `f0`, dB, T60) rows.
const fn tuned<const N: usize>(
  f0: f32,
  rows: [(f32, f32, f32); N],
) -> [PercMode; N] {
  let mut out = [m(0.0, 0.0, 0.0); N];
  let mut k = 0;
  while k < N {
    out[k] = m(f0 * rows[k].0, rows[k].1, rows[k].2);
    k += 1;
  }
  out
}

/// Modes of a drum head: the ideal-membrane ratios (Bessel zeros; (0,1),
/// (1,1), (2,1), (0,2), (3,1), (1,2), (4,1), (2,2)) — the air load and the
/// second head pull the first overtone down to `second` × f0 — with levels
/// for an off-centre hit and ring times falling with frequency.
const fn membrane(f0: f32, second: f32, t60: f32, slope: f32) -> [PercMode; 8] {
  const RATIO: [f32; 8] =
    [1.0, 1.594, 2.136, 2.296, 2.653, 2.918, 3.156, 3.501];
  const LEVEL: [f32; 8] = [0.0, -4.0, -8.0, -11.0, -13.0, -15.0, -17.0, -19.0];
  let mut out = [m(0.0, 0.0, 0.0); 8];
  let mut k = 0;
  while k < 8 {
    let r = if k == 1 { second } else { RATIO[k] };
    // T60 ∝ r^-slope (higher modes radiate and lose energy faster).
    let mut d = 1.0;
    let mut j = 0;
    while j < 4 {
      d *= 1.0 + (r - 1.0) * slope * 0.25;
      j += 1;
    }
    out[k] = m(f0 * r, LEVEL[k], t60 / d);
    k += 1;
  }
  out
}

// --- Kicks ---------------------------------------------------------------
// A heavily damped head: the fundamental falls ~25 dB per 100 ms and drops in
// pitch as the head's tension relaxes; the beater adds a 1–5 kHz click
// ~30 dB down and a short 300–900 Hz knock.

const KICK_MODES: [PercMode; 8] = membrane(52.0, 1.62, 0.3, 2.2);
const KICK_NOISE: [PercNoise; 3] = [
  // Beater on the head: broadband 0.5–4 kHz, ~30 ms.
  PercNoise {
    vel: 0.6,
    ..nz(500.0, 4_000.0, 3.0, 0.035)
  },
  nz(200.0, 800.0, 0.0, 0.03),
  // Shell and the resonant head ringing on, ~50 dB down.
  PercNoise {
    attack: 0.01,
    ..nz(150.0, 1_500.0, -14.0, 0.2)
  },
];
const KICK: PercSpec = PercSpec {
  modes: &KICK_MODES,
  glide: 0.9,
  glide_tau: 0.015,
  hardness: 2_500.0,
  noise: &KICK_NOISE,
  ..PercSpec::EMPTY
};

// Acoustic bass drum: a bigger, lower head that rings longer, with the same
// felt-beater click and knock as bass drum 1 (a weak, short click made it
// sound muffled).
const KICK_BIG_MODES: [PercMode; 8] = membrane(50.0, 1.58, 0.42, 1.8);
const KICK_BIG_NOISE: [PercNoise; 3] = [
  PercNoise {
    vel: 0.6,
    ..nz(600.0, 5_000.0, 2.0, 0.03)
  },
  nz(200.0, 900.0, -2.0, 0.035),
  PercNoise {
    attack: 0.01,
    ..nz(150.0, 1_500.0, -12.0, 0.25)
  },
];
const KICK_BIG: PercSpec = PercSpec {
  modes: &KICK_BIG_MODES,
  glide: 0.6,
  glide_tau: 0.02,
  hardness: 2_500.0,
  noise: &KICK_BIG_NOISE,
  ..PercSpec::EMPTY
};

// --- Snares --------------------------------------------------------------
// Head modes 183, 300, 450–600 Hz (T60 0.4–0.6 s), a broadband stick
// transient, and the wires: a 2–10 kHz noise band peaking at 5–6 kHz that
// follows the head (T60 ≈ 0.45 s).

const SNARE_MODES: [PercMode; 10] = [
  m(185.0, -5.0, 0.2),
  m(300.0, -3.0, 0.35),
  m(420.0, -14.0, 0.35),
  m(498.0, -14.0, 0.4),
  m(590.0, -14.0, 0.45),
  m(705.0, -14.0, 0.35),
  m(905.0, -18.0, 0.3),
  m(1_150.0, -20.0, 0.25),
  m(1_420.0, -23.0, 0.2),
  m(1_760.0, -25.0, 0.18),
];
const SNARE_NOISE: [PercNoise; 3] = [
  // Wires.
  PercNoise {
    delay: 0.002,
    attack: 0.002,
    ..nz(2_200.0, 11_000.0, 1.0, 0.55)
  },
  // Wire rattle lower down / shell.
  PercNoise {
    delay: 0.001,
    attack: 0.002,
    ..nz(500.0, 2_200.0, -16.0, 0.25)
  },
  // Stick.
  PercNoise {
    vel: 0.6,
    ..nz(1_500.0, 14_000.0, -10.0, 0.01)
  },
];
const SNARE: PercSpec = PercSpec {
  modes: &SNARE_MODES,
  glide: 0.06,
  glide_tau: 0.02,
  hardness: 2_500.0,
  noise: &SNARE_NOISE,
  ..PercSpec::EMPTY
};

// Electric snare: a smaller, tighter, higher-tuned snare with crisper wires.
const SNARE_TIGHT_MODES: [PercMode; 8] = [
  m(240.0, 0.0, 0.35),
  m(392.0, -4.0, 0.3),
  m(545.0, -9.0, 0.28),
  m(650.0, -9.0, 0.3),
  m(770.0, -11.0, 0.3),
  m(920.0, -16.0, 0.22),
  m(1_180.0, -19.0, 0.2),
  m(1_500.0, -22.0, 0.15),
];
const SNARE_TIGHT_NOISE: [PercNoise; 3] = [
  PercNoise {
    delay: 0.0015,
    attack: 0.0015,
    ..nz(3_000.0, 12_000.0, 0.0, 0.3)
  },
  PercNoise {
    delay: 0.001,
    attack: 0.002,
    ..nz(700.0, 3_000.0, -12.0, 0.18)
  },
  PercNoise {
    vel: 0.6,
    ..nz(2_000.0, 15_000.0, -5.0, 0.008)
  },
];
const SNARE_TIGHT: PercSpec = PercSpec {
  modes: &SNARE_TIGHT_MODES,
  glide: 0.05,
  glide_tau: 0.015,
  hardness: 3_500.0,
  noise: &SNARE_TIGHT_NOISE,
  ..PercSpec::EMPTY
};

// Side stick (cross-stick): the stick laid on the head clacks against the
// rim — a woody 1.0–1.8 kHz cluster gone in ~50 ms over a little head.
const SIDE_STICK_MODES: [PercMode; 11] = [
  m(188.0, -12.0, 0.15),
  m(339.0, -13.0, 0.1),
  m(468.0, -17.0, 0.08),
  m(711.0, -18.0, 0.08),
  m(1_007.0, -2.0, 0.06),
  m(1_125.0, 0.0, 0.08),
  m(1_346.0, -8.0, 0.07),
  m(1_453.0, -8.0, 0.06),
  m(1_820.0, -5.0, 0.05),
  m(2_864.0, -8.0, 0.04),
  m(3_900.0, -12.0, 0.03),
];
const SIDE_STICK_NOISE: [PercNoise; 1] = [PercNoise {
  vel: 0.4,
  ..nz(1_500.0, 14_000.0, 12.0, 0.015)
}];
const SIDE_STICK: PercSpec = PercSpec {
  modes: &SIDE_STICK_MODES,
  hardness: 6_000.0,
  noise: &SIDE_STICK_NOISE,
  ..PercSpec::EMPTY
};

// --- Hand clap -------------------------------------------------------------
// A few hands a few ms apart: each clap is a sharp ~2 ms burst coloured by
// the cupped-palm air cavity (1–2.5 kHz), then the room (fitted to the Logic
// percussion claps: tail ≈25 dB down, T60 ≈0.5 s).
const CLAP_NOISE: [PercNoise; 2] = [
  PercNoise {
    bursts: 3,
    spacing: 0.011,
    ..nz(900.0, 2_200.0, 2.0, 0.02)
  },
  PercNoise {
    delay: 0.008,
    attack: 0.006,
    ..nz(400.0, 2_400.0, -14.0, 0.45)
  },
];
const CLAP: PercSpec = PercSpec {
  noise: &CLAP_NOISE,
  ..PercSpec::EMPTY
};

// --- Toms --------------------------------------------------------------------
// Two-headed toms: membrane modes with the coupled first overtone at ~1.6–1.9
// × f0, a long-ringing fundamental (≈1 s on floor toms) and a tension pitch
// drop after the stroke; stick click on top.
const TOM_STICK: PercNoise = PercNoise {
  vel: 0.6,
  ..nz(900.0, 5_000.0, -5.0, 0.012)
};
// The dense upper head modes and the shell: darker and shorter on the big
// floor toms, wider and longer-ringing on the small rack toms (fitted).
const TOM_NOISE_FLOOR: [PercNoise; 2] = [
  TOM_STICK,
  PercNoise {
    attack: 0.002,
    ..nz(250.0, 1_500.0, -2.0, 0.4)
  },
];
const TOM_NOISE_RACK: [PercNoise; 2] = [
  TOM_STICK,
  PercNoise {
    attack: 0.002,
    ..nz(300.0, 3_000.0, -2.0, 0.6)
  },
];
const TOM_FLOOR_LO_MODES: [PercMode; 8] = membrane(82.0, 1.84, 2.6, 1.0);
const TOM_FLOOR_HI_MODES: [PercMode; 8] = membrane(98.0, 1.8, 2.2, 1.0);
const TOM_LO_MODES: [PercMode; 8] = membrane(117.0, 1.75, 1.8, 1.0);
const TOM_LO_MID_MODES: [PercMode; 8] = membrane(148.0, 1.7, 1.5, 1.0);
const TOM_HI_MID_MODES: [PercMode; 8] = membrane(178.0, 1.68, 1.3, 1.0);
const TOM_HI_MODES: [PercMode; 8] = membrane(215.0, 1.65, 1.1, 1.0);
const TOM: PercSpec = PercSpec {
  glide: 0.12,
  glide_tau: 0.04,
  hardness: 1_800.0,
  noise: &TOM_NOISE_RACK,
  ..PercSpec::EMPTY
};
const TOM_FLOOR_LO: PercSpec = PercSpec {
  modes: &TOM_FLOOR_LO_MODES,
  noise: &TOM_NOISE_FLOOR,
  ..TOM
};
const TOM_FLOOR_HI: PercSpec = PercSpec {
  modes: &TOM_FLOOR_HI_MODES,
  noise: &TOM_NOISE_FLOOR,
  ..TOM
};
const TOM_LO: PercSpec = PercSpec {
  modes: &TOM_LO_MODES,
  ..TOM
};
const TOM_LO_MID: PercSpec = PercSpec {
  modes: &TOM_LO_MID_MODES,
  ..TOM
};
const TOM_HI_MID: PercSpec = PercSpec {
  modes: &TOM_HI_MID_MODES,
  ..TOM
};
const TOM_HI: PercSpec = PercSpec {
  modes: &TOM_HI_MODES,
  ..TOM
};

// --- Hand drums -------------------------------------------------------------
// Bongos and congas: single heads struck by hand, fitted to the Logic
// percussion bongos and congas. Unlike a stick-struck drum the fundamental is
// short (the hand stays on the head a moment) while the upper modes — not at
// ideal-membrane ratios: the shell and the air column pull them about — ring
// as long or longer, so the open tone is rich rather than a pure "boom"; the
// congas add the shell's air resonance a little below the fundamental. A
// skin-slap of noise rides on top. Muted strokes have the other hand on the
// head: a lower, dead fundamental and a few surviving upper modes.
const HAND_SLAP: [PercNoise; 1] = [PercNoise {
  vel: 0.6,
  ..nz(800.0, 8_000.0, -10.0, 0.03)
}];
const BONGO_HI_MODES: [PercMode; 8] = tuned(
  420.0,
  [
    (1.0, 0.0, 0.12),
    (1.246, -11.0, 0.09),
    (1.47, -19.0, 0.1),
    (2.62, -18.0, 0.05),
    (4.35, -30.0, 0.06),
    (5.57, -30.0, 0.05),
    (6.69, -35.0, 0.1),
    (8.79, -28.0, 0.04),
  ],
);
const BONGO_LO_MODES: [PercMode; 7] = tuned(
  300.0,
  [
    (1.0, -2.0, 0.18),
    (1.617, 0.0, 0.08),
    (2.40, -9.0, 0.35),
    (3.26, -16.0, 0.22),
    (3.52, -7.0, 0.07),
    (4.62, -27.0, 0.05),
    (5.76, -27.0, 0.13),
  ],
);
const CONGA_HI_MODES: [PercMode; 10] = tuned(
  336.0,
  [
    (0.79, -7.0, 0.28),
    (1.0, 0.0, 0.39),
    (1.568, -11.0, 0.4),
    (2.17, -20.0, 0.16),
    (2.37, -25.0, 0.16),
    (2.76, -24.0, 0.25),
    (3.0, -23.0, 0.18),
    (3.12, -25.0, 0.04),
    (4.39, -29.0, 0.13),
    (5.6, -30.0, 0.06),
  ],
);
const CONGA_MUTE_MODES: [PercMode; 8] = tuned(
  300.0,
  [
    (1.0, 0.0, 0.06),
    (1.33, -11.0, 0.19),
    (2.47, -25.0, 0.13),
    (3.29, -34.0, 0.25),
    (4.07, -34.0, 0.1),
    (4.36, -38.0, 0.25),
    (6.8, -33.0, 0.04),
    (8.03, -32.0, 0.04),
  ],
);
const CONGA_LO_MODES: [PercMode; 10] = tuned(
  207.0,
  [
    (1.0, 0.0, 0.45),
    (1.377, -6.0, 0.39),
    (1.686, -4.0, 0.25),
    (2.338, -10.0, 0.07),
    (2.58, -8.0, 0.13),
    (3.256, -17.0, 0.19),
    (4.02, -26.0, 0.1),
    (4.22, -24.0, 0.12),
    (7.22, -18.0, 0.04),
    (8.25, -20.0, 0.06),
  ],
);
const SURDO_MODES: [PercMode; 8] = membrane(64.0, 1.6, 1.0, 1.4);
const SURDO_MUTE_MODES: [PercMode; 8] = membrane(66.0, 1.6, 0.18, 1.0);
const HAND: PercSpec = PercSpec {
  glide: 0.04,
  glide_tau: 0.03,
  hardness: 1_600.0,
  noise: &HAND_SLAP,
  ..PercSpec::EMPTY
};
const BONGO_HI: PercSpec = PercSpec {
  modes: &BONGO_HI_MODES,
  hardness: 2_400.0,
  ..HAND
};
const BONGO_LO: PercSpec = PercSpec {
  modes: &BONGO_LO_MODES,
  hardness: 2_000.0,
  ..HAND
};
const CONGA_MUTE: PercSpec = PercSpec {
  modes: &CONGA_MUTE_MODES,
  hardness: 2_200.0,
  ..HAND
};
const CONGA_HI: PercSpec = PercSpec {
  modes: &CONGA_HI_MODES,
  ..HAND
};
const CONGA_LO: PercSpec = PercSpec {
  modes: &CONGA_LO_MODES,
  ..HAND
};
const SURDO_NOISE: [PercNoise; 1] = [nz(300.0, 2_500.0, -24.0, 0.03)];
const SURDO: PercSpec = PercSpec {
  modes: &SURDO_MODES,
  glide: 0.1,
  glide_tau: 0.05,
  hardness: 900.0,
  noise: &SURDO_NOISE,
  ..PercSpec::EMPTY
};
const SURDO_MUTE: PercSpec = PercSpec {
  modes: &SURDO_MUTE_MODES,
  ..SURDO
};

// Timbales: thin heads on open-bottomed metal shells struck with sticks,
// fitted to the Logic percussion timbales: the main tone has a partner an
// octave below (the air column through the open shell), a couple of very
// short stick-contact modes, a few long-ringing upper head modes and a quiet
// 2.5–6 kHz ring from the steel shell.
const TIMBALE_HI_MODES: [PercMode; 15] = tuned(
  434.0,
  [
    (0.514, -2.0, 0.42),
    (1.0, 0.0, 0.42),
    (1.11, -7.0, 0.43),
    (1.40, -9.0, 0.37),
    (1.54, -2.0, 0.04),
    (1.90, -11.0, 0.07),
    (2.23, -13.0, 0.04),
    (3.15, -8.0, 0.19),
    (3.54, -13.0, 0.15),
    (6.93, -16.0, 0.16),
    // Steel shell.
    (5.67, -26.0, 0.35),
    (6.70, -24.0, 0.4),
    (7.23, -24.0, 0.4),
    (9.56, -27.0, 0.35),
    (13.9, -30.0, 0.3),
  ],
);
const TIMBALE_LO_MODES: [PercMode; 15] = tuned(
  282.0,
  [
    (0.507, 0.0, 0.61),
    (1.0, -3.0, 1.5),
    (1.43, -4.0, 0.04),
    (1.97, -13.0, 0.72),
    (2.60, -20.0, 0.51),
    (2.92, -17.0, 0.55),
    (3.32, -21.0, 0.09),
    (3.72, -19.0, 0.31),
    (4.73, -17.0, 0.42),
    (5.79, -19.0, 0.12),
    // Steel shell.
    (6.99, -28.0, 0.45),
    (10.75, -26.0, 0.5),
    (12.38, -26.0, 0.5),
    (14.7, -29.0, 0.4),
    (21.5, -31.0, 0.35),
  ],
);
const STICK_CLICK: [PercNoise; 1] = [PercNoise {
  vel: 0.5,
  ..nz(1_500.0, 12_000.0, -12.0, 0.01)
}];
const TIMBALE_HI: PercSpec = PercSpec {
  modes: &TIMBALE_HI_MODES,
  glide: 0.03,
  glide_tau: 0.02,
  hardness: 5_000.0,
  noise: &STICK_CLICK,
  ..PercSpec::EMPTY
};
const TIMBALE_LO: PercSpec = PercSpec {
  modes: &TIMBALE_LO_MODES,
  ..TIMBALE_HI
};

// --- Metal -------------------------------------------------------------------
// Fitted to the Logic percussion cowbell, agogô and triangle.
// Cowbell: a stubby box-shaped bell. A strong fundamental with a beating
// partner just below it, an octave-ish partial and a cluster at 2–2.8 kHz
// that rings longer than the fundamental; choked by the hand holding it.
const COWBELL_MODES: [PercMode; 12] = tuned(
  540.0,
  [
    (0.91, -12.0, 0.25),
    (1.0, 0.0, 0.31),
    (2.063, -14.0, 0.6),
    (2.668, -24.0, 0.5),
    (3.386, -29.0, 0.37),
    (3.994, -13.0, 0.25),
    (4.990, -14.0, 0.48),
    (5.042, -15.0, 0.39),
    (5.320, -28.0, 0.4),
    (5.451, -28.0, 0.21),
    (6.66, -25.0, 0.3),
    (8.226, -31.0, 0.25),
  ],
);
const COWBELL: PercSpec = PercSpec {
  modes: &COWBELL_MODES,
  hardness: 5_000.0,
  noise: &STICK_CLICK,
  ..PercSpec::EMPTY
};

// Agogô: two conical bells. A clear, long tone (a close pair that beats),
// short-lived stick-contact modes just below it and sparse upper partials.
const AGOGO_HI_MODES: [PercMode; 10] = tuned(
  1_189.0,
  [
    (0.842, -8.0, 0.07),
    (0.919, -17.0, 0.07),
    (1.0, 0.0, 0.6),
    (1.022, -8.0, 0.6),
    (1.100, -26.0, 0.19),
    (1.833, -25.0, 0.06),
    (2.0, -27.0, 0.34),
    (2.514, -27.0, 0.55),
    (2.999, -32.0, 0.43),
    (4.151, -31.0, 0.15),
  ],
);
const AGOGO_LO_MODES: [PercMode; 10] = tuned(
  882.0,
  [
    (0.910, -3.0, 0.09),
    (1.0, 0.0, 0.5),
    (1.139, -22.0, 1.5),
    (2.294, -18.0, 0.4),
    (4.051, -23.0, 0.4),
    (4.819, -21.0, 0.34),
    (6.314, -19.0, 0.24),
    (8.575, -20.0, 0.19),
    (8.855, -10.0, 0.06),
    (9.348, -17.0, 0.15),
  ],
);
const AGOGO_HI: PercSpec = PercSpec {
  modes: &AGOGO_HI_MODES,
  hardness: 6_000.0,
  noise: &STICK_CLICK,
  ..PercSpec::EMPTY
};
const AGOGO_LO: PercSpec = PercSpec {
  modes: &AGOGO_LO_MODES,
  ..AGOGO_HI
};

// Triangle: a bent steel rod — a few partials from 2.7 to 7 kHz ringing
// 1–4 s and a dense, strong set at 9–14 kHz ringing under a second; the
// muted stroke is stopped by the fingers, the top partials surviving longest.
const TRIANGLE_MODES: [PercMode; 12] = tuned(
  1.0,
  [
    (2_748.0, -14.0, 4.0),
    (3_297.0, -5.0, 2.9),
    (4_354.0, -15.0, 3.3),
    (5_633.0, -2.0, 2.1),
    (6_429.0, -3.0, 2.5),
    (6_920.0, -7.0, 1.1),
    (8_975.0, -6.0, 1.0),
    (10_066.0, -5.0, 0.9),
    (11_207.0, 0.0, 0.73),
    (12_215.0, -13.0, 0.64),
    (13_381.0, -4.0, 0.64),
    (13_921.0, -1.0, 0.55),
  ],
);
const TRIANGLE: PercSpec = PercSpec {
  modes: &TRIANGLE_MODES,
  hardness: 9_000.0,
  ..PercSpec::EMPTY
};
const TRIANGLE_MUTE_MODES: [PercMode; 10] = tuned(
  1.0,
  [
    (2_750.0, -16.0, 0.25),
    (5_632.0, -14.0, 0.6),
    (6_430.0, -7.0, 0.34),
    (6_730.0, -10.0, 0.28),
    (6_917.0, -10.0, 0.34),
    (8_934.0, -3.0, 0.19),
    (8_972.0, -2.0, 0.25),
    (11_203.0, -11.0, 0.45),
    (13_373.0, -19.0, 0.27),
    (13_914.0, 0.0, 0.3),
  ],
);
const TRIANGLE_MUTE: PercSpec = PercSpec {
  modes: &TRIANGLE_MUTE_MODES,
  ..TRIANGLE
};

// Bell tree: a stack of small cups struck top to bottom in a quick sweep.
const BELLTREE_MODES: [PercMode; 16] = {
  let mut out = [m(0.0, 0.0, 0.0); 16];
  let mut k = 0;
  while k < 16 {
    // From ~5.3 kHz down to ~2 kHz, 30 ms apart, each with an inharmonic
    // overtone.
    let mut f = 5_300.0;
    let mut j = 0;
    while j < k / 2 {
      f *= 0.885;
      j += 1;
    }
    let hz = if k % 2 == 0 { f } else { f * 2.76 };
    out[k] = PercMode {
      hz,
      db: if k % 2 == 0 { 0.0 } else { -10.0 },
      t60: if k % 2 == 0 { 1.6 } else { 0.8 },
      delay: 0.032 * (k / 2) as f32,
    };
    k += 1;
  }
  out
};
const BELLTREE: PercSpec = PercSpec {
  modes: &BELLTREE_MODES,
  hardness: 9_000.0,
  ..PercSpec::EMPTY
};

// Metronome bell: a small desk bell.
const METRO_BELL_MODES: [PercMode; 4] = [
  m(2_640.0, 0.0, 0.7),
  m(6_270.0, -9.0, 0.35),
  m(6_420.0, -11.0, 0.35),
  m(10_900.0, -18.0, 0.2),
];
const METRO_BELL: PercSpec = PercSpec {
  modes: &METRO_BELL_MODES,
  hardness: 8_000.0,
  noise: &STICK_CLICK,
  ..PercSpec::EMPTY
};

// Vibraslap: the ball strikes a wooden box and sets the steel handle
// vibrating; on every cycle (~60 Hz) it slaps the loose metal teeth in the box
// against the wood. So: a very regular rattle at the handle's frequency, each
// rattle a short broadband burst (0.8–8 kHz, peaking near 2.5 kHz) from many
// teeth at once, dying slowly over ~1.5 s, after the knock of the ball.
const VIBRASLAP_RES: [Resonator; 6] = [
  rz(900.0, 4.0, -12.0),
  rz(1_600.0, 5.0, -9.0),
  rz(2_500.0, 6.0, 0.0),
  rz(3_500.0, 6.0, -4.0),
  rz(5_200.0, 5.0, -6.0),
  rz(7_800.0, 4.0, -7.0),
];
const VIBRASLAP_MODES: [PercMode; 3] = [
  m(620.0, 0.0, 0.05),
  m(1_340.0, -6.0, 0.04),
  m(2_150.0, -10.0, 0.03),
];
const VIBRASLAP_KNOCK: [PercNoise; 1] = [nz(500.0, 5_000.0, 6.0, 0.02)];
const VIBRASLAP: PercSpec = PercSpec {
  modes: &VIBRASLAP_MODES,
  modes_db: -10.0,
  noise: &VIBRASLAP_KNOCK,
  particles: Some(Particles {
    rate: 62.0,
    regular: 0.97,
    delay: 0.008,
    attack: 0.002,
    hold: 0.05,
    t60: 2.2,
    scatter: 0.35,
    grain: 0.05,
    groups: 0,
    click: 0.0,
    resonators: &VIBRASLAP_RES,
    db: 10.8,
  }),
  ..PercSpec::EMPTY
};

// --- Shakers and jingles (particle collisions) --------------------------------
// Tambourine: the frame's thump plus pairs of jingles colliding for ~0.1 s
// after the hit, each ringing at its own metal resonances.
const TAMBOURINE_RES: [Resonator; 6] = [
  rz(3_100.0, 22.0, -16.0),
  rz(4_350.0, 22.0, 0.0),
  rz(5_600.0, 20.0, 0.0),
  rz(7_200.0, 18.0, 0.0),
  rz(8_900.0, 16.0, 0.0),
  rz(11_200.0, 14.0, -1.0),
];
const TAMBOURINE_MODES: [PercMode; 3] = [
  m(260.0, 0.0, 0.08),
  m(415.0, -6.0, 0.06),
  m(560.0, -10.0, 0.05),
];
const TAMBOURINE_NOISE: [PercNoise; 1] = [nz(5_000.0, 14_000.0, -18.0, 0.12)];
const TAMBOURINE: PercSpec = PercSpec {
  modes: &TAMBOURINE_MODES,
  modes_db: -30.0,
  noise: &TAMBOURINE_NOISE,
  particles: Some(Particles {
    rate: 1_400.0,
    regular: 0.0,
    delay: 0.0,
    attack: 0.02,
    hold: 0.01,
    t60: 0.3,
    scatter: 0.8,
    grain: 0.0,
    groups: 0,
    click: 0.0,
    resonators: &TAMBOURINE_RES,
    db: 6.0,
  }),
  ..PercSpec::EMPTY
};

// Jingle (sleigh) bells: a strap of small closed bells, each with a ball
// inside. Each collision strikes one bell (a resonator group) with a metallic
// click; every bell has partials near 1, 2.4–2.65, 4.1 and 6 × its pitch
// (the Logic sleigh bells: 2.6, 6.1–6.9, 10.7, 15.7 kHz; FluidSynth's: 2.5,
// 5–6.3, 10 kHz). Bells differ slightly, so the strap shimmers instead of
// repeating one chord.
const fn bell(f0: f32, second: f32) -> [Resonator; 4] {
  [
    rz(f0, 200.0, -10.0),
    rz(f0 * second, 300.0, 0.0),
    rz(f0 * 4.08, 300.0, -10.0),
    rz(f0 * 5.95, 200.0, -16.0),
  ]
}
const JINGLE_RES: [Resonator; 24] = {
  let bells = [
    bell(2_450.0, 2.62),
    bell(2_560.0, 2.45),
    bell(2_630.0, 2.63),
    bell(2_710.0, 2.38),
    bell(2_820.0, 2.55),
    bell(2_930.0, 2.42),
  ];
  let mut out = [rz(1_000.0, 1.0, 0.0); 24];
  let mut k = 0;
  while k < 24 {
    out[k] = bells[k / 4][k % 4];
    k += 1;
  }
  out
};
const JINGLE_BELL: PercSpec = PercSpec {
  particles: Some(Particles {
    rate: 350.0,
    regular: 0.0,
    delay: 0.0,
    attack: 0.005,
    hold: 0.04,
    t60: 0.5,
    scatter: 0.7,
    grain: 0.0,
    groups: 6,
    click: 0.012,
    resonators: &JINGLE_RES,
    db: 43.8,
  }),
  ..PercSpec::EMPTY
};
// Cabasa: steel bead chains rolled against a textured cylinder — a dense,
// bright scrape.
const CABASA_RES: [Resonator; 3] = [
  rz(5_200.0, 1.5, 0.0),
  rz(8_000.0, 2.0, -2.0),
  rz(11_500.0, 2.0, -6.0),
];
const CABASA: PercSpec = PercSpec {
  particles: Some(Particles {
    rate: 4_000.0,
    regular: 0.0,
    delay: 0.0,
    attack: 0.05,
    hold: 0.08,
    t60: 0.2,
    scatter: 0.9,
    grain: 0.0,
    groups: 0,
    click: 0.0,
    resonators: &CABASA_RES,
    db: 0.0,
  }),
  ..PercSpec::EMPTY
};

// Maracas: seeds hitting a gourd shell — a short, grainy burst.
const MARACAS_RES: [Resonator; 2] =
  [rz(4_000.0, 3.0, 0.0), rz(6_500.0, 3.0, -8.0)];
const MARACAS: PercSpec = PercSpec {
  particles: Some(Particles {
    rate: 2_500.0,
    regular: 0.0,
    delay: 0.0,
    attack: 0.04,
    hold: 0.01,
    t60: 0.25,
    scatter: 0.9,
    grain: 0.0,
    groups: 0,
    click: 0.0,
    resonators: &MARACAS_RES,
    db: 0.0,
  }),
  ..PercSpec::EMPTY
};

// Shaker: one shake of a tube of beads — the stream builds, peaks as the
// beads hit the end, and dies.
const SHAKER_RES: [Resonator; 4] = [
  rz(1_800.0, 1.5, -20.0),
  rz(4_800.0, 2.5, -4.0),
  rz(7_400.0, 2.2, -2.0),
  rz(10_000.0, 2.0, 0.0),
];
const SHAKER: PercSpec = PercSpec {
  particles: Some(Particles {
    rate: 3_000.0,
    regular: 0.0,
    delay: 0.0,
    attack: 0.035,
    hold: 0.01,
    t60: 0.25,
    scatter: 0.9,
    grain: 0.0,
    groups: 0,
    click: 0.0,
    resonators: &SHAKER_RES,
    db: 0.0,
  }),
  ..PercSpec::EMPTY
};

// Guiro: a stick drawn over the ridges of a gourd. Every ridge is a short
// scrape (a noise grain, not a click: isolated impulses crackle) through the
// gourd's resonances at 1.3–1.7 and 3–4 kHz. The quick short stroke passes
// ~180 ridges/s and fuses into one rasp; the slow long stroke is a ~55 Hz
// "rrrr" whose level dips ~15 dB between ridges.
const GUIRO_RES: [Resonator; 5] = [
  rz(1_300.0, 6.0, 0.0),
  rz(1_700.0, 6.0, -3.0),
  rz(3_300.0, 5.0, 0.0),
  rz(4_200.0, 5.0, -2.0),
  rz(6_000.0, 4.0, -16.0),
];
const GUIRO_SHORT: PercSpec = PercSpec {
  particles: Some(Particles {
    rate: 180.0,
    regular: 0.9,
    delay: 0.0,
    attack: 0.006,
    hold: 0.035,
    t60: 0.06,
    scatter: 0.3,
    grain: 0.012,
    groups: 0,
    click: 0.0,
    resonators: &GUIRO_RES,
    db: 14.3,
  }),
  ..PercSpec::EMPTY
};
const GUIRO_LONG: PercSpec = PercSpec {
  particles: Some(Particles {
    rate: 55.0,
    regular: 0.92,
    delay: 0.0,
    attack: 0.02,
    hold: 0.36,
    t60: 0.06,
    scatter: 0.3,
    grain: 0.04,
    groups: 0,
    click: 0.0,
    resonators: &GUIRO_RES,
    db: 4.6,
  }),
  ..PercSpec::EMPTY
};

// --- Wood --------------------------------------------------------------------
// Fitted to the Logic percussion claves, wood block, castanets and the
// Drum Kit Designer sticks.
// Claves: two hardwood sticks — a dominant bar mode near 2.2 kHz with close
// partners (the two sticks and their two bending planes) ringing ~0.15 s.
const CLAVES_MODES: [PercMode; 10] = tuned(
  2_230.0,
  [
    (1.0, 0.0, 0.15),
    (1.043, -3.0, 0.1),
    (1.06, -7.0, 0.1),
    (0.949, -24.0, 0.12),
    (0.917, -26.0, 0.16),
    (0.893, -27.0, 0.09),
    (0.718, -28.0, 0.15),
    (2.234, -26.0, 0.07),
    (2.383, -27.0, 0.07),
    (2.5, -23.0, 0.15),
  ],
);
const WOOD_CLICK: [PercNoise; 1] = [PercNoise {
  vel: 0.4,
  ..nz(1_200.0, 10_000.0, -16.0, 0.006)
}];
const CLAVES: PercSpec = PercSpec {
  modes: &CLAVES_MODES,
  hardness: 9_000.0,
  noise: &WOOD_CLICK,
  ..PercSpec::EMPTY
};

// Wood blocks: a slotted hardwood block — almost a single, quickly damped
// resonance (the slot cavity), everything else 20–50 dB down.
const fn woodblock(f0: f32, t60: f32) -> [PercMode; 9] {
  tuned(
    f0,
    [
      (1.0, 0.0, t60),
      (0.972, -20.0, t60 * 0.8),
      (0.886, -25.0, t60 * 0.8),
      (0.669, -31.0, t60),
      (0.61, -38.0, t60 * 0.7),
      (1.277, -42.0, 0.12),
      (1.396, -44.0, 0.1),
      (2.264, -48.0, 0.12),
      (3.228, -51.0, 0.16),
    ],
  )
}
const WOODBLOCK_HI_MODES: [PercMode; 9] = woodblock(1_640.0, 0.07);
const WOODBLOCK_LO_MODES: [PercMode; 9] = woodblock(1_150.0, 0.09);
const WOODBLOCK_HI: PercSpec = PercSpec {
  modes: &WOODBLOCK_HI_MODES,
  hardness: 6_000.0,
  noise: &WOOD_CLICK,
  ..PercSpec::EMPTY
};
const WOODBLOCK_LO: PercSpec = PercSpec {
  modes: &WOODBLOCK_LO_MODES,
  ..WOODBLOCK_HI
};

// Castanets: two shells clicked together — a sharp click, then the shells'
// modes (1.6–2.3 kHz, a few up to 6 kHz) ringing ~0.15 s some 20 dB down.
const CASTANETS_MODES: [PercMode; 9] = tuned(
  1_620.0,
  [
    (1.0, 0.0, 0.13),
    (1.13, -19.0, 0.15),
    (1.211, -15.0, 0.16),
    (1.441, -14.0, 0.19),
    (2.326, -34.0, 0.24),
    (3.196, -21.0, 0.18),
    (3.613, -25.0, 0.15),
    (3.897, -35.0, 0.13),
    (5.31, -26.0, 0.03),
  ],
);
const CASTANETS_NOISE: [PercNoise; 1] = [nz(1_500.0, 12_000.0, 8.0, 0.008)];
const CASTANETS: PercSpec = PercSpec {
  modes: &CASTANETS_MODES,
  modes_db: -14.0,
  hardness: 9_000.0,
  noise: &CASTANETS_NOISE,
  ..PercSpec::EMPTY
};

// Sticks: two hickory drumsticks knocked together. Wood damps fast: the
// stick's bending modes (2, 3 and 4.3 kHz in the Drum Kit Designer sticks)
// die within 30–50 ms. One mode per cluster: close pairs beat and ring like
// metal. The ~1 kHz knock and the broadband contact noise carry the "tock".
// The recording's slow 0.2 s tail is the room, decaying equally in every
// band, and is left to the reverb.
const STICKS_MODES: [PercMode; 5] = tuned(
  1.0,
  [
    (1_050.0, -4.0, 0.02),
    (2_019.0, -6.0, 0.04),
    (2_960.0, 0.0, 0.035),
    (4_290.0, -2.0, 0.03),
    (6_100.0, -10.0, 0.02),
  ],
);
const STICKS_NOISE: [PercNoise; 2] = [
  // Contact: a broadband knock.
  PercNoise {
    vel: 0.5,
    ..nz(700.0, 9_000.0, 8.0, 0.012)
  },
  // Hollow body knock of the two sticks.
  nz(600.0, 1_800.0, 6.0, 0.02),
];
const STICKS: PercSpec = PercSpec {
  modes: &STICKS_MODES,
  hardness: 9_000.0,
  noise: &STICKS_NOISE,
  ..PercSpec::EMPTY
};

// Slap (slapstick): two boards clapped together — a sharp crack.
const SLAP_MODES: [PercMode; 3] = [
  m(1_250.0, 0.0, 0.05),
  m(2_100.0, -4.0, 0.04),
  m(3_300.0, -8.0, 0.03),
];
const SLAP_NOISE: [PercNoise; 1] = [nz(800.0, 8_000.0, 0.0, 0.025)];
const SLAP: PercSpec = PercSpec {
  modes: &SLAP_MODES,
  modes_db: -4.0,
  noise: &SLAP_NOISE,
  ..PercSpec::EMPTY
};

// Metronome click: the escapement's plastic click.
const METRO_CLICK_MODES: [PercMode; 3] = [
  m(1_620.0, 0.0, 0.025),
  m(3_150.0, -4.0, 0.02),
  m(4_900.0, -10.0, 0.015),
];
const METRO_CLICK: PercSpec = PercSpec {
  modes: &METRO_CLICK_MODES,
  hardness: 9_000.0,
  noise: &WOOD_CLICK,
  ..PercSpec::EMPTY
};

// --- Tones -------------------------------------------------------------------
// Pea whistle: a ~2.8 kHz tone warbled irregularly by the pea at ~35 Hz,
// with breath.
const WHISTLE_BREATH_SHORT: [PercNoise; 1] = [PercNoise {
  attack: 0.015,
  hold: 0.1,
  ..nz(1_800.0, 7_000.0, -20.0, 0.05)
}];
const WHISTLE_BREATH_LONG: [PercNoise; 1] = [PercNoise {
  attack: 0.02,
  hold: 0.42,
  ..nz(1_800.0, 7_000.0, -20.0, 0.06)
}];
const WHISTLE_TONE: PercTone = PercTone {
  from: 2_700.0,
  to: 2_850.0,
  glide: 0.02,
  warble_hz: 36.0,
  warble: 0.035,
  rough: 0.6,
  drive: 0.4,
  formant: 0.0,
  formant_q: 1.0,
  attack: 0.015,
  hold: 0.1,
  t60: 0.05,
  db: 0.0,
};
const WHISTLE_SHORT: PercSpec = PercSpec {
  noise: &WHISTLE_BREATH_SHORT,
  tone: Some(WHISTLE_TONE),
  ..PercSpec::EMPTY
};
const WHISTLE_LONG: PercSpec = PercSpec {
  noise: &WHISTLE_BREATH_LONG,
  tone: Some(PercTone {
    attack: 0.02,
    hold: 0.42,
    t60: 0.06,
    ..WHISTLE_TONE
  }),
  ..PercSpec::EMPTY
};

// Cuíca: a friction drum — a stick rubbed inside drives the head, giving a
// nasal, gliding voice; open strokes swoop up, muted ones squeak high.
const CUICA_OPEN: PercSpec = PercSpec {
  tone: Some(PercTone {
    from: 290.0,
    to: 470.0,
    glide: 0.06,
    warble_hz: 9.0,
    warble: 0.01,
    rough: 0.5,
    drive: 2.5,
    formant: 1_000.0,
    formant_q: 1.5,
    attack: 0.012,
    hold: 0.1,
    t60: 0.18,
    db: 6.0,
  }),
  ..PercSpec::EMPTY
};
const CUICA_MUTE: PercSpec = PercSpec {
  tone: Some(PercTone {
    from: 620.0,
    to: 760.0,
    glide: 0.02,
    warble_hz: 9.0,
    warble: 0.01,
    rough: 0.5,
    drive: 2.5,
    formant: 1_300.0,
    formant_q: 1.5,
    attack: 0.006,
    hold: 0.03,
    t60: 0.07,
    db: 6.0,
  }),
  ..PercSpec::EMPTY
};

// Scratch: a record pushed / pulled under the needle — a buzzy tone swept
// up (push) or down (pull).
const SCRATCH_NOISE: [PercNoise; 1] = [PercNoise {
  attack: 0.01,
  hold: 0.05,
  ..nz(500.0, 4_000.0, -14.0, 0.04)
}];
const SCRATCH_PUSH: PercSpec = PercSpec {
  noise: &SCRATCH_NOISE,
  tone: Some(PercTone {
    from: 260.0,
    to: 1_100.0,
    glide: 0.05,
    warble_hz: 0.0,
    warble: 0.0,
    rough: 0.0,
    drive: 4.0,
    formant: 1_400.0,
    formant_q: 0.8,
    attack: 0.01,
    hold: 0.06,
    t60: 0.05,
    db: 0.0,
  }),
  ..PercSpec::EMPTY
};
const SCRATCH_PULL: PercSpec = PercSpec {
  noise: &SCRATCH_NOISE,
  tone: Some(PercTone {
    from: 1_000.0,
    to: 220.0,
    glide: 0.05,
    warble_hz: 0.0,
    warble: 0.0,
    rough: 0.0,
    drive: 4.0,
    formant: 1_200.0,
    formant_q: 0.8,
    attack: 0.01,
    hold: 0.07,
    t60: 0.05,
    db: 0.0,
  }),
  ..PercSpec::EMPTY
};

// High Q: the drum machine "Q" blip — a fast downward chirp.
const HIGH_Q: PercSpec = PercSpec {
  tone: Some(PercTone {
    from: 3_200.0,
    to: 700.0,
    glide: 0.008,
    warble_hz: 0.0,
    warble: 0.0,
    rough: 0.0,
    drive: 0.5,
    formant: 0.0,
    formant_q: 1.0,
    attack: 0.0005,
    hold: 0.0,
    t60: 0.06,
    db: 0.0,
  }),
  ..PercSpec::EMPTY
};

// Square click: a sequencer's click — a single square-ish cycle burst.
const SQUARE_CLICK: PercSpec = PercSpec {
  tone: Some(PercTone {
    from: 1_800.0,
    to: 1_800.0,
    glide: 0.01,
    warble_hz: 0.0,
    warble: 0.0,
    rough: 0.0,
    drive: 6.0,
    formant: 0.0,
    formant_q: 1.0,
    attack: 0.0003,
    hold: 0.002,
    t60: 0.012,
    db: -4.0,
  }),
  ..PercSpec::EMPTY
};

// --- Hi-hats -------------------------------------------------------------------
// Two plates: flat from ~3 to 16 kHz with a 300–500 Hz "chick". Closed falls
// 30 dB in 60–70 ms; open sustains for seconds (until choked).
const HAT_CLOSED_BANDS: [CymbalBand; 8] = [
  band(300.0, -14.0, 0.0, 0.02),
  band(450.0, -6.0, -1.0, 0.02),
  band(800.0, -12.0, 0.0, 0.02),
  band(1_600.0, -12.0, 0.0, 0.02),
  band(3_200.0, -4.0, 0.0, 0.02),
  band(6_300.0, 0.0, 0.0, 0.02),
  band(10_000.0, 0.0, 0.0, 0.02),
  band(13_000.0, -3.0, 0.0, 0.02),
];
const HAT_CLOSED: CymbalSpec = CymbalSpec {
  seed: 42,
  lo: 300.0,
  hi: 13_000.0,
  density: 3.0,
  scatter_db: 5.0,
  floor_db: 0.0,
  bands: &HAT_CLOSED_BANDS,
  ring: 0.2,
  ring_corner: 30_000.0,
  brightness: 8_000.0,
  noise_hz: 7_000.0,
  stick: 0.6,
  stick_tau: 0.006,
  sizzle: 0.25,
  sizzle_t60: 0.15,
  drive: 0.0,
  wobble: 1.2,
};
// Foot-closed ("chick"): no stick. The plates clap shut, so every band swells
// over ~10–25 ms, holds for ~40 ms and dies with T60 ≈ 0.3 s, with the same
// ~10 kHz centroid as a closed hit (fitted to the Drum Kit Designer pedal
// hats).
const HAT_PEDAL_BANDS: [CymbalBand; 8] = [
  band(300.0, -18.0, 0.9, 0.015),
  band(450.0, -10.0, 0.9, 0.015),
  band(800.0, -16.0, 0.9, 0.015),
  band(1_600.0, -14.0, 0.9, 0.015),
  band(3_200.0, -5.0, 0.9, 0.015),
  band(6_300.0, 0.0, 0.9, 0.015),
  band(10_000.0, 0.0, 0.9, 0.015),
  band(13_000.0, -3.0, 0.9, 0.015),
];
const HAT_PEDAL: CymbalSpec = CymbalSpec {
  seed: 44,
  bands: &HAT_PEDAL_BANDS,
  ring: 0.35,
  stick: 0.0,
  sizzle: 0.05,
  sizzle_t60: 0.2,
  ..HAT_CLOSED
};
// A clear ~500 Hz ring over a darker 0.7–1.3 kHz; a flat 1–10 kHz spectrum
// reads as hiss.
const HAT_OPEN_BANDS: [CymbalBand; 10] = [
  band(300.0, -24.0, 0.0, 0.05),
  band(420.0, -3.0, -1.0, 0.05),
  band(550.0, -3.0, 0.0, 0.05),
  band(800.0, -22.0, 0.0, 0.05),
  band(1_000.0, -20.0, 0.0, 0.05),
  band(1_600.0, -6.0, 0.0, 0.05),
  band(3_200.0, -3.0, 0.0, 0.05),
  band(5_000.0, 0.0, 0.0, 0.05),
  band(8_000.0, -2.0, 0.0, 0.05),
  band(12_500.0, -8.0, 0.0, 0.05),
];
const HAT_OPEN: CymbalSpec = CymbalSpec {
  seed: 46,
  bands: &HAT_OPEN_BANDS,
  floor_db: -6.0,
  ring: 3.0,
  ring_corner: 16_000.0,
  stick: 0.5,
  sizzle: 0.1,
  sizzle_t60: 1.5,
  ..HAT_CLOSED
};

pub fn drum_patch(note: u8) -> DrumPatch {
  match note {
    27 => perc(&HIGH_Q, 1.17),
    28 => perc(&SLAP, 4.11),
    29 => perc(&SCRATCH_PUSH, 0.293),
    30 => perc(&SCRATCH_PULL, 0.321),
    31 => perc(&STICKS, 0.7),
    32 => perc(&SQUARE_CLICK, 0.836),
    33 => perc(&METRO_CLICK, 0.674),
    34 => perc(&METRO_BELL, 0.745),
    35 => perc(&KICK_BIG, 1.0),
    36 => perc(&KICK, 1.0),
    37 => perc(&SIDE_STICK, 0.6),
    38 => perc(&SNARE, 0.996),
    39 => perc(&CLAP, 6.73),
    40 => perc(&SNARE_TIGHT, 0.673),
    41 => pan(perc(&TOM_FLOOR_LO, 0.621), 0.4),
    42 => pan(group(cymbal(&HAT_CLOSED, 0.157), 1), -0.35),
    43 => pan(perc(&TOM_FLOOR_HI, 0.8), 0.3),
    44 => pan(group(cymbal(&HAT_PEDAL, 0.189), 1), -0.35),
    45 => pan(perc(&TOM_LO, 0.374), 0.15),
    46 => pan(group(cymbal(&HAT_OPEN, 0.0705), 1), -0.35),
    47 => pan(perc(&TOM_LO_MID, 0.44), 0.0),
    48 => pan(perc(&TOM_HI_MID, 0.455), -0.1),
    49 => pan(cymbal(&CRASH_1, 0.19), -0.4),
    50 => pan(perc(&TOM_HI, 0.425), -0.2),
    51 => pan(cymbal(&RIDE_1, 0.052), 0.45),
    52 => pan(cymbal(&CHINA, 0.36), 0.5),
    53 => pan(cymbal(&RIDE_BELL, 0.1), 0.45),
    54 => perc(&TAMBOURINE, 7.14),
    55 => pan(cymbal(&SPLASH, 0.24), -0.2),
    56 => perc(&COWBELL, 0.594),
    57 => pan(cymbal(&CRASH_2, 0.2), 0.35),
    58 => perc(&VIBRASLAP, 1.0),
    59 => pan(cymbal(&RIDE_2, 0.048), 0.45),
    60 => pan(perc(&BONGO_HI, 1.18), 0.2),
    61 => pan(perc(&BONGO_LO, 1.05), 0.1),
    62 => pan(perc(&CONGA_MUTE, 1.17), -0.1),
    63 => pan(perc(&CONGA_HI, 0.877), -0.1),
    64 => pan(perc(&CONGA_LO, 0.984), -0.25),
    65 => pan(perc(&TIMBALE_HI, 0.659), 0.25),
    66 => pan(perc(&TIMBALE_LO, 0.601), 0.15),
    67 => perc(&AGOGO_HI, 0.4),
    68 => perc(&AGOGO_LO, 0.4),
    69 => perc(&CABASA, 2.01),
    70 => perc(&MARACAS, 6.22),
    71 => group(perc(&WHISTLE_SHORT, 0.434), 2),
    72 => group(perc(&WHISTLE_LONG, 0.395), 2),
    73 => group(perc(&GUIRO_SHORT, 1.0), 3),
    74 => group(perc(&GUIRO_LONG, 1.0), 3),
    75 => perc(&CLAVES, 1.06),
    76 => perc(&WOODBLOCK_HI, 1.66),
    77 => perc(&WOODBLOCK_LO, 1.58),
    78 => group(perc(&CUICA_MUTE, 1.32), 4),
    79 => group(perc(&CUICA_OPEN, 1.47), 4),
    80 => group(perc(&TRIANGLE_MUTE, 0.3), 5),
    81 => group(perc(&TRIANGLE, 0.201), 5),
    82 => perc(&SHAKER, 2.62),
    83 => perc(&JINGLE_BELL, 1.0),
    84 => perc(&BELLTREE, 0.386),
    85 => perc(&CASTANETS, 1.2),
    86 => group(perc(&SURDO_MUTE, 1.97), 6),
    87 => group(perc(&SURDO, 1.56), 6),
    // Outside the GM2 map: a neutral wood click.
    _ => perc(&METRO_CLICK, 0.3),
  }
}

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

  #[test]
  fn hi_hats_choke_each_other() {
    for key in [42, 44, 46] {
      assert_eq!(drum_patch(key).group, 1, "key {key}");
    }
  }

  mod perc {
    use super::*;
    use crate::synth::perc::Perc;

    const SR: f32 = 44_100.0;

    /// Render a percussion key as the voice does: level tracking (and the
    /// idle check) once per 512-sample block, *before* the block.
    /// `choke_at` releases the voice (mute group / note-off on a choked
    /// instrument) at that time. Stops early once the voice goes idle.
    fn render(key: u8, secs: f32, choke_at: Option<f32>) -> (Vec<f32>, bool) {
      let DrumSound::Perc(spec) = drum_patch(key).sound else {
        panic!("key {key} is not a percussion model");
      };
      let mut p = Perc::new(spec, 0.6, 12_345, SR);
      let mut out = Vec::new();
      while out.len() < (secs * SR) as usize {
        if choke_at.is_some_and(|t| out.len() as f32 >= t * SR) {
          p.damp();
        }
        p.update_level();
        if p.is_silent() {
          return (out, true);
        }
        out.extend((0..512).map(|_| p.tick()));
      }
      (out, false)
    }

    fn rms_db(x: &[f32], a: f32, b: f32) -> f32 {
      let (a, b) = ((a * SR) as usize, ((b * SR) as usize).min(x.len()));
      if a >= b {
        return -200.0;
      }
      let p = x[a..b].iter().map(|v| v * v).sum::<f32>() / (b - a) as f32;
      10.0 * p.max(1e-20).log10()
    }

    fn perc_keys() -> impl Iterator<Item = u8> {
      (27..=87).filter(|&k| matches!(drum_patch(k).sound, DrumSound::Perc(_)))
    }

    #[test]
    fn every_drum_sounds_and_dies_away() {
      for key in perc_keys() {
        let (x, idle) = render(key, 8.0, None);
        assert!(x.iter().all(|v| v.is_finite()), "key {key}: not finite");
        let peak = x.iter().fold(0.0f32, |m, v| m.max(v.abs()));
        assert!(peak > 1e-3, "key {key}: silent (peak {peak})");
        assert!(idle, "key {key}: still sounding after 8 s");
      }
    }

    #[test]
    fn slow_onsets_are_not_retired_at_note_on() {
      // Shakers swell for tens of ms; idle detection at the first block once
      // dropped them before they made a sound.
      for key in [54, 69, 70, 82, 83] {
        let (x, _) = render(key, 0.3, None);
        let late = rms_db(&x, 0.03, 0.1);
        assert!(late > -60.0, "key {key}: {late:.0} dB at 30–100 ms");
      }
    }

    #[test]
    fn choking_stops_the_sound() {
      // Mute groups (whistle, guiro, cuica, triangle, surdo) and note-off on
      // a ringing instrument must cut it within a fraction of a second.
      for key in [71, 72, 74, 79, 81, 87] {
        let (x, idle) = render(key, 2.0, Some(0.05));
        let before = rms_db(&x, 0.0, 0.05);
        let after = rms_db(&x, 0.3, 0.4);
        assert!(idle, "key {key}: never went idle after the choke");
        assert!(
          after < before - 40.0,
          "key {key}: {before:.0} dB → {after:.0} dB"
        );
      }
    }

    #[test]
    fn open_strokes_ring_longer_than_muted() {
      for (open, muted) in [(63, 62), (81, 80), (87, 86)] {
        let (o, _) = render(open, 1.0, None);
        let (m, _) = render(muted, 1.0, None);
        let fall = |x: &[f32]| rms_db(x, 0.0, 0.03) - rms_db(x, 0.15, 0.25);
        assert!(
          fall(&o) + 10.0 < fall(&m),
          "open {open} falls {:.0} dB, muted {muted} {:.0} dB",
          fall(&o),
          fall(&m)
        );
      }
    }
  }

  mod cymbals {
    use super::*;
    use crate::synth::cymbal::Cymbal;
    use crate::synth::waveguide::Biquad;

    const SR: f32 = 44_100.0;
    const CYMBAL_KEYS: [u8; 7] = [49, 51, 52, 53, 55, 57, 59];

    fn render(key: u8, velocity: f32, secs: f32) -> Vec<f32> {
      let DrumSound::Cymbal(spec) = drum_patch(key).sound else {
        panic!("key {key} is not a cymbal");
      };
      let mut c = Cymbal::new(spec, SR);
      c.strike(velocity, 0);
      let mut out = Vec::new();
      while out.len() < (secs * SR) as usize {
        c.cull();
        out.extend((0..512).map(|_| c.tick()));
      }
      out
    }

    fn db(x: f32) -> f32 {
      10.0 * x.max(1e-20).log10()
    }

    /// Mean power of `x[a..b]` (seconds) in a ~half-octave band at `hz`.
    fn band_power(x: &[f32], hz: f32, a: f32, b: f32) -> f32 {
      let (mut b1, mut b2) =
        (Biquad::bandpass(hz, 3.0, SR), Biquad::bandpass(hz, 3.0, SR));
      let y: Vec<f32> = x.iter().map(|&v| b2.tick(b1.tick(v))).collect();
      let seg = &y[(a * SR) as usize..(b * SR) as usize];
      seg.iter().map(|v| v * v).sum::<f32>() / seg.len() as f32
    }

    #[test]
    fn cymbals_are_partials_neither_noise_nor_glass() {
      // Over 2–3 kHz at 4 Hz resolution, the top 10% of bins sit above the
      // median by ~5 dB for white noise, 9–12 dB for recorded crashes, 15–17
      // dB for recorded rides, and ~30 dB for a sparse set of perfectly
      // clean partials (which sounds glassy).
      for key in CYMBAL_KEYS {
        let x = render(key, 0.5, 0.6);
        let seg = &x[(0.3 * SR) as usize..(0.55 * SR) as usize];
        let n = seg.len();
        let mut bins: Vec<f32> = (0..250)
          .map(|k| {
            let w = std::f32::consts::TAU * (2_000.0 + 4.0 * k as f32) / SR;
            let (mut re, mut im) = (0.0f32, 0.0f32);
            for (i, v) in seg.iter().enumerate() {
              let hann =
                0.5 - 0.5 * (std::f32::consts::TAU * i as f32 / n as f32).cos();
              re += v * hann * (w * i as f32).cos();
              im += v * hann * (w * i as f32).sin();
            }
            re * re + im * im
          })
          .collect();
        bins.sort_by(f32::total_cmp);
        let spread = db(bins[225]) - db(bins[125]);
        // The ride bell is meant to clang: a few partials dominate.
        let max = if key == 53 { 30.0 } else { 22.0 };
        assert!(
          (8.0..max).contains(&spread),
          "key {key}: p90/median {spread:.1} dB"
        );
      }
    }

    #[test]
    fn hitting_a_ringing_cymbal_restrikes_it() {
      // A cymbal is one object: a second hit excites the modes already
      // ringing instead of stacking up a second voice.
      let mut s = crate::synth::Synth::new(SR);
      let mut buf = vec![0.0f32; 1024];
      for _ in 0..4 {
        s.note_on(9, 51, 100);
        s.render(&mut buf);
      }
      assert_eq!(s.voices.len(), 1);
      assert!(buf.iter().any(|v| v.abs() > 1e-3));
    }

    #[test]
    fn crash_swells_and_ride_pings() {
      // Crash: the plate pumps energy up into 3–6 kHz after the strike.
      // Ride: the stick ping is the loudest moment there.
      for key in [49, 57] {
        let x = render(key, 0.6, 0.15);
        let (early, later) = (
          band_power(&x, 4_500.0, 0.0, 0.015),
          band_power(&x, 4_500.0, 0.06, 0.1),
        );
        assert!(
          db(later) - db(early) > 3.0,
          "key {key}: 4.5 kHz grows only {:.1} dB",
          db(later) - db(early)
        );
      }
      let x = render(51, 0.6, 0.15);
      let (early, later) = (
        band_power(&x, 4_500.0, 0.0, 0.015),
        band_power(&x, 4_500.0, 0.06, 0.1),
      );
      assert!(early > later, "ride swells instead of pinging");
    }

    #[test]
    fn cymbal_highs_die_first() {
      // The tail settles around 1–3 kHz: between 0.2 s and 1.5 s, 9 kHz
      // loses 10.5–25 dB more than 2 kHz in recorded crashes, 21–37 dB in
      // recorded rides.
      for (key, min) in [(49, 10.0), (57, 10.0), (51, 18.0), (59, 18.0)] {
        let x = render(key, 0.5, 1.6);
        let tilt = |a: f32, b: f32| {
          db(band_power(&x, 9_000.0, a, b)) - db(band_power(&x, 2_000.0, a, b))
        };
        let drop = tilt(0.2, 0.3) - tilt(1.4, 1.5);
        assert!(drop > min, "key {key}: highs fall only {drop:.1} dB faster");
      }
    }

    #[test]
    fn harder_hits_are_brighter() {
      let tilt = |v: f32| {
        let x = render(49, v, 0.5);
        db(band_power(&x, 9_000.0, 0.05, 0.4))
          - db(band_power(&x, 1_500.0, 0.05, 0.4))
      };
      let (soft, hard) = (tilt(0.1), tilt(1.0));
      assert!(
        hard - soft > 4.0,
        "ff only {:.1} dB brighter than pp",
        hard - soft
      );
    }
  }
}