audio_samples 1.0.5

A typed audio processing library for Rust that treats audio as a first-class, invariant-preserving object rather than an unstructured numeric buffer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
#![allow(unused_imports)]
//! Configuration types and support enumerations for audio processing operations.
//!
//! This module defines every configuration struct, option enum, and helper type
//! used by the audio processing traits. Types are grouped by the feature gate that
//! enables them: editing, processing, channels, VAD, resampling, transforms, pitch
//! analysis, IIR filtering, parametric EQ, dynamic range, and peak picking.
//!
//! Centralising configuration types in one place ensures consistent naming, shared
//! validation logic, and a single source of truth for parameter constraints. Callers
//! construct a typed configuration value and pass it to the relevant processing
//! method, rather than threading raw primitive arguments through call chains.
//!
//! Each struct that accepts numeric bounds exposes a `validate` method that checks
//! invariants before the value is submitted to a processing call. Using preset
//! constructors (e.g. [`CompressorConfig::vocal`], [`VadConfig::energy_only`]) is
//! the quickest path to a sensible default.
//!
//! Import the types you need from [`audio_samples::operations::types`](crate::operations::types) or via the
//! crate-level re-export. Construct a configuration using one of the provided
//! constructors or preset methods, optionally adjust fields, then pass it to the
//! corresponding processing function.
//!
//! ```
//! # #[cfg(feature = "processing")]
//! # {
//! use audio_samples::operations::types::NormalizationConfig;
//!
//! // Use a preset — peak normalise to 1.0
//! let config = NormalizationConfig::<f64>::peak_normalized();
//! assert_eq!(config.target, Some(1.0));
//! # }
//! ```

// General todos in types.rs
// - Serialisation + Deserialisation for types
// - Better derives -- more consistency
// - More "helper" trait implementations like From/Into et al.

#[cfg(feature = "processing")]
use crate::traits::StandardSample;

use crate::{AudioSampleError, AudioSampleResult, ParameterError};
use core::fmt::Display;
use std::num::NonZeroUsize;
use std::str::FromStr;

/// Which end of a buffer to pad when extending audio length.
///
/// Used by padding operations that insert silence or a repeated value at one
/// end of the sample data. Defaults to [`Right`][PadSide::Right] (append).
#[cfg(feature = "editing")]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PadSide {
    /// Prepend padding before the first sample.
    Left,
    /// Append padding after the last sample.
    #[default]
    Right,
}

#[cfg(feature = "editing")]
impl FromStr for PadSide {
    type Err = crate::AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "left" => Ok(Self::Left),
            "right" => Ok(Self::Right),
            _ => Err(AudioSampleError::Parameter(ParameterError::InvalidValue {
                parameter: s.to_string(),
                reason: "Expected 'left' or 'right'".to_string(),
            })),
        }
    }
}

/// Algorithm used to scale or centre audio sample values.
///
/// Different methods suit different goals. Choose the method that matches your
/// intended output range and tolerance for outliers.
#[cfg(any(feature = "processing", feature = "peak-picking"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum NormalizationMethod {
    /// Min-Max normalisation — scales all values into a caller-specified `[min, max]` range.
    ///
    /// Good for general level adjustment and ensuring samples stay within a
    /// target amplitude bound. Requires a valid `min` and `max` in the accompanying
    /// [`NormalizationConfig`].
    #[default]
    MinMax,

    /// Peak normalisation — scales by the maximum absolute value to reach a target peak.
    ///
    /// Preserves the dynamic shape of the signal while bringing the loudest sample
    /// to a specified level. Requires a valid `target` in the accompanying
    /// [`NormalizationConfig`].
    Peak,

    /// Mean normalisation — subtracts the mean so the output is centred at zero.
    ///
    /// Removes DC offset without altering relative amplitudes. Equivalent to
    /// zero-mean centering; does not rescale the range.
    Mean,

    /// Median normalisation — subtracts the median so the output is centred at zero.
    ///
    /// More robust than mean normalisation when the signal contains large outlier
    /// values, as the median is not skewed by extreme samples.
    Median,

    /// Z-score normalisation — transforms to zero mean and unit variance.
    ///
    /// Useful for statistical comparisons and machine-learning preprocessing where
    /// equal contribution from all features is required.
    ZScore,
}

#[cfg(feature = "processing")]
impl FromStr for NormalizationMethod {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "min_max" | "min-max" | "minmax" => Ok(Self::MinMax),
            "peak" => Ok(Self::Peak),
            "mean" => Ok(Self::Mean),
            "median" => Ok(Self::Median),
            "zscore" | "z_score" | "z-score" => Ok(Self::ZScore),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "min_max", "min-max", "minmax", "peak", "mean", "median", "zscore", "z_score",
                    "z-score",
                ]
            ))),
        }
    }
}

/// Parameters governing a single call to `normalize`.
///
/// ## Purpose
///
/// Bundles the [`NormalizationMethod`] with the numeric bounds it requires into
/// one type-safe value. This avoids ambiguous argument lists and makes it clear
/// which fields are active for a given method.
///
/// ## Intended Usage
///
/// Construct with one of the provided constructors (`peak`, `min_max`, `mean`,
/// `median`, `zscore`) or use the `f64`-specific presets (`peak_normalized`,
/// `range_normalized`). Pass the resulting value to
/// [`AudioProcessing::normalize`][crate::operations::AudioProcessing::normalize].
///
/// ## Invariants
///
/// - When `method` is [`NormalizationMethod::MinMax`]: `min` and `max` must
///   both be `Some`, and `min < max`.
/// - When `method` is [`NormalizationMethod::Peak`]: `target` must be `Some`
///   and positive.
/// - For `Mean`, `Median`, and `ZScore`: `min`, `max`, and `target` are unused;
///   their values are ignored during processing.
///
/// ## Assumptions
///
/// `T` implements [`StandardSample`][crate::traits::StandardSample], which bounds
/// it to the numeric types supported throughout the crate.
#[cfg(feature = "processing")]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct NormalizationConfig<T>
where
    T: StandardSample,
{
    /// The normalisation algorithm to apply.
    pub method: NormalizationMethod,

    /// Lower bound for [`NormalizationMethod::MinMax`].
    ///
    /// Must be `Some` and less than `max` when `method` is `MinMax`.
    /// Ignored for all other methods.
    pub min: Option<T>,

    /// Upper bound for [`NormalizationMethod::MinMax`].
    ///
    /// Must be `Some` and greater than `min` when `method` is `MinMax`.
    /// Ignored for all other methods.
    pub max: Option<T>,

    /// Target peak amplitude for [`NormalizationMethod::Peak`].
    ///
    /// Must be `Some` and positive when `method` is `Peak`.
    /// Ignored for all other methods.
    pub target: Option<T>,
}

#[cfg(feature = "processing")]
impl<T> NormalizationConfig<T>
where
    T: StandardSample,
{
    /// Creates a peak normalisation configuration targeting `target`.
    ///
    /// # Arguments
    ///
    /// – `target` – the amplitude level that the loudest sample will be scaled to.
    ///   Must be positive for meaningful output.
    ///
    /// # Returns
    ///
    /// A [`NormalizationConfig`] with `method = Peak` and `target = Some(target)`.
    #[inline]
    pub const fn peak(target: T) -> Self {
        Self {
            method: NormalizationMethod::Peak,
            min: None,
            max: None,
            target: Some(target),
        }
    }

    /// Creates a min-max normalisation configuration scaling to `[min, max]`.
    ///
    /// # Arguments
    ///
    /// – `min` – the lower bound of the target amplitude range. Must be less than `max`.\
    /// – `max` – the upper bound of the target amplitude range. Must be greater than `min`.
    ///
    /// # Returns
    ///
    /// A [`NormalizationConfig`] with `method = MinMax`, `min = Some(min)`, and
    /// `max = Some(max)`.
    #[inline]
    pub const fn min_max(min: T, max: T) -> Self {
        Self {
            method: NormalizationMethod::MinMax,
            min: Some(min),
            max: Some(max),
            target: None,
        }
    }

    /// Creates a mean normalisation configuration (zero-mean centering).
    ///
    /// # Returns
    ///
    /// A [`NormalizationConfig`] with `method = Mean`. The `min`, `max`, and `target`
    /// fields are unused and set to `None`.
    #[inline]
    #[must_use]
    pub const fn mean() -> Self {
        Self {
            method: NormalizationMethod::Mean,
            min: None,
            max: None,
            target: None,
        }
    }

    /// Creates a median normalisation configuration (median centering).
    ///
    /// # Returns
    ///
    /// A [`NormalizationConfig`] with `method = Median`. The `min`, `max`, and `target`
    /// fields are unused and set to `None`.
    #[inline]
    #[must_use]
    pub const fn median() -> Self {
        Self {
            method: NormalizationMethod::Median,
            min: None,
            max: None,
            target: None,
        }
    }

    /// Creates a z-score normalisation configuration (zero mean, unit variance).
    ///
    /// # Returns
    ///
    /// A [`NormalizationConfig`] with `method = ZScore`. The `min`, `max`, and `target`
    /// fields are unused and set to `None`.
    #[inline]
    #[must_use]
    pub const fn zscore() -> Self {
        Self {
            method: NormalizationMethod::ZScore,
            min: None,
            max: None,
            target: None,
        }
    }
}

#[cfg(feature = "processing")]
impl NormalizationConfig<f64> {
    /// Peak normalisation that scales to a maximum absolute amplitude of 1.0.
    ///
    /// # Returns
    ///
    /// Equivalent to `NormalizationConfig::<f64>::peak(1.0)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "processing")]
    /// # {
    /// use audio_samples::operations::types::NormalizationConfig;
    ///
    /// let config = NormalizationConfig::<f64>::peak_normalized();
    /// assert_eq!(config.target, Some(1.0));
    /// # }
    /// ```
    #[inline]
    #[must_use]
    pub const fn peak_normalized() -> Self {
        Self::peak(1.0)
    }

    /// Min-Max normalisation into the `[-1.0, 1.0]` range.
    ///
    /// # Returns
    ///
    /// Equivalent to `NormalizationConfig::<f64>::min_max(-1.0, 1.0)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "processing")]
    /// # {
    /// use audio_samples::operations::types::NormalizationConfig;
    ///
    /// let config = NormalizationConfig::<f64>::range_normalized();
    /// assert_eq!(config.min, Some(-1.0));
    /// assert_eq!(config.max, Some(1.0));
    /// # }
    /// ```
    #[inline]
    #[must_use]
    pub const fn range_normalized() -> Self {
        Self::min_max(-1.0, 1.0)
    }
}

/// Shape of the gain envelope applied during a fade-in or fade-out.
///
/// Each variant describes how the gain multiplier changes from 0.0 to 1.0 over
/// the fade region. The default is [`Linear`][FadeCurve::Linear].
#[cfg(feature = "editing")]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FadeCurve {
    /// Gain changes at a constant rate from start to end.
    ///
    /// Simple and predictable, but can sound abrupt at the endpoints because
    /// there is no gradual onset or release.
    #[default]
    Linear,

    /// Gain changes rapidly near the start and slowly near the end.
    ///
    /// Produces a fast attack or quick onset when fading in, and a slow tail
    /// when fading out. Perceptually emphasises the early portion of the fade.
    Exponential,

    /// Gain changes slowly near the start and rapidly near the end.
    ///
    /// Produces a gradual onset and a sharp cutoff. The perceptual complement
    /// of [`Exponential`][FadeCurve::Exponential].
    Logarithmic,

    /// S-shaped gain curve with zero first derivative at both endpoints.
    ///
    /// Starts and ends smoothly, resulting in the most perceptually natural
    /// transition. Implemented as a cubic Hermite interpolation (smoothstep).
    SmoothStep,
}

#[cfg(feature = "editing")]
impl FromStr for FadeCurve {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            return Err(AudioSampleError::parse::<Self, _>(
                "Input must not be empty. Must be one of ['linear', 'exp', 'exponential', 'log', 'logarithmic', 'smooth', 'smoothstep']",
            ));
        }

        let normalised = s.trim();
        match normalised.to_lowercase().as_str() {
            "linear" => Ok(Self::Linear),
            "exp" | "exponential" => Ok(Self::Exponential),
            "log" | "logarithmic" => Ok(Self::Logarithmic),
            "smooth" | "smoothstep" => Ok(Self::SmoothStep),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {normalised}. Must be one of ['linear', 'exp', 'exponential', 'log', 'logarithmic', 'smooth', 'smoothstep']"
            ))),
        }
    }
}

/// Algorithm for collapsing a multi-channel signal to a single mono channel.
///
/// Select the variant that best matches the intended perceptual result.
/// [`Average`][MonoConversionMethod::Average] is the default and suits general
/// downmix scenarios. Use [`Weighted`][MonoConversionMethod::Weighted] when
/// channels carry unequal acoustic importance.
#[cfg(feature = "channels")]
#[derive(Default, Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum MonoConversionMethod {
    /// Mix all channels to mono with equal weight.
    ///
    /// Each output sample is the arithmetic mean across all input channels.
    #[default]
    Average,

    /// Use only the left channel (channel index 0).
    ///
    /// Appropriate for stereo input when only left content is needed.
    Left,

    /// Use only the right channel (channel index 1).
    ///
    /// Appropriate for stereo input when only right content is needed.
    Right,

    /// Mix channels using per-channel gain weights.
    ///
    /// The inner `Vec<f64>` must contain one weight per channel. Weights need
    /// not sum to 1.0; they are applied as linear gain multipliers and the
    /// resulting values are summed. Mismatched length behaviour is
    /// implementation-defined.
    Weighted(Vec<f64>),

    /// Use the center channel when present; fall back to averaging L/R otherwise.
    ///
    /// For 5.1 and similar multichannel formats where a discrete center channel
    /// (typically index 2) carries dialogue or lead content.
    Center,
}

/// Algorithm for expanding a mono signal into a two-channel stereo pair.
///
/// Defaults to [`Duplicate`][StereoConversionMethod::Duplicate], which produces
/// a centred mono-compatible stereo signal.
#[cfg(feature = "channels")]
#[derive(Default, Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum StereoConversionMethod {
    /// Copy the mono signal identically to both left and right channels.
    ///
    /// The result is a centred, mono-compatible stereo signal.
    #[default]
    Duplicate,

    /// Pan the mono signal to a specific stereo position.
    ///
    /// The inner `f64` encodes the pan position: `-1.0` = hard left,
    /// `0.0` = centre, `1.0` = hard right. Values outside `[-1.0, 1.0]`
    /// have implementation-defined behaviour.
    Pan(f64),

    /// Route the mono signal to the left channel; fill the right with silence.
    Left,

    /// Route the mono signal to the right channel; fill the left with silence.
    Right,
}

/// Algorithm used by the Voice Activity Detector to classify frames.
///
/// Each method makes different trade-offs between simplicity, computational cost,
/// and robustness to noise. [`Energy`][VadMethod::Energy] is the default and
/// suits clean or lightly noisy speech. For challenging conditions, prefer
/// [`Combined`][VadMethod::Combined] or [`Spectral`][VadMethod::Spectral].
#[cfg(feature = "vad")]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum VadMethod {
    /// RMS energy threshold detection.
    ///
    /// A frame is classified as speech when its RMS level exceeds
    /// [`VadConfig::energy_threshold_db`]. Fast and reliable for clean audio;
    /// sensitive to background noise.
    #[default]
    Energy,

    /// Zero-crossing rate (ZCR) detection.
    ///
    /// Uses the per-frame rate of sign changes as a proxy for voiced activity.
    /// Speech typically produces ZCR in the range defined by
    /// [`VadConfig::zcr_min`] and [`VadConfig::zcr_max`].
    ZeroCrossing,

    /// Combined energy and zero-crossing rate detection.
    ///
    /// Both the energy threshold and the ZCR window must be satisfied for a
    /// frame to be classified as speech. Reduces false positives from either
    /// method alone.
    Combined,

    /// Spectral energy ratio detection.
    ///
    /// Computes the fraction of spectral energy within the speech band
    /// (`speech_band_low_hz`..`speech_band_high_hz`) and compares it to
    /// [`VadConfig::spectral_ratio_threshold`]. More computationally intensive
    /// but more discriminative in noisy environments.
    Spectral,
}

#[cfg(feature = "vad")]
impl FromStr for VadMethod {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "energy" | "rms" => Ok(Self::Energy),
            "zero_crossing" | "zero-crossing" | "zcr" => Ok(Self::ZeroCrossing),
            "combined" | "energy_zcr" | "energy-zcr" => Ok(Self::Combined),
            "spectral" | "spectrum" => Ok(Self::Spectral),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "energy",
                    "rms",
                    "zero_crossing",
                    "zero-crossing",
                    "zcr",
                    "combined",
                    "energy_zcr",
                    "energy-zcr",
                    "spectral",
                    "spectrum",
                ]
            ))),
        }
    }
}

/// How to derive a single activity decision from a multi-channel signal.
///
/// Applies only when the input has more than one channel. For mono input this
/// setting has no effect. Defaults to
/// [`AverageToMono`][VadChannelPolicy::AverageToMono].
#[cfg(feature = "vad")]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum VadChannelPolicy {
    /// Average all channels to mono before running VAD.
    ///
    /// Cheapest option; produces one activity mask independent of channel
    /// count.
    #[default]
    AverageToMono,

    /// Run VAD on every channel independently; output `true` if any is active.
    ///
    /// Use when voice may appear on any single channel (e.g. a microphone
    /// array where only some elements pick up speech).
    AnyChannel,

    /// Run VAD on every channel independently; output `true` only if all are active.
    ///
    /// Use when corroborated activity across all channels is required before
    /// classifying a frame as speech.
    AllChannels,

    /// Run VAD on only the specified channel index.
    ///
    /// The inner `usize` is the zero-based channel index. Behaviour when the
    /// index exceeds the actual channel count is implementation-defined.
    Channel(usize),
}

/// Full configuration for Voice Activity Detection.
///
/// ## Purpose
///
/// Controls every aspect of the frame-based VAD pipeline: the detection algorithm,
/// framing parameters, per-method thresholds, temporal smoothing, and multi-channel
/// handling.
///
/// ## Intended Usage
///
/// Use [`VadConfig::energy_only`] for a quick, energy-threshold VAD; use the
/// [`Default`] impl for a balanced starting point; or build a fully custom
/// configuration with [`VadConfig::new`]. Call [`validate`][VadConfig::validate]
/// before passing to a VAD function to catch invalid parameter combinations.
///
/// ## Invariants
///
/// - `hop_size` must be ≤ `frame_size`.
/// - `0.0 ≤ zcr_min ≤ zcr_max ≤ 1.0`.
/// - `0.0 < speech_band_low_hz < speech_band_high_hz`.
/// - `0.0 ≤ spectral_ratio_threshold ≤ 1.0`.
///
/// ## Assumptions
///
/// Threshold fields that are not relevant to the chosen [`VadMethod`] are
/// silently ignored during processing (e.g. `zcr_*` fields have no effect when
/// `method` is [`VadMethod::Energy`]).
#[cfg(feature = "vad")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct VadConfig {
    /// VAD method to use.
    pub method: VadMethod,
    /// Frame size in samples.
    pub frame_size: NonZeroUsize,
    /// Hop size in samples (frame step).
    pub hop_size: NonZeroUsize,
    /// Whether to include a final partial frame (zero-padded).
    pub pad_end: bool,
    /// Policy for multi-channel audio.
    pub channel_policy: VadChannelPolicy,

    /// Energy threshold in dBFS (RMS). Typical values: `-60.0` (very sensitive) to `-30.0`.
    pub energy_threshold_db: f64,
    /// Minimum acceptable zero crossing rate (ZCR), expressed as crossings per sample in `[0, 1]`.
    pub zcr_min: f64,
    /// Maximum acceptable zero crossing rate (ZCR), expressed as crossings per sample in `[0, 1]`.
    pub zcr_max: f64,

    /// Minimum number of consecutive speech frames to keep a speech region.
    pub min_speech_frames: usize,
    /// Minimum number of consecutive non-speech frames to keep a silence region.
    /// Shorter silence gaps are filled as speech.
    pub min_silence_frames: usize,
    /// Hangover in frames: keep speech active for this many frames after energy drops.
    pub hangover_frames: NonZeroUsize,
    /// Majority-vote smoothing window in frames (1 = no smoothing).
    pub smooth_frames: NonZeroUsize,

    /// Lower bound of the speech band in Hz (used by `VadMethod::Spectral`).
    pub speech_band_low_hz: f64,
    /// Upper bound of the speech band in Hz (used by `VadMethod::Spectral`).
    pub speech_band_high_hz: f64,
    /// Threshold on speech-band energy ratio (used by `VadMethod::Spectral`).
    pub spectral_ratio_threshold: f64,
}

#[cfg(feature = "vad")]
impl VadConfig {
    /// Creates a VAD configuration with every parameter specified explicitly.
    ///
    /// Prefer the [`Default`] impl or [`energy_only`][VadConfig::energy_only] for
    /// typical use cases. Use this constructor only when full control over every
    /// parameter is required.
    ///
    /// # Arguments
    ///
    /// – `method` – detection algorithm.\
    /// – `frame_size` – analysis window length in samples.\
    /// – `hop_size` – step between consecutive frames; must be ≤ `frame_size`.\
    /// – `pad_end` – whether to include a final zero-padded partial frame.\
    /// – `channel_policy` – how to derive activity from multi-channel input.\
    /// – `energy_threshold_db` – RMS threshold in dBFS; typical range `[-60.0, -20.0]`.\
    /// – `zcr_min` – minimum zero-crossing rate for voiced activity; in `[0.0, 1.0]`.\
    /// – `zcr_max` – maximum zero-crossing rate for voiced activity; in `[zcr_min, 1.0]`.\
    /// – `min_speech_frames` – minimum consecutive speech frames to confirm a region.\
    /// – `min_silence_frames` – minimum consecutive silence frames to close a region.\
    /// – `hangover_frames` – frames to remain "active" after energy drops below threshold.\
    /// – `smooth_frames` – majority-vote window size; `1` disables smoothing.\
    /// – `speech_band_low_hz` – lower boundary of the speech band (Spectral method).\
    /// – `speech_band_high_hz` – upper boundary of the speech band (Spectral method).\
    /// – `spectral_ratio_threshold` – minimum in-band energy fraction; in `[0.0, 1.0]`.
    ///
    /// # Returns
    ///
    /// A [`VadConfig`] with all fields set as provided. Use
    /// [`validate`][VadConfig::validate] to check constraints before use.
    #[inline]
    #[must_use]
    pub const fn new(
        method: VadMethod,
        frame_size: NonZeroUsize,
        hop_size: NonZeroUsize,
        pad_end: bool,
        channel_policy: VadChannelPolicy,
        energy_threshold_db: f64,
        zcr_min: f64,
        zcr_max: f64,
        min_speech_frames: usize,
        min_silence_frames: usize,
        hangover_frames: NonZeroUsize,
        smooth_frames: NonZeroUsize,
        speech_band_low_hz: f64,
        speech_band_high_hz: f64,
        spectral_ratio_threshold: f64,
    ) -> Self {
        Self {
            method,
            frame_size,
            hop_size,
            pad_end,
            channel_policy,
            energy_threshold_db,
            zcr_min,
            zcr_max,
            min_speech_frames,
            min_silence_frames,
            hangover_frames,
            smooth_frames,
            speech_band_low_hz,
            speech_band_high_hz,
            spectral_ratio_threshold,
        }
    }

    /// Creates a VAD configuration using only energy-based (RMS) detection.
    ///
    /// All parameters are taken from the [`Default`] impl; only `method` is
    /// overridden to [`VadMethod::Energy`]. This is the simplest and fastest
    /// configuration and works well for clean or lightly noisy speech.
    ///
    /// # Returns
    ///
    /// A [`VadConfig`] identical to `Default::default()` except that `method` is
    /// `VadMethod::Energy`.
    #[inline]
    #[must_use]
    pub fn energy_only() -> Self {
        Self {
            method: VadMethod::Energy,
            ..Default::default()
        }
    }

    /// Validates all parameter constraints.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all constraints are satisfied.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] describing the
    /// first violated constraint:
    ///
    /// - `hop_size` ≤ `frame_size`
    /// - `0.0 ≤ zcr_min ≤ zcr_max ≤ 1.0`
    /// - `0.0 < speech_band_low_hz < speech_band_high_hz`
    /// - `0.0 ≤ spectral_ratio_threshold ≤ 1.0`
    #[inline]
    pub fn validate(self) -> AudioSampleResult<Self> {
        if self.hop_size > self.frame_size {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "hop_size",
                "must be <= frame_size",
            )));
        }
        if self.zcr_min < 0.0 || self.zcr_max > 1.0 || self.zcr_min > self.zcr_max {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "zcr_*",
                "expected 0 <= zcr_min <= zcr_max <= 1",
            )));
        }
        if self.speech_band_low_hz <= 0.0
            || self.speech_band_high_hz <= 0.0
            || self.speech_band_low_hz >= self.speech_band_high_hz
        {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "speech_band_*",
                "expected 0 < low_hz < high_hz",
            )));
        }
        if self.spectral_ratio_threshold < 0.0 || self.spectral_ratio_threshold > 1.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "spectral_ratio_threshold",
                "expected 0 <= threshold <= 1",
            )));
        }

        Ok(self)
    }

    /// Returns a copy of `self` with the specified `method`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_method(self, method: VadMethod) -> Self {
        Self { method, ..self }
    }

    /// Returns a copy of `self` with the specified `frame_size`.
    ///
    /// Other fields are unchanged.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `frame_size` is less than the current `hop_size`.
    #[inline]
    pub fn with_frame_size(self, frame_size: NonZeroUsize) -> AudioSampleResult<Self> {
        let updated = Self { frame_size, ..self };
        if updated.hop_size > updated.frame_size {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "frame_size",
                "must be >= hop_size",
            )));
        }
        Ok(updated)
    }

    /// Returns a copy of `self` with the specified `hop_size`.
    ///
    /// Other fields are unchanged.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `hop_size` is greater than the current `frame_size`.
    #[inline]
    pub fn with_hop_size(self, hop_size: NonZeroUsize) -> AudioSampleResult<Self> {
        let updated = Self { hop_size, ..self };
        if updated.hop_size > updated.frame_size {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "hop_size",
                "must be <= frame_size",
            )));
        }
        Ok(updated)
    }

    /// Returns a copy of `self` with the specified `pad_end`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_pad_end(self, pad_end: bool) -> Self {
        Self { pad_end, ..self }
    }

    /// Returns a copy of `self` with the specified `channel_policy`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_channel_policy(self, channel_policy: VadChannelPolicy) -> Self {
        Self {
            channel_policy,
            ..self
        }
    }

    /// Returns a copy of `self` with the specified `energy_threshold_db`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_energy_threshold_db(self, energy_threshold_db: f64) -> Self {
        Self {
            energy_threshold_db,
            ..self
        }
    }

    /// Returns a copy of `self` with the specified `zcr_min`.
    ///
    /// Other fields are unchanged.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `zcr_min` is not in the range `[0.0, 1.0]` or is greater than the current `zcr_max`.
    #[inline]
    pub fn with_zcr_min(self, zcr_min: f64) -> AudioSampleResult<Self> {
        let updated = Self { zcr_min, ..self };
        if updated.zcr_min < 0.0 || updated.zcr_min > 1.0 || updated.zcr_min > updated.zcr_max {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "zcr_min",
                "expected 0 <= zcr_min <= zcr_max <= 1",
            )));
        }
        Ok(updated)
    }

    /// Returns a copy of `self` with the specified `zcr_max`.
    ///
    /// Other fields are unchanged.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `zcr_max` is not in the range `[0.0, 1.0]` or is less than the current `zcr_min`.
    #[inline]
    pub fn with_zcr_max(self, zcr_max: f64) -> AudioSampleResult<Self> {
        let updated = Self { zcr_max, ..self };
        if updated.zcr_max < 0.0 || updated.zcr_max > 1.0 || updated.zcr_min > updated.zcr_max {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "zcr_max",
                "expected 0 <= zcr_min <= zcr_max <= 1",
            )));
        }
        Ok(updated)
    }

    /// Returns a copy of `self` with the specified `min_speech_frames`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_min_speech_frames(self, min_speech_frames: usize) -> Self {
        Self {
            min_speech_frames,
            ..self
        }
    }

    /// Returns a copy of `self` with the specified `min_silence_frames`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_min_silence_frames(self, min_silence_frames: usize) -> Self {
        Self {
            min_silence_frames,
            ..self
        }
    }

    /// Returns a copy of `self` with the specified `hangover_frames`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_hangover_frames(self, hangover_frames: NonZeroUsize) -> Self {
        Self {
            hangover_frames,
            ..self
        }
    }

    /// Returns a copy of `self` with the specified `smooth_frames`.
    ///
    /// Other fields are unchanged.
    #[inline]
    #[must_use]
    pub const fn with_smooth_frames(self, smooth_frames: NonZeroUsize) -> Self {
        Self {
            smooth_frames,
            ..self
        }
    }

    /// Returns a copy of `self` with the specified `speech_band_low_hz`.
    ///
    /// Other fields are unchanged.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `speech_band_low_hz` is not positive or is not less than the current `speech_band_high_hz`.
    #[inline]
    pub fn with_speech_band_low_hz(self, speech_band_low_hz: f64) -> AudioSampleResult<Self> {
        let updated = Self {
            speech_band_low_hz,
            ..self
        };
        if updated.speech_band_low_hz <= 0.0
            || updated.speech_band_low_hz >= updated.speech_band_high_hz
        {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "speech_band_low_hz",
                "expected 0 < low_hz < high_hz",
            )));
        }
        Ok(updated)
    }

    /// Returns a copy of `self` with the specified `speech_band_high_hz`.
    ///
    /// Other fields are unchanged.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `speech_band_high_hz` is not positive or is not greater than the current `speech_band_low_hz`.
    #[inline]
    pub fn with_speech_band_high_hz(self, speech_band_high_hz: f64) -> AudioSampleResult<Self> {
        let updated = Self {
            speech_band_high_hz,
            ..self
        };
        if updated.speech_band_high_hz <= 0.0
            || updated.speech_band_low_hz >= updated.speech_band_high_hz
        {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "speech_band_high_hz",
                "expected 0 < low_hz < high_hz",
            )));
        }
        Ok(updated)
    }

    /// Returns a copy of `self` with the specified `spectral_ratio_threshold`.
    ///
    /// Other fields are unchanged.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `spectral_ratio_threshold` is not in the range `[0.0, 1.0]`.
    #[inline]
    pub fn with_spectral_ratio_threshold(
        self,
        spectral_ratio_threshold: f64,
    ) -> AudioSampleResult<Self> {
        let updated = Self {
            spectral_ratio_threshold,
            ..self
        };
        if updated.spectral_ratio_threshold < 0.0 || updated.spectral_ratio_threshold > 1.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "spectral_ratio_threshold",
                "expected 0 <= threshold <= 1",
            )));
        }
        Ok(updated)
    }
}

#[cfg(feature = "vad")]
impl Default for VadConfig {
    fn default() -> Self {
        Self::new(
            VadMethod::Energy,
            crate::nzu!(1024),
            crate::nzu!(512),
            true,
            VadChannelPolicy::AverageToMono,
            -40.0,          // energy threshold in dBFS
            0.02,           // zcr_min
            0.3,            // zcr_max
            3,              // min_speech_frames
            5,              // min_silence_frames
            crate::nzu!(2), // hangover_frames
            crate::nzu!(5), // smooth_frames
            300.0,          // speech_band_low_hz
            3400.0,         // speech_band_high_hz
            0.6,            // spectral_ratio_threshold
        )
    }
}

/// Discrete quality levels for resampling operations.
///
/// Higher quality levels trade increased computational cost and latency
/// for improved spectral fidelity, reduced aliasing, and more stable phase
/// behaviour. Lower quality levels prioritise throughput and low latency.
#[cfg(feature = "resampling")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[non_exhaustive]
pub enum ResamplingQuality {
    /// Fast but lower quality resampling.
    #[default]
    Fast,
    /// Balanced speed and quality.
    Medium,
    /// Highest quality but slower resampling.
    High,
}

#[cfg(feature = "resampling")]
impl Display for ResamplingQuality {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Fast => "fast",
            Self::Medium => "medium",
            Self::High => "high",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "resampling")]
impl FromStr for ResamplingQuality {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "fast" | "low" => Ok(Self::Fast),
            "medium" | "med" | "balanced" => Ok(Self::Medium),
            "high" | "best" => Ok(Self::High),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                ["fast", "low", "medium", "med", "balanced", "high", "best"]
            ))),
        }
    }
}

#[cfg(feature = "resampling")]
impl TryFrom<&str> for ResamplingQuality {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Scaling methods for spectrogram magnitude and frequency representations.
///
/// Different scaling approaches expose different structure in spectral content
/// and are appropriate for different analysis and visualisation tasks.
#[cfg(feature = "transforms")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum SpectrogramScale {
    /// Linear power scale.
    ///
    /// Preserves absolute magnitude relationships and is most appropriate
    /// for quantitative analysis and energy measurements.
    #[default]
    Linear,

    /// Logarithmic (decibel) magnitude scale.
    ///
    /// Compresses dynamic range to improve visualisation of low-energy
    /// components alongside strong spectral peaks.
    ///
    /// Note: The exact reference level and floor behaviour are implementation
    /// details and should not be relied upon for semantic correctness.
    Log,

    /// Mel-frequency scale.
    ///
    /// Applies a perceptually motivated nonlinear mapping of frequency
    /// designed to better approximate human auditory resolution.
    Mel,
}

#[cfg(feature = "transforms")]
impl Display for SpectrogramScale {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Linear => "linear",
            Self::Log => "log",
            Self::Mel => "mel",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "transforms")]
impl FromStr for SpectrogramScale {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            // Linear
            "linear" | "lin" => Ok(Self::Linear),

            // Logarithmic / dB
            "log" | "logarithmic" | "db" | "decibel" => Ok(Self::Log),

            // Mel
            "mel" | "mel-scale" | "melscale" => Ok(Self::Mel),

            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                ["linear", "lin", "log", "db", "decibel", "mel", "mel-scale",]
            ))),
        }
    }
}

#[cfg(feature = "transforms")]
impl TryFrom<&str> for SpectrogramScale {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Pitch detection algorithm selection.
///
/// Different algorithms trade off accuracy, robustness to noise and
/// inharmonicity, latency, and computational cost when estimating the
/// fundamental frequency of a signal.
#[cfg(feature = "pitch-analysis")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum PitchDetectionMethod {
    /// YIN pitch detection algorithm.
    ///
    /// Provides robust and accurate fundamental frequency estimation for
    /// both speech and musical signals, at moderate computational cost.
    #[default]
    Yin,

    /// Autocorrelation-based pitch detection.
    ///
    /// Simple and fast, but sensitive to noise and octave errors for
    /// complex or weakly periodic signals.
    Autocorrelation,

    /// Cepstral pitch detection.
    ///
    /// Operates in the frequency domain and performs well for voiced speech,
    /// but can degrade for dense harmonic or noisy spectra.
    Cepstrum,

    /// Harmonic Product Spectrum (HPS).
    ///
    /// Emphasises harmonic structure and is well-suited to musical signals
    /// with strong harmonic content.
    HarmonicProduct,
}

#[cfg(feature = "pitch-analysis")]
impl Display for PitchDetectionMethod {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Yin => "yin",
            Self::Autocorrelation => "autocorrelation",
            Self::Cepstrum => "cepstrum",
            Self::HarmonicProduct => "harmonic_product",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "pitch-analysis")]
impl FromStr for PitchDetectionMethod {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            // YIN
            "yin" => Ok(Self::Yin),

            // Autocorrelation
            "autocorrelation" | "auto" | "acf" => Ok(Self::Autocorrelation),

            // Cepstrum
            "cepstrum" | "cep" => Ok(Self::Cepstrum),

            // Harmonic Product Spectrum
            "harmonic_product" | "harmonic-product" | "hps" | "harmonic" => {
                Ok(Self::HarmonicProduct)
            }

            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "yin",
                    "autocorrelation",
                    "acf",
                    "cepstrum",
                    "cep",
                    "harmonic_product",
                    "hps",
                ]
            ))),
        }
    }
}

#[cfg(feature = "pitch-analysis")]
impl TryFrom<&str> for PitchDetectionMethod {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// IIR filter family selection for digital signal processing.
///
/// IIR (Infinite Impulse Response) filters provide efficient recursive
/// implementations with feedback, typically achieving sharper transition
/// bands than FIR filters for a given computational budget.
#[cfg(feature = "iir-filtering")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum IirFilterType {
    /// Butterworth filter.
    ///
    /// Exhibits a maximally flat passband with monotonic magnitude response
    /// and no ripple. Provides smooth behaviour and predictable phase
    /// characteristics at the cost of wider transition bands.
    #[default]
    Butterworth,

    /// Chebyshev Type I filter.
    ///
    /// Introduces controlled ripple in the passband to achieve a sharper
    /// transition region than Butterworth designs.
    ChebyshevI,

    /// Chebyshev Type II filter.
    ///
    /// Introduces ripple in the stopband while preserving a monotonic
    /// passband response, allowing sharper transitions than Butterworth.
    ChebyshevII,

    /// Elliptic (Cauer) filter.
    ///
    /// Introduces ripple in both passband and stopband, yielding the
    /// steepest transition region for a given filter order.
    Elliptic,
}

#[cfg(feature = "iir-filtering")]
impl Display for IirFilterType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Butterworth => "butterworth",
            Self::ChebyshevI => "chebyshev1",
            Self::ChebyshevII => "chebyshev2",
            Self::Elliptic => "elliptic",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "iir-filtering")]
impl FromStr for IirFilterType {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            // Butterworth
            "butterworth" | "butter" | "bw" => Ok(Self::Butterworth),
            "chebyshev1" | "cheby1" | "chebyshev_i" | "chebyshev-i" => Ok(Self::ChebyshevI),
            "chebyshev2" | "cheby2" | "chebyshev_ii" | "chebyshev-ii" => Ok(Self::ChebyshevII),
            "elliptic" | "cauer" | "ellip" => Ok(Self::Elliptic),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "butterworth",
                    "chebyshev1",
                    "chebyshev2",
                    "elliptic",
                    "cauer",
                ]
            ))),
        }
    }
}

#[cfg(feature = "iir-filtering")]
impl TryFrom<&str> for IirFilterType {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Filter response characteristics.
///
/// Defines the qualitative frequency response shape of a filter.
#[cfg(feature = "iir-filtering")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum FilterResponse {
    /// Low-pass filter.
    ///
    /// Attenuates frequencies above the cutoff frequency while preserving
    /// lower-frequency components.
    #[default]
    LowPass,

    /// High-pass filter.
    ///
    /// Attenuates frequencies below the cutoff frequency while preserving
    /// higher-frequency components.
    HighPass,

    /// Band-pass filter.
    ///
    /// Preserves frequencies within a specified band while attenuating
    /// frequencies outside that range.
    BandPass,

    /// Band-stop (notch) filter.
    ///
    /// Attenuates frequencies within a specified band while preserving
    /// frequencies outside that range.
    BandStop,
}

#[cfg(feature = "iir-filtering")]
impl Display for FilterResponse {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::LowPass => "lowpass",
            Self::HighPass => "highpass",
            Self::BandPass => "bandpass",
            Self::BandStop => "bandstop",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "iir-filtering")]
impl FromStr for FilterResponse {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "lowpass" | "low-pass" | "lp" => Ok(Self::LowPass),
            "highpass" | "high-pass" | "hp" => Ok(Self::HighPass),
            "bandpass" | "band-pass" | "bp" => Ok(Self::BandPass),
            "bandstop" | "band-stop" | "bs" | "notch" => Ok(Self::BandStop),

            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "lowpass", "highpass", "bandpass", "bandstop", "lp", "hp", "bp", "bs",
                ]
            ))),
        }
    }
}

#[cfg(feature = "iir-filtering")]
impl TryFrom<&str> for FilterResponse {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Design specification for an IIR digital filter.
///
/// ## Purpose
///
/// Encapsulates all parameters needed to specify a single IIR filter stage:
/// the filter family, response shape, order, cutoff frequencies, and ripple
/// or attenuation tolerances.
///
/// ## Intended Usage
///
/// Use one of the provided constructors (`butterworth_lowpass`, etc.) to obtain
/// a valid design for the most common cases. Pass the result to
/// [`AudioIirFiltering::apply_iir_filter`][crate::operations::AudioIirFiltering::apply_iir_filter].
///
/// ## Invariants
///
/// - For `LowPass` / `HighPass` responses: `cutoff_frequency` must be `Some`.
/// - For `BandPass` / `BandStop` responses: `low_frequency` and
///   `high_frequency` must both be `Some`, with `low_frequency < high_frequency`.
/// - For `ChebyshevI` and `Elliptic`: `passband_ripple` must be `Some` and
///   positive.
/// - For `ChebyshevII` and `Elliptic`: `stopband_attenuation` must be `Some`
///   and positive.
/// - All frequencies must be positive and below the Nyquist frequency of the
///   target sample rate.
///
/// ## Assumptions
///
/// Validation of frequency values against the sample rate occurs at filter
/// application time, not at construction.
#[cfg(feature = "iir-filtering")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct IirFilterDesign {
    /// The filter family (Butterworth, Chebyshev I/II, Elliptic).
    pub filter_type: IirFilterType,

    /// The frequency response shape (low-pass, high-pass, band-pass, band-stop).
    pub response: FilterResponse,

    /// Filter order — the number of poles.
    ///
    /// Higher order yields a steeper transition band at the cost of more
    /// computation and potentially less stable phase behaviour.
    pub order: NonZeroUsize,

    /// Cutoff frequency in Hz for `LowPass` and `HighPass` responses.
    ///
    /// Must be `Some` and in `(0, nyquist)` for those response types; otherwise
    /// `None`.
    pub cutoff_frequency: Option<f64>,

    /// Lower edge frequency in Hz for `BandPass` and `BandStop` responses.
    ///
    /// Must be `Some` and less than `high_frequency` for those response types;
    /// otherwise `None`.
    pub low_frequency: Option<f64>,

    /// Upper edge frequency in Hz for `BandPass` and `BandStop` responses.
    ///
    /// Must be `Some` and greater than `low_frequency` for those response types;
    /// otherwise `None`.
    pub high_frequency: Option<f64>,

    /// Passband ripple in dB for `ChebyshevI` and `Elliptic` filter types.
    ///
    /// Controls how much gain variation is permitted within the passband.
    /// Larger values yield a steeper transition but more audible ripple.
    /// Must be positive. `None` for filter types that do not use this parameter.
    pub passband_ripple: Option<f64>,

    /// Stopband attenuation in dB for `ChebyshevII` and `Elliptic` filter types.
    ///
    /// Minimum attenuation guaranteed in the stopband. Larger values yield
    /// stronger rejection but require a higher filter order to achieve.
    /// Must be positive. `None` for filter types that do not use this parameter.
    pub stopband_attenuation: Option<f64>,
}

#[cfg(feature = "iir-filtering")]
impl IirFilterDesign {
    /// Creates a Butterworth low-pass filter design.
    ///
    /// # Arguments
    ///
    /// – `order` – filter order; higher values yield a steeper roll-off.\
    /// – `cutoff_frequency` – –3 dB point in Hz; must be in `(0, nyquist)`.
    ///
    /// # Returns
    ///
    /// An [`IirFilterDesign`] with `filter_type = Butterworth` and
    /// `response = LowPass`.
    #[inline]
    #[must_use]
    pub const fn butterworth_lowpass(order: NonZeroUsize, cutoff_frequency: f64) -> Self {
        Self {
            filter_type: IirFilterType::Butterworth,
            response: FilterResponse::LowPass,
            order,
            cutoff_frequency: Some(cutoff_frequency),
            low_frequency: None,
            high_frequency: None,
            passband_ripple: None,
            stopband_attenuation: None,
        }
    }

    /// Creates a Butterworth high-pass filter design.
    ///
    /// # Arguments
    ///
    /// – `order` – filter order; higher values yield a steeper roll-off.\
    /// – `cutoff_frequency` – –3 dB point in Hz; must be in `(0, nyquist)`.
    ///
    /// # Returns
    ///
    /// An [`IirFilterDesign`] with `filter_type = Butterworth` and
    /// `response = HighPass`.
    #[inline]
    #[must_use]
    pub const fn butterworth_highpass(order: NonZeroUsize, cutoff_frequency: f64) -> Self {
        Self {
            filter_type: IirFilterType::Butterworth,
            response: FilterResponse::HighPass,
            order,
            cutoff_frequency: Some(cutoff_frequency),
            low_frequency: None,
            high_frequency: None,
            passband_ripple: None,
            stopband_attenuation: None,
        }
    }

    /// Creates a Butterworth band-pass filter design.
    ///
    /// # Arguments
    ///
    /// – `order` – filter order per edge; total order is `2 × order` for a band-pass.\
    /// – `low_frequency` – lower –3 dB edge in Hz; must be positive and less than `high_frequency`.\
    /// – `high_frequency` – upper –3 dB edge in Hz; must be less than the Nyquist frequency.
    ///
    /// # Returns
    ///
    /// An [`IirFilterDesign`] with `filter_type = Butterworth` and `response = BandPass`.
    #[inline]
    #[must_use]
    pub const fn butterworth_bandpass(
        order: NonZeroUsize,
        low_frequency: f64,
        high_frequency: f64,
    ) -> Self {
        Self {
            filter_type: IirFilterType::Butterworth,
            response: FilterResponse::BandPass,
            order,
            cutoff_frequency: None,
            low_frequency: Some(low_frequency),
            high_frequency: Some(high_frequency),
            passband_ripple: None,
            stopband_attenuation: None,
        }
    }

    /// Creates a Chebyshev Type I filter design.
    ///
    /// Chebyshev Type I filters achieve a steeper transition than Butterworth
    /// designs of the same order by allowing controlled ripple in the passband.
    ///
    /// # Arguments
    ///
    /// – `response` – frequency response shape (`LowPass` or `HighPass`).\
    /// – `order` – filter order; higher values yield a steeper roll-off.\
    /// – `cutoff_frequency` – passband edge frequency in Hz.\
    /// – `passband_ripple` – peak-to-peak ripple in dB within the passband;
    ///   must be positive. Typical values are 0.5–3.0 dB.
    ///
    /// # Returns
    ///
    /// An [`IirFilterDesign`] with `filter_type = ChebyshevI`.
    #[inline]
    #[must_use]
    pub const fn chebyshev_i(
        response: FilterResponse,
        order: NonZeroUsize,
        cutoff_frequency: f64,
        passband_ripple: f64,
    ) -> Self {
        Self {
            filter_type: IirFilterType::ChebyshevI,
            response,
            order,
            cutoff_frequency: Some(cutoff_frequency),
            low_frequency: None,
            high_frequency: None,
            passband_ripple: Some(passband_ripple),
            stopband_attenuation: None,
        }
    }
}

/// Parametric equaliser band types.
///
/// Each band type defines how gain is applied across the frequency spectrum
/// relative to a centre or cutoff frequency.
#[cfg(feature = "parametric-eq")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum EqBandType {
    /// Peaking (bell) filter.
    ///
    /// Boosts or attenuates a narrow frequency region centred at the target
    /// frequency. Positive gain produces a peak; negative gain produces a notch.
    #[default]
    Peak,

    /// Low-shelf filter.
    ///
    /// Applies a broadband boost or cut to frequencies below the corner
    /// frequency.
    LowShelf,

    /// High-shelf filter.
    ///
    /// Applies a broadband boost or cut to frequencies above the corner
    /// frequency.
    HighShelf,

    /// Low-pass filter.
    ///
    /// Attenuates frequencies above the cutoff frequency.
    LowPass,

    /// High-pass filter.
    ///
    /// Attenuates frequencies below the cutoff frequency.
    HighPass,

    /// Band-pass filter.
    ///
    /// Preserves frequencies within a specified band while attenuating
    /// frequencies outside that range.
    BandPass,

    /// Band-stop (notch) filter.
    ///
    /// Attenuates frequencies within a specified band while preserving
    /// frequencies outside that range.
    BandStop,
}

#[cfg(feature = "parametric-eq")]
impl Display for EqBandType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Peak => "peak",
            Self::LowShelf => "low_shelf",
            Self::HighShelf => "high_shelf",
            Self::LowPass => "lowpass",
            Self::HighPass => "highpass",
            Self::BandPass => "bandpass",
            Self::BandStop => "bandstop",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "parametric-eq")]
impl FromStr for EqBandType {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "peak" | "bell" | "notch" => Ok(Self::Peak),
            "low_shelf" | "low-shelf" | "lowshelf" | "ls" => Ok(Self::LowShelf),
            "high_shelf" | "high-shelf" | "highshelf" | "hs" => Ok(Self::HighShelf),
            "lowpass" | "low-pass" | "lp" => Ok(Self::LowPass),
            "highpass" | "high-pass" | "hp" => Ok(Self::HighPass),
            "bandpass" | "band-pass" | "bp" => Ok(Self::BandPass),
            "bandstop" | "band-stop" | "bs" => Ok(Self::BandStop),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "peak",
                    "bell",
                    "low_shelf",
                    "high_shelf",
                    "lowpass",
                    "highpass",
                    "bandpass",
                    "bandstop",
                ]
            ))),
        }
    }
}

#[cfg(feature = "parametric-eq")]
impl TryFrom<&str> for EqBandType {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// A single band in a parametric equaliser.
///
/// ## Purpose
///
/// Describes one frequency-shaping stage: its type (peak, shelf, pass filter),
/// the target frequency, the amount of gain or attenuation, and the bandwidth.
///
/// ## Intended Usage
///
/// Construct with one of the type-specific helpers (`peak`, `low_shelf`, etc.)
/// and add to a [`ParametricEq`] instance. Individual bands can be toggled
/// without removing them via [`set_enabled`][EqBand::set_enabled]. Call
/// [`validate`][EqBand::validate] with the target sample rate to check that
/// frequency and gain values are within acceptable limits.
///
/// ## Invariants
///
/// - `frequency` must be in `(0, nyquist)` where `nyquist = sample_rate / 2`.
/// - `q_factor` must be positive.
/// - `gain_db` must be within `[-40.0, 40.0]` dB.
///
/// ## Assumptions
///
/// The `gain_db` field is ignored by `LowPass` and `HighPass` band types;
/// those bands always set `gain_db = 0.0` at construction.
#[cfg(feature = "parametric-eq")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct EqBand {
    /// Shape of the frequency response for this band.
    pub band_type: EqBandType,

    /// Centre frequency for peak/notch bands, or corner frequency for shelves
    /// and pass filters, in Hz.
    pub frequency: f64,

    /// Gain in dB applied within the active region.
    ///
    /// Positive values boost; negative values cut. Ignored by `LowPass` and
    /// `HighPass` band types (always 0.0 for those). Valid range: `[-40.0, 40.0]`.
    pub gain_db: f64,

    /// Quality factor controlling bandwidth.
    ///
    /// Higher Q → narrower affected frequency region; lower Q → wider region.
    /// For shelf filters this controls the shelf slope. Must be positive.
    pub q_factor: f64,

    /// Whether this band is currently active.
    ///
    /// When `false`, the band is bypassed and contributes no gain change.
    pub enabled: bool,
}

#[cfg(feature = "parametric-eq")]
impl EqBand {
    /// Creates a peak (bell) EQ band.
    ///
    /// Boosts or attenuates a frequency region centred at `frequency`. Positive
    /// `gain_db` creates a peak; negative `gain_db` creates a dip.
    ///
    /// # Arguments
    ///
    /// – `frequency` – centre frequency in Hz; must be in `(0, nyquist)`.\
    /// – `gain_db` – gain at the centre frequency; positive = boost, negative = cut.\
    /// – `q_factor` – bandwidth control; higher Q = narrower bell.
    ///
    /// # Returns
    ///
    /// An enabled [`EqBand`] with `band_type = EqBandType::Peak`.
    #[inline]
    #[must_use]
    pub const fn peak(frequency: f64, gain_db: f64, q_factor: f64) -> Self {
        Self {
            band_type: EqBandType::Peak,
            frequency,
            gain_db,
            q_factor,
            enabled: true,
        }
    }

    /// Creates a low-shelf EQ band.
    ///
    /// Applies a broadband boost or cut to all frequencies below `frequency`.
    ///
    /// # Arguments
    ///
    /// – `frequency` – corner (transition) frequency in Hz.\
    /// – `gain_db` – gain applied below the corner; positive = boost, negative = cut.\
    /// – `q_factor` – shelf slope control; 0.707 gives a Butterworth-style shelf.
    ///
    /// # Returns
    ///
    /// An enabled [`EqBand`] with `band_type = EqBandType::LowShelf`.
    #[inline]
    #[must_use]
    pub const fn low_shelf(frequency: f64, gain_db: f64, q_factor: f64) -> Self {
        Self {
            band_type: EqBandType::LowShelf,
            frequency,
            gain_db,
            q_factor,
            enabled: true,
        }
    }

    /// Creates a high-shelf EQ band.
    ///
    /// Applies a broadband boost or cut to all frequencies above `frequency`.
    ///
    /// # Arguments
    ///
    /// – `frequency` – corner (transition) frequency in Hz.\
    /// – `gain_db` – gain applied above the corner; positive = boost, negative = cut.\
    /// – `q_factor` – shelf slope control; 0.707 gives a Butterworth-style shelf.
    ///
    /// # Returns
    ///
    /// An enabled [`EqBand`] with `band_type = EqBandType::HighShelf`.
    #[inline]
    #[must_use]
    pub const fn high_shelf(frequency: f64, gain_db: f64, q_factor: f64) -> Self {
        Self {
            band_type: EqBandType::HighShelf,
            frequency,
            gain_db,
            q_factor,
            enabled: true,
        }
    }

    /// Creates a low-pass filter band.
    ///
    /// Attenuates frequencies above `frequency`. No gain is applied; the band
    /// acts as a filter rather than a boost/cut stage.
    ///
    /// # Arguments
    ///
    /// – `frequency` – cutoff frequency in Hz.\
    /// – `q_factor` – filter resonance; 0.707 gives a Butterworth (maximally flat) response.
    ///
    /// # Returns
    ///
    /// An enabled [`EqBand`] with `band_type = EqBandType::LowPass` and `gain_db = 0.0`.
    #[inline]
    #[must_use]
    pub const fn low_pass(frequency: f64, q_factor: f64) -> Self {
        Self {
            band_type: EqBandType::LowPass,
            frequency,
            gain_db: 0.0,
            q_factor,
            enabled: true,
        }
    }

    /// Creates a high-pass filter band.
    ///
    /// Attenuates frequencies below `frequency`. No gain is applied; the band
    /// acts as a filter rather than a boost/cut stage.
    ///
    /// # Arguments
    ///
    /// – `frequency` – cutoff frequency in Hz.\
    /// – `q_factor` – filter resonance; 0.707 gives a Butterworth (maximally flat) response.
    ///
    /// # Returns
    ///
    /// An enabled [`EqBand`] with `band_type = EqBandType::HighPass` and `gain_db = 0.0`.
    #[inline]
    #[must_use]
    pub const fn high_pass(frequency: f64, q_factor: f64) -> Self {
        Self {
            band_type: EqBandType::HighPass,
            frequency,
            gain_db: 0.0,
            q_factor,
            enabled: true,
        }
    }

    /// Enables or disables this band.
    ///
    /// Disabled bands are bypassed by the EQ processor without being removed.
    ///
    /// # Arguments
    ///
    /// – `enabled` – `true` to activate this band; `false` to bypass it.
    #[inline]
    pub const fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    /// Returns `true` if this band is active.
    #[inline]
    #[must_use]
    pub const fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Validates that the band parameters are consistent with `sample_rate`.
    ///
    /// # Arguments
    ///
    /// – `sample_rate` – the target sample rate in Hz; used to derive the Nyquist limit.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all constraints are satisfied.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if:
    ///
    /// - `frequency` is not in `(0, nyquist)`
    /// - `q_factor` is not positive
    /// - `gain_db` has absolute value greater than 40 dB
    #[inline]
    pub fn validate(self, sample_rate: f64) -> AudioSampleResult<Self> {
        let nyquist = sample_rate / 2.0;

        if self.frequency <= 0.0 || self.frequency >= nyquist {
            return Err(AudioSampleError::Parameter(ParameterError::out_of_range(
                "frequency",
                format!("{} Hz", self.frequency),
                "0",
                format!("{nyquist}"),
                "Frequency must be between 0 and Nyquist frequency",
            )));
        }

        if self.q_factor <= 0.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "q_factor",
                "Q factor must be positive",
            )));
        }

        // Check reasonable gain limits
        if self.gain_db.abs() > 40.0 {
            return Err(AudioSampleError::Parameter(ParameterError::out_of_range(
                "gain_db",
                format!("{} dB", self.gain_db),
                "-40",
                "40",
                "Gain must be within reasonable range",
            )));
        }

        Ok(self)
    }
}

/// A multi-band parametric equaliser configuration.
///
/// ## Purpose
///
/// Holds an ordered collection of [`EqBand`] stages along with a global output
/// gain and a bypass flag. Bands are processed in the order they appear in
/// `bands`.
///
/// ## Intended Usage
///
/// Build the EQ using [`new`][ParametricEq::new] and [`add_band`][ParametricEq::add_band],
/// or use the [`three_band`][ParametricEq::three_band] / [`five_band`][ParametricEq::five_band]
/// presets as a starting point. Pass the result to
/// [`AudioParametricEq::apply_parametric_eq`][crate::operations::AudioParametricEq::apply_parametric_eq].
/// Call [`validate`][ParametricEq::validate] with the target sample rate to check
/// all band parameters before processing.
///
/// ## Invariants
///
/// - Each band in `bands` must satisfy the [`EqBand`] invariants (validated by
///   [`validate`][ParametricEq::validate]).
/// - `output_gain_db` is an unclamped linear gain applied after all bands.
///
/// ## Assumptions
///
/// Bands are applied sequentially; the order in `bands` affects the result for
/// non-minimum-phase chains.
#[cfg(feature = "parametric-eq")]
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct ParametricEq {
    /// Ordered sequence of EQ bands to apply.
    pub bands: Vec<EqBand>,

    /// Output gain in dB applied after all bands.
    ///
    /// Positive values boost the overall output; negative values attenuate it.
    /// Set to `0.0` for unity gain.
    pub output_gain_db: f64,

    /// When `true`, all band processing is skipped and the input passes through unchanged.
    pub bypassed: bool,
}

#[cfg(feature = "parametric-eq")]
impl ParametricEq {
    /// Creates an empty parametric EQ with no bands and unity output gain.
    ///
    /// # Returns
    ///
    /// A [`ParametricEq`] with an empty `bands` vector, `output_gain_db = 0.0`,
    /// and `bypassed = false`.
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            bands: Vec::new(),
            output_gain_db: 0.0,
            bypassed: false,
        }
    }

    /// Appends a band to the end of the processing chain.
    ///
    /// # Arguments
    ///
    /// – `band` – the [`EqBand`] to append.
    #[inline]
    pub fn add_band(&mut self, band: EqBand) {
        self.bands.push(band);
    }

    /// Removes the band at `index` and returns it, or `None` if out of range.
    ///
    /// # Arguments
    ///
    /// – `index` – zero-based position in `bands`.
    ///
    /// # Returns
    ///
    /// `Some(band)` if `index < band_count()`, otherwise `None`.
    #[inline]
    pub fn remove_band(&mut self, index: usize) -> Option<EqBand> {
        if index < self.bands.len() {
            Some(self.bands.remove(index))
        } else {
            None
        }
    }

    /// Returns a shared reference to the band at `index`, or `None` if out of range.
    ///
    /// # Arguments
    ///
    /// – `index` – zero-based position in `bands`.
    #[inline]
    #[must_use]
    pub fn get_band(&self, index: usize) -> Option<&EqBand> {
        self.bands.get(index)
    }

    /// Returns an exclusive reference to the band at `index`, or `None` if out of range.
    ///
    /// # Arguments
    ///
    /// – `index` – zero-based position in `bands`.
    #[inline]
    pub fn get_band_mut(&mut self, index: usize) -> Option<&mut EqBand> {
        self.bands.get_mut(index)
    }

    /// Returns the number of bands currently in the EQ.
    #[inline]
    #[must_use]
    pub const fn band_count(&self) -> usize {
        self.bands.len()
    }

    /// Sets the overall output gain applied after all bands.
    ///
    /// # Arguments
    ///
    /// – `gain_db` – gain in dB; `0.0` is unity, positive values boost, negative values attenuate.
    #[inline]
    pub const fn set_output_gain(&mut self, gain_db: f64) {
        self.output_gain_db = gain_db;
    }

    /// Sets whether the entire EQ is bypassed.
    ///
    /// When bypassed, the input signal passes through without modification.
    ///
    /// # Arguments
    ///
    /// – `bypassed` – `true` to bypass; `false` to enable processing.
    #[inline]
    pub const fn set_bypassed(&mut self, bypassed: bool) {
        self.bypassed = bypassed;
    }

    /// Returns `true` if the EQ is currently bypassed.
    #[inline]
    #[must_use]
    pub const fn is_bypassed(&self) -> bool {
        self.bypassed
    }

    /// Validates all bands against `sample_rate`.
    ///
    /// # Arguments
    ///
    /// – `sample_rate` – the target sample rate in Hz.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if every band passes [`EqBand::validate`].
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] identifying the
    /// index and error of the first failing band.
    #[inline]
    pub fn validate(self, sample_rate: f64) -> AudioSampleResult<Self> {
        for (i, band) in self.bands.iter().enumerate() {
            match band.validate(sample_rate) {
                Ok(_) => {}
                Err(er) => {
                    return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                        "band",
                        format!("Band {}/{} validation error: {}", i, self.bands.len(), er),
                    )));
                }
            }
        }
        Ok(self)
    }

    /// Creates a three-band EQ preset: low shelf, mid peak, high shelf.
    ///
    /// # Arguments
    ///
    /// – `low_freq` – corner frequency of the low shelf in Hz.\
    /// – `low_gain` – gain of the low shelf in dB.\
    /// – `mid_freq` – centre frequency of the mid peak in Hz.\
    /// – `mid_gain` – gain of the mid peak in dB.\
    /// – `mid_q` – Q factor of the mid peak band.\
    /// – `high_freq` – corner frequency of the high shelf in Hz.\
    /// – `high_gain` – gain of the high shelf in dB.
    ///
    /// # Returns
    ///
    /// A [`ParametricEq`] with three bands: low shelf at Q 0.707, mid peak, high shelf at Q 0.707.
    #[inline]
    #[must_use]
    pub fn three_band(
        low_freq: f64,
        low_gain: f64,
        mid_freq: f64,
        mid_gain: f64,
        mid_q: f64,
        high_freq: f64,
        high_gain: f64,
    ) -> Self {
        let mut eq = Self::new();
        eq.add_band(EqBand::low_shelf(low_freq, low_gain, 0.707));
        eq.add_band(EqBand::peak(mid_freq, mid_gain, mid_q));
        eq.add_band(EqBand::high_shelf(high_freq, high_gain, 0.707));
        eq
    }

    /// Creates a five-band EQ preset with all gains at 0 dB.
    ///
    /// Bands: 100 Hz low shelf, 300 Hz peak, 1 kHz peak, 3 kHz peak, 8 kHz high shelf.
    /// All gains are initialised to 0.0 dB; adjust individual bands to taste.
    ///
    /// # Returns
    ///
    /// A [`ParametricEq`] with five bands at unity gain.
    #[inline]
    #[must_use]
    pub fn five_band() -> Self {
        let mut eq = Self::new();
        eq.add_band(EqBand::low_shelf(100.0, 0.0, 0.707));
        eq.add_band(EqBand::peak(300.0, 0.0, 1.0));
        eq.add_band(EqBand::peak(1000.0, 0.0, 1.0));
        eq.add_band(EqBand::peak(3000.0, 0.0, 1.0));
        eq.add_band(EqBand::high_shelf(8000.0, 0.0, 0.707));
        eq
    }
}

#[cfg(feature = "parametric-eq")]
impl Default for ParametricEq {
    fn default() -> Self {
        Self::new()
    }
}

/// Knee characteristic for dynamic range processing.
///
/// Controls how smoothly gain reduction transitions as the signal crosses
/// the threshold.
#[cfg(any(feature = "parametric-eq", feature = "dynamic-range"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum KneeType {
    /// Hard knee.
    ///
    /// Applies an abrupt transition at the threshold, yielding precise
    /// dynamics control at the potential cost of audible artefacts.
    Hard,

    /// Soft knee.
    ///
    /// Applies a gradual transition around the threshold, producing smoother
    /// and more perceptually natural behaviour.
    #[default]
    Soft,
}

#[cfg(any(feature = "parametric-eq", feature = "dynamic-range"))]
impl Display for KneeType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Hard => "hard",
            Self::Soft => "soft",
        };
        f.write_str(s)
    }
}

#[cfg(any(feature = "parametric-eq", feature = "dynamic-range"))]
impl FromStr for KneeType {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "hard" | "hard-knee" | "hardknee" => Ok(Self::Hard),
            "soft" | "soft-knee" | "softknee" => Ok(Self::Soft),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "hard",
                    "hard-knee",
                    "hardknee",
                    "soft",
                    "soft-knee",
                    "softknee"
                ]
            ))),
        }
    }
}

#[cfg(any(feature = "parametric-eq", feature = "dynamic-range"))]
impl TryFrom<&str> for KneeType {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Detection method for dynamic range processing.
///
/// Determines how signal level is estimated for driving gain reduction.
#[cfg(feature = "dynamic-range")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum DynamicRangeMethod {
    /// RMS-based level detection.
    ///
    /// Estimates average signal power over time, producing smoother and more
    /// perceptually stable gain control.
    #[default]
    Rms,

    /// Peak-based level detection.
    ///
    /// Responds to instantaneous signal peaks, providing tight peak control
    /// with increased sensitivity to transients.
    Peak,

    /// Hybrid level detection.
    ///
    /// Combines RMS and peak estimation to balance smoothness and transient
    /// control.
    Hybrid,
}

#[cfg(feature = "dynamic-range")]
impl Display for DynamicRangeMethod {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Rms => "rms",
            Self::Peak => "peak",
            Self::Hybrid => "hybrid",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "dynamic-range")]
impl FromStr for DynamicRangeMethod {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "rms" | "average" | "avg" => Ok(Self::Rms),
            "peak" | "pk" => Ok(Self::Peak),
            "hybrid" | "mixed" | "combo" => Ok(Self::Hybrid),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "rms", "average", "avg", "peak", "pk", "hybrid", "mixed", "combo"
                ]
            ))),
        }
    }
}

#[cfg(feature = "dynamic-range")]
impl TryFrom<&str> for DynamicRangeMethod {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Side-chain configuration for dynamic range processors.
///
/// ## Purpose
///
/// Controls whether gain reduction is driven by the main signal (internal) or
/// by an external control signal, and applies optional frequency shaping to the
/// control path.
///
/// ## Intended Usage
///
/// Attach to [`CompressorConfig::side_chain`] or [`LimiterConfig::side_chain`].
/// Use [`disabled`][SideChainConfig::disabled] (the default) for standard operation.
/// Use [`enabled`][SideChainConfig::enabled] or call [`enable`][SideChainConfig::enable]
/// when side-chain triggering is required. Apply a high-pass filter to the side-chain
/// via [`set_high_pass`][SideChainConfig::set_high_pass] to reduce low-frequency pumping.
///
/// ## Invariants
///
/// - `high_pass_freq`, if `Some`, must be in `(0, nyquist)`.
/// - `low_pass_freq`, if `Some`, must be in `(0, nyquist)`.
/// - If both are `Some`, `high_pass_freq < low_pass_freq`.
/// - `external_mix` must be in `[0.0, 1.0]`.
#[cfg(feature = "dynamic-range")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct SideChainConfig {
    /// Whether side-chain processing is active.
    ///
    /// When `false` the processor uses its own output as the gain-reduction signal.
    pub enabled: bool,

    /// Optional high-pass filter on the side-chain signal, in Hz.
    ///
    /// Filtering out low frequencies prevents bass content from triggering
    /// excessive gain reduction ("pumping"). Must be in `(0, nyquist)` if `Some`.
    pub high_pass_freq: Option<f64>,

    /// Optional low-pass filter on the side-chain signal, in Hz.
    ///
    /// Limits the frequency range that can trigger gain reduction.
    /// Must be in `(0, nyquist)` and greater than `high_pass_freq` if both are `Some`.
    pub low_pass_freq: Option<f64>,

    /// Pre-emphasis gain applied to the side-chain signal in dB.
    ///
    /// Positive values make specific frequencies trigger gain reduction more
    /// easily. `0.0` disables pre-emphasis.
    pub pre_emphasis_db: f64,

    /// Mix between the internal signal (`0.0`) and external side-chain (`1.0`).
    ///
    /// Must be in `[0.0, 1.0]`. Values between 0 and 1 blend both sources.
    pub external_mix: f64,
}

#[cfg(feature = "dynamic-range")]
impl SideChainConfig {
    /// Creates a disabled side-chain configuration (the default).
    ///
    /// The processor uses its own output as the gain-detection signal.
    ///
    /// # Returns
    ///
    /// A [`SideChainConfig`] with `enabled = false` and no filter frequencies.
    #[inline]
    #[must_use]
    pub const fn disabled() -> Self {
        Self {
            enabled: false,
            high_pass_freq: None,
            low_pass_freq: None,
            pre_emphasis_db: 0.0,
            external_mix: 0.0,
        }
    }

    /// Creates an enabled side-chain configuration with a default 100 Hz high-pass filter.
    ///
    /// The 100 Hz high-pass filter reduces bass-driven pumping artefacts.
    ///
    /// # Returns
    ///
    /// A [`SideChainConfig`] with `enabled = true`, `high_pass_freq = Some(100.0)`,
    /// and `external_mix = 1.0`.
    #[inline]
    #[must_use]
    pub const fn enabled() -> Self {
        Self {
            enabled: true,
            high_pass_freq: Some(100.0),
            low_pass_freq: None,
            pre_emphasis_db: 0.0,
            external_mix: 1.0,
        }
    }

    /// Activates side-chain processing.
    #[inline]
    pub const fn enable(&mut self) {
        self.enabled = true;
    }

    /// Deactivates side-chain processing.
    #[inline]
    pub const fn disable(&mut self) {
        self.enabled = false;
    }

    /// Sets the high-pass filter frequency applied to the side-chain signal.
    ///
    /// # Arguments
    ///
    /// – `freq` – cut-off frequency in Hz; must be in `(0, nyquist)`.
    #[inline]
    pub const fn set_high_pass(&mut self, freq: f64) {
        self.high_pass_freq = Some(freq);
    }

    /// Sets the low-pass filter frequency applied to the side-chain signal.
    ///
    /// # Arguments
    ///
    /// – `freq` – cut-off frequency in Hz; must be in `(0, nyquist)` and greater than
    ///   `high_pass_freq` if that is also set.
    #[inline]
    pub const fn set_low_pass(&mut self, freq: f64) {
        self.low_pass_freq = Some(freq);
    }

    /// Validates all side-chain parameters against `sample_rate`.
    ///
    /// # Arguments
    ///
    /// – `sample_rate` – the target sample rate in Hz.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all constraints are satisfied.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if any frequency
    /// is outside `(0, nyquist)`, if `high_pass_freq ≥ low_pass_freq`, or if
    /// `external_mix` is outside `[0.0, 1.0]`.
    #[inline]
    pub fn validate(self, sample_rate: f64) -> AudioSampleResult<Self> {
        if let Some(hp_freq) = self.high_pass_freq
            && (hp_freq <= 0.0 || hp_freq >= sample_rate / 2.0)
        {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "high_pass_freq",
                "High-pass frequency must be between 0 and Nyquist frequency",
            )));
        }

        if let Some(lp_freq) = self.low_pass_freq
            && (lp_freq <= 0.0 || lp_freq >= sample_rate / 2.0)
        {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "low_pass_freq",
                "Low-pass frequency must be between 0 and Nyquist frequency",
            )));
        }

        if let (Some(hp), Some(lp)) = (self.high_pass_freq, self.low_pass_freq)
            && (hp >= lp)
        {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "high_pass_freq",
                "High-pass frequency must be less than low-pass frequency",
            )));
        }

        if self.external_mix < 0.0 || self.external_mix > 1.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "external_mix",
                "External mix must be between0.0 and1.0",
            )));
        }

        Ok(self)
    }
}

#[cfg(feature = "dynamic-range")]
impl Default for SideChainConfig {
    fn default() -> Self {
        Self::disabled()
    }
}

/// Configuration for a dynamic range compressor.
///
/// ## Purpose
///
/// Specifies all parameters governing the compressor's gain-reduction behaviour:
/// threshold, ratio, envelope times, knee shape, level detection, side-chain,
/// and lookahead.
///
/// ## Intended Usage
///
/// Use [`CompressorConfig::new`] (which delegates to [`Default`]) or one of the
/// presets (`vocal`, `drum`, `bus`) as a starting point. Call
/// [`validate`][CompressorConfig::validate] to check parameter bounds before
/// passing to a compressor function.
///
/// ## Invariants
///
/// - `threshold_db` must be ≤ 0.
/// - `ratio` must be ≥ 1.0.
/// - `attack_ms` must be in `[0.01, 1000.0]` ms.
/// - `release_ms` must be in `[1.0, 10000.0]` ms.
/// - `|makeup_gain_db|` must be ≤ 40 dB.
/// - `knee_width_db` must be in `[0.0, 20.0]` dB.
/// - `lookahead_ms` must be in `[0.0, 20.0]` ms.
/// - `side_chain` must satisfy its own invariants.
#[cfg(feature = "dynamic-range")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct CompressorConfig {
    /// Level above which gain reduction begins, in dBFS.
    ///
    /// Must be ≤ 0. Typical range: `[-40.0, 0.0]`.
    pub threshold_db: f64,

    /// Ratio of input level change to output level change above the threshold.
    ///
    /// `1.0` = no compression; `∞` (very large) = limiting. Must be ≥ 1.0.
    /// Typical values: `2.0` (light) to `10.0` (heavy).
    pub ratio: f64,

    /// Time for the compressor to reach full gain reduction after a threshold
    /// crossing, in milliseconds.
    ///
    /// Shorter attack catches transients; longer attack lets them through.
    /// Valid range: `[0.01, 1000.0]` ms.
    pub attack_ms: f64,

    /// Time for the compressor to return to unity gain after the signal drops
    /// below the threshold, in milliseconds.
    ///
    /// Shorter release may produce pumping artefacts; longer release sounds
    /// more transparent. Valid range: `[1.0, 10000.0]` ms.
    pub release_ms: f64,

    /// Gain added after compression to compensate for level reduction, in dB.
    ///
    /// Positive values restore loudness lost during gain reduction.
    /// Valid range: `[-40.0, 40.0]` dB.
    pub makeup_gain_db: f64,

    /// Transition shape at the threshold.
    pub knee_type: KneeType,

    /// Width of the soft-knee transition region around the threshold, in dB.
    ///
    /// Only meaningful when `knee_type = KneeType::Soft`. `0.0` is equivalent
    /// to a hard knee. Valid range: `[0.0, 20.0]` dB.
    pub knee_width_db: f64,

    /// Algorithm used to estimate the signal level for gain-reduction decisions.
    pub detection_method: DynamicRangeMethod,

    /// Side-chain routing and filtering configuration.
    pub side_chain: SideChainConfig,

    /// Lookahead delay in milliseconds.
    ///
    /// When greater than `0.0`, the compressor buffers the input and begins
    /// gain reduction before a peak is reached, improving transparency.
    /// Introduces latency equal to `lookahead_ms`. Valid range: `[0.0, 20.0]` ms.
    pub lookahead_ms: f64,
}

#[cfg(feature = "dynamic-range")]
impl CompressorConfig {
    /// Creates a compressor configuration using default values.
    ///
    /// Equivalent to [`CompressorConfig::default()`]: threshold −12 dBFS,
    /// ratio 4:1, attack 5 ms, release 50 ms, soft knee, no makeup gain.
    ///
    /// # Returns
    ///
    /// A [`CompressorConfig`] suitable as a general-purpose starting point.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// A preset optimised for lead vocals.
    ///
    /// Moderate ratio and soft knee for transparent vocal control.
    /// Threshold −18 dBFS, ratio 3:1, attack 2 ms, release 100 ms.
    ///
    /// # Returns
    ///
    /// A [`CompressorConfig`] tuned for typical vocal dynamics.
    #[inline]
    #[must_use]
    pub const fn vocal() -> Self {
        Self {
            threshold_db: -18.0,
            ratio: 3.0,
            attack_ms: 2.0,
            release_ms: 100.0,
            makeup_gain_db: 3.0,
            knee_type: KneeType::Soft,
            knee_width_db: 4.0,
            detection_method: DynamicRangeMethod::Rms,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 0.0,
        }
    }

    /// A preset optimised for drums and percussive material.
    ///
    /// High ratio, fast attack and release, hard knee for punchy transient
    /// control. Threshold −8 dBFS, ratio 6:1, attack 0.1 ms, release 20 ms.
    ///
    /// # Returns
    ///
    /// A [`CompressorConfig`] tuned for percussive signals.
    #[inline]
    #[must_use]
    pub const fn drum() -> Self {
        Self {
            threshold_db: -8.0,
            ratio: 6.0,
            attack_ms: 0.1,
            release_ms: 20.0,
            makeup_gain_db: 2.0,
            knee_type: KneeType::Hard,
            knee_width_db: 0.5,
            detection_method: DynamicRangeMethod::Peak,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 0.0,
        }
    }

    /// A preset for mix-bus (glue) compression.
    ///
    /// Low ratio, slow attack and release, wide soft knee for transparent
    /// cohesion across a full mix. Threshold −20 dBFS, ratio 2:1, attack 10 ms,
    /// release 200 ms.
    ///
    /// # Returns
    ///
    /// A [`CompressorConfig`] tuned for bus compression.
    #[inline]
    #[must_use]
    pub const fn bus() -> Self {
        Self {
            threshold_db: -20.0,
            ratio: 2.0,
            attack_ms: 10.0,
            release_ms: 200.0,
            makeup_gain_db: 1.0,
            knee_type: KneeType::Soft,
            knee_width_db: 6.0,
            detection_method: DynamicRangeMethod::Rms,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 0.0,
        }
    }

    /// Validates all compressor parameters.
    ///
    /// # Arguments
    ///
    /// – `sample_rate` – target sample rate in Hz; forwarded to side-chain validation.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all parameter bounds are satisfied.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] for the first
    /// violated constraint (threshold, ratio, attack, release, makeup gain, knee
    /// width, lookahead, or side-chain).
    #[inline]
    pub fn validate(self, sample_rate: f64) -> AudioSampleResult<Self> {
        if self.threshold_db > 0.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "threshold_db",
                "Threshold should be negative (below 0 dB)",
            )));
        }

        if self.ratio < 1.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "ratio",
                "Ratio must be1.0 or greater",
            )));
        }

        if self.attack_ms < 0.01 || self.attack_ms > 1000.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Attack time must be between 0.01 and 1000 ms",
            )));
        }

        if self.release_ms < 1.0 || self.release_ms > 10000.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Release time must be between1.0 and 10000 ms",
            )));
        }

        if self.makeup_gain_db.abs() > 40.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Makeup gain must be between -40.0 and +40.0 dB",
            )));
        }

        if self.knee_width_db < 0.0 || self.knee_width_db > 20.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Knee width must be between0.0 and 20.0 dB",
            )));
        }

        if self.lookahead_ms < 0.0 || self.lookahead_ms > 20.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Lookahead time must be between0.0 and 20.0 ms",
            )));
        }

        self.side_chain.validate(sample_rate)?;
        Ok(self)
    }
}

#[cfg(feature = "dynamic-range")]
impl Default for CompressorConfig {
    fn default() -> Self {
        Self {
            threshold_db: -12.0,
            ratio: 4.0,
            attack_ms: 5.0,
            release_ms: 50.0,
            makeup_gain_db: 0.0,
            knee_type: KneeType::Soft,
            knee_width_db: 2.0,
            detection_method: DynamicRangeMethod::Rms,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 0.0,
        }
    }
}

/// Configuration for a true-peak limiter.
///
/// ## Purpose
///
/// Specifies all parameters controlling the limiter's ceiling enforcement:
/// the maximum output level, envelope times, knee shape, detection method,
/// side-chain routing, lookahead, and optional inter-sample peak (ISP) limiting.
///
/// ## Intended Usage
///
/// Use one of the presets (`transparent`, `mastering`, `broadcast`) or build
/// from scratch with [`LimiterConfig::new`]. Call
/// [`validate`][LimiterConfig::validate] before passing to a limiter function.
///
/// ## Invariants
///
/// - `ceiling_db` must be ≤ 0.
/// - `attack_ms` must be in `[0.001, 100.0]` ms.
/// - `release_ms` must be in `[1.0, 10000.0]` ms.
/// - `knee_width_db` must be in `[0.0, 10.0]` dB.
/// - `lookahead_ms` must be in `[0.0, 20.0]` ms.
/// - `side_chain` must satisfy its own invariants.
#[cfg(feature = "dynamic-range")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct LimiterConfig {
    /// Maximum allowable output level in dBFS.
    ///
    /// No output sample will exceed this level when limiting is active.
    /// Must be ≤ 0. Typical range: `[-3.0, -0.1]`.
    pub ceiling_db: f64,

    /// Time for the limiter to react to a signal approaching the ceiling, in ms.
    ///
    /// Very short attack (< 1 ms) enables brick-wall peak control.
    /// Valid range: `[0.001, 100.0]` ms.
    pub attack_ms: f64,

    /// Time to return to unity gain after the signal recedes, in ms.
    ///
    /// Valid range: `[1.0, 10000.0]` ms.
    pub release_ms: f64,

    /// Transition shape at the ceiling.
    pub knee_type: KneeType,

    /// Width of the soft-knee transition region in dB.
    ///
    /// Only meaningful when `knee_type = KneeType::Soft`. Valid range: `[0.0, 10.0]` dB.
    pub knee_width_db: f64,

    /// Algorithm used to estimate peak or RMS level for gain-reduction decisions.
    pub detection_method: DynamicRangeMethod,

    /// Side-chain routing and filtering configuration.
    pub side_chain: SideChainConfig,

    /// Lookahead delay in milliseconds.
    ///
    /// Buffers audio and begins gain reduction ahead of approaching peaks,
    /// enabling cleaner limiting at the cost of added latency.
    /// Valid range: `[0.0, 20.0]` ms.
    pub lookahead_ms: f64,

    /// Whether inter-sample peak (ISP) limiting is enabled.
    ///
    /// When `true`, the limiter accounts for peaks that may exceed 0 dBFS when
    /// the digital signal is reconstructed via a DAC, preventing audible
    /// clipping on playback. Recommended for mastering and broadcast delivery.
    pub isp_limiting: bool,
}

#[cfg(feature = "dynamic-range")]
impl LimiterConfig {
    /// Creates a limiter configuration with every parameter specified explicitly.
    ///
    /// # Arguments
    ///
    /// – `ceiling_db` – maximum output level in dBFS; must be ≤ 0.\
    /// – `attack_ms` – attack time in milliseconds; must be in `[0.001, 100.0]`.\
    /// – `release_ms` – release time in milliseconds; must be in `[1.0, 10000.0]`.\
    /// – `knee_type` – transition shape at the ceiling.\
    /// – `knee_width_db` – soft-knee width in dB; in `[0.0, 10.0]`.\
    /// – `detection_method` – level estimation algorithm.\
    /// – `lookahead_ms` – lookahead delay in milliseconds; in `[0.0, 20.0]`.\
    /// – `isp_filtering` – whether inter-sample peak protection is enabled.
    ///
    /// # Returns
    ///
    /// A [`LimiterConfig`] with `side_chain` set to `SideChainConfig::disabled()`.
    /// Use [`validate`][LimiterConfig::validate] to verify constraints.
    #[inline]
    #[must_use]
    pub const fn new(
        ceiling_db: f64,
        attack_ms: f64,
        release_ms: f64,
        knee_type: KneeType,
        knee_width_db: f64,
        detection_method: DynamicRangeMethod,
        lookahead_ms: f64,
        isp_filtering: bool,
    ) -> Self {
        Self {
            ceiling_db,
            attack_ms,
            release_ms,
            knee_type,
            knee_width_db,
            detection_method,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms,
            isp_limiting: isp_filtering,
        }
    }

    /// A preset for low-audibility peak limiting.
    ///
    /// Very fast attack and soft knee for transparent operation at −0.1 dBFS.
    /// Suitable for final-stage limiting where audible artefacts are undesirable.
    ///
    /// # Returns
    ///
    /// A [`LimiterConfig`] with ceiling −0.1 dBFS, attack 0.1 ms, release 100 ms,
    /// 5 ms lookahead, and ISP limiting enabled.
    #[inline]
    #[must_use]
    pub const fn transparent() -> Self {
        Self {
            ceiling_db: -0.1,
            attack_ms: 0.1,
            release_ms: 100.0,
            knee_type: KneeType::Soft,
            knee_width_db: 2.0,
            detection_method: DynamicRangeMethod::Peak,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 5.0,
            isp_limiting: true,
        }
    }

    /// A preset for mastering-grade limiting.
    ///
    /// Hybrid detection and a longer lookahead for optimal loudness management
    /// at −0.3 dBFS. ISP limiting enabled for streaming and physical delivery.
    ///
    /// # Returns
    ///
    /// A [`LimiterConfig`] with ceiling −0.3 dBFS, attack 1 ms, release 200 ms,
    /// 10 ms lookahead, and ISP limiting enabled.
    #[inline]
    #[must_use]
    pub const fn mastering() -> Self {
        Self {
            ceiling_db: -0.3,
            attack_ms: 1.0,
            release_ms: 200.0,
            knee_type: KneeType::Soft,
            knee_width_db: 3.0,
            detection_method: DynamicRangeMethod::Hybrid,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 10.0,
            isp_limiting: true,
        }
    }

    /// A preset for broadcast delivery compliance.
    ///
    /// Hard knee and fast attack enforce a strict −1.0 dBFS ceiling suitable
    /// for loudness-normalised delivery formats. ISP limiting enabled.
    ///
    /// # Returns
    ///
    /// A [`LimiterConfig`] with ceiling −1.0 dBFS, attack 0.5 ms, release 50 ms,
    /// 2 ms lookahead, and ISP limiting enabled.
    #[inline]
    #[must_use]
    pub const fn broadcast() -> Self {
        Self {
            ceiling_db: -1.0,
            attack_ms: 0.5,
            release_ms: 50.0,
            knee_type: KneeType::Hard,
            knee_width_db: 0.5,
            detection_method: DynamicRangeMethod::Peak,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 2.0,
            isp_limiting: true,
        }
    }

    /// Validates all limiter parameters.
    ///
    /// # Arguments
    ///
    /// – `sample_rate` – target sample rate in Hz; forwarded to side-chain validation.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all constraints hold.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] for the first
    /// violated constraint (ceiling, attack, release, knee width, lookahead, or
    /// side-chain).
    #[inline]
    pub fn validate(self, sample_rate: f64) -> AudioSampleResult<Self> {
        if self.ceiling_db > 0.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Ceiling should be negative (below 0 dB)",
            )));
        }

        if self.attack_ms < 0.001 || self.attack_ms > 100.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Attack time must be between 0.001 and 100 ms",
            )));
        }

        if self.release_ms < 1.0 || self.release_ms > 10000.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Release time must be between1.0 and 10000 ms",
            )));
        }

        if self.knee_width_db < 0.0 || self.knee_width_db > 10.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Knee width must be between0.0 and 10.0 dB",
            )));
        }

        if self.lookahead_ms < 0.0 || self.lookahead_ms > 20.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Lookahead time must be between0.0 and 20.0 ms",
            )));
        }

        self.side_chain.validate(sample_rate)?;
        Ok(self)
    }
}

#[cfg(feature = "dynamic-range")]
impl Default for LimiterConfig {
    fn default() -> Self {
        Self {
            ceiling_db: -0.1,
            attack_ms: 0.5,
            release_ms: 50.0,
            knee_type: KneeType::Soft,
            knee_width_db: 1.0,
            detection_method: DynamicRangeMethod::Peak,
            side_chain: SideChainConfig::disabled(),
            lookahead_ms: 2.0,
            isp_limiting: true,
        }
    }
}

/// Adaptive thresholding strategy for peak picking.
///
/// Determines how dynamic detection thresholds are estimated from the
/// onset strength function over time.
#[cfg(feature = "peak-picking")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum AdaptiveThresholdMethod {
    /// Delta-based adaptive threshold.
    ///
    /// Tracks local maxima and applies a fixed offset to determine the
    /// detection threshold. Responds quickly to rapid changes but can be
    /// sensitive to noise and transient outliers.
    Delta,

    /// Percentile-based adaptive threshold.
    ///
    /// Estimates the threshold from rolling distribution statistics of the
    /// onset strength function, yielding increased robustness at the cost
    /// of slower adaptation.
    #[default]
    Percentile,

    /// Combined adaptive threshold.
    ///
    /// Combines delta-based and percentile-based thresholds to balance
    /// responsiveness and robustness across a wide range of signals.
    Combined,
}

#[cfg(feature = "peak-picking")]
impl Display for AdaptiveThresholdMethod {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::Delta => "delta",
            Self::Percentile => "percentile",
            Self::Combined => "combined",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "peak-picking")]
impl FromStr for AdaptiveThresholdMethod {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "delta" | "offset" => Ok(Self::Delta),
            "percentile" | "quantile" | "pct" => Ok(Self::Percentile),
            "combined" | "hybrid" | "mixed" => Ok(Self::Combined),
            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "delta",
                    "offset",
                    "percentile",
                    "quantile",
                    "pct",
                    "combined",
                    "hybrid",
                    "mixed"
                ]
            ))),
        }
    }
}

#[cfg(feature = "peak-picking")]
impl TryFrom<&str> for AdaptiveThresholdMethod {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Parameters for adaptive threshold estimation in peak picking.
///
/// ## Purpose
///
/// Controls how a dynamic detection threshold is derived from the local
/// statistics of the onset strength function. A well-tuned adaptive threshold
/// adapts to varying signal conditions and reduces both false positives and
/// missed detections compared to a fixed threshold.
///
/// ## Intended Usage
///
/// Use one of the type-specific constructors (`delta`, `percentile`, `combined`)
/// rather than `new` for most scenarios. Attach the result to
/// [`PeakPickingConfig::adaptive_threshold`]. Call
/// [`validate`][AdaptiveThresholdConfig::validate] to check parameter bounds.
///
/// ## Invariants
///
/// - `delta` must be ≥ 0.0.
/// - `percentile` must be in `[0.0, 1.0]`.
/// - `window_size` must be > 0.
/// - `min_threshold` must be ≥ 0.0.
/// - `max_threshold` must be > `min_threshold`.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg(feature = "peak-picking")]
#[non_exhaustive]
pub struct AdaptiveThresholdConfig {
    /// Algorithm used to derive the adaptive threshold.
    pub method: AdaptiveThresholdMethod,

    /// Fixed offset added to local maxima for delta-based thresholding.
    ///
    /// Larger values raise the threshold and reduce false positives at the
    /// cost of potentially missing weak onsets. Must be ≥ 0.0.
    /// Typical range: `[0.01, 0.1]`.
    pub delta: f64,

    /// Percentile of the local distribution used as the threshold.
    ///
    /// Higher percentiles (closer to 1.0) yield a more conservative threshold
    /// and fewer detections. Must be in `[0.0, 1.0]`.
    pub percentile: f64,

    /// Length of the local analysis window in samples.
    ///
    /// Larger windows produce more stable thresholds but respond more slowly
    /// to changes in signal dynamics. Must be > 0.
    pub window_size: usize,

    /// Floor below which the threshold will never drop.
    ///
    /// Prevents over-sensitivity when the onset strength is very low.
    /// Must be ≥ 0.0 and less than `max_threshold`.
    pub min_threshold: f64,

    /// Ceiling above which the threshold will never rise.
    ///
    /// Prevents under-sensitivity when the onset strength is very high.
    /// Must be > `min_threshold`.
    pub max_threshold: f64,
}

#[cfg(feature = "peak-picking")]
impl AdaptiveThresholdConfig {
    /// Creates an adaptive threshold configuration with all fields specified.
    ///
    /// Prefer the type-specific constructors (`delta`, `percentile`, `combined`)
    /// for typical usage.
    ///
    /// # Arguments
    ///
    /// – `method` – threshold estimation algorithm.\
    /// – `delta` – offset for delta-based estimation; must be ≥ 0.0.\
    /// – `percentile` – percentile for distribution-based estimation; in `[0.0, 1.0]`.\
    /// – `window_size` – local analysis window in samples; must be > 0.\
    /// – `min_threshold` – lower clamp on the computed threshold; must be ≥ 0.0.\
    /// – `max_threshold` – upper clamp on the computed threshold; must be > `min_threshold`.
    ///
    /// # Returns
    ///
    /// An [`AdaptiveThresholdConfig`] with the specified values. Use
    /// [`validate`][AdaptiveThresholdConfig::validate] to verify constraints.
    #[inline]
    #[must_use]
    pub const fn new(
        method: AdaptiveThresholdMethod,
        delta: f64,
        percentile: f64,
        window_size: usize,
        min_threshold: f64,
        max_threshold: f64,
    ) -> Self {
        Self {
            method,
            delta,
            percentile,
            window_size,
            min_threshold,
            max_threshold,
        }
    }

    /// Creates a delta-based adaptive threshold configuration.
    ///
    /// # Arguments
    ///
    /// – `delta` – fixed offset added to local maxima; must be ≥ 0.0.\
    /// – `window_size` – local analysis window in samples; must be > 0.
    ///
    /// # Returns
    ///
    /// An [`AdaptiveThresholdConfig`] with `method = Delta`, `percentile = 0.9`,
    /// and threshold bounds `[0.01, 1.0]`.
    #[inline]
    #[must_use]
    pub const fn delta(delta: f64, window_size: usize) -> Self {
        Self {
            method: AdaptiveThresholdMethod::Delta,
            delta,
            percentile: 0.9,
            window_size,
            min_threshold: 0.01,
            max_threshold: 1.0,
        }
    }

    /// Creates a percentile-based adaptive threshold configuration.
    ///
    /// # Arguments
    ///
    /// – `percentile` – distribution percentile used as the threshold; must be in `[0.0, 1.0]`.\
    /// – `window_size` – local analysis window in samples; must be > 0.
    ///
    /// # Returns
    ///
    /// An [`AdaptiveThresholdConfig`] with `method = Percentile`, `delta = 0.05`,
    /// and threshold bounds `[0.01, 1.0]`.
    #[inline]
    #[must_use]
    pub const fn percentile(percentile: f64, window_size: usize) -> Self {
        Self {
            method: AdaptiveThresholdMethod::Percentile,
            delta: 0.05,
            percentile,
            window_size,
            min_threshold: 0.01,
            max_threshold: 1.0,
        }
    }

    /// Creates a combined delta + percentile adaptive threshold configuration.
    ///
    /// # Arguments
    ///
    /// – `delta` – delta offset component; must be ≥ 0.0.\
    /// – `percentile` – percentile component; must be in `[0.0, 1.0]`.\
    /// – `window_size` – local analysis window in samples; must be > 0.
    ///
    /// # Returns
    ///
    /// An [`AdaptiveThresholdConfig`] with `method = Combined` and threshold
    /// bounds `[0.01, 1.0]`.
    #[inline]
    #[must_use]
    pub const fn combined(delta: f64, percentile: f64, window_size: usize) -> Self {
        Self {
            method: AdaptiveThresholdMethod::Combined,
            delta,
            percentile,
            window_size,
            min_threshold: 0.01,
            max_threshold: 1.0,
        }
    }

    /// Sets the lower clamp on the computed threshold.
    ///
    /// # Arguments
    ///
    /// – `min_threshold` – new minimum; must be ≥ 0.0 and < `max_threshold`.
    #[inline]
    pub const fn set_min_threshold(&mut self, min_threshold: f64) {
        self.min_threshold = min_threshold;
    }

    /// Sets the upper clamp on the computed threshold.
    ///
    /// # Arguments
    ///
    /// – `max_threshold` – new maximum; must be > `min_threshold`.
    #[inline]
    pub const fn set_max_threshold(&mut self, max_threshold: f64) {
        self.max_threshold = max_threshold;
    }

    /// Validates all parameter constraints.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all constraints hold.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] for the first
    /// violated constraint: `delta < 0`, `percentile` outside `[0, 1]`,
    /// `window_size == 0`, `min_threshold < 0`, or `max_threshold ≤ min_threshold`.
    #[inline]
    pub fn validate(self) -> AudioSampleResult<Self> {
        if self.delta < 0.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Delta must be non-negative",
            )));
        }

        if self.percentile < 0.0 || self.percentile > 1.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Percentile must be between0.0 and1.0",
            )));
        }

        if self.window_size == 0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Window size must be greater than 0",
            )));
        }

        if self.min_threshold < 0.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Minimum threshold must be non-negative",
            )));
        }

        if self.max_threshold <= self.min_threshold {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Maximum threshold must be greater than minimum threshold",
            )));
        }

        Ok(self)
    }
}

#[cfg(feature = "peak-picking")]
impl Default for AdaptiveThresholdConfig {
    fn default() -> Self {
        Self {
            method: AdaptiveThresholdMethod::Delta,
            delta: 0.05,
            percentile: 0.9,
            window_size: 1024,
            min_threshold: 0.01,
            max_threshold: 1.0,
        }
    }
}

/// Full configuration for onset peak picking.
///
/// ## Purpose
///
/// Governs every stage of the peak-picking pipeline: optional pre-emphasis
/// filtering, optional median smoothing, optional onset-strength normalisation,
/// adaptive threshold estimation, and minimum-separation enforcement.
///
/// ## Intended Usage
///
/// Use one of the content-specific presets (`music`, `speech`, `drums`) or
/// build from scratch with [`PeakPickingConfig::new`]. Attach to the relevant
/// peak-picking call. Call [`validate`][PeakPickingConfig::validate] to check
/// constraints before processing.
///
/// ## Invariants
///
/// - `pre_emphasis_coeff` must be in `[0.0, 1.0]`.
/// - `median_filter_length` must be an odd positive integer.
/// - `adaptive_threshold` must satisfy its own invariants.
///
/// ## Assumptions
///
/// Disabling `pre_emphasis`, `median_filter`, and `normalize_onset_strength`
/// is safe and reduces processing cost at the expense of robustness. These
/// flags are always respected regardless of the chosen preset.
#[cfg(feature = "peak-picking")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct PeakPickingConfig {
    /// Parameters for adaptive threshold estimation.
    pub adaptive_threshold: AdaptiveThresholdConfig,

    /// Minimum distance between two consecutive detected peaks, in samples.
    ///
    /// Prevents multiple detections from a single onset event. At 44.1 kHz,
    /// 512 samples ≈ 11.6 ms.
    pub min_peak_separation: NonZeroUsize,

    /// Whether to apply pre-emphasis (high-pass) filtering before detection.
    ///
    /// Enhances transient content by attenuating slowly-varying components.
    pub pre_emphasis: bool,

    /// First-order high-pass coefficient for pre-emphasis, in `[0.0, 1.0]`.
    ///
    /// Higher values produce stronger emphasis on rapid changes. Only used
    /// when `pre_emphasis = true`.
    pub pre_emphasis_coeff: f64,

    /// Whether to apply a median filter to the onset strength signal.
    ///
    /// Smooths impulsive noise while preserving genuine peak structure.
    pub median_filter: bool,

    /// Kernel length of the median filter; must be an odd positive integer.
    ///
    /// Larger kernels smooth more aggressively but may merge nearby peaks.
    /// Only used when `median_filter = true`.
    pub median_filter_length: NonZeroUsize,

    /// Whether to normalise the onset strength before thresholding.
    ///
    /// Normalisation makes detection thresholds consistent across signals with
    /// different overall levels.
    pub normalize_onset_strength: bool,

    /// Normalisation method applied to onset strength when
    /// `normalize_onset_strength = true`.
    pub normalization_method: NormalizationMethod,
}

#[cfg(feature = "peak-picking")]
impl PeakPickingConfig {
    /// Creates a peak-picking configuration with all fields specified.
    ///
    /// Prefer the content-specific presets (`music`, `speech`, `drums`) for
    /// common scenarios.
    ///
    /// # Arguments
    ///
    /// – `adaptive_threshold` – adaptive threshold parameters.\
    /// – `min_peak_separation` – minimum samples between adjacent peaks.\
    /// – `pre_emphasis` – whether to apply pre-emphasis filtering.\
    /// – `pre_emphasis_coeff` – first-order high-pass coefficient; in `[0.0, 1.0]`.\
    /// – `median_filter` – whether to smooth with a median filter.\
    /// – `median_filter_length` – kernel size; must be an odd positive integer.\
    /// – `normalize_onset_strength` – whether to normalise onset strength.\
    /// – `normalization_method` – normalisation algorithm when `normalize_onset_strength = true`.
    ///
    /// # Returns
    ///
    /// A [`PeakPickingConfig`] with the given values. Use
    /// [`validate`][PeakPickingConfig::validate] to verify constraints.
    #[inline]
    #[must_use]
    pub const fn new(
        adaptive_threshold: AdaptiveThresholdConfig,
        min_peak_separation: NonZeroUsize,
        pre_emphasis: bool,
        pre_emphasis_coeff: f64,
        median_filter: bool,
        median_filter_length: NonZeroUsize,
        normalize_onset_strength: bool,
        normalization_method: NormalizationMethod,
    ) -> Self {
        Self {
            adaptive_threshold,
            min_peak_separation,
            pre_emphasis,
            pre_emphasis_coeff,
            median_filter,
            median_filter_length,
            normalize_onset_strength,
            normalization_method,
        }
    }

    /// A preset optimised for musical onset detection.
    ///
    /// Combined adaptive thresholding, long minimum separation, strong
    /// pre-emphasis, and median filtering for robustness on typical music.
    ///
    /// # Returns
    ///
    /// A [`PeakPickingConfig`] with combined thresholding (delta 0.03, percentile 0.85,
    /// window 2048), min separation 1024 samples, pre-emphasis 0.95, and median filter 5.
    ///
    /// # Panics
    ///
    /// Never panics; hardcoded nonzero values are used.
    #[inline]
    #[must_use]
    pub const fn music() -> Self {
        Self {
            adaptive_threshold: AdaptiveThresholdConfig::combined(0.03, 0.85, 2048),
            min_peak_separation: NonZeroUsize::new(1024).expect("Must be non-zero"),
            pre_emphasis: true,
            pre_emphasis_coeff: 0.95,
            median_filter: true,
            median_filter_length: NonZeroUsize::new(5).expect("Must be non-zero"),
            normalize_onset_strength: true,
            normalization_method: NormalizationMethod::Peak,
        }
    }

    /// A preset optimised for speech onset detection.
    ///
    /// Delta-based thresholding with short minimum separation and a small
    /// median filter to preserve rapid speech transitions.
    ///
    /// # Returns
    ///
    /// A [`PeakPickingConfig`] with delta thresholding (delta 0.07, window 1024),
    /// min separation 256 samples, pre-emphasis 0.98, and median filter 3.
    ///
    /// # Panics
    ///
    /// Never panics; hardcoded nonzero values are used.
    #[inline]
    #[must_use]
    pub const fn speech() -> Self {
        Self {
            adaptive_threshold: AdaptiveThresholdConfig::delta(0.07, 1024),
            min_peak_separation: NonZeroUsize::new(256).expect("Must be non-zero"),
            pre_emphasis: true,
            pre_emphasis_coeff: 0.98,
            median_filter: true,
            median_filter_length: NonZeroUsize::new(3).expect("Must be non-zero"),
            normalize_onset_strength: true,
            normalization_method: NormalizationMethod::Peak,
        }
    }

    /// A preset optimised for drum and percussive onset detection.
    ///
    /// Percentile-based thresholding with very short minimum separation and
    /// no median filtering to preserve sharp transient edges.
    ///
    /// # Returns
    ///
    /// A [`PeakPickingConfig`] with percentile thresholding (0.95, window 512),
    /// min separation 128 samples, pre-emphasis 0.93, and median filter disabled.
    ///
    /// # Panics
    ///
    /// Never panics; hardcoded nonzero values are used.
    #[inline]
    #[must_use]
    pub const fn drums() -> Self {
        Self {
            adaptive_threshold: AdaptiveThresholdConfig::percentile(0.95, 512),
            min_peak_separation: NonZeroUsize::new(128).expect("Must be non-zero"),
            pre_emphasis: true,
            pre_emphasis_coeff: 0.93,
            median_filter: false,
            median_filter_length: NonZeroUsize::new(3).expect("Must be non-zero"),
            normalize_onset_strength: true,
            normalization_method: NormalizationMethod::Peak,
        }
    }

    /// Sets the minimum distance between consecutive detected peaks.
    ///
    /// # Arguments
    ///
    /// – `samples` – minimum separation in samples.
    #[inline]
    pub const fn set_min_peak_separation(&mut self, samples: NonZeroUsize) {
        self.min_peak_separation = samples;
    }

    /// Sets the minimum distance between consecutive peaks, given in milliseconds.
    ///
    /// # Arguments
    ///
    /// – `ms` – minimum separation in milliseconds.\
    /// – `sample_rate` – sample rate in Hz used to convert milliseconds to samples.
    ///
    /// # Panics
    ///
    /// Panics if `ms * sample_rate / 1000.0` rounds to zero, which would produce
    /// an invalid `NonZeroUsize`.
    #[inline]
    pub fn set_min_peak_separation_ms(&mut self, ms: f64, sample_rate: f64) {
        self.min_peak_separation =
            NonZeroUsize::new((ms * sample_rate / 1000.0) as usize).expect("Must be non-zero");
    }

    /// Configures pre-emphasis filtering.
    ///
    /// # Arguments
    ///
    /// – `enabled` – whether to apply pre-emphasis before peak picking.\
    /// – `coeff` – first-order high-pass coefficient; must be in `[0.0, 1.0]`.
    #[inline]
    pub const fn set_pre_emphasis(&mut self, enabled: bool, coeff: f64) {
        self.pre_emphasis = enabled;
        self.pre_emphasis_coeff = coeff;
    }

    /// Configures median smoothing.
    ///
    /// # Arguments
    ///
    /// – `enabled` – whether to apply median filtering to the onset strength.\
    /// – `length` – kernel size; must be an odd positive integer.
    #[inline]
    pub const fn set_median_filter(&mut self, enabled: bool, length: NonZeroUsize) {
        self.median_filter = enabled;
        self.median_filter_length = length;
    }

    /// Validates all parameter constraints.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all constraints hold.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] if
    /// `pre_emphasis_coeff` is outside `[0.0, 1.0]`, `median_filter_length`
    /// is even, or `adaptive_threshold` fails its own validation.
    #[inline]
    pub fn validate(self) -> AudioSampleResult<Self> {
        self.adaptive_threshold.validate()?;

        if self.pre_emphasis_coeff < 0.0 || self.pre_emphasis_coeff > 1.0 {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Pre-emphasis coefficient must be between0.0 and1.0",
            )));
        }

        if self.median_filter_length.get().is_multiple_of(2) {
            return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                "parameter",
                "Median filter length must be a positive odd integer",
            )));
        }

        Ok(self)
    }
}

#[cfg(feature = "peak-picking")]
impl Default for PeakPickingConfig {
    fn default() -> Self {
        Self {
            adaptive_threshold: AdaptiveThresholdConfig::default(),
            min_peak_separation: NonZeroUsize::new(512).expect("Must be non-zero"),
            pre_emphasis: true,
            pre_emphasis_coeff: 0.97,
            median_filter: true,
            median_filter_length: NonZeroUsize::new(3).expect("Must be non-zero"),
            normalize_onset_strength: true,
            normalization_method: NormalizationMethod::Peak,
        }
    }
}

/// Noise colour classification for audio perturbation and synthesis.
///
/// Different noise colours exhibit distinct spectral energy distributions,
/// influencing perceived brightness, smoothness, and temporal structure.
#[cfg(feature = "editing")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum NoiseColor {
    /// White noise.
    ///
    /// Exhibits approximately uniform spectral energy density across the
    /// frequency spectrum, resulting in a bright and broadband character.
    #[default]
    White,

    /// Pink noise.
    ///
    /// Exhibits decreasing spectral energy with increasing frequency,
    /// producing a perceptually balanced spectrum across octaves.
    Pink,

    /// Brown (red) noise.
    ///
    /// Exhibits strongly attenuated high-frequency content, yielding a
    /// smoother and more correlated temporal structure.
    Brown,
}

#[cfg(feature = "editing")]
impl Display for NoiseColor {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let s = match self {
            Self::White => "white",
            Self::Pink => "pink",
            Self::Brown => "brown",
        };
        f.write_str(s)
    }
}

#[cfg(feature = "editing")]
impl FromStr for NoiseColor {
    type Err = AudioSampleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let normalised = s.trim().to_ascii_lowercase();

        match normalised.as_str() {
            "white" | "white_noise" | "whitenoise" => Ok(Self::White),
            "pink" | "pink_noise" | "pinknoise" => Ok(Self::Pink),
            "brown" | "brown_noise" | "brownian" | "red" | "red_noise" => Ok(Self::Brown),

            _ => Err(AudioSampleError::parse::<Self, _>(format!(
                "Failed to parse {}. Got {}, must be one of {:?}",
                std::any::type_name::<Self>(),
                s,
                [
                    "white",
                    "white_noise",
                    "whitenoise",
                    "pink",
                    "pink_noise",
                    "pinknoise",
                    "brown",
                    "brownian",
                    "red",
                    "red_noise"
                ]
            ))),
        }
    }
}

#[cfg(feature = "editing")]
impl TryFrom<&str> for NoiseColor {
    type Error = AudioSampleError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        value.parse()
    }
}

/// Type of perturbation applied to audio during data augmentation.
///
/// Each variant encodes all parameters needed for one perturbation operation.
/// Use the associated constructor functions (`gaussian_noise`, `random_gain`,
/// etc.) rather than constructing variants directly, as they set sensible
/// defaults for optional fields.
#[cfg(feature = "editing")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum PerturbationMethod {
    /// Adds coloured Gaussian noise to reach a target signal-to-noise ratio.
    ///
    /// Noise is scaled so the SNR between the unperturbed signal's RMS level
    /// and the added noise matches `target_snr_db`. Valid range: `[-60, 60]` dB.
    GaussianNoise {
        /// Target signal-to-noise ratio in dB. Valid range: `[-60.0, 60.0]`.
        target_snr_db: f64,
        /// Spectral colour of the added noise.
        noise_color: NoiseColor,
    },

    /// Applies a uniformly distributed random gain within a dB range.
    ///
    /// A gain value is drawn from the uniform distribution over
    /// `[min_gain_db, max_gain_db]` and applied equally to all channels.
    RandomGain {
        /// Lower bound of the gain range in dB. Must be < `max_gain_db` and ≥ −40.0.
        min_gain_db: f64,
        /// Upper bound of the gain range in dB. Must be > `min_gain_db` and ≤ 20.0.
        max_gain_db: f64,
    },

    /// Applies a high-pass filter to attenuate low-frequency content.
    ///
    /// Simulates microphone rumble filtering or band-limited recording conditions.
    HighPassFilter {
        /// Cutoff frequency in Hz. Must be in `(0, nyquist)`.
        cutoff_hz: f64,
        /// Filter slope in dB per octave. `None` uses the default 6 dB/octave.
        /// When `Some`, must be in `[0.0, 48.0]` dB/octave.
        slope_db_per_octave: Option<f64>,
    },

    /// Applies a low-pass filter to attenuate high-frequency content.
    ///
    /// Simulates telephone bandwidth or other band-limited environments.
    LowPassFilter {
        /// Cutoff frequency in Hz. Must be in `(0, nyquist)`.
        cutoff_hz: f64,
        /// Filter slope in dB per octave. `None` uses the default 6 dB/octave.
        /// When `Some`, must be in `[0.0, 48.0]` dB/octave.
        slope_db_per_octave: Option<f64>,
    },

    /// Shifts the fundamental frequency by a fixed number of semitones.
    ///
    /// Uses a phase vocoder algorithm to preserve audio duration while shifting
    /// pitch. Valid range: `[-12.0, 12.0]` semitones.
    PitchShift {
        /// Pitch shift in semitones. Positive = higher; negative = lower.
        /// Valid range: `[-12.0, 12.0]`.
        semitones: f64,
        /// Whether to preserve spectral envelope (formants) during pitch shifting.
        ///
        /// When `true`, extracts and reapplies the spectral envelope to maintain
        /// natural vocal timbre. Useful for vocal pitch shifts to avoid "chipmunk"
        /// or "monster" effects. For non-vocal sources, this can typically be `false`.
        preserve_formants: bool,
    },
}

#[cfg(feature = "editing")]
impl PerturbationMethod {
    /// Creates a Gaussian noise perturbation targeting `target_snr_db`.
    ///
    /// # Arguments
    ///
    /// – `target_snr_db` – desired SNR in dB; valid range `[-60.0, 60.0]`.\
    /// – `noise_color` – spectral colour of the added noise.
    ///
    /// # Returns
    ///
    /// A [`PerturbationMethod::GaussianNoise`] variant.
    #[inline]
    #[must_use]
    pub const fn gaussian_noise(target_snr_db: f64, noise_color: NoiseColor) -> Self {
        Self::GaussianNoise {
            target_snr_db,
            noise_color,
        }
    }

    /// Creates a random gain perturbation within `[min_gain_db, max_gain_db]`.
    ///
    /// # Arguments
    ///
    /// – `min_gain_db` – lower bound in dB; must be ≥ −40.0 and < `max_gain_db`.\
    /// – `max_gain_db` – upper bound in dB; must be ≤ 20.0 and > `min_gain_db`.
    ///
    /// # Returns
    ///
    /// A [`PerturbationMethod::RandomGain`] variant.
    #[inline]
    #[must_use]
    pub const fn random_gain(min_gain_db: f64, max_gain_db: f64) -> Self {
        Self::RandomGain {
            min_gain_db,
            max_gain_db,
        }
    }

    /// Creates a high-pass filter perturbation with the default 6 dB/octave slope.
    ///
    /// # Arguments
    ///
    /// – `cutoff_hz` – cutoff frequency in Hz; must be in `(0, nyquist)`.
    ///
    /// # Returns
    ///
    /// A [`PerturbationMethod::HighPassFilter`] variant with `slope_db_per_octave = None`.
    #[inline]
    #[must_use]
    pub const fn high_pass_filter(cutoff_hz: f64) -> Self {
        Self::HighPassFilter {
            cutoff_hz,
            slope_db_per_octave: None,
        }
    }

    /// Creates a high-pass filter perturbation with an explicit filter slope.
    ///
    /// # Arguments
    ///
    /// – `cutoff_hz` – cutoff frequency in Hz; must be in `(0, nyquist)`.\
    /// – `slope_db_per_octave` – roll-off rate; must be in `[0.0, 48.0]` dB/octave.
    ///
    /// # Returns
    ///
    /// A [`PerturbationMethod::HighPassFilter`] variant with the given slope.
    #[inline]
    #[must_use]
    pub const fn high_pass_filter_with_slope(cutoff_hz: f64, slope_db_per_octave: f64) -> Self {
        Self::HighPassFilter {
            cutoff_hz,
            slope_db_per_octave: Some(slope_db_per_octave),
        }
    }

    /// Creates a low-pass filter perturbation.
    ///
    /// # Arguments
    ///
    /// – `cutoff_hz` – cutoff frequency in Hz; must be in `(0, nyquist)`.\
    /// – `slope_db_per_octave` – roll-off rate. `None` uses the default 6 dB/octave;
    ///   when `Some`, must be in `[0.0, 48.0]` dB/octave.
    ///
    /// # Returns
    ///
    /// A [`PerturbationMethod::LowPassFilter`] variant.
    #[inline]
    #[must_use]
    pub const fn low_pass_filter(cutoff_hz: f64, slope_db_per_octave: Option<f64>) -> Self {
        Self::LowPassFilter {
            cutoff_hz,
            slope_db_per_octave,
        }
    }

    /// Creates a pitch-shift perturbation using phase vocoder algorithm.
    ///
    /// Preserves audio duration while shifting pitch. Uses STFT-based phase vocoder
    /// for high-quality results. When formant preservation is enabled, maintains
    /// vocal timbre by preserving spectral envelope.
    ///
    /// # Arguments
    ///
    /// – `semitones` – shift amount; positive = higher, negative = lower. Valid range `[-12.0, 12.0]`.\
    /// – `preserve_formants` – whether to preserve spectral envelope (formants); recommended for vocal content.
    ///
    /// # Returns
    ///
    /// A [`PerturbationMethod::PitchShift`] variant.
    #[inline]
    #[must_use]
    pub const fn pitch_shift(semitones: f64, preserve_formants: bool) -> Self {
        Self::PitchShift {
            semitones,
            preserve_formants,
        }
    }

    /// Validates all perturbation parameters against `sample_rate`.
    ///
    /// # Arguments
    ///
    /// – `sample_rate` – sample rate in Hz; used to compute the Nyquist limit for
    ///   frequency bounds validation.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if all constraints are satisfied.
    ///
    /// # Errors
    ///
    /// Returns [crate::AudioSampleError::Parameter][crate::AudioSampleError] describing the
    /// first violated constraint.
    #[inline]
    pub fn validate(self, sample_rate: f64) -> AudioSampleResult<Self> {
        match self {
            Self::GaussianNoise { target_snr_db, .. } => {
                if !(-60.0..=60.0).contains(&target_snr_db) {
                    return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                        "parameter",
                        "Target SNR should be between -60 and 60 dB",
                    )));
                }
            }
            Self::RandomGain {
                min_gain_db,
                max_gain_db,
            } => {
                if min_gain_db >= max_gain_db {
                    return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                        "parameter",
                        "Minimum gain must be less than maximum gain",
                    )));
                }
                if min_gain_db < -40.0 || max_gain_db > 20.0 {
                    return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                        "parameter",
                        "Gain values should be between -40 dB and +20 dB",
                    )));
                }
            }
            Self::HighPassFilter {
                cutoff_hz,
                slope_db_per_octave,
            }
            | Self::LowPassFilter {
                cutoff_hz,
                slope_db_per_octave,
            } => {
                let nyquist = sample_rate / 2.0;
                if cutoff_hz <= 0.0 || cutoff_hz >= nyquist {
                    return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                        "parameter",
                        format!("Cutoff frequency must be between 0 and Nyquist ({nyquist:.1} Hz)"),
                    )));
                }
                if let Some(slope) = slope_db_per_octave
                    && !(0.0..=48.0).contains(&slope)
                {
                    return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                        "parameter",
                        "Slope must be between 0 and 48 dB/octave",
                    )));
                }
            }
            Self::PitchShift { semitones, .. } => {
                if semitones.abs() > 12.0 {
                    return Err(AudioSampleError::Parameter(ParameterError::invalid_value(
                        "parameter",
                        "Pitch shift should be between -12 and +12 semitones",
                    )));
                }
            }
        }
        Ok(self)
    }
}

/// Configuration for a single audio perturbation operation.
///
/// ## Purpose
///
/// Pairs a [`PerturbationMethod`] with an optional random seed to allow both
/// stochastic and fully reproducible perturbation pipelines.
///
/// ## Intended Usage
///
/// Use [`PerturbationConfig::new`] for randomised perturbation or
/// [`PerturbationConfig::with_seed`] when reproducibility is required.
/// Call [`validate`][PerturbationConfig::validate] with the target sample rate
/// before passing to a perturbation function.
///
/// ## Invariants
///
/// The `method` field must satisfy its own parameter constraints (checked by
/// [`validate`][PerturbationConfig::validate]).
///
/// ## Assumptions
///
/// When `seed` is `None`, the implementation uses the thread-local random
/// number generator; results will vary between runs.
#[cfg(feature = "editing")]
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct PerturbationConfig {
    /// The perturbation algorithm and its parameters.
    pub method: PerturbationMethod,

    /// Optional seed for the random number generator.
    ///
    /// When `Some`, the perturbation is fully reproducible. When `None`,
    /// the thread-local RNG is used and results will differ between runs.
    pub seed: Option<u64>,
}

#[cfg(feature = "editing")]
impl PerturbationConfig {
    /// Creates a perturbation configuration using the thread-local RNG.
    ///
    /// # Arguments
    ///
    /// – `method` – the perturbation algorithm and its parameters.
    ///
    /// # Returns
    ///
    /// A [`PerturbationConfig`] with `seed = None`.
    #[inline]
    #[must_use]
    pub const fn new(method: PerturbationMethod) -> Self {
        Self { method, seed: None }
    }

    /// Creates a perturbation configuration with a fixed random seed.
    ///
    /// Using the same `method` and `seed` on identical input guarantees
    /// identical output.
    ///
    /// # Arguments
    ///
    /// – `method` – the perturbation algorithm and its parameters.\
    /// – `seed` – value used to seed the random number generator.
    ///
    /// # Returns
    ///
    /// A [`PerturbationConfig`] with `seed = Some(seed)`.
    #[inline]
    #[must_use]
    pub const fn with_seed(method: PerturbationMethod, seed: u64) -> Self {
        Self {
            method,
            seed: Some(seed),
        }
    }

    /// Validates that the perturbation method parameters are consistent with `sample_rate`.
    ///
    /// # Arguments
    ///
    /// – `sample_rate` – sample rate in Hz; used for frequency bounds checking.
    ///
    /// # Returns
    ///
    /// `Ok(self)` if validation passes; otherwise an
    ///
    /// # Errors
    ///
    /// Returns an [crate::AudioSampleError::Parameter][crate::AudioSampleError] describing the first
    /// [crate::AudioSampleError::Parameter][crate::AudioSampleError].
    #[inline]
    pub fn validate(self, sample_rate: f64) -> AudioSampleResult<Self> {
        self.method.validate(sample_rate)?;
        Ok(self)
    }
}

#[cfg(feature = "editing")]
impl Default for PerturbationConfig {
    fn default() -> Self {
        Self::new(PerturbationMethod::GaussianNoise {
            target_snr_db: 20.0,
            noise_color: NoiseColor::White,
        })
    }
}