autoeq-iir 0.2.21

AutoEQ IIR and FIR filters
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
//! IIR filter implementation (Biquad filters and Parametric EQ)

use base64::{Engine as _, engine::general_purpose};
use byteorder::{BigEndian, WriteBytesExt};
use ndarray::Array1;
use std::f64::consts::PI;
use std::fmt;

use crate::{DEFAULT_Q_HIGH_LOW_PASS, DEFAULT_Q_HIGH_LOW_SHELF, q2bw};

pub type Peq = Vec<(f64, Biquad)>;

/// Filter types for biquad filters
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum BiquadFilterType {
    /// Low-pass filter
    Lowpass,
    /// High-pass filter
    Highpass,
    /// High-pass filter
    HighpassVariableQ,
    /// Band-pass filter
    Bandpass,
    /// Peaking filter
    Peak,
    /// Notch filter
    Notch,
    /// Low-shelf filter
    Lowshelf,
    /// High-shelf filter
    Highshelf,
}

impl BiquadFilterType {
    /// Returns the short string representation of the filter type (e.g., "LP").
    pub fn short_name(&self) -> &'static str {
        match self {
            BiquadFilterType::Lowpass => "LP",
            BiquadFilterType::Highpass => "HP",
            BiquadFilterType::HighpassVariableQ => "HPQ",
            BiquadFilterType::Bandpass => "BP",
            BiquadFilterType::Peak => "PK",
            BiquadFilterType::Notch => "NO",
            BiquadFilterType::Lowshelf => "LS",
            BiquadFilterType::Highshelf => "HS",
        }
    }

    /// Returns the long string representation of the filter type (e.g., "Lowpass").
    pub fn long_name(&self) -> &'static str {
        match self {
            BiquadFilterType::Lowpass => "Lowpass",
            BiquadFilterType::Highpass => "Highpass",
            BiquadFilterType::HighpassVariableQ => "HighpassVariableQ",
            BiquadFilterType::Bandpass => "Bandpass",
            BiquadFilterType::Peak => "Peak",
            BiquadFilterType::Notch => "Notch",
            BiquadFilterType::Lowshelf => "Lowshelf",
            BiquadFilterType::Highshelf => "Highshelf",
        }
    }
}

/// Represents a single biquad IIR filter.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Biquad {
    /// The type of filter
    pub filter_type: BiquadFilterType,
    /// Center frequency in Hz
    pub freq: f64,
    /// Sample rate in Hz
    pub srate: f64,
    /// Q factor (quality factor)
    pub q: f64,
    /// Gain in dB (for peaking and shelving filters)
    pub db_gain: f64,
    /// Filter coefficients
    a1: f64,
    a2: f64,
    b0: f64,
    b1: f64,
    b2: f64,
    /// Filter state (for processing samples)
    x1: f64,
    x2: f64,
    y1: f64,
    y2: f64,
    /// Pre-computed coefficients for fast frequency response calculation
    r_up0: f64,
    r_up1: f64,
    r_up2: f64,
    r_dw0: f64,
    r_dw1: f64,
    r_dw2: f64,
}

impl Biquad {
    /// Creates and initializes a new Biquad filter.
    pub fn new(filter_type: BiquadFilterType, freq: f64, srate: f64, q: f64, db_gain: f64) -> Self {
        let mut biquad = Biquad {
            filter_type,
            freq,
            srate,
            q,
            db_gain,
            a1: 0.0,
            a2: 0.0,
            b0: 0.0,
            b1: 0.0,
            b2: 0.0,
            x1: 0.0,
            x2: 0.0,
            y1: 0.0,
            y2: 0.0,
            r_up0: 0.0,
            r_up1: 0.0,
            r_up2: 0.0,
            r_dw0: 0.0,
            r_dw1: 0.0,
            r_dw2: 0.0,
        };

        // Adjust Q based on filter type, matching Python logic
        if biquad.filter_type == BiquadFilterType::Notch {
            biquad.q = 30.0;
        } else if biquad.q == 0.0 {
            match biquad.filter_type {
                BiquadFilterType::Bandpass
                | BiquadFilterType::Highpass
                | BiquadFilterType::Lowpass => {
                    biquad.q = DEFAULT_Q_HIGH_LOW_PASS;
                }
                BiquadFilterType::Lowshelf | BiquadFilterType::Highshelf => {
                    biquad.q = DEFAULT_Q_HIGH_LOW_SHELF;
                }
                _ => {}
            }
        }

        // Safety clamp: ensure strictly positive Q to avoid division by zero in alpha = sn/(2*q)
        if biquad.q <= 0.0 {
            biquad.q = 1.0e-2;
        }

        biquad.compute_coeffs();
        biquad
    }

    fn compute_coeffs(&mut self) {
        // Intermediate variables
        let a = 10.0_f64.powf(self.db_gain / 40.0);
        let omega = 2.0 * PI * self.freq / self.srate;
        let sn = omega.sin();
        let cs = omega.cos();
        let alpha = sn / (2.0 * self.q);
        let beta = (a + a).sqrt();

        // Raw coefficients
        let (b0, b1, b2, a0, a1, a2);

        match self.filter_type {
            BiquadFilterType::Lowpass => {
                b0 = (1.0 - cs) / 2.0;
                b1 = 1.0 - cs;
                b2 = (1.0 - cs) / 2.0;
                a0 = 1.0 + alpha;
                a1 = -2.0 * cs;
                a2 = 1.0 - alpha;
            }
            BiquadFilterType::Highpass | BiquadFilterType::HighpassVariableQ => {
                b0 = (1.0 + cs) / 2.0;
                b1 = -(1.0 + cs);
                b2 = (1.0 + cs) / 2.0;
                a0 = 1.0 + alpha;
                a1 = -2.0 * cs;
                a2 = 1.0 - alpha;
            }
            BiquadFilterType::Bandpass => {
                b0 = alpha;
                b1 = 0.0;
                b2 = -alpha;
                a0 = 1.0 + alpha;
                a1 = -2.0 * cs;
                a2 = 1.0 - alpha;
            }
            BiquadFilterType::Notch => {
                b0 = 1.0;
                b1 = -2.0 * cs;
                b2 = 1.0;
                a0 = 1.0 + alpha;
                a1 = -2.0 * cs;
                a2 = 1.0 - alpha;
            }
            BiquadFilterType::Peak => {
                b0 = 1.0 + (alpha * a);
                b1 = -2.0 * cs;
                b2 = 1.0 - (alpha * a);
                a0 = 1.0 + (alpha / a);
                a1 = -2.0 * cs;
                a2 = 1.0 - (alpha / a);
            }
            BiquadFilterType::Lowshelf => {
                b0 = a * ((a + 1.0) - (a - 1.0) * cs + beta * sn);
                b1 = 2.0 * a * ((a - 1.0) - (a + 1.0) * cs);
                b2 = a * ((a + 1.0) - (a - 1.0) * cs - beta * sn);
                a0 = (a + 1.0) + (a - 1.0) * cs + beta * sn;
                a1 = -2.0 * ((a - 1.0) + (a + 1.0) * cs);
                a2 = (a + 1.0) + (a - 1.0) * cs - beta * sn;
            }
            BiquadFilterType::Highshelf => {
                b0 = a * ((a + 1.0) + (a - 1.0) * cs + beta * sn);
                b1 = -2.0 * a * ((a - 1.0) + (a + 1.0) * cs);
                b2 = a * ((a + 1.0) + (a - 1.0) * cs - beta * sn);
                a0 = (a + 1.0) - (a - 1.0) * cs + beta * sn;
                a1 = 2.0 * ((a - 1.0) - (a + 1.0) * cs);
                a2 = (a + 1.0) - (a - 1.0) * cs - beta * sn;
            }
        }

        // Normalize coefficients
        self.b0 = b0 / a0;
        self.b1 = b1 / a0;
        self.b2 = b2 / a0;
        self.a1 = a1 / a0;
        self.a2 = a2 / a0;

        // Pre-compute for result()
        self.r_up0 = (self.b0 + self.b1 + self.b2).powi(2);
        self.r_up1 = -4.0 * (self.b0 * self.b1 + 4.0 * self.b0 * self.b2 + self.b1 * self.b2);
        self.r_up2 = 16.0 * self.b0 * self.b2;
        self.r_dw0 = (1.0 + self.a1 + self.a2).powi(2);
        self.r_dw1 = -4.0 * (self.a1 + 4.0 * self.a2 + self.a1 * self.a2);
        self.r_dw2 = 16.0 * self.a2;
    }

    /// Processes a single audio sample through the filter.
    pub fn process(&mut self, x: f64) -> f64 {
        let y = self.b0 * x + self.b1 * self.x1 + self.b2 * self.x2
            - self.a1 * self.y1
            - self.a2 * self.y2;

        self.x2 = self.x1;
        self.x1 = x;
        self.y2 = self.y1;
        self.y1 = y;

        y
    }

    /// Calculates the filter's magnitude response at a single frequency `f`.
    pub fn result(&self, f: f64) -> f64 {
        let phi = (PI * f / self.srate).sin().powi(2);
        let phi2 = phi * phi;

        let numerator = self.r_up0 + self.r_up1 * phi + self.r_up2 * phi2;
        let denominator = self.r_dw0 + self.r_dw1 * phi + self.r_dw2 * phi2;

        let result = (numerator / denominator).max(0.0);
        result.sqrt()
    }

    /// Calculates the filter's response in dB at a single frequency `f`.
    pub fn log_result(&self, f: f64) -> f64 {
        let result = self.result(f);
        if result > 0.0 {
            20.0 * result.log10()
        } else {
            -200.0 // Return a large negative number for silence
        }
    }

    /// Vectorized version to compute the SPL response for a vector of frequencies.
    /// This is the fast equivalent of the `np_log_result` Python method.
    pub fn np_log_result(&self, freq: &Array1<f64>) -> Array1<f64> {
        let coeff = PI / self.srate;
        let phi = (freq * coeff).mapv(f64::sin).mapv(|x| x.powi(2));
        let phi2 = &phi * &phi;

        let r_up = self.r_up0 + self.r_up1 * &phi + self.r_up2 * &phi2;
        let r_dw = self.r_dw0 + self.r_dw1 * &phi + self.r_dw2 * &phi2;
        let r = r_up / r_dw;

        // Clip to a minimum value to avoid log(0), then calculate dB
        let min_val = 1.0e-20;

        r.mapv(|val| val.max(min_val))
            .mapv(f64::sqrt)
            .mapv(f64::log10)
            * 20.0
    }

    /// Returns the filter coefficients as a tuple.
    pub fn constants(&self) -> (f64, f64, f64, f64, f64) {
        (self.a1, self.a2, self.b0, self.b1, self.b2)
    }
}

/// Implement the Display trait for pretty-printing, similar to __str__.
impl fmt::Display for Biquad {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Type:{},Freq:{:.1},Rate:{:.1},Q:{:.1},Gain:{:.1}",
            self.filter_type.short_name(),
            self.freq,
            self.srate,
            self.q,
            self.db_gain
        )
    }
}

/// Represents a single filter in a parametric equalizer.
#[derive(Debug, Clone, Default)]
///
/// Center frequency in Hz
/// Q factor (quality factor)
/// Gain in dB
/// Type of filter (e.g., "PK", "LP", "HP")
pub struct FilterRow {
    /// Center frequency in Hz
    pub freq: f64,
    /// Q factor (quality factor)
    pub q: f64,
    /// Gain in dB
    pub gain: f64,
    /// Type of filter (e.g., "PK", "LP", "HP")
    pub kind: &'static str,
}

/// Compute the combined PEQ response (in dB) on a given frequency grid for a Peq.
///
/// # Arguments
/// * `freqs` - Frequency points for evaluation (Hz)
/// * `peq` - Parametric equalizer containing weighted biquad filters
/// * `_sample_rate` - Sample rate in Hz (unused, kept for API compatibility)
///
/// # Returns
/// Frequency response in dB SPL at the specified frequency points
pub fn compute_peq_response(freqs: &Array1<f64>, peq: &Peq, _sample_rate: f64) -> Array1<f64> {
    if peq.is_empty() {
        return Array1::zeros(freqs.len());
    }
    let mut response = Array1::zeros(freqs.len());
    for (weight, filter) in peq {
        // Note: we're not using sample_rate here as filters already have their own srate
        response += &(filter.np_log_result(freqs) * *weight);
    }
    response
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bw2q;
    use ndarray::array;

    fn approx_eq(a: f64, b: f64, tol: f64) -> bool {
        (a - b).abs() <= tol
    }

    #[test]
    fn test_bw_q_roundtrip() {
        let qs = [0.5, 1.0, 2.0, 5.0];
        for &q in &qs {
            let bw = q2bw(q);
            let q2 = bw2q(bw);
            assert!(
                approx_eq(q, q2, 1e-9),
                "roundtrip failed: q={} -> bw={} -> q2={}",
                q,
                bw,
                q2
            );
        }
    }

    #[test]
    fn test_biquad_np_log_result_is_finite() {
        let bq = Biquad::new(BiquadFilterType::Peak, 1_000.0, 48_000.0, 1.0, 6.0);
        let freqs = array![20.0, 100.0, 1_000.0, 10_000.0, 20_000.0];
        let resp = bq.np_log_result(&freqs);
        for (i, v) in resp.iter().enumerate() {
            assert!(v.is_finite(), "response at idx {} not finite: {}", i, v);
        }
    }

    #[test]
    fn peak_with_zero_q_is_safely_clamped() {
        // q==0 for Peak should be clamped internally to a small positive value
        let bq = Biquad::new(BiquadFilterType::Peak, 1_000.0, 48_000.0, 0.0, 3.0);
        let freqs = array![20.0, 100.0, 1_000.0, 10_000.0, 20_000.0];
        let resp = bq.np_log_result(&freqs);
        for (i, v) in resp.iter().enumerate() {
            assert!(v.is_finite(), "response at idx {} not finite: {}", i, v);
        }
    }

    #[test]
    fn test_a_weighting() {
        // Test A-weighting at specific frequencies
        // At 1kHz, A-weighting should be close to 0 dB
        let w_1k = a_weighting_db(1000.0);
        assert!(
            (w_1k - 0.0).abs() < 1.0,
            "A-weighting at 1kHz should be ~0 dB"
        );

        // At 100Hz, A-weighting should be significantly negative (low frequencies attenuated)
        let w_100 = a_weighting_db(100.0);
        assert!(w_100 < -15.0, "A-weighting at 100Hz should be < -15 dB");

        // At 4kHz, A-weighting should be slightly positive
        let w_4k = a_weighting_db(4000.0);
        assert!(w_4k > 0.0, "A-weighting at 4kHz should be positive");
    }

    #[test]
    fn test_k_weighting() {
        // Test K-weighting approximation
        // Below 38Hz should be heavily attenuated
        let w_30 = k_weighting_db(30.0);
        assert!(w_30 < -5.0, "K-weighting at 30Hz should be attenuated");

        // Mid-frequencies should have less attenuation
        let w_1k = k_weighting_db(1000.0);
        assert!(
            w_1k > w_30,
            "K-weighting at 1kHz should be less attenuated than 30Hz"
        );

        // High frequencies should have boost
        let w_5k = k_weighting_db(5000.0);
        assert!(
            w_5k > w_1k,
            "K-weighting at 5kHz should have more gain than 1kHz"
        );
    }

    #[test]
    fn test_peq_loudness_gain_flat() {
        // Empty PEQ should return 0 dB
        let peq: Peq = vec![];
        let gain = peq_loudness_gain(&peq, "k");
        assert_eq!(gain, 0.0);
    }

    #[test]
    fn test_peq_loudness_gain_boost() {
        // PEQ with +6 dB peak at 1kHz should require negative gain compensation
        let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 6.0);
        let peq = vec![(1.0, bq)];

        let gain_k = peq_loudness_gain(&peq, "k");
        let gain_a = peq_loudness_gain(&peq, "a");

        println!("Test: +6 dB peak at 1kHz");
        println!("  K-weighted gain: {:.2} dB", gain_k);
        println!("  A-weighted gain: {:.2} dB", gain_a);

        // Should be negative (reducing gain to compensate for boost)
        assert!(
            gain_k < 0.0,
            "Gain compensation for boost should be negative (K-weighting)"
        );
        assert!(
            gain_a < 0.0,
            "Gain compensation for boost should be negative (A-weighting)"
        );

        // Should be roughly in the range of -1 to -4 dB for a +6 dB peak
        assert!(
            gain_k > -5.0 && gain_k < 0.0,
            "K-weighted gain should be between -5 and 0 dB"
        );
        assert!(
            gain_a > -5.0 && gain_a < 0.0,
            "A-weighted gain should be between -5 and 0 dB"
        );
    }

    #[test]
    fn test_peq_loudness_gain_demo() {
        println!("\n=== PEQ Loudness Compensation Demo ===\n");

        // Example 1: Mid-range boost
        println!("1. +6 dB peak at 1 kHz:");
        let bq1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 6.0);
        let peq1 = vec![(1.0, bq1)];
        println!(
            "   Anti-clip: {:.2} dB, K-weighted: {:.2} dB, A-weighted: {:.2} dB",
            peq_preamp_gain(&peq1),
            peq_loudness_gain(&peq1, "k"),
            peq_loudness_gain(&peq1, "a")
        );

        // Example 2: Bass boost
        println!("2. +6 dB bass at 100 Hz:");
        let bq2 = Biquad::new(BiquadFilterType::Peak, 100.0, 48000.0, 1.0, 6.0);
        let peq2 = vec![(1.0, bq2)];
        println!(
            "   Anti-clip: {:.2} dB, K-weighted: {:.2} dB, A-weighted: {:.2} dB",
            peq_preamp_gain(&peq2),
            peq_loudness_gain(&peq2, "k"),
            peq_loudness_gain(&peq2, "a")
        );

        // Example 3: Treble boost
        println!("3. +6 dB treble at 8 kHz:");
        let bq3 = Biquad::new(BiquadFilterType::Peak, 8000.0, 48000.0, 1.0, 6.0);
        let peq3 = vec![(1.0, bq3)];
        println!(
            "   Anti-clip: {:.2} dB, K-weighted: {:.2} dB, A-weighted: {:.2} dB",
            peq_preamp_gain(&peq3),
            peq_loudness_gain(&peq3, "k"),
            peq_loudness_gain(&peq3, "a")
        );

        // Example 4: V-shape EQ
        println!("4. V-shape: bass+4dB, mid-3dB, treble+3dB:");
        let bass = Biquad::new(BiquadFilterType::Lowshelf, 150.0, 48000.0, 0.7, 4.0);
        let mid = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, -3.0);
        let treble = Biquad::new(BiquadFilterType::Highshelf, 8000.0, 48000.0, 0.7, 3.0);
        let peq4 = vec![(1.0, bass), (1.0, mid), (1.0, treble)];
        println!(
            "   Anti-clip: {:.2} dB, K-weighted: {:.2} dB, A-weighted: {:.2} dB",
            peq_preamp_gain(&peq4),
            peq_loudness_gain(&peq4, "k"),
            peq_loudness_gain(&peq4, "a")
        );

        println!("\nNote: Anti-clip prevents clipping, K/A-weighted maintains loudness balance");
    }

    #[test]
    fn test_peq_loudness_gain_cut() {
        // PEQ with -6 dB cut at 1kHz should require positive gain compensation
        let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, -6.0);
        let peq = vec![(1.0, bq)];

        let gain_k = peq_loudness_gain(&peq, "k");
        let gain_a = peq_loudness_gain(&peq, "a");

        // Should be positive (increasing gain to compensate for cut)
        assert!(
            gain_k > 0.0,
            "Gain compensation for cut should be positive (K-weighting)"
        );
        assert!(
            gain_a > 0.0,
            "Gain compensation for cut should be positive (A-weighting)"
        );
    }

    #[test]
    fn test_peq_loudness_gain_bass_boost() {
        // Bass boost (100 Hz) should have different impact with K vs A weighting
        let bq = Biquad::new(BiquadFilterType::Peak, 100.0, 48000.0, 1.0, 6.0);
        let peq = vec![(1.0, bq)];

        let gain_k = peq_loudness_gain(&peq, "k");
        let gain_a = peq_loudness_gain(&peq, "a");

        // Both should be negative (compensation for boost)
        assert!(gain_k < 0.0);
        assert!(gain_a < 0.0);

        // A-weighting attenuates low frequencies more, so compensation should be less negative
        assert!(
            gain_a > gain_k,
            "A-weighted gain should be less negative (bass is less perceptually important)"
        );
    }
}

/// Check if two PEQs are equal
///
/// Compares two PEQ vectors for equality, checking both weights and biquad parameters
pub fn peq_equal(left: &Peq, right: &Peq) -> bool {
    if left.len() != right.len() {
        return false;
    }

    left.iter().zip(right.iter()).all(|((w1, b1), (w2, b2))| {
        // Compare weights
        (w1 - w2).abs() < f64::EPSILON &&
        // Compare biquad parameters
        b1.filter_type == b2.filter_type &&
        (b1.freq - b2.freq).abs() < f64::EPSILON &&
        (b1.srate - b2.srate).abs() < f64::EPSILON &&
        (b1.q - b2.q).abs() < f64::EPSILON &&
        (b1.db_gain - b2.db_gain).abs() < f64::EPSILON
    })
}

/// Compute SPL for each frequency given a PEQ
///
/// # Arguments
/// * `freq` - Array of frequencies to compute response for
/// * `peq` - PEQ vector containing weighted biquad filters
///
/// # Returns
/// * Array of SPL values in dB for each frequency
pub fn peq_spl(freq: &Array1<f64>, peq: &Peq) -> Array1<f64> {
    let mut current_filter = Array1::zeros(freq.len());

    for (weight, iir) in peq {
        current_filter += &(iir.np_log_result(freq) * *weight);
    }

    current_filter
}

/// Compute A-weighting in dB for a given frequency
///
/// A-weighting approximates the frequency response of the human ear
/// and is used for loudness estimation.
///
/// # Arguments
/// * `f` - Frequency in Hz
///
/// # Returns
/// * A-weighting value in dB
fn a_weighting_db(f: f64) -> f64 {
    // A-weighting formula (IEC 61672-1)
    let f2 = f * f;
    let f4 = f2 * f2;

    let numerator = 12194.0_f64.powi(2) * f4;
    let denominator = (f2 + 20.6_f64.powi(2))
        * ((f2 + 107.7_f64.powi(2)) * (f2 + 737.9_f64.powi(2))).sqrt()
        * (f2 + 12194.0_f64.powi(2));

    let ra = numerator / denominator;
    20.0 * ra.log10() + 2.0 // +2.0 is normalization constant
}

/// Compute K-weighting in dB for a given frequency
///
/// K-weighting is used by EBU R128 loudness measurement standard.
/// It's composed of a pre-filter and RLB weighting.
///
/// # Arguments
/// * `f` - Frequency in Hz
///
/// # Returns
/// * K-weighting value in dB (approximate)
fn k_weighting_db(f: f64) -> f64 {
    // Simplified K-weighting approximation
    // Based on high-shelf at ~1500Hz and high-pass around 40Hz

    // High-pass stage (4th order Butterworth at 38Hz)
    let f_hp = 38.0;
    let hp_response = if f > 1.0 {
        20.0 * 4.0 * (f / f_hp).log10() // 4th order = 80 dB/decade
    } else {
        -200.0
    };
    let hp_gain = hp_response.min(0.0);

    // High-shelf stage (+4 dB above 1500 Hz)
    let f_hs = 1500.0;
    let hs_gain = if f > f_hs {
        4.0 * (1.0 - (f_hs / f).powf(2.0).min(1.0))
    } else {
        0.0
    };

    hp_gain + hs_gain
}

/// Compute loudness-weighted gain adjustment for PEQ to maintain spectral balance
///
/// This function estimates the perceived loudness change caused by a PEQ
/// by analyzing its frequency response with perceptual weighting.
/// Much faster than full Replay Gain analysis.
///
/// # Arguments
/// * `peq` - PEQ vector containing weighted biquad filters
/// * `weighting` - Weighting type: "a" for A-weighting, "k" for K-weighting (EBU R128-like)
///
/// # Returns
/// * Gain adjustment in dB to maintain similar loudness (0 dB = no change needed)
///
/// # Example
/// ```no_run
/// use autoeq_iir::{Biquad, BiquadFilterType, peq_loudness_gain};
///
/// let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 6.0);
/// let peq = vec![(1.0, bq)];
/// let gain_adj = peq_loudness_gain(&peq, "k");
/// println!("Apply {} dB to maintain loudness balance", gain_adj);
/// ```
pub fn peq_loudness_gain(peq: &Peq, weighting: &str) -> f64 {
    if peq.is_empty() {
        return 0.0;
    }

    // Generate logarithmic frequency array from 20Hz to 20kHz with 500 points
    // More points than preamp_gain for better loudness integration
    let n_points = 500;
    let freq = Array1::logspace(
        10.0,
        (2.0f64 * 10.0).log10(),
        (2.0f64 * 10000.0).log10(),
        n_points,
    );

    // Get PEQ frequency response in dB
    let peq_response_db = peq_spl(&freq, peq);

    // Apply perceptual weighting
    let weighted_change: f64 = freq
        .iter()
        .zip(peq_response_db.iter())
        .map(|(f, peq_db)| {
            let weight_db = match weighting {
                "a" => a_weighting_db(*f),
                "k" => k_weighting_db(*f),
                _ => 0.0, // No weighting
            };

            // Convert to linear domain for integration
            // Original: 10^(weight/20)
            // After PEQ: 10^((weight + peq)/20)
            // Ratio: 10^(peq/20)

            let weight_linear = 10.0_f64.powf(weight_db / 20.0);
            let peq_ratio = 10.0_f64.powf(*peq_db / 20.0);

            // Weighted energy change
            weight_linear * weight_linear * (peq_ratio * peq_ratio - 1.0)
        })
        .sum();

    // Average weighted energy change across frequency
    let avg_energy_change = weighted_change / n_points as f64;

    // Convert back to dB (half because we squared for energy)
    // Negative because we want to compensate (reduce if PEQ increases loudness)
    let loudness_change_db = 10.0 * (1.0 + avg_energy_change).log10();

    -loudness_change_db
}

/// Compute preamp gain for a PEQ: well adapted to computers
///
/// # Arguments
/// * `peq` - PEQ vector containing weighted biquad filters
///
/// # Returns
/// * Preamp gain in dB (negative value to prevent clipping)
pub fn peq_preamp_gain(peq: &Peq) -> f64 {
    // Generate logarithmic frequency array from 20Hz to 20kHz with 200 points
    let freq = Array1::logspace(
        10.0,
        (2.0f64 * 10.0).log10(),
        (2.0f64 * 10000.0).log10(),
        200,
    );
    let spl = peq_spl(&freq, peq);

    // Find maximum positive gain and return its negative
    let overall = spl
        .iter()
        .cloned()
        .fold(0.0f64, |acc, x| acc.max(x.max(0.0)));
    -overall
}

/// Compute preamp gain for a PEQ and look at the worst case
///
/// Note that we add 0.2 dB to have a margin for clipping
///
/// # Arguments
/// * `peq` - PEQ vector containing weighted biquad filters
///
/// # Returns
/// * Preamp gain in dB (negative value to prevent clipping)
pub fn peq_preamp_gain_max(peq: &Peq) -> f64 {
    if peq.is_empty() {
        return 0.0;
    }

    // Generate logarithmic frequency array from 20Hz to 20kHz with 200 points
    let freq = Array1::logspace(
        10.0,
        (2.0f64 * 10.0).log10(),
        (2.0f64 * 10000.0).log10(),
        200,
    );
    let spl = peq_spl(&freq, peq);

    // Find maximum individual filter contribution
    let mut individual: f64 = 0.0;
    for (_, iir) in peq {
        let single_peq = vec![(1.0, iir.clone())];
        let single_spl = peq_spl(&freq, &single_peq);
        let single_max = single_spl.iter().cloned().fold(0.0f64, |acc, x| acc.max(x));
        individual = individual.max(single_max);
    }

    // Find overall maximum positive gain
    let overall = spl
        .iter()
        .cloned()
        .fold(0.0f64, |acc, x| acc.max(x.max(0.0)));

    // Take worst case and add safety margin
    -(individual.max(overall) + 0.2)
}

/// Format PEQ as APO configuration string
///
/// # Arguments
/// * `comment` - Comment string to include at the top
/// * `peq` - PEQ vector containing weighted biquad filters
///
/// # Returns
/// * String formatted for EqualizerAPO
pub fn peq_format_apo(comment: &str, peq: &Peq) -> String {
    let mut res = Vec::new();
    res.push(comment.to_string());
    res.push(format!("Preamp: {:.1} dB", peq_preamp_gain(peq)));
    res.push(String::new());

    // Sort filters by frequency in ascending order
    let mut sorted_peq: Vec<(f64, &Biquad)> = peq.iter().map(|(_, iir)| (iir.freq, iir)).collect();
    sorted_peq.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

    for (i, (_, iir)) in sorted_peq.iter().enumerate() {
        match iir.filter_type {
            BiquadFilterType::Peak | BiquadFilterType::Notch | BiquadFilterType::Bandpass => {
                res.push(format!(
                    "Filter {:2}: ON {:2} Fc {:5} Hz Gain {:+0.2} dB Q {:0.2}",
                    i + 1,
                    iir.filter_type.short_name(),
                    iir.freq as i32,
                    iir.db_gain,
                    iir.q
                ));
            }
            BiquadFilterType::Lowpass | BiquadFilterType::Highpass => {
                if (iir.q - DEFAULT_Q_HIGH_LOW_PASS).abs() < f64::EPSILON {
                    res.push(format!(
                        "Filter {:2}: ON {:2} Fc {:5} Hz",
                        i + 1,
                        iir.filter_type.short_name(),
                        iir.freq as i32
                    ));
                } else {
                    res.push(format!(
                        "Filter {:2}: ON {:2}Q Fc {:5} Hz Q {:0.2}",
                        i + 1,
                        iir.filter_type.short_name(),
                        iir.freq as i32,
                        iir.q
                    ));
                }
            }
            BiquadFilterType::Lowshelf | BiquadFilterType::Highshelf => {
                res.push(format!(
                    "Filter {:2}: ON {:2} Fc {:5} Hz Gain {:+0.2} dB Q {:.2}",
                    i + 1,
                    iir.filter_type.short_name(),
                    iir.freq as i32,
                    iir.db_gain,
                    iir.q
                ));
            }
            BiquadFilterType::HighpassVariableQ => {
                res.push(format!(
                    "Filter {:2}: ON HPQ Fc {:5} Hz Q {:0.2}",
                    i + 1,
                    iir.freq as i32,
                    iir.q
                ));
            }
        }
    }

    res.push(String::new());
    res.join("\n")
}

/// Compute Q values for Butterworth filters
///
/// # Arguments
/// * `order` - Filter order
///
/// # Returns
/// * Vector of Q values for each biquad section
pub fn peq_butterworth_q(order: usize) -> Vec<f64> {
    let odd = !order.is_multiple_of(2);
    let mut q_values = Vec::new();

    for i in 0..order / 2 {
        let q = 2.0 * (PI / order as f64 * (i as f64 + 0.5)).sin();
        q_values.push(1.0 / q);
    }

    if odd {
        q_values.push(-1.0);
    }

    q_values
}

/// Create Butterworth lowpass filter
///
/// # Arguments
/// * `order` - Filter order
/// * `freq` - Cutoff frequency in Hz
/// * `srate` - Sample rate in Hz
///
/// # Returns
/// * PEQ containing the Butterworth lowpass filter sections
pub fn peq_butterworth_lowpass(order: usize, freq: f64, srate: f64) -> Peq {
    let q_values = peq_butterworth_q(order);
    q_values
        .into_iter()
        .map(|q| {
            (
                1.0,
                Biquad::new(BiquadFilterType::Lowpass, freq, srate, q, 0.0),
            )
        })
        .collect()
}

/// Create Butterworth highpass filter
///
/// # Arguments
/// * `order` - Filter order
/// * `freq` - Cutoff frequency in Hz
/// * `srate` - Sample rate in Hz
///
/// # Returns
/// * PEQ containing the Butterworth highpass filter sections
pub fn peq_butterworth_highpass(order: usize, freq: f64, srate: f64) -> Peq {
    let q_values = peq_butterworth_q(order);
    q_values
        .into_iter()
        .map(|q| {
            (
                1.0,
                Biquad::new(BiquadFilterType::Highpass, freq, srate, q, 0.0),
            )
        })
        .collect()
}

/// Compute Q values for Linkwitz-Riley filters
///
/// # Arguments
/// * `order` - Filter order
///
/// # Returns
/// * Vector of Q values for each biquad section
pub fn peq_linkwitzriley_q(order: usize) -> Vec<f64> {
    let q_bw = peq_butterworth_q(order / 2);
    let mut q_values = Vec::new();

    if !order.is_multiple_of(4) {
        // Odd number of pairs
        q_values.extend_from_slice(&q_bw[..q_bw.len() - 1]);
        q_values.extend_from_slice(&q_bw[..q_bw.len() - 1]);
        q_values.push(0.5);
    } else {
        // Even number of pairs
        q_values.extend_from_slice(&q_bw);
        q_values.extend_from_slice(&q_bw);
    }

    q_values
}

/// Create Linkwitz-Riley lowpass filter
///
/// # Arguments
/// * `order` - Filter order
/// * `freq` - Cutoff frequency in Hz
/// * `srate` - Sample rate in Hz
///
/// # Returns
/// * PEQ containing the Linkwitz-Riley lowpass filter sections
pub fn peq_linkwitzriley_lowpass(order: usize, freq: f64, srate: f64) -> Peq {
    let q_values = peq_linkwitzriley_q(order);
    q_values
        .into_iter()
        .map(|q| {
            (
                1.0,
                Biquad::new(BiquadFilterType::Lowpass, freq, srate, q, 0.0),
            )
        })
        .collect()
}

/// Create Linkwitz-Riley highpass filter
///
/// # Arguments
/// * `order` - Filter order
/// * `freq` - Cutoff frequency in Hz
/// * `srate` - Sample rate in Hz
///
/// # Returns
/// * PEQ containing the Linkwitz-Riley highpass filter sections
pub fn peq_linkwitzriley_highpass(order: usize, freq: f64, srate: f64) -> Peq {
    let q_values = peq_linkwitzriley_q(order);
    q_values
        .into_iter()
        .map(|q| {
            (
                1.0,
                Biquad::new(BiquadFilterType::Highpass, freq, srate, q, 0.0),
            )
        })
        .collect()
}

/// Print a formatted table of the parametric EQ filters from a Peq.
pub fn peq_print(peq: &Peq) {
    // Build filter rows from Peq
    let mut rows: Vec<FilterRow> = Vec::new();
    for (_weight, filter) in peq {
        rows.push(FilterRow {
            freq: filter.freq,
            q: filter.q,
            gain: filter.db_gain,
            kind: filter.filter_type.short_name(),
        });
    }

    // Sort by frequency
    rows.sort_by(|a, b| {
        a.freq
            .partial_cmp(&b.freq)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    println!("+-# -|-Freq (Hz)--|-Q ---------|-Gain (dB)--|-Type-----+");
    for (i, r) in rows.iter().enumerate() {
        println!(
            "| {:<2} | {:<10.2} | {:<10.3} | {:<+10.3} | {:<8} |",
            i + 1,
            r.freq,
            r.q,
            r.gain,
            r.kind
        );
    }
    println!("+----|------------|------------|------------|----------+");
}

#[cfg(test)]
mod peq_tests {
    use super::*;
    use ndarray::array;

    #[test]
    fn test_peq_equal() {
        let bq1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq2 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq3 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 4.0);

        let peq1 = vec![(1.0, bq1.clone()), (0.5, bq2.clone())];
        let peq2 = vec![(1.0, bq1), (0.5, bq2)];
        let peq3 = vec![(1.0, bq3)];

        assert!(peq_equal(&peq1, &peq2));
        assert!(!peq_equal(&peq1, &peq3));
        assert!(!peq_equal(&peq1, &vec![]));
    }

    #[test]
    fn test_peq_spl() {
        let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 6.0);
        let peq = vec![(1.0, bq)];
        let freq = array![100.0, 1000.0, 10000.0];

        let spl = peq_spl(&freq, &peq);

        // Should have gain close to 6 dB at 1kHz
        assert!(spl[1] > 5.0 && spl[1] < 7.0);
        // Should have less gain at other frequencies
        assert!(spl[0].abs() < 1.0);
        assert!(spl[2].abs() < 1.0);
    }

    #[test]
    fn test_peq_preamp_gain() {
        let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 6.0);
        let peq = vec![(1.0, bq)];

        let gain = peq_preamp_gain(&peq);

        // Should be negative to prevent clipping
        assert!(gain < 0.0);
        // Should be around -6 dB to compensate for the +6 dB boost
        assert!(gain > -7.0 && gain < -5.0);
    }

    #[test]
    fn test_peq_format_apo() {
        let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let peq = vec![(1.0, bq)];

        let apo_str = peq_format_apo("Test EQ", &peq);

        assert!(apo_str.contains("Test EQ"));
        assert!(apo_str.contains("Preamp:"));
        assert!(apo_str.contains("Filter  1:"));
        assert!(apo_str.contains("PK"));
        assert!(apo_str.contains("1000 Hz"));
        assert!(apo_str.contains("+3.00 dB"));
    }

    #[test]
    fn test_butterworth_q() {
        let q_values = peq_butterworth_q(4);
        assert_eq!(q_values.len(), 2);

        // For 4th order Butterworth, should have specific Q values
        assert!((q_values[0] - 1.3065630).abs() < 1e-6);
        assert!((q_values[1] - 0.5411961).abs() < 1e-6);
    }

    #[test]
    fn test_butterworth_filters() {
        let lp = peq_butterworth_lowpass(4, 1000.0, 48000.0);
        let hp = peq_butterworth_highpass(4, 1000.0, 48000.0);

        assert_eq!(lp.len(), 2);
        assert_eq!(hp.len(), 2);

        // All filters should have weight 1.0 and correct type
        for (weight, bq) in &lp {
            assert_eq!(*weight, 1.0);
            assert_eq!(bq.filter_type, BiquadFilterType::Lowpass);
            assert_eq!(bq.freq, 1000.0);
        }

        for (weight, bq) in &hp {
            assert_eq!(*weight, 1.0);
            assert_eq!(bq.filter_type, BiquadFilterType::Highpass);
            assert_eq!(bq.freq, 1000.0);
        }
    }

    #[test]
    fn test_linkwitzriley_filters() {
        let lp = peq_linkwitzriley_lowpass(4, 1000.0, 48000.0);
        let hp = peq_linkwitzriley_highpass(4, 1000.0, 48000.0);

        // 4th order LR should have 2 sections (each with Q = 0.7071...)
        assert_eq!(lp.len(), 2);
        assert_eq!(hp.len(), 2);

        // All should be unit weight
        for (weight, _) in &lp {
            assert_eq!(*weight, 1.0);
        }
        for (weight, _) in &hp {
            assert_eq!(*weight, 1.0);
        }
    }
}

// ----------------------------------------------------------------------
// RME Format Functions
// ----------------------------------------------------------------------

/// Convert BiquadFilterType to RME format code
///
/// # Arguments
/// * `filter_type` - The biquad filter type
/// * `pos` - The position (1-based index) of the filter in the chain
///
/// # Returns
/// * RME type code as f64, or -1.0 if unsupported
///
/// # Notes
/// RME format codes depend on both filter type and position:
/// - PK (Peak): 0.0
/// - LP (Lowpass): 3.0 at pos 1, 2.0 at pos 3 or 9
/// - HP (Highpass): 2.0 at pos 1, 3.0 at pos 3 or 9
/// - LS/HS (Lowshelf/Highshelf): 1.0 at pos 1, 3, or 9
fn biquad_to_rme_type(filter_type: BiquadFilterType, pos: usize) -> f64 {
    match filter_type {
        BiquadFilterType::Peak => 0.0,
        BiquadFilterType::Lowpass => {
            if pos == 1 {
                3.0
            } else if pos == 3 || pos == 9 {
                2.0
            } else {
                -1.0
            }
        }
        BiquadFilterType::Highpass | BiquadFilterType::HighpassVariableQ => {
            if pos == 1 {
                2.0
            } else if pos == 3 || pos == 9 {
                3.0
            } else {
                -1.0
            }
        }
        BiquadFilterType::Lowshelf | BiquadFilterType::Highshelf => {
            if pos == 1 || pos == 3 || pos == 9 {
                1.0
            } else {
                -1.0
            }
        }
        _ => -1.0,
    }
}

/// Format PEQ as RME TotalMix channel preset XML
///
/// # Arguments
/// * `peq` - PEQ vector containing weighted biquad filters
///
/// # Returns
/// * String formatted as RME TotalMix channel preset XML
///
/// # Notes
/// Generates XML in the format expected by RME TotalMix channel EQ (max 3 bands).
/// Includes LC Grade and LC Freq defaults, followed by Band parameters for
/// frequency, Q, and gain, then Band Type specifications.
pub fn peq_format_rme_channel(peq: &Peq) -> String {
    #[allow(clippy::vec_init_then_push)]
    let mut lines = vec![
        "<Preset>".to_string(),
        "  <Equalizer>".to_string(),
        "    <Params>".to_string(),
        "\t<val e=\"LC Grade\" v=\"1.00,\"/>".to_string(),
        "\t<val e=\"LC Freq\" v=\"20.00,\"/>".to_string(),
    ];

    // Add Band parameters (freq, Q, gain)
    for (i, (_, biquad)) in peq.iter().enumerate() {
        lines.push(format!(
            "      <val e=\"Band{} Freq\" v=\"{:7.2},\"/>",
            i + 1,
            biquad.freq
        ));
        lines.push(format!(
            "      <val e=\"Band{} Q\" v=\"{:4.2},\"/>",
            i + 1,
            biquad.q
        ));
        lines.push(format!(
            "        <val e=\"Band{} Gain\" v=\"{:4.2},\"/>",
            i + 1,
            biquad.db_gain
        ));
    }

    // Add Band types
    for (i, (_, biquad)) in peq.iter().enumerate() {
        let rme_type = biquad_to_rme_type(biquad.filter_type, i + 1);
        if rme_type >= 0.0 {
            lines.push(format!(
                "        <val e=\"Band{} Type\" v=\"{:4.2},\"/>",
                i + 1,
                rme_type
            ));
        }
    }

    lines.push("    </Params>".to_string());
    lines.push("  </Equalizer>".to_string());
    lines.push("</Preset>".to_string());

    lines.join("\n")
}

// ----------------------------------------------------------------------
// RME TotalMix Room EQ Format Functions
// ----------------------------------------------------------------------

/// Get priority for filter type (higher number = higher priority)
///
/// # Arguments
/// * `filter_type` - The type of filter
///
/// # Returns
/// * Priority value (higher = more important to keep)
///
/// # Notes
/// Priority levels:
/// - Lowshelf/Highshelf: 9 (important for overall curve)
/// - Lowpass/Highpass: 7 (medium priority)
/// - Bandpass: 5 (lower priority)
/// - Peak: 3 (lowest priority, most common)
/// - Default: 1
#[allow(dead_code)]
fn get_filter_priority(filter_type: BiquadFilterType) -> u8 {
    match filter_type {
        BiquadFilterType::Lowshelf | BiquadFilterType::Highshelf => 9,
        BiquadFilterType::Lowpass => 7,
        BiquadFilterType::Highpass | BiquadFilterType::HighpassVariableQ => 7,
        BiquadFilterType::Bandpass => 5,
        BiquadFilterType::Peak => 3,
        _ => 1,
    }
}

/// Filter PEQs while preserving order and prioritizing important filter types
///
/// # Arguments
/// * `peqs` - List of PEQ items in original order
/// * `max_count` - Maximum number of PEQ items to keep
///
/// # Returns
/// * List of PEQ items in original order, limited to max_count, with low-priority/low-gain filters removed
///
/// # Notes
/// If the input has fewer than or equal to max_count items, returns a clone.
/// Otherwise, sorts by priority (descending) then by absolute gain (descending),
/// takes the top max_count items, and returns them in their original order.
#[allow(dead_code)]
fn filter_peqs_by_gain(peqs: &Peq, max_count: usize) -> Peq {
    if peqs.len() <= max_count {
        return peqs.clone();
    }

    // Create list of (index, peq_item, priority, abs_gain) for sorting
    let mut indexed_peqs: Vec<(usize, &(f64, Biquad), u8, f64)> = peqs
        .iter()
        .enumerate()
        .map(|(i, item)| {
            let priority = get_filter_priority(item.1.filter_type);
            let abs_gain = item.1.db_gain.abs();
            (i, item, priority, abs_gain)
        })
        .collect();

    // Sort by priority (descending) then by absolute gain (descending)
    // This puts high-priority, high-gain filters first
    indexed_peqs.sort_by(|a, b| {
        b.2.cmp(&a.2) // Compare priority (descending)
            .then_with(|| b.3.partial_cmp(&a.3).unwrap_or(std::cmp::Ordering::Equal))
        // Then abs_gain (descending)
    });

    // Take the top max_count filters
    let mut selected: Vec<(usize, (f64, Biquad))> = indexed_peqs
        .into_iter()
        .take(max_count)
        .map(|(idx, item, _, _)| (idx, item.clone()))
        .collect();

    // Sort selected filters back to original order by index
    selected.sort_by_key(|(idx, _)| *idx);

    // Extract just the PEQ items
    selected.into_iter().map(|(_, item)| item).collect()
}

/// Enforce RME room EQ constraints
///
/// # Arguments
/// * `peqs` - List of PEQ items
///
/// # Returns
/// * List of exactly 9 PEQ items with RME room EQ constraints applied:
///   - Position 1 can be: PK, LS, HS, LP, or HP (lowest freq non-PK preferred)
///   - Positions 2-8 can only be PK
///   - Position 9 can be: PK, LS, HS, LP, or HP (highest freq non-PK preferred)
///   - Missing slots filled with zero-gain PK filters at 1kHz
///
/// # Notes
/// RME room EQ hardware constraints:
/// - Total of 9 bands maximum
/// - Only positions 1 and 9 support non-PK filter types
/// - If more than 2 non-PK filters exist, picks lowest and highest frequency
fn enforce_rme_room_filter_constraints(peqs: &Peq) -> Peq {
    // Separate filters by category
    let mut pk_filters: Vec<(f64, Biquad)> = Vec::new();
    let mut non_pk_filters: Vec<(f64, Biquad)> = Vec::new();

    for item in peqs {
        match item.1.filter_type {
            BiquadFilterType::Peak => pk_filters.push(item.clone()),
            BiquadFilterType::Lowshelf
            | BiquadFilterType::Highshelf
            | BiquadFilterType::Lowpass
            | BiquadFilterType::Highpass
            | BiquadFilterType::HighpassVariableQ => non_pk_filters.push(item.clone()),
            _ => {
                // Convert unsupported types to PK
                eprintln!(
                    "Warning: Filter type {:?} not supported by RME room EQ, converting to PK",
                    item.1.filter_type
                );
                let mut converted = item.1.clone();
                converted.filter_type = BiquadFilterType::Peak;
                pk_filters.push((item.0, converted));
            }
        }
    }

    // Select non-PK filters for positions 1 and 9
    let mut selected_low: Option<(f64, Biquad)> = None;
    let mut selected_high: Option<(f64, Biquad)> = None;

    if non_pk_filters.len() > 2 {
        eprintln!(
            "Warning: RME room EQ supports at most 2 non-PK filters (positions 1 and 9). \
             Found {} non-PK filters. Selecting lowest and highest frequency filters.",
            non_pk_filters.len()
        );
    }

    if !non_pk_filters.is_empty() {
        // Sort non-PK filters by frequency
        let mut sorted_non_pk = non_pk_filters.clone();
        sorted_non_pk.sort_by(|a, b| {
            a.1.freq
                .partial_cmp(&b.1.freq)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Select lowest frequency for position 1
        selected_low = Some(sorted_non_pk[0].clone());

        // If we have more than one non-PK, select highest frequency for position 9
        if sorted_non_pk.len() > 1 {
            selected_high = Some(sorted_non_pk[sorted_non_pk.len() - 1].clone());
        }
    }

    // Build the result with exactly 9 bands
    let mut result: Vec<(f64, Biquad)> = Vec::new();

    // Position 1: non-PK (if available) or first PK
    if let Some(low) = selected_low {
        result.push(low);
    } else if !pk_filters.is_empty() {
        result.push(pk_filters.remove(0));
    } else {
        // Create dummy zero-gain PK filter
        result.push((
            1.0,
            Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 0.0),
        ));
    }

    // Positions 2-8: PK filters only (7 slots)
    let mut middle_count = 0;
    while middle_count < 7 {
        if !pk_filters.is_empty() {
            result.push(pk_filters.remove(0));
        } else {
            // Fill with zero-gain PK filters
            result.push((
                1.0,
                Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 0.0),
            ));
        }
        middle_count += 1;
    }

    // Position 9: non-PK (if available) or PK
    if let Some(high) = selected_high {
        result.push(high);
    } else if !pk_filters.is_empty() {
        result.push(pk_filters.remove(0));
    } else {
        // Create dummy zero-gain PK filter
        result.push((
            1.0,
            Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 0.0),
        ));
    }

    // Warn if we had to drop PK filters
    if !pk_filters.is_empty() {
        eprintln!(
            "Warning: {} PK filters were dropped due to 9-band limit",
            pk_filters.len()
        );
    }

    result
}

/// Format PEQ as RME TotalMix room EQ preset XML (dual channel)
///
/// # Arguments
/// * `left` - PEQ vector for left channel
/// * `right` - PEQ vector for right channel (if empty, left channel is used for both)
///
/// # Returns
/// * String formatted as RME TotalMix room EQ preset XML
///
/// # Notes
/// Generates XML in the format expected by RME TotalMix room EQ.
/// Always outputs exactly 9 bands per channel with RME constraints:
/// - Position 1 can be: PK, LS, HS, LP, or HP (lowest freq non-PK preferred)
/// - Positions 2-8 can only be PK
/// - Position 9 can be: PK, LS, HS, LP, or HP (highest freq non-PK preferred)
pub fn peq_format_rme_room(left: &Peq, right: &Peq) -> String {
    // Apply RME room EQ constraints - returns exactly 9 bands
    let left_constrained = enforce_rme_room_filter_constraints(left);
    let right_constrained = if !right.is_empty() {
        enforce_rme_room_filter_constraints(right)
    } else {
        left_constrained.clone()
    };

    let mut lines = Vec::new();

    // Helper closure to process a channel's PEQ items
    let process_channel = |peqs: &Peq, lines: &mut Vec<String>| {
        // Add frequency, Q, and gain parameters
        for (i, (_, biquad)) in peqs.iter().enumerate() {
            lines.push(format!(
                "        <val e=\"REQ Band{} Freq\" v=\"{:7.2},\"/>",
                i + 1,
                biquad.freq
            ));
            lines.push(format!(
                "        <val e=\"REQ Band{} Q\" v=\"{:4.2},\"/>",
                i + 1,
                biquad.q
            ));
            lines.push(format!(
                "        <val e=\"REQ Band{} Gain\" v=\"{:4.2},\"/>",
                i + 1,
                biquad.db_gain
            ));
        }

        // Add type parameters
        for (i, (_, biquad)) in peqs.iter().enumerate() {
            let rme_type = biquad_to_rme_type(biquad.filter_type, i + 1);
            if rme_type >= 0.0 {
                lines.push(format!(
                    "        <val e=\"REQ Band{} Type\" v=\"{:4.2},\"/>",
                    i + 1,
                    rme_type
                ));
            }
        }
    };

    lines.push("<Preset>".to_string());

    // Process left channel
    let preamp_gain = 0.0;
    lines.push("  <Room EQ L>".to_string());
    lines.push("    <Params>".to_string());
    lines.push("\t<val e=\"REQ Delay\" v=\"0.00,\"/>".to_string());
    process_channel(&left_constrained, &mut lines);
    lines.push(format!(
        "\t<val e=\"REQ Chan Gain\" v=\"{},\"/>",
        preamp_gain
    ));
    lines.push("    </Params>".to_string());
    lines.push("  </Room EQ L>".to_string());

    // Process right channel (use left if right is empty)
    lines.push("  <Room EQ R>".to_string());
    lines.push("    <Params>".to_string());
    lines.push("\t<val e=\"REQ Delay\" v=\"0.00,\"/>".to_string());
    if !right_constrained.is_empty() {
        process_channel(&right_constrained, &mut lines);
    } else {
        process_channel(&left_constrained, &mut lines);
    }
    lines.push(format!(
        "\t<val e=\"REQ Chan Gain\" v=\"{},\"/>",
        preamp_gain
    ));
    lines.push("    </Params>".to_string());
    lines.push("  </Room EQ R>".to_string());

    lines.push("</Preset>".to_string());

    lines.join("\n")
}

// ----------------------------------------------------------------------
// Apple AUNBandEQ (aupreset) Format Functions
// ----------------------------------------------------------------------

// Apple AUNBandEQ parameter constants
const K_AUNBANDEQ_PARAM_BYPASS_BAND: i32 = 1000;
const K_AUNBANDEQ_PARAM_FILTER_TYPE: i32 = 2000;
const K_AUNBANDEQ_PARAM_FREQUENCY: i32 = 3000;
const K_AUNBANDEQ_PARAM_GAIN: i32 = 4000;
const K_AUNBANDEQ_PARAM_BANDWIDTH: i32 = 5000;

// Apple AUNBandEQ filter type constants
const K_AUNBANDEQ_FILTER_TYPE_PARAMETRIC: i32 = 0;
#[allow(dead_code)]
const K_AUNBANDEQ_FILTER_TYPE_2ND_ORDER_BUTTERWORTH_LOW_PASS: i32 = 1;
#[allow(dead_code)]
const K_AUNBANDEQ_FILTER_TYPE_2ND_ORDER_BUTTERWORTH_HIGH_PASS: i32 = 2;
const K_AUNBANDEQ_FILTER_TYPE_RESONANT_LOW_PASS: i32 = 3;
const K_AUNBANDEQ_FILTER_TYPE_RESONANT_HIGH_PASS: i32 = 4;
const K_AUNBANDEQ_FILTER_TYPE_BAND_PASS: i32 = 5;
const K_AUNBANDEQ_FILTER_TYPE_LOW_SHELF: i32 = 7;
const K_AUNBANDEQ_FILTER_TYPE_HIGH_SHELF: i32 = 8;

/// Convert BiquadFilterType to Apple AUNBandEQ filter type constant
///
/// # Arguments
/// * `filter_type` - The biquad filter type
///
/// # Returns
/// * Apple AUNBandEQ filter type constant, or -1 if unsupported
fn biquad_to_apple_type(filter_type: BiquadFilterType) -> i32 {
    match filter_type {
        BiquadFilterType::Peak => K_AUNBANDEQ_FILTER_TYPE_PARAMETRIC,
        BiquadFilterType::Highshelf => K_AUNBANDEQ_FILTER_TYPE_HIGH_SHELF,
        BiquadFilterType::Lowshelf => K_AUNBANDEQ_FILTER_TYPE_LOW_SHELF,
        BiquadFilterType::Highpass | BiquadFilterType::HighpassVariableQ => {
            K_AUNBANDEQ_FILTER_TYPE_RESONANT_HIGH_PASS
        }
        BiquadFilterType::Lowpass => K_AUNBANDEQ_FILTER_TYPE_RESONANT_LOW_PASS,
        BiquadFilterType::Bandpass => K_AUNBANDEQ_FILTER_TYPE_BAND_PASS,
        _ => -1,
    }
}

/// Format PEQ as Apple AUNBandEQ preset (aupreset) plist XML
///
/// # Arguments
/// * `peq` - PEQ vector containing weighted biquad filters
/// * `name` - Name for the preset
///
/// # Returns
/// * String formatted as Apple AUNBandEQ preset plist XML
///
/// # Notes
/// Generates a plist XML file containing base64-encoded binary data
/// in the format expected by Apple's AUNBandEQ audio unit.
/// Supports up to 16 bands with parameters for bypass, type, frequency,
/// gain, and bandwidth.
pub fn peq_format_aupreset(peq: &Peq, name: &str) -> String {
    let len_peq = peq.len().min(16); // Max 16 bands for Apple
    let preamp_gain = peq_preamp_gain(peq);

    // Build binary data structure
    let mut buffer = Vec::new();

    // Header: 5 values (4 integers + 1 float)
    // Structure: [0, 0, ndata (81), 0, preamp_gain]
    buffer.write_i32::<BigEndian>(0).unwrap();
    buffer.write_i32::<BigEndian>(0).unwrap();
    buffer.write_i32::<BigEndian>(81).unwrap(); // ndata is always 81
    buffer.write_i32::<BigEndian>(0).unwrap();
    buffer.write_f32::<BigEndian>(preamp_gain as f32).unwrap();

    // Create parameter map
    let mut params = std::collections::BTreeMap::new();

    // Add parameters for each band
    for (i, (_, biquad)) in peq.iter().take(16).enumerate() {
        let idx = i as i32;
        params.insert(K_AUNBANDEQ_PARAM_BYPASS_BAND + idx, 0.0f32); // 0.0 = enabled
        params.insert(
            K_AUNBANDEQ_PARAM_FILTER_TYPE + idx,
            biquad_to_apple_type(biquad.filter_type) as f32,
        );
        params.insert(K_AUNBANDEQ_PARAM_FREQUENCY + idx, biquad.freq as f32);
        params.insert(K_AUNBANDEQ_PARAM_GAIN + idx, biquad.db_gain as f32);
        params.insert(K_AUNBANDEQ_PARAM_BANDWIDTH + idx, q2bw(biquad.q) as f32);
    }

    // Fill remaining bands (up to 16) with disabled/zero values
    for i in len_peq..16 {
        let idx = i as i32;
        params.insert(K_AUNBANDEQ_PARAM_BYPASS_BAND + idx, 1.0f32); // 1.0 = disabled
        params.insert(K_AUNBANDEQ_PARAM_FILTER_TYPE + idx, 0.0f32);
        params.insert(K_AUNBANDEQ_PARAM_FREQUENCY + idx, 0.0f32);
        params.insert(K_AUNBANDEQ_PARAM_GAIN + idx, 0.0f32);
        params.insert(K_AUNBANDEQ_PARAM_BANDWIDTH + idx, 0.0f32);
    }

    // Write parameters in sorted order (param_id, value) pairs
    for (param_id, value) in params.iter() {
        buffer.write_i32::<BigEndian>(*param_id).unwrap();
        buffer.write_f32::<BigEndian>(*value).unwrap();
    }

    // Base64 encode the buffer
    let b64_text = general_purpose::STANDARD.encode(&buffer);

    // Format as chunks of 68 characters with tabs
    let chunk_size = 68;
    let mut data_lines = Vec::new();
    for chunk in b64_text.as_bytes().chunks(chunk_size) {
        data_lines.push(format!("\t{}", String::from_utf8_lossy(chunk)));
    }
    let data_section = data_lines.join("\n");

    // Build the plist XML
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>ParametricType</key>
	<integer>11</integer>
	<key>data</key>
	<data>
{}
	</data>
	<key>manufacturer</key>
	<integer>1634758764</integer>
	<key>name</key>
	<string>{}</string>
	<key>numberOfBands</key>
	<integer>{}</integer>
	<key>subtype</key>
	<integer>1851942257</integer>
	<key>type</key>
	<integer>1635083896</integer>
	<key>version</key>
	<integer>0</integer>
</dict>
</plist>
"#,
        data_section, name, len_peq
    )
}

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

    #[test]
    fn test_peq_format_rme_channel_single_peak() {
        let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let peq = vec![(1.0, bq)];
        let rme_str = peq_format_rme_channel(&peq);

        // Verify structure
        assert!(rme_str.contains("<Preset>"));
        assert!(rme_str.contains("<Equalizer>"));
        assert!(rme_str.contains("<Params>"));
        assert!(rme_str.contains("LC Grade"));
        assert!(rme_str.contains("LC Freq"));
        assert!(rme_str.contains("Band1 Freq"));
        assert!(rme_str.contains("Band1 Q"));
        assert!(rme_str.contains("Band1 Gain"));
        assert!(rme_str.contains("Band1 Type"));
        assert!(rme_str.contains("</Preset>"));

        // Peak filter should have type 0.0
        assert!(rme_str.contains("0.00"));
    }

    #[test]
    fn test_peq_format_rme_channel_empty() {
        let peq: Peq = vec![];
        let rme_str = peq_format_rme_channel(&peq);

        // Should still have basic structure
        assert!(rme_str.contains("<Preset>"));
        assert!(rme_str.contains("<Equalizer>"));
        assert!(rme_str.contains("LC Grade"));
        assert!(rme_str.contains("</Preset>"));
    }

    #[test]
    fn test_peq_format_rme_channel_multiple_bands() {
        let bq1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq2 = Biquad::new(BiquadFilterType::Peak, 2000.0, 48000.0, 2.0, -2.0);
        let peq = vec![(1.0, bq1), (1.0, bq2)];
        let rme_str = peq_format_rme_channel(&peq);

        assert!(rme_str.contains("Band1 Freq"));
        assert!(rme_str.contains("Band2 Freq"));
        assert!(rme_str.contains("Band1 Type"));
        assert!(rme_str.contains("Band2 Type"));
    }

    #[test]
    fn test_peq_format_aupreset_single_peak() {
        let bq = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let peq = vec![(1.0, bq)];
        let aupreset_str = peq_format_aupreset(&peq, "Test EQ");

        // Verify plist structure
        assert!(aupreset_str.contains("<?xml version="));
        assert!(aupreset_str.contains("<!DOCTYPE plist"));
        assert!(aupreset_str.contains("<plist version=\"1.0\">"));
        assert!(aupreset_str.contains("<dict>"));
        assert!(aupreset_str.contains("<key>ParametricType</key>"));
        assert!(aupreset_str.contains("<key>data</key>"));
        assert!(aupreset_str.contains("<data>"));
        assert!(aupreset_str.contains("<key>name</key>"));
        assert!(aupreset_str.contains("<string>Test EQ</string>"));
        assert!(aupreset_str.contains("<key>numberOfBands</key>"));
        assert!(aupreset_str.contains("<integer>1</integer>"));
        assert!(aupreset_str.contains("</plist>"));
    }

    #[test]
    fn test_peq_format_aupreset_empty() {
        let peq: Peq = vec![];
        let aupreset_str = peq_format_aupreset(&peq, "Empty EQ");

        // Should still generate valid plist
        assert!(aupreset_str.contains("<?xml version="));
        assert!(aupreset_str.contains("<string>Empty EQ</string>"));
        assert!(aupreset_str.contains("<integer>0</integer>"));
    }

    #[test]
    fn test_peq_format_aupreset_multiple_bands() {
        let bq1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq2 = Biquad::new(BiquadFilterType::Highshelf, 8000.0, 48000.0, 0.7, 2.0);
        let bq3 = Biquad::new(BiquadFilterType::Lowshelf, 100.0, 48000.0, 0.7, -1.0);
        let peq = vec![(1.0, bq1), (1.0, bq2), (1.0, bq3)];
        let aupreset_str = peq_format_aupreset(&peq, "Multi Band EQ");

        assert!(aupreset_str.contains("<string>Multi Band EQ</string>"));
        assert!(aupreset_str.contains("<integer>3</integer>"));
        // Should have base64 encoded data
        assert!(aupreset_str.contains("<data>"));
    }

    #[test]
    fn test_peq_format_aupreset_max_bands() {
        // Test with more than 16 bands (should cap at 16)
        let mut peq = Vec::new();
        for i in 0..20 {
            let freq = 100.0 + (i as f64 * 100.0);
            let bq = Biquad::new(BiquadFilterType::Peak, freq, 48000.0, 1.0, 1.0);
            peq.push((1.0, bq));
        }
        let aupreset_str = peq_format_aupreset(&peq, "Max Bands EQ");

        // Should cap at 16 bands
        assert!(aupreset_str.contains("<integer>16</integer>"));
    }

    #[test]
    fn test_biquad_to_apple_type() {
        assert_eq!(
            biquad_to_apple_type(BiquadFilterType::Peak),
            K_AUNBANDEQ_FILTER_TYPE_PARAMETRIC
        );
        assert_eq!(
            biquad_to_apple_type(BiquadFilterType::Highshelf),
            K_AUNBANDEQ_FILTER_TYPE_HIGH_SHELF
        );
        assert_eq!(
            biquad_to_apple_type(BiquadFilterType::Lowshelf),
            K_AUNBANDEQ_FILTER_TYPE_LOW_SHELF
        );
        assert_eq!(
            biquad_to_apple_type(BiquadFilterType::Highpass),
            K_AUNBANDEQ_FILTER_TYPE_RESONANT_HIGH_PASS
        );
        assert_eq!(
            biquad_to_apple_type(BiquadFilterType::Lowpass),
            K_AUNBANDEQ_FILTER_TYPE_RESONANT_LOW_PASS
        );
        assert_eq!(
            biquad_to_apple_type(BiquadFilterType::Bandpass),
            K_AUNBANDEQ_FILTER_TYPE_BAND_PASS
        );
    }

    #[test]
    fn test_biquad_to_rme_type() {
        // Peak should always be 0.0
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Peak, 1), 0.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Peak, 2), 0.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Peak, 3), 0.0);

        // Lowpass position-dependent
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowpass, 1), 3.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowpass, 3), 2.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowpass, 9), 2.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowpass, 2), -1.0);

        // Highpass position-dependent
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highpass, 1), 2.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highpass, 3), 3.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highpass, 9), 3.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highpass, 2), -1.0);

        // Lowshelf position-dependent
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowshelf, 1), 1.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowshelf, 3), 1.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowshelf, 9), 1.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Lowshelf, 2), -1.0);

        // Highshelf position-dependent
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highshelf, 1), 1.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highshelf, 3), 1.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highshelf, 9), 1.0);
        assert_eq!(biquad_to_rme_type(BiquadFilterType::Highshelf, 2), -1.0);
    }

    #[test]
    fn test_get_filter_priority() {
        assert_eq!(get_filter_priority(BiquadFilterType::Lowshelf), 9);
        assert_eq!(get_filter_priority(BiquadFilterType::Highshelf), 9);
        assert_eq!(get_filter_priority(BiquadFilterType::Lowpass), 7);
        assert_eq!(get_filter_priority(BiquadFilterType::Highpass), 7);
        assert_eq!(get_filter_priority(BiquadFilterType::HighpassVariableQ), 7);
        assert_eq!(get_filter_priority(BiquadFilterType::Bandpass), 5);
        assert_eq!(get_filter_priority(BiquadFilterType::Peak), 3);
        assert_eq!(get_filter_priority(BiquadFilterType::Notch), 1);
    }

    #[test]
    fn test_filter_peqs_by_gain_under_limit() {
        let bq1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq2 = Biquad::new(BiquadFilterType::Peak, 2000.0, 48000.0, 1.0, 2.0);
        let peq = vec![(1.0, bq1), (1.0, bq2)];
        let filtered = filter_peqs_by_gain(&peq, 5);
        assert_eq!(filtered.len(), 2);
    }

    #[test]
    fn test_filter_peqs_by_gain_over_limit() {
        // Create 12 peak filters with varying gains
        let mut peq = Vec::new();
        for i in 0..12 {
            let gain = (i as f64) * 0.5 + 0.5; // gains from 0.5 to 6.0
            let bq = Biquad::new(
                BiquadFilterType::Peak,
                1000.0 + (i as f64 * 100.0),
                48000.0,
                1.0,
                gain,
            );
            peq.push((1.0, bq));
        }
        let filtered = filter_peqs_by_gain(&peq, 9);
        assert_eq!(filtered.len(), 9);
        // Should keep the ones with higher gains (indices 3-11)
        // First frequency should be from index 3 (1300 Hz)
        assert!((filtered[0].1.freq - 1300.0).abs() < 1.0);
    }

    #[test]
    fn test_filter_peqs_by_gain_priority() {
        // Mix of different filter types with varying gains
        let mut peq = Vec::new();
        // Add 6 peaks with high gains
        for i in 0..6 {
            let bq = Biquad::new(
                BiquadFilterType::Peak,
                1000.0 + (i as f64 * 100.0),
                48000.0,
                1.0,
                5.0,
            );
            peq.push((1.0, bq));
        }
        // Add 2 lowshelf with lower gain but higher priority
        let ls1 = Biquad::new(BiquadFilterType::Lowshelf, 100.0, 48000.0, 0.7, 2.0);
        let ls2 = Biquad::new(BiquadFilterType::Lowshelf, 120.0, 48000.0, 0.7, 2.5);
        peq.push((1.0, ls1));
        peq.push((1.0, ls2));
        // Add 3 more peaks
        for i in 6..9 {
            let bq = Biquad::new(
                BiquadFilterType::Peak,
                1000.0 + (i as f64 * 100.0),
                48000.0,
                1.0,
                4.0,
            );
            peq.push((1.0, bq));
        }
        let filtered = filter_peqs_by_gain(&peq, 9);
        assert_eq!(filtered.len(), 9);
        // Both lowshelf filters should be kept due to higher priority
        let lowshelf_count = filtered
            .iter()
            .filter(|(_, bq)| bq.filter_type == BiquadFilterType::Lowshelf)
            .count();
        assert_eq!(lowshelf_count, 2);
    }

    #[test]
    fn test_enforce_rme_room_filter_constraints_empty() {
        let peq: Peq = vec![];
        let result = enforce_rme_room_filter_constraints(&peq);
        // Should always return exactly 9 bands (filled with zero-gain PK)
        assert_eq!(result.len(), 9);
        // All should be zero-gain PK filters
        for (_, bq) in &result {
            assert_eq!(bq.filter_type, BiquadFilterType::Peak);
            assert_eq!(bq.db_gain, 0.0);
        }
    }

    #[test]
    fn test_enforce_rme_room_filter_constraints_no_shelves() {
        let bq1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq2 = Biquad::new(BiquadFilterType::Peak, 2000.0, 48000.0, 1.0, 2.0);
        let peq = vec![(1.0, bq1), (1.0, bq2)];
        let result = enforce_rme_room_filter_constraints(&peq);
        // Should always return exactly 9 bands
        assert_eq!(result.len(), 9);
        // First 2 should be the input PK filters
        assert!((result[0].1.freq - 1000.0).abs() < 1.0);
        assert!((result[1].1.freq - 2000.0).abs() < 1.0);
        // Rest should be zero-gain PK filters
        for i in 2..9 {
            assert_eq!(result[i].1.filter_type, BiquadFilterType::Peak);
            assert_eq!(result[i].1.db_gain, 0.0);
        }
    }

    #[test]
    fn test_enforce_rme_room_filter_constraints_single_lowshelf() {
        let ls = Biquad::new(BiquadFilterType::Lowshelf, 100.0, 48000.0, 0.7, 2.0);
        let pk1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let pk2 = Biquad::new(BiquadFilterType::Peak, 2000.0, 48000.0, 1.0, 2.0);
        let peq = vec![(1.0, pk1), (1.0, ls.clone()), (1.0, pk2)];
        let result = enforce_rme_room_filter_constraints(&peq);
        // Should always return exactly 9 bands
        assert_eq!(result.len(), 9);
        // Lowshelf should be in position 1 (lowest freq non-PK)
        assert_eq!(result[0].1.filter_type, BiquadFilterType::Lowshelf);
        assert!((result[0].1.freq - 100.0).abs() < 1.0);
        // PK filters in positions 2-9
        for i in 1..9 {
            assert_eq!(result[i].1.filter_type, BiquadFilterType::Peak);
        }
    }

    #[test]
    fn test_enforce_rme_room_filter_constraints_multiple_lowshelf() {
        let ls1 = Biquad::new(BiquadFilterType::Lowshelf, 100.0, 48000.0, 0.7, 2.0);
        let ls2 = Biquad::new(BiquadFilterType::Lowshelf, 120.0, 48000.0, 0.7, 4.0);
        let pk = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let peq = vec![(1.0, ls1), (1.0, pk), (1.0, ls2)];
        let result = enforce_rme_room_filter_constraints(&peq);
        // Should always return exactly 9 bands
        assert_eq!(result.len(), 9);
        // Position 1: lowest freq lowshelf (ls1 at 100Hz)
        assert_eq!(result[0].1.filter_type, BiquadFilterType::Lowshelf);
        assert!((result[0].1.freq - 100.0).abs() < 1.0);
        // Position 9: highest freq lowshelf (ls2 at 120Hz)
        assert_eq!(result[8].1.filter_type, BiquadFilterType::Lowshelf);
        assert!((result[8].1.freq - 120.0).abs() < 1.0);
    }

    #[test]
    fn test_enforce_rme_room_filter_constraints_multiple_highshelf() {
        let hs1 = Biquad::new(BiquadFilterType::Highshelf, 8000.0, 48000.0, 0.7, 1.5);
        let hs2 = Biquad::new(BiquadFilterType::Highshelf, 10000.0, 48000.0, 0.7, 3.0);
        let pk = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let peq = vec![(1.0, pk), (1.0, hs1), (1.0, hs2)];
        let result = enforce_rme_room_filter_constraints(&peq);
        // Should always return exactly 9 bands
        assert_eq!(result.len(), 9);
        // Position 1: lowest freq highshelf (hs1 at 8000Hz)
        assert_eq!(result[0].1.filter_type, BiquadFilterType::Highshelf);
        assert!((result[0].1.freq - 8000.0).abs() < 1.0);
        // Position 9: highest freq highshelf (hs2 at 10000Hz)
        assert_eq!(result[8].1.filter_type, BiquadFilterType::Highshelf);
        assert!((result[8].1.freq - 10000.0).abs() < 1.0);
    }

    #[test]
    fn test_enforce_rme_room_filter_constraints_both_shelves() {
        let ls = Biquad::new(BiquadFilterType::Lowshelf, 100.0, 48000.0, 0.7, 2.0);
        let hs = Biquad::new(BiquadFilterType::Highshelf, 8000.0, 48000.0, 0.7, 1.5);
        let pk1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let pk2 = Biquad::new(BiquadFilterType::Peak, 2000.0, 48000.0, 1.0, 2.0);
        let peq = vec![(1.0, pk1), (1.0, ls), (1.0, pk2), (1.0, hs)];
        let result = enforce_rme_room_filter_constraints(&peq);
        // Should always return exactly 9 bands
        assert_eq!(result.len(), 9);
        // Position 1: Lowshelf (lowest freq non-PK at 100Hz)
        assert_eq!(result[0].1.filter_type, BiquadFilterType::Lowshelf);
        assert!((result[0].1.freq - 100.0).abs() < 1.0);
        // Position 9: Highshelf (highest freq non-PK at 8000Hz)
        assert_eq!(result[8].1.filter_type, BiquadFilterType::Highshelf);
        assert!((result[8].1.freq - 8000.0).abs() < 1.0);
        // Positions 2-8 should be PK filters
        for i in 1..8 {
            assert_eq!(result[i].1.filter_type, BiquadFilterType::Peak);
        }
    }

    #[test]
    fn test_peq_format_rme_room_single_channel() {
        let bq1 = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq2 = Biquad::new(BiquadFilterType::Peak, 2000.0, 48000.0, 2.0, -2.0);
        let left = vec![(1.0, bq1), (1.0, bq2)];
        let right: Peq = vec![];
        let rme_str = peq_format_rme_room(&left, &right);

        // Verify structure
        assert!(rme_str.contains("<Preset>"));
        assert!(rme_str.contains("<Room EQ L>"));
        assert!(rme_str.contains("<Room EQ R>"));
        assert!(rme_str.contains("<Params>"));
        assert!(rme_str.contains("REQ Delay"));
        assert!(rme_str.contains("REQ Band1 Freq"));
        assert!(rme_str.contains("REQ Band1 Q"));
        assert!(rme_str.contains("REQ Band1 Gain"));
        assert!(rme_str.contains("REQ Band1 Type"));
        assert!(rme_str.contains("REQ Band2 Freq"));
        assert!(rme_str.contains("REQ Chan Gain"));
        assert!(rme_str.contains("</Preset>"));
    }

    #[test]
    fn test_peq_format_rme_room_dual_channel() {
        let bq_left = Biquad::new(BiquadFilterType::Peak, 1000.0, 48000.0, 1.0, 3.0);
        let bq_right = Biquad::new(BiquadFilterType::Peak, 2000.0, 48000.0, 2.0, -2.0);
        let left = vec![(1.0, bq_left)];
        let right = vec![(1.0, bq_right)];
        let rme_str = peq_format_rme_room(&left, &right);

        // Both channels should have different frequencies
        assert!(rme_str.contains("1000.00"));
        assert!(rme_str.contains("2000.00"));
    }

    #[test]
    fn test_peq_format_rme_room_max_bands() {
        // Test with 9 bands (at limit)
        let mut left = Vec::new();
        for i in 0..9 {
            let freq = 100.0 + (i as f64 * 100.0);
            let bq = Biquad::new(BiquadFilterType::Peak, freq, 48000.0, 1.0, 1.0);
            left.push((1.0, bq));
        }
        let right: Peq = vec![];
        let rme_str = peq_format_rme_room(&left, &right);

        // Should contain all 9 bands
        assert!(rme_str.contains("REQ Band1 Freq"));
        assert!(rme_str.contains("REQ Band9 Freq"));
    }

    #[test]
    fn test_peq_format_rme_room_over_limit() {
        // Test with more than 9 bands (should be filtered)
        let mut left = Vec::new();
        for i in 0..12 {
            let freq = 100.0 + (i as f64 * 100.0);
            let gain = (i as f64) * 0.5 + 0.5;
            let bq = Biquad::new(BiquadFilterType::Peak, freq, 48000.0, 1.0, gain);
            left.push((1.0, bq));
        }
        let right: Peq = vec![];
        let rme_str = peq_format_rme_room(&left, &right);

        // Should only contain 9 bands
        assert!(rme_str.contains("REQ Band9 Freq"));
        assert!(!rme_str.contains("REQ Band10 Freq"));
    }
}