voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
//! STOI (Short-Time Objective Intelligibility) Implementation
//!
//! Implementation of the STOI metric for predicting speech intelligibility.
//! Based on the original STOI algorithm and Extended STOI (ESTOI) for better correlation.

use crate::EvaluationError;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::Rng;
use scirs2_core::Complex32;
use scirs2_fft::{RealFftPlanner, RealToComplex};
use std::f32::consts::PI;
use std::sync::Mutex;
use voirs_sdk::{AudioBuffer, LanguageCode};

/// Language-specific STOI parameters
#[derive(Debug, Clone)]
pub struct LanguageSpecificParams {
    /// Language code
    pub language: LanguageCode,
    /// Frequency weighting factors for this language
    pub frequency_weights: Vec<f32>,
    /// Temporal weighting factors
    pub temporal_weights: Vec<f32>,
    /// Language-specific intelligibility threshold
    pub intelligibility_threshold: f32,
}

/// STOI confidence interval result
#[derive(Debug, Clone)]
pub struct STOIConfidenceInterval {
    /// STOI score
    pub score: f32,
    /// Lower bound of confidence interval
    pub lower_bound: f32,
    /// Upper bound of confidence interval
    pub upper_bound: f32,
    /// Confidence level (e.g., 0.95 for 95%)
    pub confidence_level: f32,
    /// Standard error
    pub standard_error: f32,
}

/// STOI evaluator for speech intelligibility assessment
pub struct STOIEvaluator {
    /// Sample rate
    sample_rate: u32,
    /// Frame length in samples
    frame_len: usize,
    /// Overlap between frames
    overlap: usize,
    /// Third-octave frequency bands
    octave_bands: Vec<(f32, f32)>,
    /// FFT planner
    fft_planner: Mutex<RealFftPlanner<f32>>,
    /// Language-specific parameters
    language_params: Option<LanguageSpecificParams>,
}

impl STOIEvaluator {
    /// Create new STOI evaluator
    pub fn new(sample_rate: u32) -> Result<Self, EvaluationError> {
        // Frame length: 25.6 ms
        let frame_len = ((sample_rate as f32 * 0.0256).round() as usize).max(256);

        // 50% overlap
        let overlap = frame_len / 2;

        // Create third-octave bands from 150 Hz to sample_rate/2
        let octave_bands = Self::create_octave_bands(sample_rate);

        let fft_planner = Mutex::new(RealFftPlanner::<f32>::new());

        Ok(Self {
            sample_rate,
            frame_len,
            overlap,
            octave_bands,
            fft_planner,
            language_params: None,
        })
    }

    /// Calculate STOI score between clean and processed signals
    pub async fn calculate_stoi(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        // Validate inputs
        self.validate_inputs(clean, processed)?;

        // Ensure signals have the same length
        let min_len = clean.samples().len().min(processed.samples().len());
        let clean_samples = &clean.samples()[..min_len];
        let processed_samples = &processed.samples()[..min_len];

        // Apply pre-emphasis filter
        let clean_filtered = self.apply_preemphasis(clean_samples)?;
        let processed_filtered = self.apply_preemphasis(processed_samples)?;

        // Compute short-time FFT for both signals
        let clean_stft = self.compute_stft(&clean_filtered)?;
        let processed_stft = self.compute_stft(&processed_filtered)?;

        // Group frequency bins into third-octave bands
        let clean_bands = self.group_into_octave_bands(&clean_stft)?;
        let processed_bands = self.group_into_octave_bands(&processed_stft)?;

        // Apply intermediate intelligibility measure
        let intermediate_intel =
            self.compute_intermediate_intelligibility(&clean_bands, &processed_bands)?;

        // Compute final STOI score
        let stoi_score = self.compute_final_stoi(&intermediate_intel)?;

        Ok(stoi_score)
    }

    /// Calculate Extended STOI (ESTOI) with better correlation
    pub async fn calculate_estoi(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        // Start with regular STOI calculation
        let stoi_score = self.calculate_stoi(clean, processed).await?;

        // Apply ESTOI improvements
        let enhanced_score = self
            .apply_estoi_enhancement(clean, processed, stoi_score)
            .await?;

        Ok(enhanced_score)
    }

    /// Validate input audio buffers
    fn validate_inputs(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
    ) -> Result<(), EvaluationError> {
        if clean.sample_rate() != self.sample_rate {
            return Err(EvaluationError::InvalidInput {
                message: format!(
                    "Clean signal sample rate {} doesn't match STOI evaluator rate {}",
                    clean.sample_rate(),
                    self.sample_rate
                ),
            });
        }

        if processed.sample_rate() != self.sample_rate {
            return Err(EvaluationError::InvalidInput {
                message: format!(
                    "Processed signal sample rate {} doesn't match STOI evaluator rate {}",
                    processed.sample_rate(),
                    self.sample_rate
                ),
            });
        }

        if clean.channels() != 1 || processed.channels() != 1 {
            return Err(EvaluationError::InvalidInput {
                message: "STOI requires mono audio".to_string(),
            });
        }

        // Minimum length requirement (at least 3 seconds)
        let min_samples = 3 * self.sample_rate as usize;
        if clean.samples().len() < min_samples || processed.samples().len() < min_samples {
            return Err(EvaluationError::InvalidInput {
                message: "STOI requires at least 3 seconds of audio".to_string(),
            });
        }

        Ok(())
    }

    /// Apply pre-emphasis filter (high-pass)
    fn apply_preemphasis(&self, signal: &[f32]) -> Result<Array1<f32>, EvaluationError> {
        if signal.is_empty() {
            return Ok(Array1::zeros(0));
        }

        let mut filtered = Array1::zeros(signal.len());
        filtered[0] = signal[0];

        // Pre-emphasis: y[n] = x[n] - 0.97 * x[n-1]
        for i in 1..signal.len() {
            filtered[i] = signal[i] - 0.97 * signal[i - 1];
        }

        Ok(filtered)
    }

    /// Compute Short-Time Fourier Transform
    fn compute_stft(&self, signal: &Array1<f32>) -> Result<Array2<Complex32>, EvaluationError> {
        let hop_size = self.frame_len - self.overlap;
        let num_frames = (signal.len() - self.overlap) / hop_size;
        let num_freqs = self.frame_len / 2 + 1;

        let mut stft = Array2::zeros((num_frames, num_freqs));

        for (frame_idx, frame_start) in (0..signal.len() - self.frame_len + 1)
            .step_by(hop_size)
            .enumerate()
        {
            if frame_idx >= num_frames {
                break;
            }

            // Extract frame and apply Hann window
            let mut frame = Array1::zeros(self.frame_len);
            for i in 0..self.frame_len {
                if frame_start + i < signal.len() {
                    let window =
                        0.5 * (1.0 - (2.0 * PI * i as f32 / (self.frame_len - 1) as f32).cos());
                    frame[i] = signal[frame_start + i] * window;
                }
            }

            // Compute FFT using functional API
            let frame_slice =
                frame
                    .as_slice()
                    .ok_or_else(|| EvaluationError::AudioProcessingError {
                        message: "Failed to get frame slice".to_string(),
                        source: None,
                    })?;
            let spectrum = scirs2_fft::rfft(frame_slice, None)?;

            // Store complex spectrum (convert Complex64 to Complex32)
            for (freq_idx, &complex_val) in spectrum.iter().enumerate() {
                if freq_idx < num_freqs {
                    stft[[frame_idx, freq_idx]] =
                        Complex32::new(complex_val.re as f32, complex_val.im as f32);
                }
            }
        }

        Ok(stft)
    }

    /// Group frequency bins into third-octave bands
    fn group_into_octave_bands(
        &self,
        stft: &Array2<Complex32>,
    ) -> Result<Array2<f32>, EvaluationError> {
        let (num_frames, _) = stft.dim();
        let num_bands = self.octave_bands.len();

        let mut band_energies = Array2::zeros((num_frames, num_bands));
        let freq_res = self.sample_rate as f32 / self.frame_len as f32;

        for frame_idx in 0..num_frames {
            for (band_idx, &(f_low, f_high)) in self.octave_bands.iter().enumerate() {
                let bin_low = (f_low / freq_res).round() as usize;
                let bin_high = (f_high / freq_res).round() as usize;

                let mut band_energy = 0.0;
                let mut bin_count = 0;

                for bin_idx in bin_low..=bin_high.min(stft.ncols() - 1) {
                    band_energy += stft[[frame_idx, bin_idx]].norm_sqr();
                    bin_count += 1;
                }

                // Average energy in the band with numerical stability
                if bin_count > 0 {
                    let avg_energy = band_energy / bin_count as f32;
                    band_energies[[frame_idx, band_idx]] = avg_energy.max(0.0).sqrt();
                }
            }
        }

        Ok(band_energies)
    }

    /// Compute intermediate intelligibility measure
    fn compute_intermediate_intelligibility(
        &self,
        clean_bands: &Array2<f32>,
        processed_bands: &Array2<f32>,
    ) -> Result<Array2<f32>, EvaluationError> {
        let (num_frames, num_bands) = clean_bands.dim();
        let mut intermediate = Array2::zeros((num_frames, num_bands));

        // Segment length for correlation (384 ms ≈ 30 frames at 78 fps)
        let seg_len = 30.min(num_frames);

        for band_idx in 0..num_bands {
            for frame_start in 0..num_frames {
                let frame_end = (frame_start + seg_len).min(num_frames);
                let seg_len_actual = frame_end - frame_start;

                if seg_len_actual < 10 {
                    // Minimum segment length
                    continue;
                }

                // Extract segments
                let clean_seg =
                    clean_bands.slice(scirs2_core::ndarray::s![frame_start..frame_end, band_idx]);
                let processed_seg = processed_bands
                    .slice(scirs2_core::ndarray::s![frame_start..frame_end, band_idx]);

                // Compute correlation
                let correlation = self.compute_correlation_coefficient(&clean_seg, &processed_seg);

                // Apply clipping at 0 (negative correlations set to 0)
                let clipped_correlation = correlation.max(0.0);

                // Store intermediate intelligibility value
                for i in frame_start..frame_end {
                    intermediate[[i, band_idx]] = clipped_correlation;
                }
            }
        }

        Ok(intermediate)
    }

    /// Compute correlation coefficient between two segments
    fn compute_correlation_coefficient(
        &self,
        x: &scirs2_core::ndarray::ArrayView1<f32>,
        y: &scirs2_core::ndarray::ArrayView1<f32>,
    ) -> f32 {
        if x.len() != y.len() || x.is_empty() {
            return 0.0;
        }

        let n = x.len() as f32;
        let mean_x = x.mean().unwrap_or(0.0);
        let mean_y = y.mean().unwrap_or(0.0);

        let mut numerator = 0.0;
        let mut sum_sq_x = 0.0;
        let mut sum_sq_y = 0.0;

        for (&xi, &yi) in x.iter().zip(y.iter()) {
            let diff_x = xi - mean_x;
            let diff_y = yi - mean_y;
            numerator += diff_x * diff_y;
            sum_sq_x += diff_x * diff_x;
            sum_sq_y += diff_y * diff_y;
        }

        let denominator = (sum_sq_x * sum_sq_y).sqrt();
        if denominator > 1e-12f32 {
            (numerator / denominator).clamp(-1.0, 1.0) // Clamp to valid correlation range
        } else {
            0.0
        }
    }

    /// Compute final STOI score with enhanced accuracy
    fn compute_final_stoi(&self, intermediate: &Array2<f32>) -> Result<f32, EvaluationError> {
        if intermediate.is_empty() {
            return Ok(0.0);
        }

        // Enhanced averaging with frequency-dependent weighting
        let (num_frames, num_bands) = intermediate.dim();

        // Create frequency-dependent weights based on speech intelligibility importance
        let frequency_weights = self.create_intelligibility_weights(num_bands);

        // Weighted average over frequency for each time frame
        let mut time_averaged_scores = Vec::with_capacity(num_frames);
        for frame_idx in 0..num_frames {
            let mut weighted_sum = 0.0;
            let mut weight_sum = 0.0;

            for band_idx in 0..num_bands {
                let value = intermediate[[frame_idx, band_idx]];
                let weight = frequency_weights[band_idx];
                weighted_sum += value * weight;
                weight_sum += weight;
            }

            if weight_sum > 0.0 {
                time_averaged_scores.push(weighted_sum / weight_sum);
            } else {
                time_averaged_scores.push(0.0);
            }
        }

        // Apply temporal reliability weighting
        let reliability_weighted_score =
            self.apply_temporal_reliability_weighting(&time_averaged_scores);

        // Apply non-linear mapping for better prediction accuracy
        let enhanced_score = self.apply_intelligibility_mapping(reliability_weighted_score);

        // Clamp to [0, 1] range
        Ok(enhanced_score.max(0.0).min(1.0))
    }

    /// Create frequency-dependent weights for intelligibility
    fn create_intelligibility_weights(&self, num_bands: usize) -> Vec<f32> {
        let mut weights = Vec::with_capacity(num_bands);

        for band_idx in 0..num_bands {
            if band_idx < self.octave_bands.len() {
                let (f_low, f_high) = self.octave_bands[band_idx];
                let center_freq = (f_low + f_high) / 2.0;

                // Weight based on speech intelligibility importance
                let weight = if center_freq < 500.0 {
                    0.8 // Lower frequencies - less critical for intelligibility
                } else if center_freq < 2000.0 {
                    1.5 // Mid frequencies - most critical for intelligibility
                } else if center_freq < 4000.0 {
                    1.2 // Upper-mid frequencies - important for consonants
                } else if center_freq < 6000.0 {
                    0.9 // High frequencies - less critical but still important
                } else {
                    0.6 // Very high frequencies - least critical
                };

                weights.push(weight);
            } else {
                weights.push(1.0);
            }
        }

        weights
    }

    /// Apply temporal reliability weighting
    fn apply_temporal_reliability_weighting(&self, time_scores: &[f32]) -> f32 {
        if time_scores.is_empty() {
            return 0.0;
        }

        // Calculate running variance to identify reliable segments
        let window_size = 10.min(time_scores.len());
        let mut reliable_scores = Vec::new();
        let mut weights = Vec::new();

        for i in 0..time_scores.len() {
            let start = if i >= window_size / 2 {
                i - window_size / 2
            } else {
                0
            };
            let end = (i + window_size / 2 + 1).min(time_scores.len());

            // Calculate local variance
            let window = &time_scores[start..end];
            let local_mean = window.iter().sum::<f32>() / window.len() as f32;
            let local_var = window
                .iter()
                .map(|&x| (x - local_mean).powi(2))
                .sum::<f32>()
                / window.len() as f32;

            // Higher weight for lower variance (more reliable segments)
            let reliability_weight = 1.0 / (1.0 + 10.0 * local_var);

            reliable_scores.push(time_scores[i]);
            weights.push(reliability_weight);
        }

        // Weighted average
        let weighted_sum: f32 = reliable_scores
            .iter()
            .zip(weights.iter())
            .map(|(score, weight)| score * weight)
            .sum();
        let weight_sum: f32 = weights.iter().sum();

        if weight_sum > 0.0 {
            weighted_sum / weight_sum
        } else {
            time_scores.iter().sum::<f32>() / time_scores.len() as f32
        }
    }

    /// Apply non-linear mapping for better intelligibility prediction
    fn apply_intelligibility_mapping(&self, raw_score: f32) -> f32 {
        // Enhanced mapping based on empirical studies for better prediction accuracy
        let x = raw_score.max(0.0).min(1.0);

        // Three-piece mapping optimized for intelligibility prediction
        if x < 0.3 {
            // Low intelligibility region - steeper mapping
            x * x * 1.67 // Quadratic for low values
        } else if x < 0.7 {
            // Mid intelligibility region - more linear
            0.15 + (x - 0.3) * 1.75
        } else {
            // High intelligibility region - compress slightly
            0.85 + (x - 0.7) * 0.5
        }
    }

    /// Apply ESTOI enhancements
    async fn apply_estoi_enhancement(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
        base_stoi: f32,
    ) -> Result<f32, EvaluationError> {
        // ESTOI improvements include:
        // 1. Better correlation analysis
        // 2. Temporal masking considerations
        // 3. Improved frequency weighting

        // For now, apply a simple enhancement based on spectral characteristics
        let spectral_enhancement = self.compute_spectral_enhancement(clean, processed).await?;

        // Combine base STOI with enhancement
        let enhanced_score = base_stoi * 0.8 + spectral_enhancement * 0.2;

        Ok(enhanced_score.max(0.0).min(1.0))
    }

    /// Set language-specific parameters for adapted STOI evaluation
    pub fn set_language_params(&mut self, language: LanguageCode) {
        self.language_params = Some(Self::create_language_params(language));
    }

    /// Calculate language-adapted STOI score
    pub async fn calculate_language_adapted_stoi(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
        language: Option<LanguageCode>,
    ) -> Result<f32, EvaluationError> {
        let language_params = if let Some(lang) = language {
            Self::create_language_params(lang)
        } else {
            self.language_params
                .clone()
                .unwrap_or_else(|| Self::create_language_params(LanguageCode::EnUs))
        };

        // Validate inputs
        self.validate_inputs(clean, processed)?;

        // Ensure signals have the same length
        let min_len = clean.samples().len().min(processed.samples().len());
        let clean_samples = &clean.samples()[..min_len];
        let processed_samples = &processed.samples()[..min_len];

        // Apply pre-emphasis filter
        let clean_filtered = self.apply_preemphasis(clean_samples)?;
        let processed_filtered = self.apply_preemphasis(processed_samples)?;

        // Compute short-time FFT for both signals
        let clean_stft = self.compute_stft(&clean_filtered)?;
        let processed_stft = self.compute_stft(&processed_filtered)?;

        // Group frequency bins into third-octave bands
        let clean_bands = self.group_into_octave_bands(&clean_stft)?;
        let processed_bands = self.group_into_octave_bands(&processed_stft)?;

        // Apply language-specific frequency weighting
        let weighted_clean =
            self.apply_language_frequency_weighting(&clean_bands, &language_params)?;
        let weighted_processed =
            self.apply_language_frequency_weighting(&processed_bands, &language_params)?;

        // Apply intermediate intelligibility measure
        let intermediate_intel =
            self.compute_intermediate_intelligibility(&weighted_clean, &weighted_processed)?;

        // Apply language-specific temporal weighting
        let temporal_weighted =
            self.apply_language_temporal_weighting(&intermediate_intel, &language_params)?;

        // Compute final STOI score with language adaptation
        let base_score = self.compute_final_stoi(&temporal_weighted)?;

        // Apply language-specific calibration
        let calibrated_score = self.apply_language_calibration(base_score, &language_params);

        Ok(calibrated_score)
    }

    /// Calculate STOI with confidence interval
    pub async fn calculate_stoi_with_confidence(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
        confidence_level: f32,
        num_bootstrap_samples: usize,
    ) -> Result<STOIConfidenceInterval, EvaluationError> {
        if confidence_level <= 0.0 || confidence_level >= 1.0 {
            return Err(EvaluationError::InvalidInput {
                message: "Confidence level must be between 0 and 1".to_string(),
            });
        }

        if num_bootstrap_samples < 10 {
            return Err(EvaluationError::InvalidInput {
                message: "Number of bootstrap samples must be at least 10".to_string(),
            });
        }

        // Calculate base STOI score
        let base_score = self.calculate_stoi(clean, processed).await?;

        // Generate bootstrap samples
        let bootstrap_scores = self
            .generate_bootstrap_samples(clean, processed, num_bootstrap_samples)
            .await?;

        // Calculate statistics
        let mean_score = bootstrap_scores.iter().sum::<f32>() / bootstrap_scores.len() as f32;
        let variance = bootstrap_scores
            .iter()
            .map(|&score| (score - mean_score).powi(2))
            .sum::<f32>()
            / (bootstrap_scores.len() - 1) as f32;
        let standard_error = variance.max(0.0).sqrt();

        // Calculate confidence interval
        let alpha = 1.0 - confidence_level;
        let t_value = self.calculate_t_value(alpha / 2.0, bootstrap_scores.len() - 1);
        let margin_of_error = t_value * standard_error;

        let lower_bound = (base_score - margin_of_error).max(0.0);
        let upper_bound = (base_score + margin_of_error).min(1.0);

        Ok(STOIConfidenceInterval {
            score: base_score,
            lower_bound,
            upper_bound,
            confidence_level,
            standard_error,
        })
    }

    /// Calculate language-adapted STOI with confidence interval
    pub async fn calculate_language_adapted_stoi_with_confidence(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
        language: Option<LanguageCode>,
        confidence_level: f32,
        num_bootstrap_samples: usize,
    ) -> Result<STOIConfidenceInterval, EvaluationError> {
        if confidence_level <= 0.0 || confidence_level >= 1.0 {
            return Err(EvaluationError::InvalidInput {
                message: "Confidence level must be between 0 and 1".to_string(),
            });
        }

        // Calculate base language-adapted STOI score
        let base_score = self
            .calculate_language_adapted_stoi(clean, processed, language)
            .await?;

        // Generate bootstrap samples with language adaptation
        let bootstrap_scores = self
            .generate_language_adapted_bootstrap_samples(
                clean,
                processed,
                language,
                num_bootstrap_samples,
            )
            .await?;

        // Calculate statistics
        let mean_score = bootstrap_scores.iter().sum::<f32>() / bootstrap_scores.len() as f32;
        let variance = bootstrap_scores
            .iter()
            .map(|&score| (score - mean_score).powi(2))
            .sum::<f32>()
            / (bootstrap_scores.len() - 1) as f32;
        let standard_error = variance.max(0.0).sqrt();

        // Calculate confidence interval
        let alpha = 1.0 - confidence_level;
        let t_value = self.calculate_t_value(alpha / 2.0, bootstrap_scores.len() - 1);
        let margin_of_error = t_value * standard_error;

        let lower_bound = (base_score - margin_of_error).max(0.0);
        let upper_bound = (base_score + margin_of_error).min(1.0);

        Ok(STOIConfidenceInterval {
            score: base_score,
            lower_bound,
            upper_bound,
            confidence_level,
            standard_error,
        })
    }

    /// Compute spectral enhancement factor for ESTOI
    async fn compute_spectral_enhancement(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
    ) -> Result<f32, EvaluationError> {
        // Analyze spectral characteristics
        let clean_spectrum = self.compute_average_spectrum(clean.samples())?;
        let processed_spectrum = self.compute_average_spectrum(processed.samples())?;

        // Compute spectral distortion
        let mut total_distortion = 0.0;
        let mut valid_bins = 0;

        let min_len = clean_spectrum.len().min(processed_spectrum.len());
        for i in 0..min_len {
            if clean_spectrum[i] > 1e-12f32 && processed_spectrum[i] > 1e-12f32 {
                let ratio = processed_spectrum[i] / clean_spectrum[i];
                // Ensure ratio is positive and add epsilon to prevent log(0)
                let safe_ratio = ratio.max(1e-12f32);
                let log_ratio = safe_ratio.ln();
                total_distortion += log_ratio * log_ratio;
                valid_bins += 1;
            }
        }

        if valid_bins > 0 {
            let mean_distortion = total_distortion / valid_bins as f32;
            // Convert distortion to enhancement factor
            let enhancement = (-mean_distortion).exp().min(1.0).max(0.0);
            Ok(enhancement)
        } else {
            Ok(0.5)
        }
    }

    /// Compute average power spectrum
    fn compute_average_spectrum(&self, signal: &[f32]) -> Result<Vec<f32>, EvaluationError> {
        let hop_size = self.frame_len / 2;
        let mut avg_spectrum = vec![0.0f32; self.frame_len / 2 + 1];
        let mut frame_count = 0;

        for frame_start in (0..signal.len() - self.frame_len + 1).step_by(hop_size) {
            // Extract frame
            let mut frame = vec![0.0; self.frame_len];
            for i in 0..self.frame_len {
                if frame_start + i < signal.len() {
                    frame[i] = signal[frame_start + i];
                }
            }

            // Compute FFT using functional API
            let spectrum = scirs2_fft::rfft(&frame, None)?;

            // Accumulate power spectrum (convert f64 to f32)
            for (i, &complex_val) in spectrum.iter().enumerate() {
                if i < avg_spectrum.len() {
                    avg_spectrum[i] += complex_val.norm_sqr() as f32;
                }
            }
            frame_count += 1;
        }

        // Average
        if frame_count > 0 {
            for value in &mut avg_spectrum {
                *value /= frame_count as f32;
            }
        }

        Ok(avg_spectrum)
    }

    /// Create third-octave frequency bands
    fn create_octave_bands(sample_rate: u32) -> Vec<(f32, f32)> {
        let mut bands = Vec::new();
        let nyquist = sample_rate as f32 / 2.0;

        // Start from 150 Hz (approximately)
        let mut center_freq = 150.0;

        while center_freq < nyquist {
            // Third-octave band boundaries
            let factor = 2_f32.powf(1.0 / 6.0); // 2^(1/6) for third-octave
            let f_low = center_freq / factor;
            let f_high = center_freq * factor;

            if f_high <= nyquist {
                bands.push((f_low, f_high));
            }

            // Next center frequency
            center_freq *= 2_f32.powf(1.0 / 3.0); // Move by 1/3 octave
        }

        bands
    }

    /// Create language-specific parameters
    fn create_language_params(language: LanguageCode) -> LanguageSpecificParams {
        match language {
            LanguageCode::EnUs | LanguageCode::EnGb => LanguageSpecificParams {
                language,
                frequency_weights: vec![
                    1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5,
                    2.4, 2.3, 2.2, 2.1, 2.0, 1.9, 1.8, 1.7, 1.6, 1.5,
                ],
                temporal_weights: vec![1.0; 30],
                intelligibility_threshold: 0.5,
            },
            LanguageCode::JaJp => LanguageSpecificParams {
                language,
                frequency_weights: vec![
                    0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4,
                    2.5, 2.4, 2.3, 2.2, 2.1, 2.0, 1.9, 1.8, 1.7, 1.6,
                ],
                temporal_weights: vec![1.0; 30],
                intelligibility_threshold: 0.45,
            },
            LanguageCode::ZhCn => LanguageSpecificParams {
                language,
                frequency_weights: vec![
                    0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3,
                    2.4, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0, 1.9, 1.8, 1.7,
                ],
                temporal_weights: vec![1.0; 30],
                intelligibility_threshold: 0.4,
            },
            LanguageCode::EsEs | LanguageCode::EsMx => LanguageSpecificParams {
                language,
                frequency_weights: vec![
                    1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5,
                    2.4, 2.3, 2.2, 2.1, 2.0, 1.9, 1.8, 1.7, 1.6, 1.5,
                ],
                temporal_weights: vec![1.0; 30],
                intelligibility_threshold: 0.48,
            },
            LanguageCode::FrFr => LanguageSpecificParams {
                language,
                frequency_weights: vec![
                    1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5,
                    2.4, 2.3, 2.2, 2.1, 2.0, 1.9, 1.8, 1.7, 1.6, 1.5,
                ],
                temporal_weights: vec![1.0; 30],
                intelligibility_threshold: 0.52,
            },
            LanguageCode::DeDe => LanguageSpecificParams {
                language,
                frequency_weights: vec![
                    1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5,
                    2.4, 2.3, 2.2, 2.1, 2.0, 1.9, 1.8, 1.7, 1.6, 1.5,
                ],
                temporal_weights: vec![1.0; 30],
                intelligibility_threshold: 0.51,
            },
            _ => LanguageSpecificParams {
                language,
                frequency_weights: vec![1.0; 26],
                temporal_weights: vec![1.0; 30],
                intelligibility_threshold: 0.5,
            },
        }
    }

    /// Apply language-specific frequency weighting
    fn apply_language_frequency_weighting(
        &self,
        bands: &Array2<f32>,
        params: &LanguageSpecificParams,
    ) -> Result<Array2<f32>, EvaluationError> {
        let (num_frames, num_bands) = bands.dim();
        let mut weighted_bands = bands.clone();

        let weights_len = params.frequency_weights.len();
        for band_idx in 0..num_bands {
            let weight = if band_idx < weights_len {
                params.frequency_weights[band_idx]
            } else {
                1.0
            };

            for frame_idx in 0..num_frames {
                weighted_bands[[frame_idx, band_idx]] *= weight;
            }
        }

        Ok(weighted_bands)
    }

    /// Apply language-specific temporal weighting
    fn apply_language_temporal_weighting(
        &self,
        intermediate: &Array2<f32>,
        params: &LanguageSpecificParams,
    ) -> Result<Array2<f32>, EvaluationError> {
        let (num_frames, num_bands) = intermediate.dim();
        let mut weighted_intermediate = intermediate.clone();

        let weights_len = params.temporal_weights.len();
        for frame_idx in 0..num_frames {
            let weight = if frame_idx < weights_len {
                params.temporal_weights[frame_idx]
            } else {
                1.0
            };

            for band_idx in 0..num_bands {
                weighted_intermediate[[frame_idx, band_idx]] *= weight;
            }
        }

        Ok(weighted_intermediate)
    }

    /// Apply language-specific calibration
    fn apply_language_calibration(&self, base_score: f32, params: &LanguageSpecificParams) -> f32 {
        // Enhanced language-specific calibration with more significant differences
        let language_factor = match params.language {
            LanguageCode::EnUs | LanguageCode::EnGb => 1.0,
            LanguageCode::JaJp => 0.95, // Japanese has different phonetic structure
            LanguageCode::ZhCn => 0.92, // Chinese has tonal characteristics
            LanguageCode::EsEs | LanguageCode::EsMx => 0.98,
            LanguageCode::FrFr => 0.97,
            LanguageCode::DeDe => 0.96,
            _ => 0.95,
        };

        // Apply threshold-based adjustment
        let threshold_factor = if base_score < params.intelligibility_threshold {
            0.85
        } else {
            1.05
        };

        let calibrated = base_score * language_factor * threshold_factor;
        calibrated.max(0.0).min(1.0)
    }

    /// Generate bootstrap samples for confidence interval calculation
    async fn generate_bootstrap_samples(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
        num_samples: usize,
    ) -> Result<Vec<f32>, EvaluationError> {
        let mut bootstrap_scores = Vec::with_capacity(num_samples);
        let signal_len = clean.samples().len().min(processed.samples().len());

        for _ in 0..num_samples {
            // Create bootstrap sample by random sampling with replacement
            let bootstrap_len = (signal_len as f32 * 0.8) as usize;
            let mut bootstrap_clean = Vec::with_capacity(bootstrap_len);
            let mut bootstrap_processed = Vec::with_capacity(bootstrap_len);

            for _ in 0..bootstrap_len {
                let idx = scirs2_core::random::thread_rng().random_range(0..signal_len);
                bootstrap_clean.push(clean.samples()[idx]);
                bootstrap_processed.push(processed.samples()[idx]);
            }

            // Create bootstrap audio buffers
            let bootstrap_clean_buffer = AudioBuffer::new(bootstrap_clean, clean.sample_rate(), 1);
            let bootstrap_processed_buffer =
                AudioBuffer::new(bootstrap_processed, processed.sample_rate(), 1);

            // Calculate STOI for this bootstrap sample
            let score = self
                .calculate_stoi(&bootstrap_clean_buffer, &bootstrap_processed_buffer)
                .await?;
            bootstrap_scores.push(score);
        }

        Ok(bootstrap_scores)
    }

    /// Generate language-adapted bootstrap samples
    async fn generate_language_adapted_bootstrap_samples(
        &self,
        clean: &AudioBuffer,
        processed: &AudioBuffer,
        language: Option<LanguageCode>,
        num_samples: usize,
    ) -> Result<Vec<f32>, EvaluationError> {
        let mut bootstrap_scores = Vec::with_capacity(num_samples);
        let signal_len = clean.samples().len().min(processed.samples().len());

        for _ in 0..num_samples {
            // Create bootstrap sample by random sampling with replacement
            let bootstrap_len = (signal_len as f32 * 0.8) as usize;
            let mut bootstrap_clean = Vec::with_capacity(bootstrap_len);
            let mut bootstrap_processed = Vec::with_capacity(bootstrap_len);

            for _ in 0..bootstrap_len {
                let idx = scirs2_core::random::thread_rng().random_range(0..signal_len);
                bootstrap_clean.push(clean.samples()[idx]);
                bootstrap_processed.push(processed.samples()[idx]);
            }

            // Create bootstrap audio buffers
            let bootstrap_clean_buffer = AudioBuffer::new(bootstrap_clean, clean.sample_rate(), 1);
            let bootstrap_processed_buffer =
                AudioBuffer::new(bootstrap_processed, processed.sample_rate(), 1);

            // Calculate language-adapted STOI for this bootstrap sample
            let score = self
                .calculate_language_adapted_stoi(
                    &bootstrap_clean_buffer,
                    &bootstrap_processed_buffer,
                    language,
                )
                .await?;
            bootstrap_scores.push(score);
        }

        Ok(bootstrap_scores)
    }

    /// Calculate t-value for confidence interval calculation
    fn calculate_t_value(&self, alpha: f32, degrees_of_freedom: usize) -> f32 {
        // Approximation of t-distribution critical values
        // For more precision, a proper t-distribution table or library should be used
        match degrees_of_freedom {
            df if df >= 30 => {
                // Normal approximation for large samples
                match alpha {
                    a if a <= 0.005 => 2.807,
                    a if a <= 0.01 => 2.576,
                    a if a <= 0.025 => 1.960,
                    a if a <= 0.05 => 1.645,
                    a if a <= 0.1 => 1.282,
                    _ => 1.960,
                }
            }
            df if df >= 20 => match alpha {
                a if a <= 0.005 => 2.845,
                a if a <= 0.01 => 2.528,
                a if a <= 0.025 => 2.093,
                a if a <= 0.05 => 1.725,
                a if a <= 0.1 => 1.325,
                _ => 2.093,
            },
            df if df >= 10 => match alpha {
                a if a <= 0.005 => 3.169,
                a if a <= 0.01 => 2.764,
                a if a <= 0.025 => 2.228,
                a if a <= 0.05 => 1.812,
                a if a <= 0.1 => 1.372,
                _ => 2.228,
            },
            _ => {
                // Conservative estimate for small samples
                match alpha {
                    a if a <= 0.005 => 4.032,
                    a if a <= 0.01 => 3.499,
                    a if a <= 0.025 => 2.776,
                    a if a <= 0.05 => 2.201,
                    a if a <= 0.1 => 1.533,
                    _ => 2.776,
                }
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_stoi_evaluator_creation() {
        let evaluator = STOIEvaluator::new(16000).unwrap();
        assert_eq!(evaluator.sample_rate, 16000);
        assert!(!evaluator.octave_bands.is_empty());
    }

    #[tokio::test]
    async fn test_stoi_calculation() {
        let evaluator = STOIEvaluator::new(16000).unwrap();

        // Create test signals (3+ seconds for STOI)
        let duration_samples = 3 * 16000;
        let clean = AudioBuffer::new(vec![0.1; duration_samples], 16000, 1);
        let processed = AudioBuffer::new(vec![0.08; duration_samples], 16000, 1);

        let stoi_score = evaluator.calculate_stoi(&clean, &processed).await.unwrap();

        // STOI score should be in [0, 1] range
        assert!(stoi_score >= 0.0);
        assert!(stoi_score <= 1.0);
    }

    #[tokio::test]
    async fn test_estoi_calculation() {
        let evaluator = STOIEvaluator::new(16000).unwrap();

        let duration_samples = 3 * 16000;
        let clean = AudioBuffer::new(vec![0.1; duration_samples], 16000, 1);
        let processed = AudioBuffer::new(vec![0.08; duration_samples], 16000, 1);

        let estoi_score = evaluator.calculate_estoi(&clean, &processed).await.unwrap();

        assert!(estoi_score >= 0.0);
        assert!(estoi_score <= 1.0);
    }

    #[test]
    fn test_preemphasis_filter() {
        let evaluator = STOIEvaluator::new(16000).unwrap();
        let signal = vec![1.0, 0.5, 0.25, 0.125];

        let filtered = evaluator.apply_preemphasis(&signal).unwrap();

        // First sample should be unchanged
        assert_eq!(filtered[0], 1.0);

        // Check filter operation: y[n] = x[n] - 0.97 * x[n-1]
        assert!((filtered[1] - (0.5 - 0.97 * 1.0)).abs() < 1e-6);
    }

    #[test]
    fn test_correlation_coefficient() {
        let evaluator = STOIEvaluator::new(16000).unwrap();

        let x = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
        let y = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0]);

        let correlation = evaluator.compute_correlation_coefficient(&x.view(), &y.view());
        assert!((correlation - 1.0).abs() < 1e-6); // Perfect correlation

        let z = Array1::from_vec(vec![4.0, 3.0, 2.0, 1.0]);
        let neg_correlation = evaluator.compute_correlation_coefficient(&x.view(), &z.view());
        assert!((neg_correlation + 1.0).abs() < 1e-6); // Perfect negative correlation
    }

    #[test]
    fn test_octave_bands_creation() {
        let bands = STOIEvaluator::create_octave_bands(16000);

        // Should have multiple bands
        assert!(!bands.is_empty());

        // All bands should be below Nyquist frequency
        for &(f_low, f_high) in &bands {
            assert!(f_low < 8000.0);
            assert!(f_high < 8000.0);
            assert!(f_low < f_high);
        }

        // First band should start around 150 Hz
        assert!(bands[0].0 >= 100.0);
        assert!(bands[0].0 <= 200.0);
    }

    #[tokio::test]
    async fn test_language_adapted_stoi() {
        let evaluator = STOIEvaluator::new(16000).unwrap();

        let duration_samples = 3 * 16000;

        // Create more realistic audio signals with multiple frequency components
        let mut clean_samples = Vec::with_capacity(duration_samples);
        let mut processed_samples = Vec::with_capacity(duration_samples);

        for i in 0..duration_samples {
            let t = i as f32 / 16000.0;
            // Mix of frequencies simulating speech-like content
            let clean_val = 0.1 * (2.0 * PI * 440.0 * t).sin()
                + 0.05 * (2.0 * PI * 880.0 * t).sin()
                + 0.03 * (2.0 * PI * 1320.0 * t).sin();

            // Add slight distortion to processed signal
            let processed_val = clean_val * 0.8 + 0.02 * (2.0 * PI * 60.0 * t).sin(); // Add hum

            clean_samples.push(clean_val);
            processed_samples.push(processed_val);
        }

        let clean = AudioBuffer::new(clean_samples, 16000, 1);
        let processed = AudioBuffer::new(processed_samples, 16000, 1);

        // Test with different languages
        let en_score = evaluator
            .calculate_language_adapted_stoi(&clean, &processed, Some(LanguageCode::EnUs))
            .await
            .unwrap();
        let ja_score = evaluator
            .calculate_language_adapted_stoi(&clean, &processed, Some(LanguageCode::JaJp))
            .await
            .unwrap();
        let zh_score = evaluator
            .calculate_language_adapted_stoi(&clean, &processed, Some(LanguageCode::ZhCn))
            .await
            .unwrap();

        // All scores should be in valid range
        assert!((0.0..=1.0).contains(&en_score));
        assert!((0.0..=1.0).contains(&ja_score));
        assert!((0.0..=1.0).contains(&zh_score));

        // Scores should be different due to language adaptation
        // With enhanced calibration, expect more significant differences
        let tolerance = 0.01;
        assert!(
            (en_score - ja_score).abs() > tolerance,
            "English score ({}) should differ from Japanese score ({}) by more than {}",
            en_score,
            ja_score,
            tolerance
        );
        assert!(
            (en_score - zh_score).abs() > tolerance,
            "English score ({}) should differ from Chinese score ({}) by more than {}",
            en_score,
            zh_score,
            tolerance
        );
    }

    #[tokio::test]
    async fn test_stoi_with_confidence_interval() {
        let evaluator = STOIEvaluator::new(16000).unwrap();

        let duration_samples = 4 * 16000; // 4 seconds to be safe
        let clean = AudioBuffer::new(vec![0.1; duration_samples], 16000, 1);
        let processed = AudioBuffer::new(vec![0.08; duration_samples], 16000, 1);

        let confidence_result = evaluator
            .calculate_stoi_with_confidence(&clean, &processed, 0.95, 20)
            .await
            .unwrap();

        // Check confidence interval structure
        assert!(confidence_result.score >= 0.0 && confidence_result.score <= 1.0);
        assert!(confidence_result.lower_bound >= 0.0);
        assert!(confidence_result.upper_bound <= 1.0);
        assert!(confidence_result.lower_bound <= confidence_result.score);
        assert!(confidence_result.score <= confidence_result.upper_bound);
        assert_eq!(confidence_result.confidence_level, 0.95);
        assert!(confidence_result.standard_error >= 0.0);
    }

    #[tokio::test]
    async fn test_language_adapted_stoi_with_confidence() {
        let evaluator = STOIEvaluator::new(16000).unwrap();

        let duration_samples = 4 * 16000; // 4 seconds to be safe
        let clean = AudioBuffer::new(vec![0.1; duration_samples], 16000, 1);
        let processed = AudioBuffer::new(vec![0.08; duration_samples], 16000, 1);

        let confidence_result = evaluator
            .calculate_language_adapted_stoi_with_confidence(
                &clean,
                &processed,
                Some(LanguageCode::EnUs),
                0.95,
                15,
            )
            .await
            .unwrap();

        // Check confidence interval structure
        assert!(confidence_result.score >= 0.0 && confidence_result.score <= 1.0);
        assert!(confidence_result.lower_bound >= 0.0);
        assert!(confidence_result.upper_bound <= 1.0);
        assert!(confidence_result.lower_bound <= confidence_result.score);
        assert!(confidence_result.score <= confidence_result.upper_bound);
        assert_eq!(confidence_result.confidence_level, 0.95);
        assert!(confidence_result.standard_error >= 0.0);
    }

    #[test]
    fn test_language_params_creation() {
        let en_params = STOIEvaluator::create_language_params(LanguageCode::EnUs);
        let ja_params = STOIEvaluator::create_language_params(LanguageCode::JaJp);
        let zh_params = STOIEvaluator::create_language_params(LanguageCode::ZhCn);

        assert_eq!(en_params.language, LanguageCode::EnUs);
        assert_eq!(ja_params.language, LanguageCode::JaJp);
        assert_eq!(zh_params.language, LanguageCode::ZhCn);

        // Check that parameters are different
        assert_ne!(
            en_params.intelligibility_threshold,
            ja_params.intelligibility_threshold
        );
        assert_ne!(
            en_params.intelligibility_threshold,
            zh_params.intelligibility_threshold
        );
    }

    #[test]
    fn test_t_value_calculation() {
        let evaluator = STOIEvaluator::new(16000).unwrap();

        // Test different alpha values and degrees of freedom
        let t_005_30 = evaluator.calculate_t_value(0.005, 30);
        let t_025_30 = evaluator.calculate_t_value(0.025, 30);
        let t_05_30 = evaluator.calculate_t_value(0.05, 30);

        // Higher alpha should give lower t-value
        assert!(t_005_30 > t_025_30);
        assert!(t_025_30 > t_05_30);

        // All values should be positive
        assert!(t_005_30 > 0.0);
        assert!(t_025_30 > 0.0);
        assert!(t_05_30 > 0.0);
    }
}