audio_samples 2.0.1

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
//! Statistical analysis operations for [`AudioSamples`].
//!
//! This module implements the [`AudioStatistics`] trait, providing statistical
//! and signal analysis methods for mono and multi-channel audio data. Operations
//! cover both time-domain statistics and, when the `fft` feature is enabled,
//! frequency-domain descriptors computed via the [`spectrograms`] crate.
//!
//! Statistical measures are a core part of audio analysis. Isolating them into a
//! single trait keeps the [`AudioSamples`] API organised and lets users access
//! only the statistical surface they need. This module is the sole implementor of
//! [`AudioStatistics`] for [`AudioSamples`].
//!
//! All operations are available on any [`AudioSamples<T>`] where `T` is a
//! supported sample type (`u8`, `i16`, `I24`, `i32`, `f32`, `f64`). Import
//! [`AudioStatistics`] and call methods on your audio directly.
//!
//! ### Time-domain statistics
//! - [`peak`](AudioStatistics::peak): Maximum absolute amplitude value.
//! - [`min_sample`](AudioStatistics::min_sample): Minimum sample value.
//! - [`max_sample`](AudioStatistics::max_sample): Maximum sample value.
//! - [`mean`](AudioStatistics::mean): Arithmetic mean of all samples.
//! - [`midpoint_sample`](AudioStatistics::midpoint_sample): Temporal midpoint value of a mono signal (the middle sample by position).
//! - [`rms`](AudioStatistics::rms): Root Mean Square energy.
//! - [`variance`](AudioStatistics::variance): Variance of sample values.
//! - [`std_dev`](AudioStatistics::std_dev): Standard deviation of sample values.
//!
//! ### Temporal signal properties
//! - [`zero_crossings`](AudioStatistics::zero_crossings): Counts sign changes in the waveform.
//! - [`zero_crossing_rate`](AudioStatistics::zero_crossing_rate): Normalised zero-crossing frequency (per second).
//! - [`cross_correlation`](AudioStatistics::cross_correlation): Computes correlation between two signals.
//!
//! ### Frequency-domain features *(requires `fft` feature)*
//! - [`autocorrelation`](AudioStatistics::autocorrelation): Signal self-similarity up to a given lag.
//! - [`spectral_centroid`](AudioStatistics::spectral_centroid): Spectral center of mass (mono only).
//! - [`spectral_rolloff`](AudioStatistics::spectral_rolloff): Frequency below which a given proportion of spectral energy lies.
//!
//! ## Example: Time-domain analysis
//!
//! ```rust
//! use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
//! use ndarray::array;
//!
//! let data = array![1.0f32, -1.0, 0.5, -0.5];
//! let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
//!
//! let peak = audio.peak();
//! let rms  = audio.rms();
//! let mean = audio.mean();
//!
//! println!("Peak: {peak}, RMS: {rms:.4}, Mean: {mean:.4}");
//! ```
//!
//! ## Example: Frequency-domain analysis
//!
//! ```rust,ignore
//! // Requires the "transforms" feature.
//! use audio_samples::{sine_wave, AudioSamples, AudioStatistics, sample_rate};
//! use audio_samples::operations::types::ChannelReduction;
//! use std::time::Duration;
//!
//! let sr       = sample_rate!(44100);
//! let freq     = 1000.0;
//! let duration = Duration::from_secs(1);
//! let audio    = sine_wave::<f64>(freq, duration, sr, 0.5);
//!
//! let centroid = audio.spectral_centroid(ChannelReduction::Error).unwrap();
//! let rolloff  = audio.spectral_rolloff(0.85, ChannelReduction::Error).unwrap();
//!
//! println!("Spectral centroid: {:.2} Hz", centroid);
//! println!("Spectral rolloff (85%): {:.2} Hz", rolloff);
//! ```
//!
//! ## Error Handling
//!
//! Most operations return plain numeric results. Methods that perform FFT
//! computation or validate input parameters return [`crate::AudioSampleResult`].
//! An [`crate::AudioSampleError::Processing`] is returned for mathematical
//! failures, and [`crate::AudioSampleError::Parameter`] for invalid arguments.
//!
//! ## Feature Flags
//!
//! - `fft`: Enables spectral and autocorrelation analyses via the
//!   [`spectrograms`] crate.
//!
//! ## See Also
//!
//! - [`AudioSamples`]: The core data structure providing access to underlying
//!   sample buffers.
//! - [`AudioStatistics`]: The trait defining all statistical operations.
//!
//! [`AudioStatistics`]: crate::operations::traits::AudioStatistics

use std::num::NonZeroUsize;

use crate::operations::traits::AudioStatistics;
#[cfg(feature = "transforms")]
use crate::operations::types::ChannelReduction;
use crate::repr::AudioData;
use crate::traits::StandardSample;
use crate::{AudioSampleResult, AudioSamples, ParameterError};

#[cfg(feature = "transforms")]
use crate::{AudioSampleError, ProcessingError};
#[cfg(feature = "transforms")]
use num_complex::Complex;

#[cfg(feature = "transforms")]
use non_empty_slice::NonEmptySlice;

use ndarray::Axis;
use non_empty_slice::NonEmptyVec;

#[cfg(feature = "transforms")]
use spectrograms::FftPlanner;

#[cfg(feature = "transforms")]
use std::cell::RefCell;

#[cfg(feature = "transforms")]
thread_local! {
    /// Thread-local [`FftPlanner`] reused across all FFT-based statistics in
    /// this module. `spectrograms::FftPlanner` wraps a real-FFT planner that
    /// memoizes plans by size internally, so reconstructing it per call
    /// discarded that cache. One planner per thread lets repeated same-size
    /// transforms reuse cached plans. The planner is `!Sync`, so `thread_local`
    /// is the correct scope.
    static FFT_PLANNER: RefCell<FftPlanner> = RefCell::new(FftPlanner::new());
}

/// Runs `f` with the thread-local cached [`FftPlanner`] borrowed mutably.
///
/// The borrow is released as soon as `f` returns.
#[cfg(feature = "transforms")]
#[inline]
fn with_fft_planner<R>(f: impl FnOnce(&mut FftPlanner) -> R) -> R {
    FFT_PLANNER.with(|p| f(&mut p.borrow_mut()))
}

/// Reduces a (possibly multi-channel) signal to a single `f64` sample vector
/// according to `reduction`, for the spectral analysis ops that produce one
/// scalar result.
///
/// Samples are converted with audio-aware [`ConvertTo`](crate::traits::ConvertTo)
/// scaling. Returns an error when `reduction` forbids the channel layout (e.g.
/// [`ChannelReduction::Error`] on multi-channel input) or names an out-of-range
/// channel.
#[cfg(feature = "transforms")]
fn reduce_to_mono_f64<T>(
    audio: &AudioSamples<'_, T>,
    reduction: ChannelReduction,
    operation: &str,
) -> AudioSampleResult<Vec<f64>>
where
    T: StandardSample,
{
    match &audio.data() {
        AudioData::Mono(arr) => Ok(arr.iter().map(|&x| x.convert_to()).collect()),
        AudioData::Multi(multi) => {
            let view = multi.as_view();
            let num_channels = view.nrows();
            match reduction {
                ChannelReduction::Error => Err(crate::AudioSampleError::Parameter(
                    ParameterError::invalid_value(
                        "channels",
                        format!(
                            "{operation} is only defined for mono signals; pass a \
                             ChannelReduction other than `Error` to reduce \
                             {num_channels} channels to one"
                        ),
                    ),
                )),
                ChannelReduction::First => {
                    Ok(view.row(0).iter().map(|&x| x.convert_to()).collect())
                }
                ChannelReduction::Channel(idx) => {
                    if idx >= num_channels {
                        return Err(crate::AudioSampleError::Parameter(
                            ParameterError::out_of_range(
                                "channel",
                                idx.to_string(),
                                "0",
                                (num_channels - 1).to_string(),
                                format!(
                                    "{operation}: channel index {idx} out of range for \
                                     {num_channels} channels"
                                ),
                            ),
                        ));
                    }
                    Ok(view.row(idx).iter().map(|&x| x.convert_to()).collect())
                }
                ChannelReduction::Average => {
                    let n = view.ncols();
                    let inv = 1.0 / num_channels as f64;
                    let mut out = vec![0.0f64; n];
                    for row in view.rows() {
                        for (o, &x) in out.iter_mut().zip(row.iter()) {
                            let v: f64 = x.convert_to();
                            *o += v * inv;
                        }
                    }
                    Ok(out)
                }
            }
        }
    }
}

impl<T> AudioStatistics for AudioSamples<'_, T>
where
    T: StandardSample,
{
    /// Returns the peak (maximum absolute value) across all samples and channels.
    ///
    /// # Returns
    /// The maximum absolute sample value, in the native sample type `T`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, -3.0, 2.5, -1.5];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// assert_eq!(audio.peak(), 3.0);
    /// ```
    #[inline]
    fn peak(&self) -> T {
        // Four independent accumulators let LLVM's SLP vectoriser pack them into
        // a single SIMD register (e.g. 4-wide ymm for f32) without requiring
        // -ffast-math. Falls back to ndarray fold for non-contiguous views.
        let zero = T::default();
        let abs_max_slice = |slice: &[T]| -> T {
            if let Some(result) = T::avx2_abs_max(slice) {
                return result;
            }
            let mut acc = [zero; 4];
            let chunks = slice.chunks_exact(4);
            let rem = chunks.remainder();
            for chunk in chunks {
                for j in 0..4 {
                    let x = chunk[j];
                    let ax = if x < zero { zero - x } else { x };
                    if ax > acc[j] {
                        acc[j] = ax;
                    }
                }
            }
            for &x in rem {
                let ax = if x < zero { zero - x } else { x };
                if ax > acc[0] {
                    acc[0] = ax;
                }
            }
            acc.iter().fold(zero, |a, &b| if b > a { b } else { a })
        };
        let fold_ndarray = |acc: T, &x: &T| {
            let ax = if x < zero { zero - x } else { x };
            if ax > acc { ax } else { acc }
        };
        match &self.data() {
            AudioData::Mono(arr) => match arr.as_slice() {
                Some(s) => abs_max_slice(s),
                None => arr.fold(zero, fold_ndarray),
            },
            AudioData::Multi(arr) => match arr.as_slice() {
                Some(s) => abs_max_slice(s),
                None => arr.fold(zero, fold_ndarray),
            },
        }
    }

    /// Returns the minimum sample value across all channels.
    ///
    /// # Returns
    /// The smallest sample value found, in the native sample type `T`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, -3.0, 2.5, -1.5];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// assert_eq!(audio.min_sample(), -3.0);
    /// ```
    #[inline]
    fn min_sample(&self) -> T {
        match &self.data() {
            AudioData::Mono(arr) => {
                // Use ndarray's efficient fold operation for vectorized minimum finding
                arr.fold(arr[0], |acc, &x| if x < acc { x } else { acc })
            }
            AudioData::Multi(arr) => {
                // Vectorized minimum across entire multi-channel array
                arr.fold(arr[[0, 0]], |acc, &x| if x < acc { x } else { acc })
            }
        }
    }

    /// Returns the maximum sample value across all channels.
    ///
    /// # Returns
    /// The largest sample value found, in the native sample type `T`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, -3.0, 2.5, -1.5];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// assert_eq!(audio.max_sample(), 2.5);
    /// ```
    #[inline]
    fn max_sample(&self) -> T {
        match &self.data() {
            AudioData::Mono(arr) => {
                // Use ndarray's efficient fold operation for vectorized maximum finding
                arr.fold(arr[0], |acc, &x| if x > acc { x } else { acc })
            }
            AudioData::Multi(arr) => {
                // Vectorized maximum across entire multi-channel array
                arr.fold(arr[[0, 0]], |acc, &x| if x > acc { x } else { acc })
            }
        }
    }

    /// Computes the arithmetic mean of all samples across all channels.
    ///
    /// # Returns
    /// The mean value as `f64`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, -1.0, 2.0, -2.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// assert_eq!(audio.mean(), 0.0);
    /// ```
    #[inline]
    fn mean(&self) -> f64 {
        match &self.data() {
            AudioData::Mono(mono_data) => mono_data.mean().cast_into(),
            AudioData::Multi(multi_data) => multi_data.mean().cast_into(),
        }
    }

    /// Returns the value at the temporal midpoint of a mono signal.
    ///
    /// For even-length signals the result is the average of the two samples at
    /// the two central indices. For odd-length signals the single central sample
    /// is returned directly. Samples are selected by index position; the buffer
    /// is not sorted.
    ///
    /// # Returns
    /// `Some(value)` for mono audio, or `None` if the signal is multi-channel.
    ///
    /// # Assumptions
    /// For even-length signals the sum of the two central samples must not
    /// overflow the sample type `T`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, 3.0, 5.0, 7.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// // Central indices 1 and 2: (3.0 + 5.0) / 2.0 = 4.0
    /// assert_eq!(audio.midpoint_sample(), Some(4.0));
    /// ```
    #[inline]
    fn midpoint_sample(&self) -> Option<f64> {
        let mono = self.as_mono()?;
        let mono_len = mono.len().get();
        Some(if mono_len.is_multiple_of(2) {
            let first_idx = (mono_len / 2) - 1;
            let first_val = self[first_idx];
            let second_val = self[first_idx + 1];
            let sum: f64 = (first_val + second_val).cast_into();

            sum / 2.0
        } else {
            self[self.len().get() / 2].cast_into()
        })
    }

    /// Computes the Root Mean Square (RMS) of all samples across all channels.
    ///
    /// RMS is the square root of the mean of squared sample values. It provides
    /// a measure of the signal's average energy and is commonly used for
    /// loudness estimation.
    ///
    /// # Returns
    /// The RMS value as `f64`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, -1.0, 1.0, -1.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// let rms = audio.rms();
    /// assert!((rms - 1.0).abs() < 1e-6);
    /// ```
    #[inline]
    fn rms(&self) -> f64 {
        // Four independent f64 accumulators break the sequential dependency chain
        // so LLVM's SLP vectoriser can pack them into a ymm register (4 f64 wide)
        // without -ffast-math. Falls back to ndarray iter for non-contiguous views.
        let sum_sq_slice = |slice: &[T]| -> f64 {
            let mut acc = [0.0f64; 4];
            let chunks = slice.chunks_exact(4);
            let rem = chunks.remainder();
            for chunk in chunks {
                let f0: f64 = chunk[0].cast_into();
                let f1: f64 = chunk[1].cast_into();
                let f2: f64 = chunk[2].cast_into();
                let f3: f64 = chunk[3].cast_into();
                acc[0] += f0 * f0;
                acc[1] += f1 * f1;
                acc[2] += f2 * f2;
                acc[3] += f3 * f3;
            }
            for &x in rem {
                let f: f64 = x.cast_into();
                acc[0] += f * f;
            }
            acc[0] + acc[1] + acc[2] + acc[3]
        };
        let (sum_sq, n) = match &self.data() {
            AudioData::Mono(arr) => {
                let s = match arr.as_slice() {
                    Some(s) => sum_sq_slice(s),
                    None => arr
                        .iter()
                        .map(|&x| {
                            let f: f64 = x.cast_into();
                            f * f
                        })
                        .sum(),
                };
                (s, arr.len().get())
            }
            AudioData::Multi(arr) => {
                let s = match arr.as_slice() {
                    Some(s) => sum_sq_slice(s),
                    None => arr
                        .iter()
                        .map(|&x| {
                            let f: f64 = x.cast_into();
                            f * f
                        })
                        .sum(),
                };
                (s, arr.len().get())
            }
        };
        (sum_sq / n as f64).sqrt()
    }

    #[inline]
    fn rms_and_peak(&self) -> (f64, T) {
        // Single-pass combined computation with four independent accumulators per
        // quantity. Reading the data once is critical for signals larger than L3
        // cache where two separate calls would double memory bandwidth.
        let zero = T::default();

        let combined_slice = |slice: &[T]| -> (f64, T) {
            // Eight independent accumulators: sq uses 2 ymm f64 registers,
            // pk uses 1 ymm f32 register. Explicit constant-index loads
            // let LLVM keep all 8 values in registers (no re-read from memory)
            // and pack both sq and pk into SIMD via SLP vectorisation.
            let mut sq = [0.0f64; 8];
            let mut pk = [zero; 8];
            let chunks = slice.chunks_exact(8);
            let rem = chunks.remainder();
            for chunk in chunks {
                // Load all 8 elements once; compiler keeps them in registers.
                let x0 = chunk[0];
                let x1 = chunk[1];
                let x2 = chunk[2];
                let x3 = chunk[3];
                let x4 = chunk[4];
                let x5 = chunk[5];
                let x6 = chunk[6];
                let x7 = chunk[7];
                // sq: 8 independent f64 chains → 2 ymm accumulator registers.
                let f0: f64 = x0.cast_into();
                sq[0] += f0 * f0;
                let f1: f64 = x1.cast_into();
                sq[1] += f1 * f1;
                let f2: f64 = x2.cast_into();
                sq[2] += f2 * f2;
                let f3: f64 = x3.cast_into();
                sq[3] += f3 * f3;
                let f4: f64 = x4.cast_into();
                sq[4] += f4 * f4;
                let f5: f64 = x5.cast_into();
                sq[5] += f5 * f5;
                let f6: f64 = x6.cast_into();
                sq[6] += f6 * f6;
                let f7: f64 = x7.cast_into();
                sq[7] += f7 * f7;
                // pk: 8-wide abs-max, SLP-vectorisable to vmaxps + vandps.
                let ax0 = if x0 < zero { zero - x0 } else { x0 };
                if ax0 > pk[0] {
                    pk[0] = ax0;
                }
                let ax1 = if x1 < zero { zero - x1 } else { x1 };
                if ax1 > pk[1] {
                    pk[1] = ax1;
                }
                let ax2 = if x2 < zero { zero - x2 } else { x2 };
                if ax2 > pk[2] {
                    pk[2] = ax2;
                }
                let ax3 = if x3 < zero { zero - x3 } else { x3 };
                if ax3 > pk[3] {
                    pk[3] = ax3;
                }
                let ax4 = if x4 < zero { zero - x4 } else { x4 };
                if ax4 > pk[4] {
                    pk[4] = ax4;
                }
                let ax5 = if x5 < zero { zero - x5 } else { x5 };
                if ax5 > pk[5] {
                    pk[5] = ax5;
                }
                let ax6 = if x6 < zero { zero - x6 } else { x6 };
                if ax6 > pk[6] {
                    pk[6] = ax6;
                }
                let ax7 = if x7 < zero { zero - x7 } else { x7 };
                if ax7 > pk[7] {
                    pk[7] = ax7;
                }
            }
            for &x in rem {
                let f: f64 = x.cast_into();
                sq[0] += f * f;
                let ax = if x < zero { zero - x } else { x };
                if ax > pk[0] {
                    pk[0] = ax;
                }
            }
            let sum_sq = sq[0] + sq[1] + sq[2] + sq[3] + sq[4] + sq[5] + sq[6] + sq[7];
            let peak = pk.iter().fold(zero, |a, &b| if b > a { b } else { a });
            (sum_sq, peak)
        };

        let (sum_sq, peak, n) = match &self.data() {
            AudioData::Mono(arr) => {
                let (sq, pk) = match arr.as_slice() {
                    Some(s) => combined_slice(s),
                    None => {
                        let sq = arr
                            .iter()
                            .map(|&x| {
                                let f: f64 = x.cast_into();
                                f * f
                            })
                            .sum();
                        let pk = arr.fold(zero, |acc, &x| {
                            let ax = if x < zero { zero - x } else { x };
                            if ax > acc { ax } else { acc }
                        });
                        (sq, pk)
                    }
                };
                (sq, pk, arr.len().get())
            }
            AudioData::Multi(arr) => {
                let (sq, pk) = match arr.as_slice() {
                    Some(s) => combined_slice(s),
                    None => {
                        let sq = arr
                            .iter()
                            .map(|&x| {
                                let f: f64 = x.cast_into();
                                f * f
                            })
                            .sum();
                        let pk = arr.fold(zero, |acc, &x| {
                            let ax = if x < zero { zero - x } else { x };
                            if ax > acc { ax } else { acc }
                        });
                        (sq, pk)
                    }
                };
                (sq, pk, arr.len().get())
            }
        };

        ((sum_sq / n as f64).sqrt(), peak)
    }

    /// Computes the population variance of the audio samples.
    ///
    /// Variance measures the spread of sample values around the mean.
    /// For mono audio this is the standard population variance of all samples.
    /// For multi-channel audio the variance is computed per sample position
    /// across channels and the results are averaged over time.
    ///
    /// # Returns
    /// The variance as `f64`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, 2.0, 3.0, 4.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// let variance = audio.variance();
    /// assert!((variance - 1.25).abs() < 1e-6);
    /// ```
    #[inline]
    fn variance(&self) -> f64 {
        match &self.data() {
            AudioData::Mono(mono_data) => mono_data.variance(),
            AudioData::Multi(multi_data) => multi_data
                .variance_axis(Axis(0))
                .mean()
                .expect("Non empty data will produce a mean"),
        }
    }

    /// Computes the standard deviation of the audio samples.
    ///
    /// Standard deviation is the square root of [`AudioStatistics::variance`].
    /// It expresses the spread of sample values in the same units as the
    /// original data.
    ///
    /// # Returns
    /// The standard deviation as `f64`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, 2.0, 3.0, 4.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// let std_dev = audio.std_dev();
    /// assert!((std_dev - 1.25_f64.sqrt()).abs() < 1e-6);
    /// ```
    #[inline]
    fn std_dev(&self) -> f64 {
        self.variance().sqrt()
    }

    /// Counts the number of zero crossings in the audio signal.
    ///
    /// Zero crossings occur when the signal changes sign between adjacent samples.
    /// This metric is useful for pitch detection, signal analysis, and estimating
    /// the noisiness of a signal.
    ///
    /// # Returns
    /// The total number of zero crossings across all channels. Returns 0 if the
    /// audio has fewer than 2 samples.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, -1.0, 1.0, -1.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// assert_eq!(audio.zero_crossings(), 3);
    /// ```
    #[inline]
    fn zero_crossings(&self) -> usize {
        match &self.data() {
            AudioData::Mono(arr) => {
                if arr.len() < nzu!(2) {
                    return 0;
                }

                let mut crossings = 0;
                for i in 1..arr.len().get() {
                    let prev: T = arr[i - 1];
                    let curr: T = arr[i];

                    // Check for sign change (zero crossing)
                    if (prev > T::zero() && curr <= T::zero())
                        || (prev <= T::zero() && curr > T::zero())
                    {
                        crossings += 1;
                    }
                }
                crossings
            }
            AudioData::Multi(arr) => {
                // For multi-channel, count zero crossings in each channel and sum them
                let mut total_crossings = 0;

                for channel in arr.axis_iter(Axis(0)) {
                    if channel.len() < 2 {
                        continue;
                    }

                    for i in 1..channel.len() {
                        let prev: T = channel[i - 1];
                        let curr: T = channel[i];

                        if (prev > T::zero() && curr <= T::zero())
                            || (prev <= T::zero() && curr > T::zero())
                        {
                            total_crossings += 1;
                        }
                    }
                }
                total_crossings
            }
        }
    }

    /// Computes the zero crossing rate (crossings per second).
    ///
    /// This normalises the zero crossing count by the signal duration, providing
    /// a frequency-like measure that is independent of signal length.
    ///
    /// # Returns
    /// The number of zero crossings per second as `f64`.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    ///
    /// let data = array![1.0f32, -1.0, 1.0, -1.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// let zcr = audio.zero_crossing_rate();
    /// assert!(zcr > 0.0);
    /// ```
    #[inline]
    fn zero_crossing_rate(&self) -> f64 {
        let crossings = self.zero_crossings() as f64;
        let duration_seconds = self.duration_seconds(); // guaranteed > 0.0 since we do not allow empty audio
        crossings / duration_seconds
    }

    /// Computes the autocorrelation function up to `max_lag` samples.
    ///
    /// Autocorrelation measures the similarity of a signal with a time-shifted
    /// copy of itself. The value at lag 0 is the signal's mean-square value;
    /// subsequent lags decrease as the shift increases.
    ///
    /// For multi-channel audio only the first channel is used.
    ///
    /// Reference: [Autocorrelation — Wikipedia](https://en.wikipedia.org/wiki/Autocorrelation)
    ///
    /// # Arguments
    /// - `max_lag` — the maximum lag offset in samples. The effective maximum
    ///   lag is clamped to `signal_length - 1`.
    ///
    /// # Returns
    /// A [`NonEmptyVec`] of correlation values for lags `0` through
    /// `min(max_lag, signal_length - 1)`, or `None` if the FFT computation
    /// fails.
    ///
    /// # Examples
    /// ```ignore
    /// // Requires the "transforms" feature.
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    /// use std::num::NonZeroUsize;
    ///
    /// let data = array![1.0f32, 0.5, -0.5, -1.0];
    /// let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();
    /// let autocorr = audio.autocorrelation(NonZeroUsize::new(3).unwrap()).unwrap();
    /// assert_eq!(autocorr.len(), NonZeroUsize::new(4).unwrap()); // lags 0..=3
    /// ```
    #[cfg(feature = "transforms")]
    #[inline]
    fn autocorrelation(&self, max_lag: NonZeroUsize) -> Option<NonEmptyVec<f64>> {
        let max_lag = max_lag.get();

        // Extract signal as Vec<f64> — mono uses the array directly,
        // multi-channel uses the first channel (row 0).
        let (signal, n) = match &self.data() {
            AudioData::Mono(arr) => {
                let n = arr.len().get();
                let sig: Vec<f64> = arr.iter().map(|&x| x.cast_into()).collect();
                (sig, n)
            }
            AudioData::Multi(arr) => {
                let first_channel = arr.row(0);
                let n = first_channel.len();
                let sig: Vec<f64> = first_channel.iter().map(|&x| x.cast_into()).collect();
                (sig, n)
            }
        };

        let effective_max_lag = max_lag.min(n - 1);

        // Zero-pad to next power of two of (2n − 1) to avoid circular correlation
        let fft_size = (2 * n - 1).next_power_of_two();
        let mut padded: Vec<f64> = Vec::with_capacity(fft_size);
        padded.extend_from_slice(&signal);
        padded.resize(fft_size, 0.0);

        // safety: padded is non-empty (fft_size >= 1)
        let padded_slice = unsafe { NonEmptySlice::new_unchecked(&padded[..]) };
        let fft_size_nz =
            // safety: fft_size is a next_power_of_two of a value >= 1
            unsafe { NonZeroUsize::new_unchecked(fft_size) };

        // Inverse real FFT → NonEmptyVec<f64> of length fft_size
        let raw = with_fft_planner(|planner| {
            // Forward FFT → Array1<Complex<f64>> of length fft_size/2 + 1
            let spectrum = planner.fft(padded_slice, fft_size_nz).ok()?;

            // Power spectrum: |X[k]|² as complex values for irfft
            let power: Vec<Complex<f64>> = spectrum
                .iter()
                .map(|c| Complex {
                    re: c.norm_sqr(),
                    im: 0.0,
                })
                .collect();
            // safety: power has the same length as spectrum (fft_size/2 + 1 >= 1)
            let power_slice = unsafe { NonEmptySlice::new_unchecked(&power[..]) };

            planner.irfft(power_slice, fft_size_nz).ok()
        })?;

        // Normalize: divide by fft_size (IFFT scaling) then by overlap count (n − lag)
        let fft_size_f = fft_size as f64;
        let mut correlations: Vec<f64> = Vec::with_capacity(effective_max_lag + 1);
        for lag in 0..=effective_max_lag {
            let overlap_count = (n - lag) as f64;
            correlations.push(raw[lag] / fft_size_f / overlap_count);
        }

        // safety: correlations has effective_max_lag + 1 >= 1 elements
        Some(unsafe { NonEmptyVec::new_unchecked(correlations) })
    }

    /// Computes cross-correlation with another audio signal.
    ///
    /// Cross-correlation measures the similarity between two signals as a
    /// function of the displacement of one relative to the other. It is useful
    /// for signal alignment, pattern matching, and delay estimation.
    ///
    /// For multi-channel audio only the first channels of both signals are
    /// correlated.
    ///
    /// # Arguments
    /// - `other` — the second audio signal. Must have the same number of
    ///   channels as `self`.
    /// - `max_lag` — the maximum lag offset in samples. The effective maximum
    ///   lag is clamped to `min(len_self, len_other) - 1`.
    ///
    /// # Returns
    /// A [`NonEmptyVec`] of correlation values for lags `0` through the
    /// effective maximum lag.
    ///
    /// # Errors
    /// Returns an error if the two signals have different numbers of channels,
    /// or if one is mono and the other is multi-channel.
    ///
    /// # Examples
    /// ```
    /// use audio_samples::{AudioSamples, AudioStatistics, sample_rate};
    /// use ndarray::array;
    /// use std::num::NonZeroUsize;
    ///
    /// let data1 = array![1.0f32, 0.0, -1.0, 0.0];
    /// let data2 = array![0.0, 1.0, 0.0, -1.0];
    /// let audio1 = AudioSamples::new_mono(data1, sample_rate!(44100)).unwrap();
    /// let audio2 = AudioSamples::new_mono(data2, sample_rate!(44100)).unwrap();
    /// let max_lag = NonZeroUsize::new(3).unwrap();
    /// let xcorr = audio1.cross_correlation(&audio2, max_lag).unwrap();
    /// assert_eq!(xcorr.len(), NonZeroUsize::new(4).unwrap()); // lags 0..=3
    /// ```
    #[inline]
    fn cross_correlation(
        &self,
        other: &Self,
        max_lag: NonZeroUsize,
    ) -> AudioSampleResult<NonEmptyVec<f64>> {
        // Verify compatible signals
        if self.num_channels() != other.num_channels() {
            return Err(crate::AudioSampleError::Parameter(
                ParameterError::invalid_value(
                    "channels",
                    "Signals must have the same number of channels for cross-correlation",
                ),
            ));
        }

        match (&self.data(), &other.data()) {
            (AudioData::Mono(arr1), AudioData::Mono(arr2)) => {
                let n1 = arr1.len();
                let n2 = arr2.len();

                let effective_max_lag: usize = max_lag.get().min(n1.get().min(n2.get()) - 1);
                let correlations = Vec::with_capacity(effective_max_lag + 1);
                // safety: we just allocated with capacity effective_max_lag + 1 which is at least 1
                // effective_max_lag is at least 0 because max_lag is NonZeroUsize and n1 and n2 are at least 1
                let mut correlations = unsafe { NonEmptyVec::new_unchecked(correlations) };

                for lag in 0..=effective_max_lag {
                    let mut correlation: T = T::zero();

                    let count = n1.get().min(n2.get() - lag);

                    for i in 0..count {
                        let s1 = arr1[i];
                        let s2 = arr2[i + lag];
                        correlation += s1 * s2;
                    }
                    let mut correlation = correlation.cast_into();
                    correlation /= count as f64;
                    correlations.push(correlation);
                }

                Ok(correlations)
            }
            (AudioData::Multi(arr1), AudioData::Multi(arr2)) => {
                // For multi-channel, correlate the first channels
                let ch1 = arr1.row(0);
                let ch2 = arr2.row(0);

                let n1 = ch1.len();
                let n2 = ch2.len();
                let effective_max_lag: usize = max_lag.get().min(n1.min(n2 - 1));
                let correlations = Vec::with_capacity(effective_max_lag + 1);
                // safety: we just allocated with capacity effective_max_lag + 1 which is at
                // least 1
                let mut correlations = unsafe { NonEmptyVec::new_unchecked(correlations) };

                for lag in 0..=effective_max_lag {
                    let mut correlation = T::zero();
                    let count = n1.min(n2 - lag);

                    for i in 0..count {
                        let s1 = ch1[i];
                        let s2 = ch2[i + lag];
                        correlation += s1 * s2;
                    }
                    let mut correlation: f64 = correlation.cast_into();
                    correlation /= count as f64;
                    correlations.push(correlation);
                }

                Ok(correlations)
            }
            _ => {
                // Mixed mono/multi-channel case is a structural mismatch, not a
                // type conversion — report it as a layout incompatibility.
                Err(crate::AudioSampleError::Layout(
                    crate::LayoutError::IncompatibleFormat {
                        operation: "cross_correlation".to_string(),
                        reason: "cannot correlate mono and multi-channel signals; \
                                 convert both inputs to the same channel layout first"
                            .to_string(),
                    },
                ))
            }
        }
    }

    /// Computes the spectral centroid of a mono signal.
    ///
    /// The spectral centroid is the frequency-weighted mean of the power
    /// spectrum and serves as a measure of spectral brightness. Higher values
    /// indicate energy concentrated at higher frequencies.
    ///
    /// Reference: [Spectral centroid — Wikipedia](https://en.wikipedia.org/wiki/Spectral_centroid)
    ///
    /// # Returns
    /// The spectral centroid frequency in Hz. Returns `0.0` when the signal
    /// is silence (zero total spectral energy).
    ///
    /// # Arguments
    /// - `reduction` — the [`ChannelReduction`] policy for multi-channel input.
    ///
    /// # Errors
    /// - [`crate::AudioSampleError::Parameter`] if the signal is multi-channel
    ///   and `reduction` is [`ChannelReduction::Error`], or a
    ///   [`ChannelReduction::Channel`] index is out of bounds.
    /// - [`crate::AudioSampleError::Processing`] if the FFT computation fails.
    #[cfg(feature = "transforms")]
    #[inline]
    fn spectral_centroid(&self, reduction: ChannelReduction) -> AudioSampleResult<f64> {
        // Reduce to a single f64 channel per the requested policy.
        let working_vec = reduce_to_mono_f64(self, reduction, "spectral_centroid")?;

        // safety: self is guaranteed non-empty by design, so the reduced
        // channel has at least one sample.
        let working_samples = unsafe { NonEmptySlice::new_unchecked(working_vec.as_slice()) };

        // safety: working_vec is non-empty (see above).
        let n = unsafe { NonZeroUsize::new_unchecked(working_vec.len()) };
        // ceil(n/2) + 1, computed without NonZero::div_ceil (MSRV 1.92) — the
        // overflow-safe `n/2 + n%2 + 1` keeps the minimum supported Rust at 1.87.
        let fft_output_size = NonZeroUsize::new(n.get() / 2 + (n.get() % 2) + 1).expect(
            "n is non-zero so n/2 + n%2 + 1 >= 2; n/2 is always << usize::MAX so this cannot overflow",
        );
        let power_spectrum =
            with_fft_planner(|planner| planner.power_spectrum(working_samples, n, None))?;

        let nyquist = self.nyquist();
        let freq_step = nyquist / (fft_output_size.get() - 1) as f64;
        // Compute weighted sum and total energy
        let (weighted_sum, total_energy) = power_spectrum.iter().enumerate().fold(
            (0.0, 0.0),
            |(mut w_sum, mut t_energy), (i, &power)| {
                let frequency = i as f64 * freq_step;
                w_sum += frequency * power;
                t_energy += power;
                (w_sum, t_energy)
            },
        );

        // Compute centroid
        if total_energy > 0.0 {
            Ok(weighted_sum / total_energy)
        } else if total_energy == 0.0 {
            // A silent (zero-energy) signal has no spectral content; 0.0 Hz is
            // the conventional, well-defined result for this degenerate case.
            Ok(0.0)
        } else {
            Err(AudioSampleError::Processing(
                ProcessingError::MathematicalFailure {
                    operation: "spectral_centroid".to_string(),
                    reason: format!(
                        "Total spectral energy is negative, cannot compute centroid: total_energy={total_energy},weighted_sum={weighted_sum}"
                    ),
                },
            ))
        }
    }

    /// Computes the spectral rolloff frequency.
    ///
    /// The spectral rolloff is the frequency below which a specified proportion
    /// of the total spectral energy is contained. It is commonly used to
    /// distinguish harmonic signals from noise-like signals.
    ///
    /// # Arguments
    /// - `rolloff_percent` — the energy proportion threshold. Must lie in the
    ///   open interval `(0.0, 1.0)`. A typical value is `0.85`.
    /// - `reduction` — the [`ChannelReduction`] policy for multi-channel input.
    ///   Pass [`ChannelReduction::First`] to reproduce the historical channel-0
    ///   behaviour.
    ///
    /// # Returns
    /// The rolloff frequency in Hz. Returns `0.0` when the signal is silence
    /// (zero total spectral energy).
    ///
    /// # Errors
    /// - [`crate::AudioSampleError::Parameter`] if `rolloff_percent` is not in
    ///   `(0.0, 1.0)`, if the signal is multi-channel and `reduction` is
    ///   [`ChannelReduction::Error`], or a [`ChannelReduction::Channel`] index is
    ///   out of bounds.
    /// - [`crate::AudioSampleError::Processing`] if the FFT computation fails.
    #[cfg(feature = "transforms")]
    #[inline]
    fn spectral_rolloff(
        &self,
        rolloff_percent: f64,
        reduction: ChannelReduction,
    ) -> AudioSampleResult<f64> {
        if rolloff_percent <= 0.0 || rolloff_percent >= 1.0 {
            return Err(crate::AudioSampleError::Parameter(
                ParameterError::out_of_range(
                    "rolloff_percent",
                    rolloff_percent.to_string(),
                    "0.0",
                    "1.0",
                    "rolloff_percent must be between 0.0 and 1.0",
                ),
            ));
        }

        // Reduce to a single f64 channel per the requested policy.
        let input_vec = reduce_to_mono_f64(self, reduction, "spectral_rolloff")?;

        // safety: self is guaranteed non-empty by design, so the reduced
        // channel has at least one sample.
        let n = unsafe { NonZeroUsize::new_unchecked(input_vec.len()) };
        // SAFETY: input_vec is the reduced channel of a non-empty signal, so it
        // has at least one element and is a valid NonEmptyVec.
        let input: NonEmptyVec<f64> = unsafe { NonEmptyVec::new_unchecked(input_vec) };

        let power_spectrum = with_fft_planner(|planner| {
            planner.power_spectrum(input.as_non_empty_slice(), n, None)
        })?;

        // Generate frequency bins
        let nyquist = self.nyquist();
        let freq_step = nyquist / (power_spectrum.len().get() as f64 - 1.0);

        // Compute total energy
        let total_energy: f64 = power_spectrum.iter().fold(0.0, |acc, &x| acc + x);
        if total_energy == 0.0 {
            // A silent (zero-energy) signal has no spectral content; 0.0 Hz is
            // the conventional, well-defined rolloff for this degenerate case.
            return Ok(0.0);
        } else if total_energy < 0.0 {
            return Err(AudioSampleError::Processing(
                ProcessingError::MathematicalFailure {
                    operation: "spectral_rolloff".to_string(),
                    reason: "Total spectral energy is negative, cannot compute rolloff frequency"
                        .to_string(),
                },
            ));
        }

        // Find rolloff frequency
        let target_energy = total_energy * rolloff_percent;
        let mut cumulative_energy = 0.0;

        for (i, &power) in power_spectrum.iter().enumerate() {
            cumulative_energy += power;
            if cumulative_energy >= target_energy {
                let frequency = i as f64 * freq_step;
                return Ok(frequency);
            }
        }

        // If we reach here, return Nyquist frequency
        Ok(nyquist)
    }

    /// Computes the spectral bandwidth (magnitude-weighted spread about the centroid).
    ///
    /// `sqrt( Σ (f_k − centroid)² · mag_k / Σ mag_k )`. The centroid is computed
    /// from the same magnitude spectrum used here.
    ///
    /// # Errors
    /// - [`crate::AudioSampleError::Parameter`] on a forbidden channel layout.
    /// - [`crate::AudioSampleError::Processing`] if the FFT computation fails.
    #[cfg(feature = "transforms")]
    #[inline]
    fn spectral_bandwidth(&self, reduction: ChannelReduction) -> AudioSampleResult<f64> {
        let working_vec = reduce_to_mono_f64(self, reduction, "spectral_bandwidth")?;
        // safety: self is non-empty by design.
        let n = unsafe { NonZeroUsize::new_unchecked(working_vec.len()) };
        // SAFETY: working_vec is the reduced channel of a non-empty signal, so it
        // has at least one element and is a valid NonEmptyVec.
        let input: NonEmptyVec<f64> = unsafe { NonEmptyVec::new_unchecked(working_vec) };
        let mag = with_fft_planner(|planner| {
            planner.magnitude_spectrum(input.as_non_empty_slice(), n, None)
        })?;

        let nyquist = self.nyquist();
        let freq_step = nyquist / (mag.len().get() as f64 - 1.0);

        // Magnitude-weighted centroid.
        let (weighted_sum, total_mag) =
            mag.iter().enumerate().fold((0.0, 0.0), |(w, t), (i, &m)| {
                let f = i as f64 * freq_step;
                (w + f * m, t + m)
            });
        if total_mag <= 0.0 {
            return Ok(0.0);
        }
        let centroid = weighted_sum / total_mag;

        // Weighted variance about the centroid.
        let var = mag.iter().enumerate().fold(0.0, |acc, (i, &m)| {
            let f = i as f64 * freq_step;
            let d = f - centroid;
            acc + d * d * m
        }) / total_mag;

        Ok(var.max(0.0).sqrt())
    }

    /// Computes the spectral flatness (Wiener entropy): `geo_mean(power) / arith_mean(power)`.
    ///
    /// The geometric mean is evaluated in the log domain with an epsilon floor.
    /// Result is clamped to `[0, 1]`.
    ///
    /// # Errors
    /// - [`crate::AudioSampleError::Parameter`] on a forbidden channel layout.
    /// - [`crate::AudioSampleError::Processing`] if the FFT computation fails.
    #[cfg(feature = "transforms")]
    #[inline]
    fn spectral_flatness(&self, reduction: ChannelReduction) -> AudioSampleResult<f64> {
        let working_vec = reduce_to_mono_f64(self, reduction, "spectral_flatness")?;
        // safety: self is non-empty by design.
        let working_samples = unsafe { NonEmptySlice::new_unchecked(working_vec.as_slice()) };
        // SAFETY: working_vec is the reduced channel of a non-empty signal, so its
        // length is non-zero.
        let n = unsafe { NonZeroUsize::new_unchecked(working_vec.len()) };
        let power = with_fft_planner(|planner| planner.power_spectrum(working_samples, n, None))?;

        // Small epsilon floor relative to the spectrum scale keeps log() finite
        // for empty bins without biasing a genuinely flat spectrum.
        const EPS: f64 = 1e-10;
        let count = power.len().get() as f64;
        let arith_mean = power.iter().fold(0.0, |a, &p| a + p) / count;
        if arith_mean <= 0.0 {
            return Ok(0.0);
        }
        // Geometric mean via mean of logs.
        let log_sum = power.iter().fold(0.0, |a, &p| a + (p + EPS).ln());
        let geo_mean = (log_sum / count).exp();

        Ok((geo_mean / arith_mean).clamp(0.0, 1.0))
    }

    /// Computes the spectral crest factor: `max(mag) / mean(mag)`.
    ///
    /// # Errors
    /// - [`crate::AudioSampleError::Parameter`] on a forbidden channel layout.
    /// - [`crate::AudioSampleError::Processing`] if the FFT computation fails.
    #[cfg(feature = "transforms")]
    #[inline]
    fn spectral_crest(&self, reduction: ChannelReduction) -> AudioSampleResult<f64> {
        let working_vec = reduce_to_mono_f64(self, reduction, "spectral_crest")?;
        // safety: self is non-empty by design.
        let n = unsafe { NonZeroUsize::new_unchecked(working_vec.len()) };
        // SAFETY: working_vec is the reduced channel of a non-empty signal, so it
        // has at least one element and is a valid NonEmptyVec.
        let input: NonEmptyVec<f64> = unsafe { NonEmptyVec::new_unchecked(working_vec) };
        let mag = with_fft_planner(|planner| {
            planner.magnitude_spectrum(input.as_non_empty_slice(), n, None)
        })?;

        let count = mag.len().get() as f64;
        let (sum, peak) = mag
            .iter()
            .fold((0.0, 0.0_f64), |(s, p), &m| (s + m, p.max(m)));
        let mean = sum / count;
        if mean <= 0.0 {
            return Ok(0.0);
        }
        Ok(peak / mean)
    }

    /// Computes the spectral slope: ordinary least-squares slope of the **linear
    /// magnitude** spectrum versus frequency (magnitude per Hz).
    ///
    /// # Errors
    /// - [`crate::AudioSampleError::Parameter`] on a forbidden channel layout.
    /// - [`crate::AudioSampleError::Processing`] if the FFT computation fails.
    #[cfg(feature = "transforms")]
    #[inline]
    fn spectral_slope(&self, reduction: ChannelReduction) -> AudioSampleResult<f64> {
        let working_vec = reduce_to_mono_f64(self, reduction, "spectral_slope")?;
        // safety: self is non-empty by design.
        let n = unsafe { NonZeroUsize::new_unchecked(working_vec.len()) };
        // SAFETY: working_vec is the reduced channel of a non-empty signal, so it
        // has at least one element and is a valid NonEmptyVec.
        let input: NonEmptyVec<f64> = unsafe { NonEmptyVec::new_unchecked(working_vec) };
        let mag = with_fft_planner(|planner| {
            planner.magnitude_spectrum(input.as_non_empty_slice(), n, None)
        })?;

        let len = mag.len().get();
        if len < 2 {
            return Ok(0.0);
        }
        if mag.iter().all(|&m| m <= 0.0) {
            return Ok(0.0);
        }

        let nyquist = self.nyquist();
        let freq_step = nyquist / (len as f64 - 1.0);

        // OLS slope: Σ (x − x̄)(y − ȳ) / Σ (x − x̄)².
        let count = len as f64;
        let (sum_x, sum_y) = mag
            .iter()
            .enumerate()
            .fold((0.0, 0.0), |(sx, sy), (i, &m)| {
                (sx + i as f64 * freq_step, sy + m)
            });
        let mean_x = sum_x / count;
        let mean_y = sum_y / count;
        let (num, den) = mag
            .iter()
            .enumerate()
            .fold((0.0, 0.0), |(num, den), (i, &m)| {
                let dx = i as f64 * freq_step - mean_x;
                (num + dx * (m - mean_y), den + dx * dx)
            });
        if den <= 0.0 {
            return Ok(0.0);
        }
        Ok(num / den)
    }

    /// Computes octave-band spectral contrast (librosa-style): per band, the dB
    /// difference between the mean of the top quantile and the bottom quantile.
    ///
    /// # Errors
    /// - [`crate::AudioSampleError::Parameter`] on a forbidden channel layout.
    /// - [`crate::AudioSampleError::Processing`] if the FFT computation fails.
    #[cfg(feature = "transforms")]
    #[inline]
    fn spectral_contrast(
        &self,
        n_bands: NonZeroUsize,
        reduction: ChannelReduction,
    ) -> AudioSampleResult<Vec<f64>> {
        let working_vec = reduce_to_mono_f64(self, reduction, "spectral_contrast")?;
        // safety: self is non-empty by design.
        let n = unsafe { NonZeroUsize::new_unchecked(working_vec.len()) };
        // SAFETY: working_vec is the reduced channel of a non-empty signal, so it
        // has at least one element and is a valid NonEmptyVec.
        let input: NonEmptyVec<f64> = unsafe { NonEmptyVec::new_unchecked(working_vec) };
        // Hann window before the FFT: reduces spectral leakage so a tonal band's
        // peak/valley gap is not flattened by sidelobes (librosa windows too).
        let mag = with_fft_planner(|planner| {
            planner.magnitude_spectrum(
                input.as_non_empty_slice(),
                n,
                Some(spectrograms::WindowType::Hanning),
            )
        })?;

        let bins = mag.len().get();
        let n_bands = n_bands.get();

        // Octave-spaced band edges over bin indices [1, bins): edge_b grows
        // geometrically so each band spans roughly one octave. Bin 0 (DC) is
        // excluded.
        // First non-DC bin: index 1, clamped into the available bin range so it
        // never exceeds the last valid bin when the spectrum is tiny.
        let lo_bin = 1usize.clamp(1, bins.max(1));
        let hi_bin = bins; // exclusive
        let span = (hi_bin as f64 / lo_bin as f64).max(1.0);
        let ratio = span.powf(1.0 / n_bands as f64);

        // Quantile fraction for peak/valley pooling (librosa default 0.02).
        const QUANTILE: f64 = 0.02;
        // dB floor so log10 of an empty bin stays finite.
        const EPS: f64 = 1e-10;

        let mut out = Vec::with_capacity(n_bands);
        for b in 0..n_bands {
            let start = (lo_bin as f64 * ratio.powi(b as i32)).floor() as usize;
            let mut end = (lo_bin as f64 * ratio.powi(b as i32 + 1)).floor() as usize;
            let start = start.min(bins);
            if b == n_bands - 1 {
                end = bins;
            }
            let end = end.clamp(start, bins);

            if end <= start {
                out.push(0.0);
                continue;
            }

            let mut band: Vec<f64> = mag[start..end]
                .iter()
                .map(|&m| 10.0 * (m + EPS).log10())
                .collect();
            band.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

            let len = band.len();
            let k = ((len as f64 * QUANTILE).round() as usize).clamp(1, len);
            let valley: f64 = band[..k].iter().sum::<f64>() / k as f64;
            let peak: f64 = band[len - k..].iter().sum::<f64>() / k as f64;
            out.push(peak - valley);
        }

        Ok(out)
    }
}

/// Returns the signed lag (in samples) that maximises the FFT cross-correlation
/// between `reference` and `query`, searching ±`max_lag` samples.
///
/// Positive lag → `query` is delayed relative to `reference`; negative → `query` leads.
/// Uses only the first channel of each signal.  Returns `None` if the FFT fails
/// or either signal is empty.
#[cfg(feature = "transforms")]
pub fn fft_alignment_lag<T: StandardSample>(
    reference: &AudioSamples<'_, T>,
    query: &AudioSamples<'_, T>,
    max_lag: usize,
) -> Option<i64> {
    let extract = |audio: &AudioSamples<'_, T>| -> Vec<f64> {
        match &audio.data() {
            AudioData::Mono(arr) => arr.iter().map(|&x| x.cast_into()).collect(),
            AudioData::Multi(arr) => arr.row(0).iter().map(|&x| x.cast_into()).collect(),
        }
    };

    let ref_sig = extract(reference);
    let deg_sig = extract(query);
    let n1 = ref_sig.len();
    let n2 = deg_sig.len();
    if n1 == 0 || n2 == 0 {
        return None;
    }

    let fft_size = (n1 + n2 - 1).next_power_of_two();
    // SAFETY: n1 and n2 are both non-zero (checked above), so n1 + n2 - 1 >= 1
    // and its next_power_of_two() is therefore non-zero.
    let fft_nz = unsafe { NonZeroUsize::new_unchecked(fft_size) };

    let mut ref_buf = ref_sig;
    ref_buf.resize(fft_size, 0.0);
    let mut deg_buf = deg_sig;
    deg_buf.resize(fft_size, 0.0);

    let xcorr = with_fft_planner(|planner| {
        // SAFETY: ref_buf was resized to fft_size (>= 1), so it is non-empty.
        let ref_spec = planner
            .fft(unsafe { NonEmptySlice::new_unchecked(&ref_buf) }, fft_nz)
            .ok()?;
        // SAFETY: deg_buf was resized to fft_size (>= 1), so it is non-empty.
        let deg_spec = planner
            .fft(unsafe { NonEmptySlice::new_unchecked(&deg_buf) }, fft_nz)
            .ok()?;

        let cross: Vec<Complex<f64>> = ref_spec
            .iter()
            .zip(deg_spec.iter())
            .map(|(r, d)| r * d.conj())
            .collect();

        // SAFETY: cross has one element per rfft bin (fft_size/2 + 1 >= 1 since
        // fft_size >= 1), so it is non-empty.
        planner
            .irfft(unsafe { NonEmptySlice::new_unchecked(&cross) }, fft_nz)
            .ok()
    })?;

    let scale = 1.0 / fft_size as f64;
    let search = max_lag.min(n1.saturating_sub(1));
    let mut best_val = f64::NEG_INFINITY;
    let mut best_lag = 0i64;

    for k in 0..=search {
        let v = xcorr[k] * scale;
        if v > best_val {
            best_val = v;
            best_lag = k as i64;
        }
    }
    for k in 1..=search {
        let v = xcorr[fft_size - k] * scale;
        if v > best_val {
            best_val = v;
            best_lag = -(k as i64);
        }
    }

    Some(best_lag)
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "transforms")]
    use std::time::Duration;

    use super::*;
    use crate::sample_rate;
    use approx_eq::assert_approx_eq;
    use ndarray::{Array1, array};

    #[test]
    fn test_midpoint_sample_odd_and_even() {
        // Odd length: single central sample at index len/2 (3 / 2 == 1 -> value 3.0).
        let odd = AudioSamples::new_mono(array![1.0f32, 3.0, 5.0], sample_rate!(44100)).unwrap();
        assert_eq!(odd.midpoint_sample(), Some(3.0));

        // Even length: average of the two central samples.
        let even =
            AudioSamples::new_mono(array![1.0f32, 3.0, 5.0, 7.0], sample_rate!(44100)).unwrap();
        // Central indices 1 and 2: (3.0 + 5.0) / 2.0 = 4.0
        assert_eq!(even.midpoint_sample(), Some(4.0));

        // Multi-channel returns None.
        let stereo = AudioSamples::new_multi_channel(
            ndarray::array![[1.0f32, 2.0], [3.0, 4.0]],
            sample_rate!(44100),
        )
        .unwrap();
        assert_eq!(stereo.midpoint_sample(), None);
    }

    #[test]
    fn test_peak_min_max_existing_methods() {
        let data = array![-3.0f32, -1.0, 0.0, 2.0, 4.0];
        let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();

        // These should use the existing native implementations
        assert_eq!(audio.peak(), 4.0);
        assert_eq!(audio.min_sample(), -3.0);
        assert_eq!(audio.max_sample(), 4.0);
    }

    #[test]
    fn test_rms_computation() {
        // Simple test case where we can verify RMS manually
        let data = array![1.0f32, -1.0, 1.0, -1.0];
        let audio: AudioSamples<'static, f32> =
            AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();

        let rms = audio.rms(); // RMS of [1, -1, 1, -1] = sqrt((1^2 + 1^2 + 1^2 + 1^2)/4) = sqrt(1) = 1.0
        assert_approx_eq!(rms, 1.0, 1e-6);
    }

    #[test]
    fn test_variance_and_std_dev() {
        let data = array![1.0f32, 2.0, 3.0, 4.0, 5.0];
        let audio: AudioSamples<'_, f32> =
            AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();

        let variance = audio.variance();
        let std_dev = audio.std_dev();

        // Mean = 3.0, variance = mean((1-3)^2 + (2-3)^2 + ... + (5-3)^2) = mean(4+1+0+1+4) = 2.0
        assert_approx_eq!(variance, 2.0, 1e-6);
        assert_approx_eq!(std_dev, 2.0_f64.sqrt(), 1e-6);
    }

    #[test]
    fn test_zero_crossings() {
        let data = array![1.0f32, -1.0, 1.0, -1.0, 1.0];
        let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();

        let crossings = audio.zero_crossings();
        // Crossings occur at: 1->-1, -1->1, 1->-1, -1->1 = 4 crossings
        assert_eq!(crossings, 4);
    }

    #[test]
    fn test_zero_crossing_rate() {
        // Create 1 second of 4 Hz square wave: 1,-1,1,-1 pattern every 1/4 second
        let sample_rate = 44100;
        let duration = 1.0; // 1 second
        let freq = 4.0; // 4 Hz

        let n_samples = (sample_rate as f64 * duration) as usize;
        let mut data = Vec::with_capacity(n_samples);

        for i in 0..n_samples {
            let t = i as f64 / sample_rate as f64;
            // Square wave using sine wave sign: +1 when sin > 0, -1 when sin <= 0
            let phase = 2.0 * std::f64::consts::PI * freq * t;
            let value = if phase.sin() >= 0.0 { 1.0 } else { -1.0 };
            data.push(value);
        }

        let audio = AudioSamples::new_mono(Array1::from(data), sample_rate!(44100)).unwrap();
        let zcr = audio.zero_crossing_rate();

        // 4 Hz square wave has ~8 zero crossings per second (2 per cycle)
        // Due to discrete sampling, we might get 7-8 crossings
        assert!(
            (zcr - 8.0f64).abs() <= 1.0,
            "Expected ~8 crossings/sec, got {}",
            zcr
        );
    }

    #[test]
    #[cfg(feature = "transforms")]
    fn test_autocorrelation() {
        let data = array![1.0f32, 0.0, -1.0, 0.0];
        let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();

        let autocorr = audio.autocorrelation(crate::nzu!(2)).unwrap();

        // Should have correlations for lags 0, 1, 2
        assert_eq!(autocorr.len(), crate::nzu!(3));

        // Lag 0 should be the highest (signal correlated with itself)
        assert!(autocorr[0] >= autocorr[1]);
        assert!(autocorr[0] >= autocorr[2]);
    }

    #[test]
    #[cfg(feature = "transforms")]
    fn test_cross_correlation() {
        let data1 = array![1.0f32, 0.0, -1.0];
        let data2 = array![1.0f32, 0.0, -1.0]; // Same signal
        let audio1 = AudioSamples::new_mono(data1, sample_rate!(44100)).unwrap();
        let audio2 = AudioSamples::new_mono(data2, sample_rate!(44100)).unwrap();

        let cross_corr = audio1.cross_correlation(&audio2, crate::nzu!(1)).unwrap();

        // Cross-correlation of identical signals should be same as autocorrelation
        let autocorr = audio1.autocorrelation(crate::nzu!(1)).unwrap();
        assert_eq!(cross_corr.len(), autocorr.len());
    }

    #[test]
    fn test_multi_channel_statistics() {
        let data = array![[1.0f32, 2.0], [-1.0, 1.0]]; // 2 channels, 2 samples each
        let audio = AudioSamples::new_multi_channel(data, sample_rate!(44100)).unwrap();

        let rms: f64 = audio.rms();
        let variance = audio.variance();
        let crossings = audio.zero_crossings();

        // Should compute across all samples
        assert!(rms > 0.0);
        assert!(variance >= 0.0);
        assert_eq!(crossings, 1); // One crossing in channel 0: 1.0 -> -1.0
    }

    #[test]
    fn test_edge_cases() {
        // Single sample
        let single_data = array![1.0f32];
        let single_audio = AudioSamples::new_mono(single_data, sample_rate!(44100)).unwrap();

        assert_eq!(single_audio.zero_crossings(), 0);
        assert_eq!(
            single_audio.rms(),
            1.0,
            "RMS of single sample should be the sample itself"
        );
    }

    #[test]
    fn test_empty_audio_rejected() {
        // Empty audio should be rejected at construction time
        let empty_data: Array1<f32> = Array1::from(vec![]);
        let empty_audio = AudioSamples::new_mono(empty_data, sample_rate!(44100));
        assert!(empty_audio.is_err(), "Empty audio should not be created");
    }

    #[test]
    #[cfg(feature = "transforms")]
    fn test_spectral_centroid() {
        // Test with a simple sine wave that should have energy concentrated at a specific frequency
        let sample_rate = sample_rate!(44100);
        let duration = Duration::from_secs_f32(1.0); // 1 second
        let freq = 1000.0; // 1kHz sine wave

        let audio = crate::sine_wave::<f64>(freq, duration, sample_rate, 0.5);

        // Generate 1kHz sine wave
        let centroid = audio
            .spectral_centroid(ChannelReduction::Error)
            .expect("Failed to compute spectral centroid");

        // For a pure sine wave, the spectral centroid should be close to the frequency
        // Allow some tolerance due to FFT discretization and numerical precision
        assert!(
            (centroid - freq).abs() < 50.0,
            "Centroid {} should be close to {}",
            centroid,
            freq
        );
    }

    #[test]
    #[cfg(all(feature = "transforms", feature = "random-generation"))]
    fn test_spectral_rolloff() {
        // Test with white noise - rolloff should be around 85% of Nyquist for 85% rolloff
        let sample_rate = sample_rate!(8000); // Use lower sample rate for faster test
        let duration = Duration::from_secs_f32(1.0);
        // Generate white noise
        let audio = crate::white_noise::<f64>(duration, sample_rate, 0.5, None);

        let rolloff = audio
            .spectral_rolloff(0.85, ChannelReduction::Error)
            .expect("Failed to compute spectral rolloff");
        let nyquist = audio.nyquist();

        // For noise-like signals, rolloff should be somewhere reasonable
        assert!(rolloff > 0.0, "Rolloff should be positive");
        assert!(
            rolloff <= nyquist,
            "Rolloff should not exceed Nyquist frequency"
        );
    }

    #[test]
    #[cfg(feature = "transforms")]
    fn test_spectral_rolloff_validation() {
        let data = array![1.0f32, -1.0, 1.0];
        let audio = AudioSamples::new_mono(data, sample_rate!(44100)).unwrap();

        // Test invalid rolloff percentages
        assert!(
            audio
                .spectral_rolloff(0.0, ChannelReduction::Error)
                .is_err()
        );
        assert!(
            audio
                .spectral_rolloff(1.0, ChannelReduction::Error)
                .is_err()
        );
        assert!(
            audio
                .spectral_rolloff(-0.1, ChannelReduction::Error)
                .is_err()
        );
        assert!(
            audio
                .spectral_rolloff(1.1, ChannelReduction::Error)
                .is_err()
        );

        // Test valid rolloff percentage
        assert!(
            audio
                .spectral_rolloff(0.85, ChannelReduction::Error)
                .is_ok()
        );
    }

    #[test]
    #[cfg(feature = "transforms")]
    fn test_spectral_centroid_channel_reduction() {
        // Stereo signal: channel 0 is a 1 kHz tone, channel 1 is silence.
        let sample_rate = sample_rate!(44100);
        let duration = Duration::from_secs_f32(0.25);
        let tone = crate::sine_wave::<f64>(1000.0, duration, sample_rate, 0.5);
        let tone_ch = tone.as_mono().expect("mono tone");
        let n = tone_ch.len().get();
        let tone_vec: Vec<f64> = (0..n).map(|i| tone[i]).collect();

        let mut data = ndarray::Array2::<f64>::zeros((2, n));
        for (i, &v) in tone_vec.iter().enumerate() {
            data[[0, i]] = v;
        }
        let stereo = AudioSamples::new_multi_channel(data, sample_rate).unwrap();

        // Error: multi-channel must be rejected by default.
        assert!(stereo.spectral_centroid(ChannelReduction::Error).is_err());

        // First: uses channel 0 (the tone) -> centroid near 1 kHz.
        let first = stereo
            .spectral_centroid(ChannelReduction::First)
            .expect("First channel centroid");
        assert!(
            (first - 1000.0).abs() < 50.0,
            "First-channel centroid {first} should be near 1000 Hz"
        );

        // Channel(0) must match First.
        let ch0 = stereo
            .spectral_centroid(ChannelReduction::Channel(0))
            .expect("Channel(0) centroid");
        assert!((ch0 - first).abs() < 1e-6);

        // Channel(1) is silence -> zero-energy -> 0.0.
        let ch1 = stereo
            .spectral_centroid(ChannelReduction::Channel(1))
            .expect("Channel(1) centroid");
        assert_eq!(ch1, 0.0);

        // Out-of-range channel errors.
        assert!(
            stereo
                .spectral_centroid(ChannelReduction::Channel(2))
                .is_err()
        );

        // Average: mean of the tone and silence -> still tonal, centroid near 1 kHz.
        let avg = stereo
            .spectral_centroid(ChannelReduction::Average)
            .expect("Average centroid");
        assert!(
            (avg - 1000.0).abs() < 50.0,
            "Averaged centroid {avg} should be near 1000 Hz"
        );
    }

    /// Sanity check that the thread-local cached [`FftPlanner`] yields
    /// bit-identical results across repeated calls of the same size — i.e.
    /// reusing the planner did not corrupt or mutate its cached state in a way
    /// that changes the numeric output.
    #[test]
    #[cfg(feature = "transforms")]
    fn test_cached_fft_planner_repeatable() {
        let sample_rate = sample_rate!(44100);
        let duration = Duration::from_secs_f32(0.1);
        let tone = crate::sine_wave::<f64>(1000.0, duration, sample_rate, 0.5);

        // spectral_centroid uses the cached planner (power_spectrum path).
        let c1 = tone.spectral_centroid(ChannelReduction::First).unwrap();
        let c2 = tone.spectral_centroid(ChannelReduction::First).unwrap();
        assert_eq!(c1, c2, "centroid must be identical across cached calls");

        // autocorrelation uses the cached planner (fft + irfft path).
        let a1 = tone.autocorrelation(nzu!(64)).unwrap();
        let a2 = tone.autocorrelation(nzu!(64)).unwrap();
        assert_eq!(
            a1.as_slice(),
            a2.as_slice(),
            "autocorrelation must be identical across cached calls"
        );

        // fft_alignment_lag uses the cached planner (two ffts + irfft path).
        let l1 = fft_alignment_lag(&tone, &tone, 32);
        let l2 = fft_alignment_lag(&tone, &tone, 32);
        assert_eq!(
            l1, l2,
            "alignment lag must be identical across cached calls"
        );
    }

    // ---- New spectral feature validation ----

    /// White noise should have flatness near 1; a pure sine near 0 with high crest.
    #[test]
    #[cfg(all(feature = "transforms", feature = "random-generation"))]
    fn test_spectral_flatness_noise_vs_tone() {
        let sr = sample_rate!(16000);
        let dur = Duration::from_secs_f32(1.0);

        let noise = crate::white_noise::<f64>(dur, sr, 0.5, None);
        let flat_noise = noise.spectral_flatness(ChannelReduction::Error).unwrap();
        assert!(
            flat_noise > 0.5,
            "white-noise flatness {flat_noise} should be > 0.5"
        );

        let tone = crate::sine_wave::<f64>(1000.0, dur, sr, 0.5);
        let flat_tone = tone.spectral_flatness(ChannelReduction::Error).unwrap();
        assert!(
            flat_tone < 0.1,
            "pure-tone flatness {flat_tone} should be < 0.1"
        );

        // A pure tone is strongly peaked -> high crest; noise is comparatively low.
        let crest_tone = tone.spectral_crest(ChannelReduction::Error).unwrap();
        let crest_noise = noise.spectral_crest(ChannelReduction::Error).unwrap();
        assert!(
            crest_tone > crest_noise,
            "tone crest {crest_tone} should exceed noise crest {crest_noise}"
        );
        assert!(crest_tone > 10.0, "tone crest {crest_tone} should be large");
    }

    /// A two-tone signal has larger spectral bandwidth than a single tone.
    #[test]
    #[cfg(feature = "transforms")]
    fn test_spectral_bandwidth_two_tone_vs_one() {
        let sr = sample_rate!(16000);
        let n = 16000usize;
        let single: Vec<f64> = (0..n)
            .map(|i| {
                let t = i as f64 / sr.get() as f64;
                0.5 * (2.0 * std::f64::consts::PI * 1000.0 * t).sin()
            })
            .collect();
        let two: Vec<f64> = (0..n)
            .map(|i| {
                let t = i as f64 / sr.get() as f64;
                0.25 * (2.0 * std::f64::consts::PI * 500.0 * t).sin()
                    + 0.25 * (2.0 * std::f64::consts::PI * 5000.0 * t).sin()
            })
            .collect();

        let single = AudioSamples::new_mono(Array1::from(single), sr).unwrap();
        let two = AudioSamples::new_mono(Array1::from(two), sr).unwrap();

        let bw1 = single.spectral_bandwidth(ChannelReduction::Error).unwrap();
        let bw2 = two.spectral_bandwidth(ChannelReduction::Error).unwrap();
        assert!(
            bw2 > bw1,
            "two-tone bandwidth {bw2} should exceed single-tone bandwidth {bw1}"
        );
    }

    /// A low-pass-weighted (energy concentrated low) spectrum has negative slope;
    /// flat/white is near zero.
    #[test]
    #[cfg(all(feature = "transforms", feature = "random-generation"))]
    fn test_spectral_slope_lowpass_vs_white() {
        let sr = sample_rate!(16000);
        let dur = Duration::from_secs_f32(1.0);

        // Low-frequency tone: magnitude concentrated near 0 Hz -> negative slope.
        let low = crate::sine_wave::<f64>(200.0, dur, sr, 0.5);
        let slope_low = low.spectral_slope(ChannelReduction::Error).unwrap();
        assert!(
            slope_low < 0.0,
            "low-frequency-weighted slope {slope_low} should be negative"
        );

        // White noise: roughly flat magnitude -> slope near zero. Use a generous
        // bound since noise realisations vary.
        let noise = crate::white_noise::<f64>(dur, sr, 0.5, None);
        let slope_white = noise.spectral_slope(ChannelReduction::Error).unwrap();
        assert!(
            slope_white.abs() < slope_low.abs(),
            "white-noise slope {slope_white} should be flatter than low-tone slope {slope_low}"
        );
    }

    /// Spectral contrast returns one value per band and is higher for a tonal
    /// signal than for noise.
    #[test]
    #[cfg(all(feature = "transforms", feature = "random-generation"))]
    fn test_spectral_contrast_shape_and_tonality() {
        let sr = sample_rate!(16000);
        let dur = Duration::from_secs_f32(1.0);

        let tone = crate::sine_wave::<f64>(1000.0, dur, sr, 0.5);
        let contrast = tone
            .spectral_contrast(nzu!(4), ChannelReduction::Error)
            .unwrap();
        assert_eq!(contrast.len(), 4, "one contrast value per band");
        assert!(
            contrast.iter().all(|&c| c.is_finite() && c >= 0.0),
            "contrast values must be finite and non-negative: {contrast:?}"
        );
        // A tone produces a strong peak/valley gap in at least one band.
        let max_tone = contrast.iter().cloned().fold(0.0_f64, f64::max);
        assert!(
            max_tone > 1.0,
            "tonal signal should yield a band contrast > 1 dB, got {contrast:?}"
        );
    }

    /// ChannelReduction policies on a stereo signal for the new features.
    #[test]
    #[cfg(feature = "transforms")]
    fn test_new_features_channel_reduction() {
        let sr = sample_rate!(44100);
        let dur = Duration::from_secs_f32(0.25);
        let tone = crate::sine_wave::<f64>(1000.0, dur, sr, 0.5);
        let tone_ch = tone.as_mono().expect("mono tone");
        let n = tone_ch.len().get();
        let tone_vec: Vec<f64> = (0..n).map(|i| tone[i]).collect();

        // Channel 0 = tone, channel 1 = silence.
        let mut data = ndarray::Array2::<f64>::zeros((2, n));
        for (i, &v) in tone_vec.iter().enumerate() {
            data[[0, i]] = v;
        }
        let stereo = AudioSamples::new_multi_channel(data, sr).unwrap();

        // Error rejects multi-channel.
        assert!(stereo.spectral_bandwidth(ChannelReduction::Error).is_err());
        assert!(stereo.spectral_flatness(ChannelReduction::Error).is_err());
        assert!(stereo.spectral_crest(ChannelReduction::Error).is_err());
        assert!(stereo.spectral_slope(ChannelReduction::Error).is_err());
        assert!(
            stereo
                .spectral_contrast(nzu!(4), ChannelReduction::Error)
                .is_err()
        );

        // First selects the tone channel -> tonal (low flatness).
        let flat_first = stereo.spectral_flatness(ChannelReduction::First).unwrap();
        assert!(
            flat_first < 0.1,
            "First-channel flatness {flat_first} should be tonal"
        );

        // Average mixes tone with silence -> still tonal.
        let flat_avg = stereo.spectral_flatness(ChannelReduction::Average).unwrap();
        assert!(
            flat_avg < 0.2,
            "Average flatness {flat_avg} should be tonal"
        );
    }
}