xmrs 0.14.7

Read, edit and serialize SoundTracker music with pleasure — MOD/XM/S3M/IT/DW import plus SID & OPL chip synthesis, no_std.
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
//! RFC §3B — native signal-graph building blocks: insert **devices**,
//! ordered **chains**, and aux/return **bus** routing.
//!
//! This is the *plumbing* half of the signal graph. A [`DeviceChain`]
//! is an ordered, bypassable list of inserts processing one mixer
//! channel's stereo output; [`Bus`] / [`Send`] route post-insert
//! signal into aux/return buses. Rich DSP content (EQ, reverb,
//! compressor, …) is out of this RFC's scope (§6.2 — "3B ne livre que
//! la plomberie, pas le contenu"); [`Device`] is the typed slot those
//! will extend. The built-ins today are a stateless linear
//! [`Device::Gain`], a one-pole [`Device::LowPass`], and the native
//! fixed-point effects [`Device::ParamEq`] and [`Device::Distortion`]
//! (RBJ biquad / waveshaper built on [`crate::core::daw::dsp`]). Devices
//! may carry per-channel state — [`Device::process`] takes `&mut self` —
//! and time-based devices precompute coefficients in [`Device::prepare`].
//!
//! It is deliberately **separate** from [`crate::tracker::mix_plugin`]
//! (`MixPlugins`): those are opaque, host-dependent VST chunks the
//! player can't execute, preserved only for round-trip. This chain is
//! native and player-processable.
//!
//! **Compatibility.** Every tracker import leaves the chains empty, so
//! the mixer skips them ([`DeviceChain::is_active`] is `false`) and the
//! render is bit-identical to the pre-3B player. A `DeviceChain` is a
//! pure pass-through when empty or bypassed.

use serde::{Deserialize, Serialize};

use alloc::vec::Vec;

use crate::core::daw::dsp::{self, Biquad};
use crate::core::fixed::fixed::Q15;
use crate::core::fixed::units::{Amp, Volume};

/// Map a normalized Q1.15 automation value (treated as `[0, 1]`,
/// negatives clamped) onto an inclusive integer range `[lo, hi]`.
#[inline]
fn lerp_param(value: Q15, lo: i32, hi: i32) -> i32 {
    let frac = value.raw().max(0) as i64; // 0..=Q15::ONE
    lo + (((hi - lo) as i64 * frac) / Q15::ONE.raw() as i64) as i32
}

/// Recompute the peaking-EQ biquad from its parameters (no-op until the
/// output rate is known).
fn recompute_param_eq(
    eq: &mut Biquad,
    rate_hz: u32,
    center_hz: u16,
    bandwidth_semitones: u8,
    gain_milli_db: i16,
) {
    if rate_hz == 0 {
        return;
    }
    // Bandwidth: semitones → octaves (Q16.16) = semitones/12.
    let bw_oct_q16_16 = ((bandwidth_semitones.max(1) as i64) * 65_536) / 12;
    eq.set_peaking(
        rate_hz,
        center_hz as u32,
        bw_oct_q16_16.max(1),
        gain_milli_db as i32,
    );
}

/// Recompute the distortion's pre/post filters and output gain.
#[allow(clippy::too_many_arguments)]
fn recompute_distortion(
    pre_lp: &mut Biquad,
    post_eq: &mut Biquad,
    out_gain_q15: &mut i32,
    rate_hz: u32,
    gain_milli_db: i32,
    post_eq_center_hz: u16,
    post_eq_bandwidth_hz: u16,
    pre_lowpass_hz: u16,
) {
    if rate_hz == 0 {
        return;
    }
    pre_lp.set_lowpass(
        rate_hz,
        pre_lowpass_hz as u32,
        dsp::TWO_Q_BUTTERWORTH_Q16_16,
    );
    // Post filter as a resonant low-pass: Q ≈ centre / bandwidth.
    let q_q16_16 = ((post_eq_center_hz.max(1) as i64) << 16) / post_eq_bandwidth_hz.max(1) as i64;
    post_eq.set_lowpass(rate_hz, post_eq_center_hz as u32, (2 * q_q16_16).max(1));
    // Output level (≤ 0 dB): Q16.16 linear → Q1.15 (>> 1), clamp.
    let lin = dsp::db_milli_to_lin_q16_16(gain_milli_db);
    *out_gain_q15 = (lin >> 1).clamp(0, Q15::ONE.raw() as i64) as i32;
}

/// Recompute the compressor's detector times and derived log-domain
/// constants (threshold, slope, make-up).
#[allow(clippy::too_many_arguments)]
fn recompute_compressor(
    env: &mut dsp::EnvelopeFollower,
    thresh_oct_q16_16: &mut i32,
    slope_q16_16: &mut i32,
    makeup_oct_q16_16: &mut i32,
    rate_hz: u32,
    threshold_milli_db: i32,
    ratio_q8_8: u16,
    attack_us: u32,
    release_us: u32,
    makeup_milli_db: i32,
) {
    if rate_hz == 0 {
        return;
    }
    env.set_times(rate_hz, attack_us, release_us);
    *thresh_oct_q16_16 = dsp::milli_db_to_oct_q16_16(threshold_milli_db);
    *makeup_oct_q16_16 = dsp::milli_db_to_oct_q16_16(makeup_milli_db);
    // 1/ratio in Q16.16 = (65536·256)/ratio_q8_8 ; slope = 1/ratio − 1.
    let inv_ratio = (16_777_216i64 / ratio_q8_8.max(1) as i64) as i32;
    *slope_q16_16 = inv_ratio - 65_536;
}

/// Topology preset for [`Device::ModDelay`]. Chorus and flanger share
/// one LFO-modulated fractional-delay engine and differ only in their
/// typical delay range and feedback usage.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModDelayMode {
    /// Short base delay (≈ 0–4 ms), feedback creates the swept comb notch.
    Flanger,
    /// Longer base delay (≈ 0–20 ms), little/no feedback, thicker voice.
    Chorus,
}

/// Tuning preset for [`Device::Reverb`]. Both share one Schroeder–Moorer
/// engine; the mode selects the room-size → feedback mapping (WavesReverb
/// reaches longer tails).
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReverbMode {
    /// Jezar's Freeverb tuning / feedback curve.
    Freeverb,
    /// Longer-tail mapping approximating the DirectX `DSFXWavesReverb`.
    WavesReverb,
}

/// A built-in mixer insert.
///
/// Devices may be **stateful**: [`Device::process`] takes `&mut self`,
/// so a device carries its own per-channel state across samples (e.g.
/// a filter's previous output). That state is runtime-only — it lives
/// in the player's runtime chain clone and is `#[serde(skip)]`'d so the
/// serialised model holds parameters, not transient signal history.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Device {
    /// Linear gain, Q1.15 in `[0, 1]` (an attenuator —
    /// [`Volume::FULL`] is unity and leaves the signal untouched).
    /// Stateless.
    Gain(Volume),
    /// One-pole low-pass filter (a stateful built-in, mostly to
    /// exercise the `&mut self` processing path). `cutoff` is the
    /// per-sample smoothing coefficient `a` in Q1.15 `[0, 1]`:
    /// `y[n] = y[n-1] + a·(x[n] − y[n-1])`. `a = 1` (`Q15::ONE`) is a
    /// pass-through; smaller `a` rolls off more high end. `state` is
    /// the previous output per channel (runtime-only).
    LowPass {
        cutoff: Q15,
        #[serde(skip)]
        state: (i32, i32),
    },
    /// RBJ **peaking equaliser** (native equivalent of the DirectX
    /// `DSFXParamEq` DMO). A boost/cut at `center_hz` with a
    /// `bandwidth_semitones`-wide skirt. The biquad coefficients and
    /// history are runtime-only ([`Device::prepare`] derives them from
    /// the output rate). Inserted into an empty chain it is the only
    /// device, so an un-prepared instance is the identity pass-through.
    ParamEq {
        /// Centre frequency in Hz (DMO range 80–16000).
        center_hz: u16,
        /// Bandwidth in semitones (DMO range 1–36).
        bandwidth_semitones: u8,
        /// Peak gain in **milli-decibels** (dB × 1000), signed
        /// (DMO range −15000…+15000).
        gain_milli_db: i16,
        #[serde(skip)]
        rate_hz: u32,
        #[serde(skip)]
        eq: Biquad,
    },
    /// **Distortion** (native equivalent of the DirectX `DSFXDistortion`
    /// DMO): an optional pre-low-pass, a memoryless cubic soft-clip whose
    /// drive is set by `edge`, a resonant post-low-pass that tames the
    /// clipping harmonics, and an output level. Filters/coefficients are
    /// runtime-only; an un-prepared instance passes through.
    Distortion {
        /// Output level in **milli-decibels** (DMO `Gain`, −60000…0).
        gain_milli_db: i32,
        /// Distortion amount (DMO `Edge`, 0–100 %) as Q1.15 `[0, 1]`.
        edge: Q15,
        /// Post-EQ centre / resonant low-pass cutoff in Hz (100–8000).
        post_eq_center_hz: u16,
        /// Post-EQ bandwidth in Hz (100–8000); sets the post filter `Q`.
        post_eq_bandwidth_hz: u16,
        /// Pre-low-pass cutoff in Hz (100–8000).
        pre_lowpass_hz: u16,
        #[serde(skip)]
        rate_hz: u32,
        #[serde(skip)]
        pre_lp: Biquad,
        #[serde(skip)]
        post_eq: Biquad,
        /// Derived output gain, Q1.15.
        #[serde(skip)]
        out_gain_q15: i32,
    },
    /// **Compressor** (native equivalent of the DirectX `DSFXCompressor`
    /// DMO; also the substitute for the "JS Compresseur" VST): a
    /// stereo-linked peak [`crate::core::daw::dsp::EnvelopeFollower`]
    /// feeding a log-domain gain computer (hard knee). Above `threshold`
    /// the gain falls at `(1/ratio − 1)`; `makeup` then lifts the whole
    /// signal. Detector state is runtime-only; an un-prepared instance
    /// passes through.
    Compressor {
        /// Threshold in **milli-decibels** (DMO `Threshold`, −60000…0).
        threshold_milli_db: i32,
        /// Ratio as Q8.8 (`256` = 1:1, `25600` = 100:1; DMO `Ratio`).
        ratio_q8_8: u16,
        /// Attack time in microseconds (DMO `Attack`, 10…500000).
        attack_us: u32,
        /// Release time in microseconds (DMO `Release`, 50000…3000000).
        release_us: u32,
        /// Make-up / output gain in milli-dB (DMO `Gain`, −60000…+60000).
        makeup_milli_db: i32,
        #[serde(skip)]
        rate_hz: u32,
        #[serde(skip)]
        env: dsp::EnvelopeFollower,
        /// Derived: threshold as octaves (log2 linear), Q16.16.
        #[serde(skip)]
        thresh_oct_q16_16: i32,
        /// Derived: `(1/ratio − 1)`, Q16.16 (≤ 0).
        #[serde(skip)]
        slope_q16_16: i32,
        /// Derived: make-up gain as octaves, Q16.16.
        #[serde(skip)]
        makeup_oct_q16_16: i32,
    },
    /// **Echo / delay** (native equivalent of the DirectX `DSFXEcho`
    /// DMO): independent left/right delay taps with feedback and a
    /// wet/dry mix. The recirculating path is held in Q15.16 (16
    /// fractional bits) so long tails decay smoothly. Buffers are
    /// allocated in [`Device::prepare`]; un-prepared ⇒ pass-through.
    Echo {
        /// Wet/dry mix as Q1.15 (`0` = dry, `1` = fully wet; DMO
        /// `WetDryMix` 0–100 %).
        wet_dry: Q15,
        /// Feedback amount as Q1.15 `[0, 1)` (DMO `Feedback` 0–100 %).
        feedback: Q15,
        /// Left delay in milliseconds (DMO `LeftDelay`, 1–2000).
        left_delay_ms: u16,
        /// Right delay in milliseconds (DMO `RightDelay`, 1–2000).
        right_delay_ms: u16,
        /// Swap L/R on each successive echo (DMO `PanDelay`).
        pan_delay: bool,
        #[serde(skip)]
        rate_hz: u32,
        #[serde(skip)]
        dl_l: dsp::DelayLine,
        #[serde(skip)]
        dl_r: dsp::DelayLine,
    },
    /// **Modulated delay** — chorus or flanger (native equivalent of the
    /// DirectX `DSFXChorus` / `DSFXFlanger` DMOs, which share a shape). An
    /// LFO sweeps a short fractional delay; feedback (signed, for the
    /// flanger comb) and a wet/dry mix complete it. Buffers/LFO state are
    /// runtime-only; un-prepared ⇒ pass-through.
    ModDelay {
        mode: ModDelayMode,
        /// Wet/dry mix, Q1.15 (DMO `WetDryMix`).
        wet_dry: Q15,
        /// Modulation depth, Q1.15 `[0, 1]` (DMO `Depth`).
        depth: Q15,
        /// Feedback, **signed** Q1.15 `(-1, 1)` (DMO `Feedback`; the
        /// flanger uses negative values for its characteristic notch).
        feedback: Q15,
        /// LFO rate in millihertz (DMO `Frequency`, 0–10000).
        frequency_milli_hz: u32,
        /// LFO shape: sine (`true`) or triangle (`false`) (DMO `Waveform`).
        waveform_sine: bool,
        /// Base (centre) delay in milliseconds, Q8.8 (DMO `Delay`).
        base_delay_ms_q8_8: u16,
        /// 90° L/R LFO offset for stereo width (DMO `Phase`).
        quadrature: bool,
        #[serde(skip)]
        rate_hz: u32,
        #[serde(skip)]
        dl_l: dsp::DelayLine,
        #[serde(skip)]
        dl_r: dsp::DelayLine,
        #[serde(skip)]
        lfo_phase: u32,
        #[serde(skip)]
        lfo_inc: u32,
    },
    /// **Reverb** (native equivalent of the DirectX `DSFXWavesReverb` and
    /// of the Freeverb VST) — a Schroeder–Moorer network (8 parallel
    /// damped combs + 4 series all-passes per channel) built on
    /// [`crate::core::daw::dsp::Comb`] / [`crate::core::daw::dsp::Allpass`].
    /// Banks/state are runtime-only; un-prepared ⇒ pass-through.
    Reverb {
        mode: ReverbMode,
        /// Room size / decay (Q1.15) — larger ⇒ longer tail.
        room_size: Q15,
        /// High-frequency damping (Q1.15) — larger ⇒ darker tail.
        damping: Q15,
        /// Stereo width (Q1.15).
        width: Q15,
        /// Wet (reverb) level, Q1.15. Scaled by Freeverb's `scalewet = 3`
        /// internally, so it is an independent gain (not a wet/dry
        /// crossfade) that can reach 3×.
        wet: Q15,
        /// Dry (direct) level, Q1.15. Scaled by `scaledry = 2` internally.
        dry: Q15,
        /// Freeze: hold the tail with unit feedback and no damping while
        /// muting the input (Freeverb's `freezemode`) — an infinite,
        /// glassy sustain.
        freeze: bool,
        #[serde(skip)]
        rate_hz: u32,
        #[serde(skip)]
        combs_l: Vec<dsp::Comb>,
        #[serde(skip)]
        combs_r: Vec<dsp::Comb>,
        #[serde(skip)]
        ap_l: Vec<dsp::Allpass>,
        #[serde(skip)]
        ap_r: Vec<dsp::Allpass>,
    },
    /// **I3DL2 environmental reverb** (native approximation of the
    /// DirectX `DSFXI3DL2Reverb` DMO) — an early-reflection tap network
    /// summed with the Freeverb late engine, driven by the audibly
    /// dominant I3DL2 parameters. This is the C1-engine approximation the
    /// plan permits (a single corpus file uses the real DMO); it is *not*
    /// a full 12-parameter environmental model. Runtime banks only ⇒
    /// un-prepared passes through.
    I3DL2Reverb {
        /// Overall reverb level in milli-dB (DMO `Room`, −10000…0).
        room_milli_db: i16,
        /// Late-decay time in ms (DMO `DecayTime`, 100…20000).
        decay_time_ms: u16,
        /// HF decay ratio ×1000 (DMO `DecayHFRatio`, 100…2000 = 0.1…2.0).
        decay_hf_milli: u16,
        /// Early-reflections level in milli-dB (DMO `Reflections`).
        reflections_milli_db: i16,
        /// Early-reflections pre-delay in ms (DMO `ReflectionsDelay`,
        /// 0…300).
        reflections_delay_ms: u16,
        /// Late-reverb level in milli-dB (DMO `Reverb`).
        reverb_milli_db: i16,
        /// Late-reverb pre-delay in ms (DMO `ReverbDelay`, 0…100).
        reverb_delay_ms: u16,
        #[serde(skip)]
        rate_hz: u32,
        #[serde(skip)]
        early_l: dsp::DelayLine,
        #[serde(skip)]
        early_r: dsp::DelayLine,
        #[serde(skip)]
        combs_l: Vec<dsp::Comb>,
        #[serde(skip)]
        combs_r: Vec<dsp::Comb>,
        #[serde(skip)]
        ap_l: Vec<dsp::Allpass>,
        #[serde(skip)]
        ap_r: Vec<dsp::Allpass>,
        /// Derived linear gains (Q1.15): early, late, overall room.
        #[serde(skip)]
        early_gain_q15: i32,
        #[serde(skip)]
        reverb_gain_q15: i32,
        #[serde(skip)]
        room_gain_q15: i32,
    },
    /// **Gargle** (native equivalent of the DirectX `DSFXGargle` DMO): a
    /// periodic **amplitude modulation** — the signal is multiplied by a
    /// unipolar `[0, 1]` triangle (ramp up then down) or square (on/off)
    /// wave at `rate_hz`. A coarse, "gargling" tremolo. The cycle counter
    /// is runtime-only; un-prepared ⇒ pass-through.
    Gargle {
        /// Modulation rate in Hz (DMO `Rate`, 1–1000).
        rate_hz: u16,
        /// Waveform: `false` = triangle, `true` = square (DMO `WaveShape`).
        wave_square: bool,
        #[serde(skip)]
        sample_rate: u32,
        /// Cycle length in frames (derived: `sample_rate / rate_hz`, ≥ 2).
        #[serde(skip)]
        period: u32,
        #[serde(skip)]
        counter: u32,
    },
}

/// Recompute an I3DL2 reverb's late-engine coefficients and derived
/// linear gains from its parameters.
#[allow(clippy::too_many_arguments)]
fn recompute_i3dl2(
    decay_time_ms: u16,
    decay_hf_milli: u16,
    reflections_milli_db: i16,
    reverb_milli_db: i16,
    room_milli_db: i16,
    combs_l: &mut [dsp::Comb],
    combs_r: &mut [dsp::Comb],
    early_gain_q15: &mut i32,
    reverb_gain_q15: &mut i32,
    room_gain_q15: &mut i32,
) {
    let fb = i3dl2_feedback_q15(decay_time_ms);
    let damp = i3dl2_damping_q15(decay_hf_milli);
    for c in combs_l.iter_mut().chain(combs_r.iter_mut()) {
        c.set_feedback(fb);
        c.set_damp(damp);
    }
    // milli-dB (≤ 0) → Q1.15 linear gain.
    let to_gain = |mdb: i16| -> i32 {
        (dsp::db_milli_to_lin_q16_16(mdb as i32) >> 1).clamp(0, Q15::ONE.raw() as i64) as i32
    };
    *early_gain_q15 = to_gain(reflections_milli_db);
    *reverb_gain_q15 = to_gain(reverb_milli_db);
    *room_gain_q15 = to_gain(room_milli_db);
}

/// Maximum modulated-delay time the buffers are sized for (covers chorus
/// base delay plus full sweep).
const MODDELAY_MAX_MS: u64 = 50;

// --- Freeverb tuning (canonical comb/all-pass lengths in samples,
// measured at 44100 Hz in `tuning.h`; scaled to the run rate) ----------
const FREEVERB_COMB_TUNING: [usize; 8] = [1116, 1188, 1277, 1356, 1422, 1491, 1557, 1617];
const FREEVERB_ALLPASS_TUNING: [usize; 4] = [556, 441, 341, 225];
const FREEVERB_STEREO_SPREAD: usize = 23;
/// The sample rate (Hz) at which the comb/all-pass lengths above were
/// tuned; lengths are rescaled from this to the actual output rate.
const FREEVERB_TUNING_SAMPLE_RATE_HZ: u64 = 44_100;

/// Freeverb input attenuation (`fixedgain = 0.015`), Q1.15.
const REVERB_FIXED_GAIN_Q15: i64 = dsp::q15_ratio(15, 1000) as i64;
/// Freeverb `scalewet` — the wet level can reach 3×.
const REVERB_SCALEWET: i64 = 3;
/// Freeverb `scaledry` — the dry level can reach 2×.
const REVERB_SCALEDRY: i64 = 2;
/// Comb feedback for a **frozen** tail: exact unit gain so the
/// recirculating signal neither grows nor decays (Freeverb `roomsize1 = 1`).
///
/// `q15_ratio(1, 1)` is `32768 = 2^15`, which is the *exact* unity for the
/// comb's `(filterstore * feedback) >> 15` recirculation (not `Q15::ONE =
/// 32767`, which would introduce a slow decay). It is deliberately above
/// `REVERB_FEEDBACK_MAX_Q15` (0.992): the normal-mode clamp keeps tails
/// stable, while freeze mode wants true 1.0. The damping stage (`damp2`)
/// already holds the closed-loop gain just under 1.0, so it never grows.
const REVERB_FREEZE_FEEDBACK: i32 = dsp::q15_ratio(1, 1);

/// Freeverb room-size mapping: `feedback = room·scaleroom + offsetroom`.
const FREEVERB_SCALEROOM_Q15: i64 = dsp::q15_ratio(28, 100) as i64; // 0.28
const FREEVERB_OFFSETROOM_Q15: i32 = dsp::q15_ratio(70, 100); // 0.70
/// WavesReverb mode reaches longer tails: feedback spans `[min, max]`.
const WAVESREVERB_FEEDBACK_MIN_Q15: i32 = dsp::q15_ratio(79, 100); // 0.79
const WAVESREVERB_FEEDBACK_MAX_Q15: i32 = dsp::q15_ratio(97, 100); // 0.97
/// Stability ceiling on any comb feedback (just under 1.0).
const REVERB_FEEDBACK_MAX_Q15: i32 = dsp::q15_ratio(992, 1000); // 0.992

/// Room-size (Q1.15) → comb feedback (Q1.15 raw) for a reverb mode.
/// Capped below 1.0 for stability.
fn reverb_feedback_q15(mode: ReverbMode, room_size: Q15) -> i32 {
    let room = room_size.raw().max(0) as i64;
    let fb = match mode {
        ReverbMode::Freeverb => {
            ((room * FREEVERB_SCALEROOM_Q15) >> 15) as i32 + FREEVERB_OFFSETROOM_Q15
        }
        ReverbMode::WavesReverb => {
            let span = (WAVESREVERB_FEEDBACK_MAX_Q15 - WAVESREVERB_FEEDBACK_MIN_Q15) as i64;
            ((room * span) >> 15) as i32 + WAVESREVERB_FEEDBACK_MIN_Q15
        }
    };
    fb.min(REVERB_FEEDBACK_MAX_Q15)
}

/// (Re)build a reverb's comb/all-pass banks for `rate_hz` (lengths scaled
/// from the 44.1 kHz tuning; the right channel is offset for stereo
/// decorrelation).
fn build_reverb_banks(
    rate_hz: u32,
    combs_l: &mut Vec<dsp::Comb>,
    combs_r: &mut Vec<dsp::Comb>,
    ap_l: &mut Vec<dsp::Allpass>,
    ap_r: &mut Vec<dsp::Allpass>,
) {
    let scale = |t: usize, off: usize| -> usize {
        (((t + off) as u64 * rate_hz as u64) / FREEVERB_TUNING_SAMPLE_RATE_HZ).max(1) as usize
    };
    combs_l.clear();
    combs_r.clear();
    ap_l.clear();
    ap_r.clear();
    for &t in &FREEVERB_COMB_TUNING {
        let mut c = dsp::Comb::new();
        c.prepare(scale(t, 0));
        combs_l.push(c);
        let mut c = dsp::Comb::new();
        c.prepare(scale(t, FREEVERB_STEREO_SPREAD));
        combs_r.push(c);
    }
    for &t in &FREEVERB_ALLPASS_TUNING {
        let mut a = dsp::Allpass::new();
        a.prepare(scale(t, 0));
        ap_l.push(a);
        let mut a = dsp::Allpass::new();
        a.prepare(scale(t, FREEVERB_STEREO_SPREAD));
        ap_r.push(a);
    }
}

/// Apply room-size / damping (or the frozen preset) to every comb in a
/// reverb's banks. When `freeze` is set, the combs run with unit feedback
/// and no damping (Freeverb's `freezemode`); the caller mutes the input.
fn recompute_reverb(
    mode: ReverbMode,
    room_size: Q15,
    damping: Q15,
    freeze: bool,
    combs_l: &mut [dsp::Comb],
    combs_r: &mut [dsp::Comb],
) {
    let (fb, damp) = if freeze {
        (REVERB_FREEZE_FEEDBACK, 0)
    } else {
        (
            reverb_feedback_q15(mode, room_size),
            damping.raw().max(0) as i32,
        )
    };
    for c in combs_l.iter_mut().chain(combs_r.iter_mut()) {
        c.set_feedback(fb);
        c.set_damp(damp);
    }
}

/// Early-reflection pattern for the I3DL2 approximation: `(delay ms, gain
/// Q1.15)` taps, a sparse decaying set evoking a room's first echoes.
const I3DL2_EARLY_TAPS: [(u32, i32); 6] = [
    (7, dsp::q15_ratio(79, 100)),  // 0.79
    (11, dsp::q15_ratio(58, 100)), // 0.58
    (17, dsp::q15_ratio(46, 100)), // 0.46
    (23, dsp::q15_ratio(34, 100)), // 0.34
    (29, dsp::q15_ratio(26, 100)), // 0.26
    (37, dsp::q15_ratio(20, 100)), // 0.20
];
/// Buffer length (ms) for the I3DL2 early-reflection / pre-delay line
/// (covers max ReflectionsDelay 300 ms + the tap spread).
const I3DL2_EARLY_MAX_MS: u64 = 360;
/// Right-channel early-tap offset (samples-equivalent ms) for stereo.
const I3DL2_EARLY_SPREAD_MS: u32 = 3;

/// I3DL2 `DecayTime` range (ms) and the comb-feedback range it maps to.
const I3DL2_DECAY_MS_MIN: i64 = 100;
const I3DL2_DECAY_MS_MAX: i64 = 20_000;
const I3DL2_FEEDBACK_MIN_Q15: i32 = dsp::q15_ratio(60, 100); // 0.60
const I3DL2_FEEDBACK_MAX_Q15: i32 = dsp::q15_ratio(966, 1000); // 0.966

/// Map a 100…20000 ms I3DL2 `DecayTime` onto a Freeverb comb feedback
/// (Q1.15), monotonically (longer decay ⇒ higher feedback).
fn i3dl2_feedback_q15(decay_time_ms: u16) -> i32 {
    let t = decay_time_ms.clamp(I3DL2_DECAY_MS_MIN as u16, I3DL2_DECAY_MS_MAX as u16) as i64;
    let span = (I3DL2_FEEDBACK_MAX_Q15 - I3DL2_FEEDBACK_MIN_Q15) as i64;
    let fb = I3DL2_FEEDBACK_MIN_Q15 as i64
        + ((t - I3DL2_DECAY_MS_MIN) * span) / (I3DL2_DECAY_MS_MAX - I3DL2_DECAY_MS_MIN);
    fb.clamp(0, REVERB_FEEDBACK_MAX_Q15 as i64) as i32
}

/// I3DL2 `DecayHFRatio` range (milli-ratio: 100 = 0.1, 2000 = 2.0).
const I3DL2_DECAY_HF_MILLI_MIN: i64 = 100;
const I3DL2_DECAY_HF_MILLI_MAX: i64 = 2_000;

/// Map an I3DL2 `DecayHFRatio` onto a damping coefficient (Q1.15): a high
/// ratio (≈ 2.0) ⇒ bright tail (no damping); a low ratio (≈ 0.1) ⇒ dark
/// tail (full damping).
fn i3dl2_damping_q15(decay_hf_milli: u16) -> i32 {
    let r = decay_hf_milli.clamp(
        I3DL2_DECAY_HF_MILLI_MIN as u16,
        I3DL2_DECAY_HF_MILLI_MAX as u16,
    ) as i64;
    let full_damp = Q15::ONE.raw() as i64;
    (((I3DL2_DECAY_HF_MILLI_MAX - r) * full_damp)
        / (I3DL2_DECAY_HF_MILLI_MAX - I3DL2_DECAY_HF_MILLI_MIN))
        .clamp(0, full_damp) as i32
}

/// Feedback parameter ceiling, just under unity (`0.99`), to keep delay
/// lines from self-oscillating. The signed `MIN` is for the flanger comb.
const FEEDBACK_MAX_Q15: Q15 = Q15::from_ratio(99, 100);
const FEEDBACK_MIN_Q15: Q15 = Q15::from_ratio(-99, 100);

/// Maximum echo/delay time the buffers are sized for (DMO range tops out
/// at 2000 ms).
const ECHO_MAX_DELAY_MS: u64 = 2_000;

/// Clamp a Q15.16 recirculating sample to ±(1<<30) — generous headroom
/// (±16384.0) that keeps the feedback sum inside `i32` without ever
/// clipping a musically-sane echo tail.
#[inline]
fn clamp_recirc(v: i64) -> i32 {
    v.clamp(-(1 << 30), 1 << 30) as i32
}

impl Device {
    /// Construct a peaking EQ (DMO-range params). Call
    /// [`Device::prepare`] before processing.
    pub fn param_eq(center_hz: u16, bandwidth_semitones: u8, gain_milli_db: i16) -> Self {
        Device::ParamEq {
            center_hz,
            bandwidth_semitones,
            gain_milli_db,
            rate_hz: 0,
            eq: Biquad::default(),
        }
    }

    /// Construct a distortion (DMO-range params). Call
    /// [`Device::prepare`] before processing.
    pub fn distortion(
        gain_milli_db: i32,
        edge: Q15,
        post_eq_center_hz: u16,
        post_eq_bandwidth_hz: u16,
        pre_lowpass_hz: u16,
    ) -> Self {
        Device::Distortion {
            gain_milli_db,
            edge,
            post_eq_center_hz,
            post_eq_bandwidth_hz,
            pre_lowpass_hz,
            rate_hz: 0,
            pre_lp: Biquad::default(),
            post_eq: Biquad::default(),
            out_gain_q15: 0,
        }
    }

    /// Construct a compressor (DMO-range params). `ratio_q8_8` is the
    /// ratio in Q8.8 (`256` = 1:1). Call [`Device::prepare`] before
    /// processing.
    pub fn compressor(
        threshold_milli_db: i32,
        ratio_q8_8: u16,
        attack_us: u32,
        release_us: u32,
        makeup_milli_db: i32,
    ) -> Self {
        Device::Compressor {
            threshold_milli_db,
            ratio_q8_8,
            attack_us,
            release_us,
            makeup_milli_db,
            rate_hz: 0,
            env: dsp::EnvelopeFollower::default(),
            thresh_oct_q16_16: 0,
            slope_q16_16: 0,
            makeup_oct_q16_16: 0,
        }
    }

    /// Construct an echo/delay (DMO-range params). Call
    /// [`Device::prepare`] before processing.
    pub fn echo(
        wet_dry: Q15,
        feedback: Q15,
        left_delay_ms: u16,
        right_delay_ms: u16,
        pan_delay: bool,
    ) -> Self {
        // Clamp to the buffer-backed maximum: `prepare` only allocates
        // `ECHO_MAX_DELAY_MS` of delay line, so a longer request would be
        // silently truncated at read time. In-range DMO params are
        // unaffected.
        let left_delay_ms = left_delay_ms.min(ECHO_MAX_DELAY_MS as u16);
        let right_delay_ms = right_delay_ms.min(ECHO_MAX_DELAY_MS as u16);
        Device::Echo {
            wet_dry,
            feedback,
            left_delay_ms,
            right_delay_ms,
            pan_delay,
            rate_hz: 0,
            dl_l: dsp::DelayLine::new(),
            dl_r: dsp::DelayLine::new(),
        }
    }

    /// Construct a chorus/flanger (DMO-range params). `base_delay_ms_q8_8`
    /// is the centre delay in Q8.8 ms. Call [`Device::prepare`] before
    /// processing.
    #[allow(clippy::too_many_arguments)]
    pub fn mod_delay(
        mode: ModDelayMode,
        wet_dry: Q15,
        depth: Q15,
        feedback: Q15,
        frequency_milli_hz: u32,
        waveform_sine: bool,
        base_delay_ms_q8_8: u16,
        quadrature: bool,
    ) -> Self {
        // Clamp the centre delay to the buffer-backed maximum (Q8.8 ms);
        // `prepare` allocates only `MODDELAY_MAX_MS`. In-range DMO params
        // are unaffected.
        let base_delay_ms_q8_8 = base_delay_ms_q8_8.min((MODDELAY_MAX_MS * 256) as u16);
        Device::ModDelay {
            mode,
            wet_dry,
            depth,
            feedback,
            frequency_milli_hz,
            waveform_sine,
            base_delay_ms_q8_8,
            quadrature,
            rate_hz: 0,
            dl_l: dsp::DelayLine::new(),
            dl_r: dsp::DelayLine::new(),
            lfo_phase: 0,
            lfo_inc: 0,
        }
    }

    /// Construct a reverb (not frozen). `wet`/`dry` are independent levels
    /// (Freeverb scaling: wet ×3, dry ×2). Call [`Device::prepare`] before
    /// processing.
    pub fn reverb(
        mode: ReverbMode,
        room_size: Q15,
        damping: Q15,
        width: Q15,
        wet: Q15,
        dry: Q15,
    ) -> Self {
        Device::Reverb {
            mode,
            room_size,
            damping,
            width,
            wet,
            dry,
            freeze: false,
            rate_hz: 0,
            combs_l: Vec::new(),
            combs_r: Vec::new(),
            ap_l: Vec::new(),
            ap_r: Vec::new(),
        }
    }

    /// Construct an I3DL2 environmental reverb. Call [`Device::prepare`]
    /// before processing.
    #[allow(clippy::too_many_arguments)]
    pub fn i3dl2_reverb(
        room_milli_db: i16,
        decay_time_ms: u16,
        decay_hf_milli: u16,
        reflections_milli_db: i16,
        reflections_delay_ms: u16,
        reverb_milli_db: i16,
        reverb_delay_ms: u16,
    ) -> Self {
        Device::I3DL2Reverb {
            room_milli_db,
            decay_time_ms,
            decay_hf_milli,
            reflections_milli_db,
            reflections_delay_ms,
            reverb_milli_db,
            reverb_delay_ms,
            rate_hz: 0,
            early_l: dsp::DelayLine::new(),
            early_r: dsp::DelayLine::new(),
            combs_l: Vec::new(),
            combs_r: Vec::new(),
            ap_l: Vec::new(),
            ap_r: Vec::new(),
            early_gain_q15: 0,
            reverb_gain_q15: 0,
            room_gain_q15: 0,
        }
    }

    /// Construct a gargle (amplitude-modulation) effect. `rate_hz` is the
    /// modulation rate (1–1000). Call [`Device::prepare`] before
    /// processing.
    pub fn gargle(rate_hz: u16, wave_square: bool) -> Self {
        Device::Gargle {
            rate_hz,
            wave_square,
            sample_rate: 0,
            period: 0,
            counter: 0,
        }
    }
}

impl Device {
    /// Process one stereo `(left, right)` sample, advancing any
    /// internal state.
    #[inline]
    pub fn process(&mut self, sample: (Amp, Amp)) -> (Amp, Amp) {
        match self {
            Device::Gain(g) => (g.apply(sample.0), g.apply(sample.1)),
            Device::LowPass { cutoff, state } => {
                // Q1.15 one-pole: y += (x − y)·a >> 15. State kept in
                // i32 for headroom; output clamps back to Q1.15 `Amp`.
                let a = cutoff.raw().max(0) as i32;
                let step = |y: &mut i32, x: Amp| -> Amp {
                    let xr = x.as_q15_i16() as i32;
                    *y += ((xr - *y) * a) >> 15;
                    Amp::from_q15_i16((*y).clamp(i16::MIN as i32, i16::MAX as i32) as i16)
                };
                (step(&mut state.0, sample.0), step(&mut state.1, sample.1))
            }
            // Un-prepared (`rate_hz == 0`) the biquad is the identity, so
            // this is a pass-through until `prepare` derives coefficients.
            Device::ParamEq { eq, .. } => eq.process(sample.0, sample.1),
            Device::Distortion {
                rate_hz,
                edge,
                pre_lp,
                post_eq,
                out_gain_q15,
                ..
            } => {
                if *rate_hz == 0 {
                    return sample; // un-prepared ⇒ safe pass-through
                }
                // pre-LP → soft-clip → post-EQ → output gain.
                let (l, r) = pre_lp.process(sample.0, sample.1);
                // Drive 1×…32× across edge 0…100 %.
                let drive_q8_8 = 256 + ((edge.raw().max(0) as i32 * (31 * 256)) >> 15);
                let l = dsp::soft_clip_q15(l.as_q15_i16() as i32, drive_q8_8);
                let r = dsp::soft_clip_q15(r.as_q15_i16() as i32, drive_q8_8);
                let (l, r) = post_eq.process(Amp::from_q15_i32_sat(l), Amp::from_q15_i32_sat(r));
                let g = *out_gain_q15;
                (
                    Amp::from_q15_i32_sat((l.as_q15_i16() as i32 * g) >> 15),
                    Amp::from_q15_i32_sat((r.as_q15_i16() as i32 * g) >> 15),
                )
            }
            Device::Compressor {
                rate_hz,
                env,
                thresh_oct_q16_16,
                slope_q16_16,
                makeup_oct_q16_16,
                ..
            } => {
                if *rate_hz == 0 {
                    return sample; // un-prepared ⇒ safe pass-through
                }
                let l = sample.0.as_q15_i16() as i32;
                let r = sample.1.as_q15_i16() as i32;
                // Stereo-linked peak detector.
                let target = l.abs().max(r.abs());
                let e = env.process(target);
                // Log-domain gain (octaves): compress above threshold,
                // then apply make-up. Below threshold ⇒ make-up only.
                let gain_oct = if e > 0 {
                    let level_oct = dsp::log2_q16_16((e as i64) << 1); // Q15→Q16.16
                    let over = level_oct - *thresh_oct_q16_16;
                    if over > 0 {
                        (((over as i64) * (*slope_q16_16 as i64)) >> 16) as i32 + *makeup_oct_q16_16
                    } else {
                        *makeup_oct_q16_16
                    }
                } else {
                    *makeup_oct_q16_16
                };
                let g = dsp::pow2_q16_16(gain_oct); // Q16.16
                let app = |x: i32| -> Amp { Amp::from_q15_i32_sat(((x as i64 * g) >> 16) as i32) };
                (app(l), app(r))
            }
            Device::Echo {
                wet_dry,
                feedback,
                left_delay_ms,
                right_delay_ms,
                pan_delay,
                rate_hz,
                dl_l,
                dl_r,
            } => {
                if *rate_hz == 0 {
                    return sample; // un-prepared ⇒ safe pass-through
                }
                let to_samples =
                    |ms: u16| -> usize { ((ms as u64 * *rate_hz as u64) / 1000).max(1) as usize };
                let ld = to_samples(*left_delay_ms);
                let rd = to_samples(*right_delay_ms);
                // Delayed (recirculated) signal, Q15.16.
                let echo_l = dl_l.tap(ld) as i64;
                let echo_r = dl_r.tap(rd) as i64;

                let dry_l = sample.0.as_q15_i16() as i64;
                let dry_r = sample.1.as_q15_i16() as i64;
                let fb = feedback.raw().max(0) as i64; // 0..32767

                // Write input + feedback·(previous echo). PanDelay feeds
                // each line from the *other* channel's tail so successive
                // echoes bounce L↔R.
                let (src_l, src_r) = if *pan_delay {
                    (echo_r, echo_l)
                } else {
                    (echo_l, echo_r)
                };
                dl_l.write(clamp_recirc((dry_l << 16) + ((src_l * fb) >> 15)));
                dl_r.write(clamp_recirc((dry_r << 16) + ((src_r * fb) >> 15)));

                // Wet/dry mix (wet tap back to Q1.15 via >> 16).
                let wet = wet_dry.raw().max(0) as i64; // 0..32767
                let dry = Q15::ONE.raw() as i64 - wet;
                let out_l = ((dry_l * dry) + ((echo_l >> 16) * wet)) >> 15;
                let out_r = ((dry_r * dry) + ((echo_r >> 16) * wet)) >> 15;
                (
                    Amp::from_q15_i32_sat(out_l as i32),
                    Amp::from_q15_i32_sat(out_r as i32),
                )
            }
            Device::ModDelay {
                wet_dry,
                depth,
                feedback,
                waveform_sine,
                base_delay_ms_q8_8,
                quadrature,
                rate_hz,
                dl_l,
                dl_r,
                lfo_phase,
                lfo_inc,
                ..
            } => {
                if *rate_hz == 0 {
                    return sample; // un-prepared ⇒ safe pass-through
                }
                // Centre delay (Q16.16 samples), at least 2 samples so the
                // fractional tap has neighbours and the sweep stays > 0.
                let base_q16 = (((*base_delay_ms_q8_8 as u64 * *rate_hz as u64 * 256) / 1000)
                    as i64)
                    .max(2 << 16);
                // Sweep ≤ 90 % of the base so modulated delay never ≤ 0.
                let mod_q16 = (base_q16 * depth.raw().max(0) as i64 * 9) / (32_768 * 10);
                let max_delay = (dl_l.len().saturating_sub(1)) as i64;

                let delay_for = |lfo_q15: i32| -> u32 {
                    let d = base_q16 + ((mod_q16 * lfo_q15 as i64) >> 15);
                    d.clamp(1 << 16, max_delay << 16) as u32
                };
                let lfo_l = dsp::lfo_value_q15(*lfo_phase, *waveform_sine);
                let lfo_r = dsp::lfo_value_q15(
                    lfo_phase.wrapping_add(if *quadrature { 1 << 30 } else { 0 }),
                    *waveform_sine,
                );

                let wet_l = dl_l.tap_frac(delay_for(lfo_l)) as i64; // Q15.16
                let wet_r = dl_r.tap_frac(delay_for(lfo_r)) as i64;

                let dry_l = sample.0.as_q15_i16() as i64;
                let dry_r = sample.1.as_q15_i16() as i64;
                let fb = feedback.raw() as i64; // signed

                dl_l.write(clamp_recirc((dry_l << 16) + ((wet_l * fb) >> 15)));
                dl_r.write(clamp_recirc((dry_r << 16) + ((wet_r * fb) >> 15)));

                *lfo_phase = lfo_phase.wrapping_add(*lfo_inc);

                let wet = wet_dry.raw().max(0) as i64;
                let dry = Q15::ONE.raw() as i64 - wet;
                let out_l = ((dry_l * dry) + ((wet_l >> 16) * wet)) >> 15;
                let out_r = ((dry_r * dry) + ((wet_r >> 16) * wet)) >> 15;
                (
                    Amp::from_q15_i32_sat(out_l as i32),
                    Amp::from_q15_i32_sat(out_r as i32),
                )
            }
            Device::Reverb {
                width,
                wet,
                dry,
                freeze,
                rate_hz,
                combs_l,
                combs_r,
                ap_l,
                ap_r,
                ..
            } => {
                if *rate_hz == 0 || combs_l.is_empty() {
                    return sample; // un-prepared ⇒ safe pass-through
                }
                let dry_l = sample.0.as_q15_i16() as i64;
                let dry_r = sample.1.as_q15_i16() as i64;
                // Freeze mutes the input: the frozen tail recirculates with
                // unit feedback on its own (Freeverb `gain = muted`).
                let input = if *freeze {
                    0
                } else {
                    ((((dry_l + dry_r) * REVERB_FIXED_GAIN_Q15) >> 15) << 16) as i32
                };

                // Parallel combs, summed per channel.
                let mut acc_l = 0i64;
                for c in combs_l.iter_mut() {
                    acc_l += c.process(input) as i64;
                }
                let mut acc_r = 0i64;
                for c in combs_r.iter_mut() {
                    acc_r += c.process(input) as i64;
                }
                // Series all-passes.
                let mut sl = acc_l.clamp(-(1 << 30), 1 << 30) as i32;
                for a in ap_l.iter_mut() {
                    sl = a.process(sl);
                }
                let mut sr = acc_r.clamp(-(1 << 30), 1 << 30) as i32;
                for a in ap_r.iter_mut() {
                    sr = a.process(sr);
                }

                // Independent wet (×scalewet) and dry (×scaledry) levels,
                // with the Freeverb stereo cross-mix on the wet path:
                //   out = revL·wet1 + revR·wet2 + dryL·dry  (and mirror).
                let wet_g = (wet.raw().max(0) as i64) * REVERB_SCALEWET;
                let dry_g = (dry.raw().max(0) as i64) * REVERB_SCALEDRY;
                let w = width.raw().max(0) as i64;
                let half = Q15::HALF.raw() as i64; // 0.5
                let wet1 = (wet_g * ((w >> 1) + half)) >> 15; // wet·(width/2 + 0.5)
                let wet2 = (wet_g * (half - (w >> 1))) >> 15; // wet·((1 − width)/2)
                let sl15 = sl as i64 >> 16; // reverb output, Q1.15
                let sr15 = sr as i64 >> 16;
                let out_l = ((sl15 * wet1 + sr15 * wet2) >> 15) + ((dry_l * dry_g) >> 15);
                let out_r = ((sr15 * wet1 + sl15 * wet2) >> 15) + ((dry_r * dry_g) >> 15);
                (
                    Amp::from_q15_i32_sat(out_l as i32),
                    Amp::from_q15_i32_sat(out_r as i32),
                )
            }
            Device::I3DL2Reverb {
                reflections_delay_ms,
                reverb_delay_ms,
                rate_hz,
                early_l,
                early_r,
                combs_l,
                combs_r,
                ap_l,
                ap_r,
                early_gain_q15,
                reverb_gain_q15,
                room_gain_q15,
                ..
            } => {
                if *rate_hz == 0 || combs_l.is_empty() {
                    return sample; // un-prepared ⇒ safe pass-through
                }
                let dry_l = sample.0.as_q15_i16() as i64;
                let dry_r = sample.1.as_q15_i16() as i64;
                let mono = ((dry_l + dry_r) >> 1) as i32; // Q15
                early_l.write(mono << 16);
                early_r.write(mono << 16);

                let ms =
                    |m: u32| -> usize { ((m as u64 * *rate_hz as u64) / 1000).max(1) as usize };
                let refl_base = ms(*reflections_delay_ms as u32);
                let spread = ms(I3DL2_EARLY_SPREAD_MS);

                // Early reflections (Q15.16).
                let mut er_l = 0i64;
                let mut er_r = 0i64;
                for (tap_ms, gain) in I3DL2_EARLY_TAPS {
                    let d = refl_base + ms(tap_ms);
                    er_l += (early_l.tap(d) as i64 * gain as i64) >> 15;
                    er_r += (early_r.tap(d + spread) as i64 * gain as i64) >> 15;
                }
                let er_l = (er_l * *early_gain_q15 as i64) >> 15;
                let er_r = (er_r * *early_gain_q15 as i64) >> 15;

                // Late reverb fed by the pre-delayed, attenuated mono.
                let late_raw = early_l.tap(ms(*reverb_delay_ms as u32)) >> 16; // Q15 mono
                let late_in = (((late_raw as i64 * REVERB_FIXED_GAIN_Q15) >> 15) << 16)
                    .clamp(-(1 << 30), 1 << 30) as i32;
                let mut acc_l = 0i64;
                for c in combs_l.iter_mut() {
                    acc_l += c.process(late_in) as i64;
                }
                let mut acc_r = 0i64;
                for c in combs_r.iter_mut() {
                    acc_r += c.process(late_in) as i64;
                }
                let mut sl = acc_l.clamp(-(1 << 30), 1 << 30) as i32;
                for a in ap_l.iter_mut() {
                    sl = a.process(sl);
                }
                let mut sr = acc_r.clamp(-(1 << 30), 1 << 30) as i32;
                for a in ap_r.iter_mut() {
                    sr = a.process(sr);
                }
                let late_l = (sl as i64 * *reverb_gain_q15 as i64) >> 15;
                let late_r = (sr as i64 * *reverb_gain_q15 as i64) >> 15;

                // Early + late, scaled by overall Room gain, added to dry.
                let wet_l = (((er_l + late_l) * *room_gain_q15 as i64) >> 15) >> 16;
                let wet_r = (((er_r + late_r) * *room_gain_q15 as i64) >> 15) >> 16;
                (
                    Amp::from_q15_i32_sat((dry_l + wet_l) as i32),
                    Amp::from_q15_i32_sat((dry_r + wet_r) as i32),
                )
            }
            Device::Gargle {
                wave_square,
                period,
                counter,
                ..
            } => {
                if *period < 2 {
                    return sample; // un-prepared ⇒ safe pass-through
                }
                let period_half = (*period / 2).max(1);
                // Unipolar [0, 1] gain: triangle ramps 0→1→0 over the
                // cycle; square is full for the first half, silent for the
                // second (mirrors OpenMPT `Gargle::Process`).
                let unity = Q15::ONE.raw() as u32;
                let gain_q15: i32 = if *counter < period_half {
                    if *wave_square {
                        unity as i32
                    } else {
                        ((*counter * unity) / period_half) as i32
                    }
                } else if *wave_square {
                    0
                } else {
                    (((*period - *counter) * unity) / period_half) as i32
                };
                *counter += 1;
                if *counter >= *period {
                    *counter = 0;
                }
                let g = gain_q15.clamp(0, Q15::ONE.raw() as i32);
                (
                    Amp::from_q15_i32_sat((sample.0.as_q15_i16() as i32 * g) >> 15),
                    Amp::from_q15_i32_sat((sample.1.as_q15_i16() as i32 * g) >> 15),
                )
            }
        }
    }

    /// RFC §3A — set a device parameter from a normalized
    /// ([`AutomationValue::Normalized`]) automation value. The `param`
    /// id is device-specific; unknown ids are ignored.
    ///
    /// * [`Device::Gain`] — `param 0` = gain (the Q1.15 value mapped to
    ///   a `[0, 1]` [`Volume`]; negative values clamp to silence).
    /// * [`Device::ParamEq`] — `0` = centre (80–16000 Hz), `1` =
    ///   bandwidth (1–36 semitones), `2` = gain (−15…+15 dB).
    /// * [`Device::Distortion`] — `0` = output gain (−60…0 dB), `1` =
    ///   edge (0–100 %), `2` = post-EQ centre, `3` = post-EQ bandwidth,
    ///   `4` = pre-low-pass cutoff (all Hz, 100–8000).
    /// * [`Device::Compressor`] — `0` = threshold (−60…0 dB), `1` =
    ///   ratio (1…100), `2` = attack (0.01…500 ms), `3` = release
    ///   (50…3000 ms), `4` = make-up gain (−60…+60 dB).
    /// * [`Device::Echo`] — `0` = wet/dry, `1` = feedback, `2` = left
    ///   delay (1…2000 ms), `3` = right delay (1…2000 ms), `4` = pan-delay
    ///   toggle.
    /// * [`Device::ModDelay`] — `0` = wet/dry, `1` = depth, `2` = feedback
    ///   (signed), `3` = LFO rate (0…10 Hz), `4` = base delay (0…20 ms).
    /// * [`Device::Reverb`] — `0` = room size, `1` = damping, `2` = width,
    ///   `3` = wet level, `4` = dry level, `5` = freeze (≥ 0.5 = on).
    /// * [`Device::I3DL2Reverb`] — `0` = room, `1` = decay time, `2` =
    ///   decay-HF ratio, `3` = reflections level, `4` = reflections delay,
    ///   `5` = reverb level, `6` = reverb delay.
    /// * [`Device::Gargle`] — `0` = rate (1–1000 Hz), `1` = waveform
    ///   (triangle / square).
    ///
    /// The normalized value is mapped linearly onto each range; the
    /// device recomputes its coefficients in place.
    ///
    /// [`AutomationValue::Normalized`]: crate::core::daw::automation::AutomationValue::Normalized
    #[inline]
    pub fn set_param(&mut self, param: u16, value: Q15) {
        match self {
            Device::Gain(g) => {
                if param == 0 {
                    *g = Volume::from_q15(value.max(Q15::ZERO));
                }
            }
            Device::LowPass { cutoff, .. } => {
                if param == 0 {
                    *cutoff = value.max(Q15::ZERO);
                }
            }
            Device::ParamEq {
                center_hz,
                bandwidth_semitones,
                gain_milli_db,
                rate_hz,
                eq,
            } => {
                match param {
                    0 => *center_hz = lerp_param(value, 80, 16_000) as u16,
                    1 => *bandwidth_semitones = lerp_param(value, 1, 36) as u8,
                    2 => *gain_milli_db = lerp_param(value, -15_000, 15_000) as i16,
                    _ => return,
                }
                recompute_param_eq(
                    eq,
                    *rate_hz,
                    *center_hz,
                    *bandwidth_semitones,
                    *gain_milli_db,
                );
            }
            Device::Distortion {
                gain_milli_db,
                edge,
                post_eq_center_hz,
                post_eq_bandwidth_hz,
                pre_lowpass_hz,
                rate_hz,
                pre_lp,
                post_eq,
                out_gain_q15,
            } => {
                match param {
                    0 => *gain_milli_db = lerp_param(value, -60_000, 0),
                    1 => *edge = value.max(Q15::ZERO),
                    2 => *post_eq_center_hz = lerp_param(value, 100, 8_000) as u16,
                    3 => *post_eq_bandwidth_hz = lerp_param(value, 100, 8_000) as u16,
                    4 => *pre_lowpass_hz = lerp_param(value, 100, 8_000) as u16,
                    _ => return,
                }
                recompute_distortion(
                    pre_lp,
                    post_eq,
                    out_gain_q15,
                    *rate_hz,
                    *gain_milli_db,
                    *post_eq_center_hz,
                    *post_eq_bandwidth_hz,
                    *pre_lowpass_hz,
                );
            }
            Device::Compressor {
                threshold_milli_db,
                ratio_q8_8,
                attack_us,
                release_us,
                makeup_milli_db,
                rate_hz,
                env,
                thresh_oct_q16_16,
                slope_q16_16,
                makeup_oct_q16_16,
            } => {
                match param {
                    0 => *threshold_milli_db = lerp_param(value, -60_000, 0),
                    1 => *ratio_q8_8 = lerp_param(value, 256, 25_600) as u16,
                    2 => *attack_us = lerp_param(value, 10, 500_000) as u32,
                    3 => *release_us = lerp_param(value, 50_000, 3_000_000) as u32,
                    4 => *makeup_milli_db = lerp_param(value, -60_000, 60_000),
                    _ => return,
                }
                recompute_compressor(
                    env,
                    thresh_oct_q16_16,
                    slope_q16_16,
                    makeup_oct_q16_16,
                    *rate_hz,
                    *threshold_milli_db,
                    *ratio_q8_8,
                    *attack_us,
                    *release_us,
                    *makeup_milli_db,
                );
            }
            Device::Echo {
                wet_dry,
                feedback,
                left_delay_ms,
                right_delay_ms,
                pan_delay,
                ..
            } => match param {
                0 => *wet_dry = value.max(Q15::ZERO),
                1 => *feedback = value.clamp(Q15::ZERO, FEEDBACK_MAX_Q15),
                2 => *left_delay_ms = lerp_param(value, 1, 2_000) as u16,
                3 => *right_delay_ms = lerp_param(value, 1, 2_000) as u16,
                4 => *pan_delay = value.raw() > 0,
                _ => {}
            },
            Device::ModDelay {
                wet_dry,
                depth,
                feedback,
                frequency_milli_hz,
                base_delay_ms_q8_8,
                rate_hz,
                lfo_inc,
                ..
            } => {
                match param {
                    0 => *wet_dry = value.max(Q15::ZERO),
                    1 => *depth = value.max(Q15::ZERO),
                    // Feedback is signed (centre = no feedback).
                    2 => *feedback = value.clamp(FEEDBACK_MIN_Q15, FEEDBACK_MAX_Q15),
                    3 => {
                        *frequency_milli_hz = lerp_param(value, 0, 10_000) as u32;
                        *lfo_inc = dsp::lfo_increment(*frequency_milli_hz, *rate_hz);
                    }
                    4 => *base_delay_ms_q8_8 = lerp_param(value, 0, 20 * 256) as u16,
                    _ => {}
                }
            }
            Device::Reverb {
                mode,
                room_size,
                damping,
                width,
                wet,
                dry,
                freeze,
                combs_l,
                combs_r,
                ..
            } => {
                match param {
                    0 => *room_size = value.max(Q15::ZERO),
                    1 => *damping = value.max(Q15::ZERO),
                    2 => *width = value.max(Q15::ZERO),
                    3 => *wet = value.max(Q15::ZERO),
                    4 => *dry = value.max(Q15::ZERO),
                    5 => *freeze = value.raw() >= Q15::HALF.raw(),
                    _ => return,
                }
                recompute_reverb(*mode, *room_size, *damping, *freeze, combs_l, combs_r);
            }
            Device::I3DL2Reverb {
                room_milli_db,
                decay_time_ms,
                decay_hf_milli,
                reflections_milli_db,
                reflections_delay_ms,
                reverb_milli_db,
                reverb_delay_ms,
                combs_l,
                combs_r,
                early_gain_q15,
                reverb_gain_q15,
                room_gain_q15,
                ..
            } => {
                match param {
                    0 => *room_milli_db = lerp_param(value, -10_000, 0) as i16,
                    1 => *decay_time_ms = lerp_param(value, 100, 20_000) as u16,
                    2 => *decay_hf_milli = lerp_param(value, 100, 2_000) as u16,
                    3 => *reflections_milli_db = lerp_param(value, -10_000, 1_000) as i16,
                    4 => *reflections_delay_ms = lerp_param(value, 0, 300) as u16,
                    5 => *reverb_milli_db = lerp_param(value, -10_000, 2_000) as i16,
                    6 => *reverb_delay_ms = lerp_param(value, 0, 100) as u16,
                    _ => return,
                }
                recompute_i3dl2(
                    *decay_time_ms,
                    *decay_hf_milli,
                    *reflections_milli_db,
                    *reverb_milli_db,
                    *room_milli_db,
                    combs_l,
                    combs_r,
                    early_gain_q15,
                    reverb_gain_q15,
                    room_gain_q15,
                );
            }
            Device::Gargle {
                rate_hz,
                wave_square,
                sample_rate,
                period,
                ..
            } => {
                match param {
                    0 => *rate_hz = lerp_param(value, 1, 1_000) as u16,
                    1 => *wave_square = value.raw() > Q15::HALF.raw(),
                    _ => return,
                }
                if *sample_rate > 0 {
                    *period = (*sample_rate / (*rate_hz).max(1) as u32).max(2);
                }
            }
        }
    }

    /// Prepare the device for playback at `sample_rate` (Hz): size any
    /// time-based buffers (delay lines, reverb combs) and precompute
    /// rate-dependent coefficients (filter cutoffs, attack/release time
    /// constants, LFO increments).
    ///
    /// **Invariant.** Transient runtime state is `#[serde(skip)]`'d and
    /// therefore empty after deserialisation, so the runtime **must**
    /// call this (via [`DeviceChain::prepare`]) before the first
    /// [`Device::process`]. An unprepared device must still be a safe
    /// pass-through, never a panic. Re-call on a sample-rate change.
    /// Stateless devices ([`Device::Gain`]) ignore it.
    pub fn prepare(&mut self, sample_rate: u32) {
        let _ = sample_rate;
        match self {
            Device::Gain(_) => {}
            // One-pole `cutoff` is already a per-sample coefficient, not
            // a frequency, so nothing to precompute yet.
            Device::LowPass { .. } => {}
            Device::ParamEq {
                center_hz,
                bandwidth_semitones,
                gain_milli_db,
                rate_hz,
                eq,
            } => {
                *rate_hz = sample_rate;
                recompute_param_eq(
                    eq,
                    sample_rate,
                    *center_hz,
                    *bandwidth_semitones,
                    *gain_milli_db,
                );
            }
            Device::Distortion {
                gain_milli_db,
                post_eq_center_hz,
                post_eq_bandwidth_hz,
                pre_lowpass_hz,
                rate_hz,
                pre_lp,
                post_eq,
                out_gain_q15,
                ..
            } => {
                *rate_hz = sample_rate;
                recompute_distortion(
                    pre_lp,
                    post_eq,
                    out_gain_q15,
                    sample_rate,
                    *gain_milli_db,
                    *post_eq_center_hz,
                    *post_eq_bandwidth_hz,
                    *pre_lowpass_hz,
                );
            }
            Device::Compressor {
                threshold_milli_db,
                ratio_q8_8,
                attack_us,
                release_us,
                makeup_milli_db,
                rate_hz,
                env,
                thresh_oct_q16_16,
                slope_q16_16,
                makeup_oct_q16_16,
            } => {
                *rate_hz = sample_rate;
                recompute_compressor(
                    env,
                    thresh_oct_q16_16,
                    slope_q16_16,
                    makeup_oct_q16_16,
                    sample_rate,
                    *threshold_milli_db,
                    *ratio_q8_8,
                    *attack_us,
                    *release_us,
                    *makeup_milli_db,
                );
            }
            Device::Echo {
                rate_hz,
                dl_l,
                dl_r,
                ..
            } => {
                *rate_hz = sample_rate;
                let max_len = ((ECHO_MAX_DELAY_MS * sample_rate as u64) / 1000 + 1) as usize;
                dl_l.prepare(max_len);
                dl_r.prepare(max_len);
            }
            Device::ModDelay {
                frequency_milli_hz,
                rate_hz,
                dl_l,
                dl_r,
                lfo_inc,
                ..
            } => {
                *rate_hz = sample_rate;
                let max_len = ((MODDELAY_MAX_MS * sample_rate as u64) / 1000 + 1) as usize;
                dl_l.prepare(max_len);
                dl_r.prepare(max_len);
                *lfo_inc = dsp::lfo_increment(*frequency_milli_hz, sample_rate);
            }
            Device::Reverb {
                mode,
                room_size,
                damping,
                freeze,
                rate_hz,
                combs_l,
                combs_r,
                ap_l,
                ap_r,
                ..
            } => {
                *rate_hz = sample_rate;
                build_reverb_banks(sample_rate, combs_l, combs_r, ap_l, ap_r);
                recompute_reverb(*mode, *room_size, *damping, *freeze, combs_l, combs_r);
            }
            Device::I3DL2Reverb {
                room_milli_db,
                decay_time_ms,
                decay_hf_milli,
                reflections_milli_db,
                reverb_milli_db,
                rate_hz,
                early_l,
                early_r,
                combs_l,
                combs_r,
                ap_l,
                ap_r,
                early_gain_q15,
                reverb_gain_q15,
                room_gain_q15,
                ..
            } => {
                *rate_hz = sample_rate;
                let early_len = ((I3DL2_EARLY_MAX_MS * sample_rate as u64) / 1000 + 1) as usize;
                early_l.prepare(early_len);
                early_r.prepare(early_len);
                build_reverb_banks(sample_rate, combs_l, combs_r, ap_l, ap_r);
                recompute_i3dl2(
                    *decay_time_ms,
                    *decay_hf_milli,
                    *reflections_milli_db,
                    *reverb_milli_db,
                    *room_milli_db,
                    combs_l,
                    combs_r,
                    early_gain_q15,
                    reverb_gain_q15,
                    room_gain_q15,
                );
            }
            Device::Gargle {
                rate_hz,
                sample_rate: sr,
                period,
                counter,
                ..
            } => {
                *sr = sample_rate;
                *period = (sample_rate / (*rate_hz).max(1) as u32).max(2);
                *counter = 0;
            }
        }
    }

    /// Clear transient signal state (filter history, delay tails,
    /// envelope followers) **without** changing parameters or buffer
    /// sizes. Called on seek so a jump stays deterministic — mirrors the
    /// player's cursor-reset discipline (`reset_for_goto`). Stateless
    /// devices ignore it.
    pub fn reset(&mut self) {
        match self {
            Device::Gain(_) => {}
            Device::LowPass { state, .. } => *state = (0, 0),
            Device::ParamEq { eq, .. } => eq.reset_history(),
            Device::Distortion {
                pre_lp, post_eq, ..
            } => {
                pre_lp.reset_history();
                post_eq.reset_history();
            }
            Device::Compressor { env, .. } => env.reset(),
            Device::Echo { dl_l, dl_r, .. } => {
                dl_l.reset();
                dl_r.reset();
            }
            Device::ModDelay {
                dl_l,
                dl_r,
                lfo_phase,
                ..
            } => {
                dl_l.reset();
                dl_r.reset();
                *lfo_phase = 0;
            }
            Device::Reverb {
                combs_l,
                combs_r,
                ap_l,
                ap_r,
                ..
            } => {
                for c in combs_l.iter_mut().chain(combs_r.iter_mut()) {
                    c.reset();
                }
                for a in ap_l.iter_mut().chain(ap_r.iter_mut()) {
                    a.reset();
                }
            }
            Device::I3DL2Reverb {
                early_l,
                early_r,
                combs_l,
                combs_r,
                ap_l,
                ap_r,
                ..
            } => {
                early_l.reset();
                early_r.reset();
                for c in combs_l.iter_mut().chain(combs_r.iter_mut()) {
                    c.reset();
                }
                for a in ap_l.iter_mut().chain(ap_r.iter_mut()) {
                    a.reset();
                }
            }
            Device::Gargle { counter, .. } => *counter = 0,
        }
    }
}

/// An ordered insert chain on one mixer channel. Empty (the default,
/// and the only state any tracker import produces) ⇒ pass-through.
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct DeviceChain {
    /// Inserts, processed front-to-back.
    pub devices: Vec<Device>,
    /// Master bypass for the whole chain (editor A/B compare).
    pub bypass: bool,
}

impl DeviceChain {
    /// `true` when the chain would actually change the signal — lets
    /// the mixer skip the per-sample call entirely on the (universal)
    /// empty / bypassed case.
    #[inline]
    pub fn is_active(&self) -> bool {
        !self.bypass && !self.devices.is_empty()
    }

    /// Run a stereo sample through every insert in order, advancing
    /// each device's state.
    #[inline]
    pub fn process(&mut self, mut sample: (Amp, Amp)) -> (Amp, Amp) {
        if self.bypass {
            return sample;
        }
        for d in &mut self.devices {
            sample = d.process(sample);
        }
        sample
    }

    /// Prepare every device for `sample_rate` (Hz). The runtime calls
    /// this when it materialises the chain (and on a rate change), before
    /// any [`DeviceChain::process`]. Empty for every tracker import.
    #[inline]
    pub fn prepare(&mut self, sample_rate: u32) {
        for d in &mut self.devices {
            d.prepare(sample_rate);
        }
    }

    /// Clear every device's transient state on seek (parameters kept).
    #[inline]
    pub fn reset(&mut self) {
        for d in &mut self.devices {
            d.reset();
        }
    }
}

/// RFC §3B — a **return/aux bus**: a named mix destination with its
/// own insert chain. Channels feed it via [`Send`]s; its processed
/// output is summed back into the master mix. Empty `buses` (every
/// tracker import) ⇒ no extra routing.
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct Bus {
    pub name: alloc::string::String,
    pub chain: DeviceChain,
}

/// RFC §3B — one channel→bus send: a copy of the channel's
/// post-insert signal, scaled by `level`, added to bus `bus`'s input.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub struct Send {
    /// Destination index into [`crate::core::module::Module::buses`].
    pub bus: u16,
    /// Send amount, Q1.15 `[0, 1]`.
    pub level: Volume,
}

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

    #[test]
    fn empty_chain_is_passthrough() {
        let mut c = DeviceChain::default();
        assert!(!c.is_active());
        let s = (Amp::from_q15_i16(10_000), Amp::from_q15_i16(-8_000));
        assert_eq!(c.process(s), s);
    }

    #[test]
    fn bypass_is_passthrough() {
        let mut c = DeviceChain {
            devices: alloc::vec![Device::Gain(Volume::SILENT)],
            bypass: true,
        };
        assert!(!c.is_active());
        let s = (Amp::from_q15_i16(10_000), Amp::from_q15_i16(-8_000));
        assert_eq!(c.process(s), s);
    }

    #[test]
    fn gain_scales_both_channels() {
        let half = Volume::from_q15(Q15::from_ratio(1, 2)); // ~0.5
        let mut c = DeviceChain {
            devices: alloc::vec![Device::Gain(half)],
            bypass: false,
        };
        assert!(c.is_active());
        let s = (Amp::from_q15_i16(20_000), Amp::from_q15_i16(-20_000));
        let (l, r) = c.process(s);
        // ~half amplitude, both channels.
        assert!(l.as_q15_i16() > 9_000 && l.as_q15_i16() < 11_000);
        assert!(r.as_q15_i16() < -9_000 && r.as_q15_i16() > -11_000);
    }

    #[test]
    fn lowpass_unity_cutoff_is_passthrough() {
        // a = 1.0 ⇒ y[n] = x[n] (the previous-output term cancels).
        let mut d = Device::LowPass {
            cutoff: Q15::ONE,
            state: (0, 0),
        };
        for raw in [12_000_i16, -9_000, 4_000, 0, 30_000] {
            let s = (Amp::from_q15_i16(raw), Amp::from_q15_i16(raw));
            let (l, _) = d.process(s);
            // Within one LSB of the input (a = 32767/32768, not exactly 1).
            assert!((l.as_q15_i16() as i32 - raw as i32).abs() <= 1);
        }
    }

    #[test]
    fn prepared_empty_chain_is_passthrough() {
        // The runtime always `prepare`s a chain before processing;
        // preparing an empty (every tracker import) or unity-gain chain
        // must leave it a bit-exact pass-through.
        let mut empty = DeviceChain::default();
        empty.prepare(48_000);
        let s = (Amp::from_q15_i16(7_777), Amp::from_q15_i16(-5_555));
        assert_eq!(empty.process(s), s);

        let mut unity = DeviceChain {
            devices: alloc::vec![Device::Gain(Volume::FULL)],
            bypass: false,
        };
        unity.prepare(48_000);
        assert_eq!(unity.process(s), s);
    }

    #[test]
    fn reset_clears_stateful_history() {
        // After driving a low-cutoff filter to a non-zero state, `reset`
        // must restore it so the next input sees the same response as a
        // fresh device — deterministic seeks depend on this.
        let fresh = || Device::LowPass {
            cutoff: Q15::from_ratio(12, 100),
            state: (0, 0),
        };
        let step = (Amp::from_q15_i16(20_000), Amp::from_q15_i16(20_000));

        let mut a = fresh();
        a.prepare(48_000);
        let first = a.process(step);

        let mut b = fresh();
        b.prepare(48_000);
        for _ in 0..16 {
            b.process(step); // warm up to a non-zero history
        }
        b.reset();
        let after_reset = b.process(step);

        assert_eq!(after_reset, first, "reset did not restore initial state");
    }

    #[test]
    fn param_eq_unprepared_is_passthrough() {
        // Inserted but never prepared ⇒ identity biquad ⇒ bit-exact.
        let mut d = Device::param_eq(1_000, 12, 6_000);
        for raw in [0i16, 12_345, -9_876, 30_000] {
            let (l, r) = d.process((Amp::from_q15_i16(raw), Amp::from_q15_i16(-raw)));
            assert!((l.as_q15_i16() as i32 - raw as i32).abs() <= 1);
            assert!((r.as_q15_i16() as i32 + raw as i32).abs() <= 1);
        }
    }

    #[test]
    fn param_eq_boost_raises_centre_tone() {
        use crate::core::fixed::tables::sine;
        use crate::core::fixed::units::Phase;
        let centre = 3_000u32;
        let rate = 48_000u32;
        let peak = |gain_milli_db: i16| -> i32 {
            let mut d = Device::param_eq(centre as u16, 12, gain_milli_db);
            d.prepare(rate);
            let inc = (((centre as u64) << 16) / rate as u64) as u16;
            let mut ph = 0u16;
            let mut p = 0i32;
            for n in 0..800 {
                let s = (sine(Phase::from_raw(ph)).raw() as i32 * 4_000) >> 15;
                let (l, _) = d.process((Amp::from_q15_i16(s as i16), Amp::from_q15_i16(s as i16)));
                if n >= 400 {
                    p = p.max(l.as_q15_i16().unsigned_abs() as i32);
                }
                ph = ph.wrapping_add(inc);
            }
            p
        };
        assert!(peak(12_000) > peak(0) + 1_000, "boost did not raise centre");
        assert!(peak(-12_000) < peak(0) - 500, "cut did not lower centre");
    }

    #[test]
    fn distortion_unprepared_is_passthrough_and_chain_inert() {
        // Bare device un-prepared.
        let mut d = Device::distortion(0, Q15::from_ratio(61, 100), 2_000, 1_000, 6_000);
        let s = (Amp::from_q15_i16(15_000), Amp::from_q15_i16(-7_000));
        assert_eq!(d.process(s), s, "un-prepared distortion not pass-through");

        // A bypassed chain holding a prepared distortion is bit-exact.
        let mut chain = DeviceChain {
            devices: alloc::vec![Device::distortion(
                -3_000,
                Q15::from_ratio(49, 100),
                2_000,
                1_000,
                6_000
            )],
            bypass: true,
        };
        chain.prepare(48_000);
        assert_eq!(chain.process(s), s);
    }

    #[test]
    fn distortion_adds_harmonics_and_drives_signal() {
        // A driven sine should clip toward the rails (peak grows / sticks
        // high relative to a clean unity path), proving the waveshaper is
        // engaged after prepare.
        use crate::core::fixed::tables::sine;
        use crate::core::fixed::units::Phase;
        let mut d = Device::distortion(0, Q15::ONE, 8_000, 4_000, 8_000); // max edge
        d.prepare(48_000);
        let inc = (((2_000u64) << 16) / 48_000) as u16;
        let mut ph = 0u16;
        let mut energy_in = 0i64;
        let mut energy_out = 0i64;
        for _ in 0..400 {
            let s = (sine(Phase::from_raw(ph)).raw() as i32 * 3_000) >> 15;
            let (l, _) = d.process((Amp::from_q15_i16(s as i16), Amp::from_q15_i16(s as i16)));
            energy_in += (s as i64) * (s as i64);
            let o = l.as_q15_i16() as i64;
            energy_out += o * o;
            ph = ph.wrapping_add(inc);
        }
        // High drive lifts a small input substantially.
        assert!(
            energy_out > energy_in,
            "distortion did not add gain/harmonics"
        );
    }

    /// Steady output peak of a sine tone driven through a device long
    /// enough for the envelope to settle.
    fn steady_tone_peak(d: &mut Device, freq: u32, rate: u32, ampl: i32) -> i32 {
        use crate::core::fixed::tables::sine;
        use crate::core::fixed::units::Phase;
        let inc = (((freq as u64) << 16) / rate as u64) as u16;
        let mut ph = 0u16;
        let mut peak = 0i32;
        let total = 4_000usize; // ample settle time for slow release
        for n in 0..total {
            let s = (sine(Phase::from_raw(ph)).raw() as i32 * ampl) >> 15;
            let (l, _) = d.process((Amp::from_q15_i16(s as i16), Amp::from_q15_i16(s as i16)));
            if n >= total * 3 / 4 {
                peak = peak.max(l.as_q15_i16().unsigned_abs() as i32);
            }
            ph = ph.wrapping_add(inc);
        }
        peak
    }

    #[test]
    fn compressor_unprepared_is_passthrough() {
        let mut d = Device::compressor(-20_000, 1_024, 1_000, 100_000, 0);
        let s = (Amp::from_q15_i16(25_000), Amp::from_q15_i16(-12_000));
        assert_eq!(d.process(s), s);
    }

    #[test]
    fn compressor_below_threshold_is_unity() {
        // −24 dBFS tone, threshold −20 dB ⇒ untouched (make-up 0).
        let mut d = Device::compressor(-20_000, 1_024, 1_000, 100_000, 0);
        d.prepare(48_000);
        let ampl = 2_000; // ≈ −24 dBFS
        let peak = steady_tone_peak(&mut d, 1_000, 48_000, ampl);
        assert!(
            (peak - ampl).abs() < ampl / 8,
            "below-thresh not unity: {peak} vs {ampl}"
        );
    }

    #[test]
    fn compressor_reduces_above_threshold() {
        // ≈ −1 dBFS tone, threshold −20 dB, ratio 4:1 ⇒ ~−14 dB gain
        // reduction ⇒ output ≈ ×0.19. Check it is clearly reduced and in
        // the right ballpark.
        let mut comp = Device::compressor(-20_000, 1_024, 1_000, 100_000, 0);
        comp.prepare(48_000);
        let ampl = 29_000;
        let out = steady_tone_peak(&mut comp, 1_000, 48_000, ampl);
        assert!(out < ampl / 2, "not compressed: {out} vs in {ampl}");
        // Expected ≈ 0.19·29000 ≈ 5500; allow a wide tolerance band.
        assert!((3_000..=9_000).contains(&out), "gain reduction off: {out}");

        // Higher ratio ⇒ more reduction.
        let mut hard = Device::compressor(-20_000, 25_600, 1_000, 100_000, 0); // 100:1
        hard.prepare(48_000);
        let out_hard = steady_tone_peak(&mut hard, 1_000, 48_000, ampl);
        assert!(
            out_hard < out,
            "100:1 not harder than 4:1: {out_hard} vs {out}"
        );
    }

    #[test]
    fn compressor_makeup_lifts_quiet_signal() {
        // Below threshold, +12 dB make-up ⇒ ×~4 (clamped by headroom).
        let mut d = Device::compressor(-20_000, 1_024, 1_000, 100_000, 12_000);
        d.prepare(48_000);
        let ampl = 4_000;
        let peak = steady_tone_peak(&mut d, 1_000, 48_000, ampl);
        assert!(
            peak > ampl + ampl / 2,
            "make-up did not lift: {peak} vs {ampl}"
        );
    }

    /// Run an impulse (then silence) through a device and return the
    /// per-sample left-channel output magnitude.
    fn impulse_response(d: &mut Device, n: usize) -> alloc::vec::Vec<i32> {
        let mut out = alloc::vec::Vec::with_capacity(n);
        for i in 0..n {
            let x = if i == 0 { 32_767 } else { 0 };
            let (l, _) = d.process((Amp::from_q15_i16(x), Amp::from_q15_i16(x)));
            out.push(l.as_q15_i16() as i32);
        }
        out
    }

    fn argmax(v: &[i32]) -> usize {
        let mut bi = 0;
        let mut bv = i32::MIN;
        for (i, &x) in v.iter().enumerate() {
            if x.abs() > bv {
                bv = x.abs();
                bi = i;
            }
        }
        bi
    }

    #[test]
    fn echo_unprepared_is_passthrough() {
        let mut d = Device::echo(Q15::HALF, Q15::from_ratio(1, 2), 100, 120, false);
        let s = (Amp::from_q15_i16(20_000), Amp::from_q15_i16(-9_000));
        assert_eq!(d.process(s), s);
    }

    #[test]
    fn echo_taps_at_delay_time() {
        // 10 ms delay at 48 kHz = 480 samples; full wet, no feedback ⇒
        // a single echo of the impulse at exactly that offset.
        let mut d = Device::echo(Q15::ONE, Q15::ZERO, 10, 10, false);
        d.prepare(48_000);
        let resp = impulse_response(&mut d, 1_200);
        let peak = argmax(&resp);
        assert!(
            (peak as i64 - 480).abs() <= 1,
            "echo at {peak}, expected 480"
        );
        // Only one echo (feedback 0): nothing significant after it.
        assert!(resp[960].abs() < 100, "spurious second echo: {}", resp[960]);
    }

    #[test]
    fn echo_feedback_decays_monotonically() {
        // 5 ms delay = 240 samples; feedback 0.5 ⇒ echoes at 240, 480,
        // 720 … each smaller than the last, none blowing up.
        let mut d = Device::echo(Q15::ONE, Q15::from_ratio(1, 2), 5, 5, false);
        d.prepare(48_000);
        let resp = impulse_response(&mut d, 1_600);
        let e1 = resp[240].abs();
        let e2 = resp[480].abs();
        let e3 = resp[720].abs();
        assert!(e1 > e2 && e2 > e3, "not decaying: {e1} {e2} {e3}");
        assert!(e3 > 0, "tail died too early");
        // ~0.5 ratio between successive echoes (band [0.3, 0.7]).
        assert!(
            e2 * 10 > e1 * 3 && e2 * 10 < e1 * 7,
            "feedback ratio off: {e1}→{e2}"
        );
    }

    /// Drive a sine and return `(min, max)` of the per-window output peak
    /// across the run (first window skipped as transient). A modulating
    /// effect produces a wide spread; a static one a narrow one.
    fn windowed_peak_spread(
        d: &mut Device,
        freq: u32,
        rate: u32,
        ampl: i32,
        total: usize,
        win: usize,
    ) -> (i32, i32) {
        use crate::core::fixed::tables::sine;
        use crate::core::fixed::units::Phase;
        let inc = (((freq as u64) << 16) / rate as u64) as u16;
        let mut ph = 0u16;
        let mut lo = i32::MAX;
        let mut hi = 0i32;
        let mut wpeak = 0i32;
        for n in 0..total {
            let s = (sine(Phase::from_raw(ph)).raw() as i32 * ampl) >> 15;
            let (l, _) = d.process((Amp::from_q15_i16(s as i16), Amp::from_q15_i16(s as i16)));
            wpeak = wpeak.max(l.as_q15_i16().unsigned_abs() as i32);
            if n % win == win - 1 {
                if n >= win {
                    // skip the very first window (transient)
                    lo = lo.min(wpeak);
                    hi = hi.max(wpeak);
                }
                wpeak = 0;
            }
            ph = ph.wrapping_add(inc);
        }
        (lo, hi)
    }

    #[test]
    fn moddelay_unprepared_is_passthrough() {
        let mut d = Device::mod_delay(
            ModDelayMode::Chorus,
            Q15::HALF,
            Q15::HALF,
            Q15::ZERO,
            2_000,
            true,
            512,
            true,
        );
        let s = (Amp::from_q15_i16(18_000), Amp::from_q15_i16(-6_000));
        assert_eq!(d.process(s), s);
    }

    #[test]
    fn flanger_modulates_a_steady_tone() {
        // Flanger with depth + feedback ⇒ a swept comb ⇒ the output of a
        // steady tone rises and falls over the LFO cycle.
        let mut fl = Device::mod_delay(
            ModDelayMode::Flanger,
            Q15::HALF,
            Q15::from_ratio(85, 100),  // deep
            Q15::from_ratio(-61, 100), // negative feedback (flanger notch)
            8_000,                     // 8 Hz LFO (fast, for a short test)
            true,
            512, // 2 ms base delay (Q8.8)
            false,
        );
        fl.prepare(48_000);
        let (lo, hi) = windowed_peak_spread(&mut fl, 1_000, 48_000, 16_000, 12_000, 400);
        assert!(hi - lo > 2_000, "flanger did not modulate: lo {lo} hi {hi}");

        // depth = 0 ⇒ a static comb ⇒ steady output (narrow spread).
        let mut flat = Device::mod_delay(
            ModDelayMode::Flanger,
            Q15::HALF,
            Q15::ZERO,
            Q15::from_ratio(-61, 100),
            8_000,
            true,
            512,
            false,
        );
        flat.prepare(48_000);
        let (lo2, hi2) = windowed_peak_spread(&mut flat, 1_000, 48_000, 16_000, 12_000, 400);
        assert!(hi2 - lo2 < (hi - lo), "depth=0 should modulate less");
    }

    #[test]
    fn chorus_is_stable_and_audible() {
        // A chorus should pass a stable, non-silent signal (no runaway).
        let mut ch = Device::mod_delay(
            ModDelayMode::Chorus,
            Q15::HALF,
            Q15::from_ratio(61, 100),
            Q15::from_ratio(12, 100), // light feedback
            3_000,                    // 3 Hz
            true,
            2_560, // 10 ms base (Q8.8)
            true,
        );
        ch.prepare(48_000);
        let (lo, hi) = windowed_peak_spread(&mut ch, 800, 48_000, 14_000, 16_000, 500);
        assert!(lo > 1_000, "chorus went silent: lo {lo}");
        assert!(hi < 32_767, "chorus ran away to clipping: hi {hi}");
    }

    #[test]
    fn reverb_unprepared_is_passthrough() {
        let mut d = Device::reverb(
            ReverbMode::Freeverb,
            Q15::from_ratio(61, 100),
            Q15::from_ratio(49, 100),
            Q15::ONE,
            Q15::from_ratio(31, 100),
            Q15::from_ratio(31, 100),
        );
        let s = (Amp::from_q15_i16(15_000), Amp::from_q15_i16(-7_000));
        assert_eq!(d.process(s), s);
    }

    #[test]
    fn reverb_produces_decaying_bounded_tail() {
        let mut d = Device::reverb(
            ReverbMode::Freeverb,
            Q15::from_ratio(73, 100), // largish room
            Q15::from_ratio(31, 100),
            Q15::ONE,
            Q15::from_ratio(1, 3), // wet ≈ 1.0 (×scalewet)
            Q15::ZERO,             // no dry — measure the tail alone
        );
        d.prepare(48_000);
        let resp = impulse_response(&mut d, 24_000);
        // Energy in an early window vs a late window of the tail.
        let win_energy = |from: usize, to: usize| -> i64 {
            resp[from..to]
                .iter()
                .map(|&x| (x as i64) * (x as i64))
                .sum()
        };
        let early = win_energy(1_000, 3_000);
        let late = win_energy(20_000, 22_000);
        assert!(early > 0, "no reverb tail produced");
        assert!(
            late < early,
            "tail did not decay: early {early} late {late}"
        );
        // Never runs away: stays well inside full scale.
        let peak = resp.iter().map(|&x| x.unsigned_abs()).max().unwrap();
        assert!(peak < 32_767, "reverb clipped/ran away: peak {peak}");
    }

    #[test]
    fn reverb_dry_only_is_passthrough() {
        // wet = 0, dry = 0.5 ⇒ dry gain ×scaledry(2) = unity ⇒ exact
        // pass-through of the direct signal.
        let mut d = Device::reverb(
            ReverbMode::WavesReverb,
            Q15::from_ratio(61, 100),
            Q15::from_ratio(49, 100),
            Q15::ONE,
            Q15::ZERO,
            Q15::HALF,
        );
        d.prepare(48_000);
        for raw in [0i16, 12_345, -9_000, 25_000] {
            let (l, _) = d.process((Amp::from_q15_i16(raw), Amp::from_q15_i16(raw)));
            assert!(
                (l.as_q15_i16() as i32 - raw as i32).abs() <= 2,
                "dry mix off at {raw}"
            );
        }
    }

    #[test]
    fn reverb_scales_to_any_sample_rate() {
        // The comb / all-pass lengths are tuned at 44.1 kHz and rescaled
        // to the run rate (`len = tuning · rate / 44100`), so the reverb
        // must work — decaying, bounded tail, no panic — at any rate,
        // 48 kHz and beyond included (the player is never assumed 44.1).
        for &rate in &[22_050u32, 44_100, 48_000, 96_000] {
            let mut d = Device::reverb(
                ReverbMode::Freeverb,
                Q15::from_ratio(67, 100),
                Q15::from_ratio(24, 100),
                Q15::ONE,
                Q15::from_ratio(1, 3),
                Q15::ZERO,
            );
            d.prepare(rate);
            let n = (rate / 2) as usize; // 0.5 s
            let resp = impulse_response(&mut d, n);
            let mid: i64 = resp[n / 8..n / 4].iter().map(|&x| (x as i64).pow(2)).sum();
            let peak = resp.iter().map(|&x| x.unsigned_abs()).max().unwrap();
            assert!(mid > 0, "no reverb tail at {rate} Hz");
            assert!(peak < 32_767, "reverb clipped/ran away at {rate} Hz");
        }
    }

    #[test]
    fn reverb_wet_and_dry_are_independent() {
        // Dry at unity plus a wet send adds reverb energy ON TOP of the
        // dry signal (not a crossfade): the wet+dry render has strictly
        // more energy than dry-only.
        let render = |wet: Q15, dry: Q15| -> i64 {
            let mut d = Device::reverb(
                ReverbMode::Freeverb,
                Q15::from_ratio(67, 100),
                Q15::from_ratio(24, 100),
                Q15::ONE,
                wet,
                dry,
            );
            d.prepare(48_000);
            // A short tone burst then silence; sum output energy.
            let mut e = 0i64;
            for n in 0..6_000 {
                let x = if n < 500 {
                    (n as i16 % 64 - 32) * 400
                } else {
                    0
                };
                let (l, _) = d.process((Amp::from_q15_i16(x), Amp::from_q15_i16(x)));
                e += (l.as_q15_i16() as i64).pow(2);
            }
            e
        };
        let dry_only = render(Q15::ZERO, Q15::HALF);
        let wet_plus_dry = render(Q15::from_ratio(24, 100), Q15::HALF);
        assert!(
            wet_plus_dry > dry_only,
            "wet did not add on top of dry: {wet_plus_dry} vs {dry_only}"
        );
    }

    #[test]
    fn reverb_freeze_sustains_the_tail() {
        // Two identical reverbs are excited, then silence is fed. The
        // frozen one keeps its tail (unit feedback, muted input); the
        // free one decays. The frozen late tail must dwarf the free one.
        let late_tail = |freeze: bool| -> i64 {
            let mut d = Device::reverb(
                ReverbMode::Freeverb,
                Q15::from_ratio(61, 100),
                Q15::from_ratio(12, 100),
                Q15::ONE,
                Q15::from_ratio(1, 3),
                Q15::ZERO,
            );
            d.prepare(48_000);
            // Fill the network with a burst.
            for n in 0..400 {
                let x = (n as i16 % 50 - 25) * 600;
                d.process((Amp::from_q15_i16(x), Amp::from_q15_i16(x)));
            }
            if freeze {
                d.set_param(5, Q15::ONE); // engage freeze
            }
            // Feed silence; measure energy in a late window.
            let mut e = 0i64;
            for n in 0..40_000 {
                let (l, _) = d.process((Amp::SILENCE, Amp::SILENCE));
                if n >= 36_000 {
                    e += (l.as_q15_i16() as i64).pow(2);
                }
            }
            e
        };
        let frozen = late_tail(true);
        let free = late_tail(false);
        assert!(
            frozen > free * 8,
            "freeze did not sustain: frozen {frozen} free {free}"
        );
    }

    #[test]
    fn i3dl2_unprepared_is_passthrough() {
        let mut d = Device::i3dl2_reverb(0, 1_500, 1_000, -1_000, 20, -1_000, 40);
        let s = (Amp::from_q15_i16(15_000), Amp::from_q15_i16(-7_000));
        assert_eq!(d.process(s), s);
    }

    #[test]
    fn i3dl2_has_early_reflections_and_decaying_tail() {
        // Room 0 dB, audible reflections + reverb, 20 ms pre-delay.
        let mut d = Device::i3dl2_reverb(0, 2_000, 1_000, 0, 20, 0, 30);
        d.prepare(48_000);
        let resp = impulse_response(&mut d, 20_000);
        // An early reflection appears shortly after the pre-delay
        // (20 ms + first tap 7 ms ≈ 27 ms ≈ 1296 samples), before the
        // late tail builds.
        let early_peak = resp[1_000..2_500]
            .iter()
            .map(|&x| x.unsigned_abs())
            .max()
            .unwrap();
        assert!(early_peak > 50, "no early reflections: {early_peak}");
        // The late tail decays.
        let mid: i64 = resp[3_000..5_000].iter().map(|&x| (x as i64).pow(2)).sum();
        let late: i64 = resp[17_000..19_000]
            .iter()
            .map(|&x| (x as i64).pow(2))
            .sum();
        assert!(mid > 0 && late < mid, "tail did not decay: {mid} → {late}");
        // Past the direct impulse (n=0) the wet tail must stay bounded
        // (no instability); the dry impulse itself is full-scale.
        let tail_peak = resp[100..].iter().map(|&x| x.unsigned_abs()).max().unwrap();
        assert!(tail_peak < 32_767, "i3dl2 tail ran away: {tail_peak}");
    }

    #[test]
    fn i3dl2_longer_decay_extends_tail() {
        let tail_energy = |decay_ms: u16| -> i64 {
            let mut d = Device::i3dl2_reverb(0, decay_ms, 1_500, -20_000, 10, 0, 20);
            d.prepare(48_000);
            let resp = impulse_response(&mut d, 24_000);
            resp[18_000..22_000]
                .iter()
                .map(|&x| (x as i64).pow(2))
                .sum()
        };
        let short = tail_energy(300);
        let long = tail_energy(12_000);
        assert!(
            long > short,
            "longer DecayTime did not extend tail: {short} vs {long}"
        );
    }

    #[test]
    fn gargle_unprepared_is_passthrough() {
        let mut d = Device::gargle(50, false);
        let s = (Amp::from_q15_i16(20_000), Amp::from_q15_i16(-12_000));
        assert_eq!(d.process(s), s);
    }

    #[test]
    fn gargle_triangle_modulates_amplitude() {
        // A steady DC-ish input through a triangle gargle must trace a
        // 0→peak→0 amplitude envelope over one cycle.
        let mut d = Device::gargle(100, false); // 100 Hz
        d.prepare(48_000); // period = 480 frames
        let period = 480usize;
        let mut peaks = Vec::new();
        for _ in 0..period {
            let (l, _) = d.process((Amp::from_q15_i16(30_000), Amp::from_q15_i16(30_000)));
            peaks.push(l.as_q15_i16() as i32);
        }
        // Starts near silence, reaches near full near the middle, returns
        // near silence at the end.
        assert!(peaks[0] < 1_000, "start not silent: {}", peaks[0]);
        assert!(
            peaks[period / 2] > 25_000,
            "mid not loud: {}",
            peaks[period / 2]
        );
        assert!(
            peaks[period - 1] < 2_000,
            "end not silent: {}",
            peaks[period - 1]
        );
    }

    #[test]
    fn gargle_square_gates_audio() {
        // Square: full for the first half, silent for the second.
        let mut d = Device::gargle(100, true);
        d.prepare(48_000); // period = 480
        let mut first_half_loud = false;
        let mut second_half_silent = true;
        for n in 0..480 {
            let (l, _) = d.process((Amp::from_q15_i16(20_000), Amp::from_q15_i16(20_000)));
            let v = l.as_q15_i16().unsigned_abs() as i32;
            if n < 240 && v > 19_000 {
                first_half_loud = true;
            }
            if n >= 240 && v != 0 {
                second_half_silent = false;
            }
        }
        assert!(first_half_loud, "square first half not passing audio");
        assert!(second_half_silent, "square second half not silenced");
    }

    #[test]
    fn lowpass_is_stateful() {
        // A step into a low-cutoff filter rises gradually: each output
        // depends on the previous one (proves per-sample state).
        let mut d = Device::LowPass {
            cutoff: Q15::from_ratio(12, 100), // ~0.12
            state: (0, 0),
        };
        let step = Amp::from_q15_i16(20_000);
        let y1 = d.process((step, step)).0.as_q15_i16();
        let y2 = d.process((step, step)).0.as_q15_i16();
        let y3 = d.process((step, step)).0.as_q15_i16();
        assert!(
            0 < y1 && y1 < y2 && y2 < y3 && y3 < 20_000,
            "{y1} {y2} {y3}"
        );
    }
}