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
//! Voiced/Unvoiced (VUV) analysis and evaluation
//!
//! This module provides comprehensive VUV decision analysis, including:
//! - Multiple VUV detection algorithms
//! - VUV decision accuracy evaluation
//! - Temporal VUV pattern analysis
//! - Cross-algorithm VUV comparison

use crate::EvaluationResult;
use voirs_sdk::AudioBuffer;

/// VUV analysis configuration
#[derive(Debug, Clone)]
pub struct VuvConfig {
    /// Frame length in seconds
    pub frame_length: f32,
    /// Frame hop in seconds
    pub frame_hop: f32,
    /// Energy threshold for voicing decision
    pub energy_threshold: f32,
    /// Zero-crossing rate threshold
    pub zcr_threshold: f32,
    /// Autocorrelation threshold
    pub autocorr_threshold: f32,
    /// Spectral centroid threshold
    pub spectral_centroid_threshold: f32,
    /// Spectral rolloff threshold
    pub spectral_rolloff_threshold: f32,
    /// Voicing probability threshold
    pub voicing_prob_threshold: f32,
}

impl Default for VuvConfig {
    fn default() -> Self {
        Self {
            frame_length: 0.025,     // 25ms frames
            frame_hop: 0.01,         // 10ms hop
            energy_threshold: -30.0, // dB energy threshold (more sensitive)
            zcr_threshold: 0.3,
            autocorr_threshold: 0.3,
            spectral_centroid_threshold: 1000.0,
            spectral_rolloff_threshold: 0.85,
            voicing_prob_threshold: 0.5,
        }
    }
}

/// VUV decision for a single frame
#[derive(Debug, Clone, PartialEq)]
pub struct VuvFrame {
    /// Time in seconds
    pub time: f32,
    /// Voiced decision (true = voiced, false = unvoiced)
    pub is_voiced: bool,
    /// Voicing probability [0.0, 1.0]
    pub voicing_probability: f32,
    /// Individual feature values
    pub features: VuvFeatures,
}

/// VUV feature values for analysis
#[derive(Debug, Clone, PartialEq)]
pub struct VuvFeatures {
    /// Energy (log scale)
    pub energy: f32,
    /// Zero-crossing rate
    pub zcr: f32,
    /// Autocorrelation peak
    pub autocorr_peak: f32,
    /// Spectral centroid (Hz)
    pub spectral_centroid: f32,
    /// Spectral rolloff (ratio)
    pub spectral_rolloff: f32,
    /// Harmonic-to-noise ratio
    pub hnr: f32,
    /// Spectral flatness
    pub spectral_flatness: f32,
}

/// Complete VUV analysis result
#[derive(Debug, Clone)]
pub struct VuvAnalysis {
    /// VUV frames
    pub frames: Vec<VuvFrame>,
    /// Sample rate of original audio
    pub sample_rate: u32,
    /// Configuration used
    pub config: VuvConfig,
    /// Algorithm used
    pub algorithm: VuvAlgorithm,
    /// Overall statistics
    pub statistics: VuvStatistics,
}

/// Available VUV detection algorithms
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VuvAlgorithm {
    /// Energy-based decision
    Energy,
    /// Zero-crossing rate based
    ZeroCrossing,
    /// Autocorrelation based
    Autocorrelation,
    /// Spectral features based
    SpectralFeatures,
    /// Multi-feature combination
    MultiFature,
    /// Machine learning based (simplified)
    MachineLearning,
}

/// VUV analysis statistics
#[derive(Debug, Clone, PartialEq)]
pub struct VuvStatistics {
    /// Total number of frames
    pub total_frames: usize,
    /// Number of voiced frames
    pub voiced_frames: usize,
    /// Number of unvoiced frames
    pub unvoiced_frames: usize,
    /// Voicing rate (fraction of voiced frames)
    pub voicing_rate: f32,
    /// Average voicing probability for voiced frames
    pub avg_voiced_probability: f32,
    /// Average voicing probability for unvoiced frames
    pub avg_unvoiced_probability: f32,
    /// Longest voiced segment (frames)
    pub longest_voiced_segment: usize,
    /// Longest unvoiced segment (frames)
    pub longest_unvoiced_segment: usize,
    /// Number of voicing transitions
    pub voicing_transitions: usize,
}

/// VUV comparison result
#[derive(Debug, Clone)]
pub struct VuvComparison {
    /// Reference VUV analysis
    pub reference: VuvAnalysis,
    /// Test VUV analysis
    pub test: VuvAnalysis,
    /// Accuracy metrics
    pub accuracy: VuvAccuracy,
    /// Temporal alignment
    pub alignment: VuvAlignment,
}

/// VUV accuracy metrics
#[derive(Debug, Clone, PartialEq)]
pub struct VuvAccuracy {
    /// Overall accuracy (correct decisions / total decisions)
    pub overall_accuracy: f32,
    /// Voiced accuracy (correctly identified voiced frames / total voiced frames)
    pub voiced_accuracy: f32,
    /// Unvoiced accuracy (correctly identified unvoiced frames / total unvoiced frames)
    pub unvoiced_accuracy: f32,
    /// Precision for voiced detection
    pub voiced_precision: f32,
    /// Recall for voiced detection
    pub voiced_recall: f32,
    /// F1 score for voiced detection
    pub voiced_f1: f32,
    /// False positive rate (unvoiced identified as voiced)
    pub false_positive_rate: f32,
    /// False negative rate (voiced identified as unvoiced)
    pub false_negative_rate: f32,
    /// Correlation coefficient between voicing probabilities
    pub probability_correlation: f32,
}

/// VUV temporal alignment analysis
#[derive(Debug, Clone, PartialEq)]
pub struct VuvAlignment {
    /// Alignment accuracy
    pub alignment_accuracy: f32,
    /// Average time shift (seconds)
    pub avg_time_shift: f32,
    /// Segment boundary accuracy
    pub boundary_accuracy: f32,
    /// Transition timing errors
    pub transition_errors: Vec<f32>,
}

/// VUV analyzer implementation
pub struct VuvAnalyzer {
    config: VuvConfig,
}

impl VuvAnalyzer {
    /// Create a new VUV analyzer
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(VuvConfig::default())
    }

    /// Create with custom configuration
    #[must_use]
    pub fn with_config(config: VuvConfig) -> Self {
        Self { config }
    }

    /// Analyze VUV decisions in audio
    pub async fn analyze(
        &self,
        audio: &AudioBuffer,
        algorithm: VuvAlgorithm,
    ) -> EvaluationResult<VuvAnalysis> {
        let samples = audio.samples();
        let sample_rate = audio.sample_rate() as f32;

        let frame_length_samples = (self.config.frame_length * sample_rate) as usize;
        let frame_hop_samples = (self.config.frame_hop * sample_rate) as usize;

        let mut frames = Vec::new();
        let mut pos = 0;

        while pos + frame_length_samples <= samples.len() {
            let frame_samples = &samples[pos..pos + frame_length_samples];
            let time = pos as f32 / sample_rate;

            let features = self.extract_features(frame_samples, sample_rate).await?;
            let (is_voiced, voicing_probability) = self.make_vuv_decision(&features, algorithm)?;

            frames.push(VuvFrame {
                time,
                is_voiced,
                voicing_probability,
                features,
            });

            pos += frame_hop_samples;
        }

        let statistics = self.calculate_statistics(&frames);

        Ok(VuvAnalysis {
            frames,
            sample_rate: audio.sample_rate(),
            config: self.config.clone(),
            algorithm,
            statistics,
        })
    }

    /// Compare VUV decisions between reference and test audio
    pub async fn compare(
        &self,
        reference: &AudioBuffer,
        test: &AudioBuffer,
        algorithm: VuvAlgorithm,
    ) -> EvaluationResult<VuvComparison> {
        let ref_analysis = self.analyze(reference, algorithm).await?;
        let test_analysis = self.analyze(test, algorithm).await?;

        let accuracy = self.calculate_accuracy(&ref_analysis, &test_analysis)?;
        let alignment = self.analyze_alignment(&ref_analysis, &test_analysis)?;

        Ok(VuvComparison {
            reference: ref_analysis,
            test: test_analysis,
            accuracy,
            alignment,
        })
    }

    /// Extract VUV features from audio frame
    async fn extract_features(
        &self,
        frame: &[f32],
        sample_rate: f32,
    ) -> EvaluationResult<VuvFeatures> {
        let energy = self.calculate_energy(frame);
        let zcr = self.calculate_zcr(frame);
        let autocorr_peak = self.calculate_autocorr_peak(frame)?;
        let spectral_centroid = self.calculate_spectral_centroid(frame, sample_rate).await?;
        let spectral_rolloff = self.calculate_spectral_rolloff(frame, sample_rate).await?;
        let hnr = self.calculate_hnr(frame)?;
        let spectral_flatness = self.calculate_spectral_flatness(frame).await?;

        Ok(VuvFeatures {
            energy,
            zcr,
            autocorr_peak,
            spectral_centroid,
            spectral_rolloff,
            hnr,
            spectral_flatness,
        })
    }

    /// Make VUV decision based on features and algorithm
    fn make_vuv_decision(
        &self,
        features: &VuvFeatures,
        algorithm: VuvAlgorithm,
    ) -> EvaluationResult<(bool, f32)> {
        let voicing_prob = match algorithm {
            VuvAlgorithm::Energy => {
                // Energy is dB scale, higher values are more voiced

                if features.energy > self.config.energy_threshold {
                    ((features.energy - self.config.energy_threshold) / 30.0)
                        .min(1.0)
                        .max(0.0)
                } else {
                    0.0
                }
            }
            VuvAlgorithm::ZeroCrossing => {
                let zcr_score = 1.0 - (features.zcr / self.config.zcr_threshold).min(1.0);
                zcr_score.max(0.0)
            }
            VuvAlgorithm::Autocorrelation => (features.autocorr_peak
                / self.config.autocorr_threshold)
                .min(1.0)
                .max(0.0),
            VuvAlgorithm::SpectralFeatures => {
                let centroid_score =
                    if features.spectral_centroid < self.config.spectral_centroid_threshold {
                        1.0 - features.spectral_centroid / self.config.spectral_centroid_threshold
                    } else {
                        0.0
                    };
                let rolloff_score = features.spectral_rolloff;
                let flatness_score = 1.0 - features.spectral_flatness;

                (centroid_score + rolloff_score + flatness_score) / 3.0
            }
            VuvAlgorithm::MultiFature => {
                // Combine multiple features with weights
                let energy_score = if features.energy > self.config.energy_threshold {
                    ((features.energy - self.config.energy_threshold) / 30.0)
                        .min(1.0)
                        .max(0.0)
                } else {
                    0.0
                };
                let zcr_score = 1.0 - (features.zcr / self.config.zcr_threshold).min(1.0).max(0.0);
                let autocorr_score = (features.autocorr_peak / self.config.autocorr_threshold)
                    .min(1.0)
                    .max(0.0);
                let hnr_score = (features.hnr / 10.0).min(1.0).max(0.0); // Assume 10 dB as good HNR

                // Weighted combination
                0.3 * energy_score + 0.2 * zcr_score + 0.3 * autocorr_score + 0.2 * hnr_score
            }
            VuvAlgorithm::MachineLearning => {
                // Simplified ML-like decision using feature combination
                let feature_vector = [
                    features.energy,
                    features.zcr,
                    features.autocorr_peak,
                    features.spectral_centroid / 1000.0, // Normalize
                    features.spectral_rolloff,
                    features.hnr / 20.0, // Normalize
                    features.spectral_flatness,
                ];

                // Simple linear combination (could be replaced with actual ML model)
                let weights = [0.2, -0.15, 0.25, -0.1, 0.15, 0.2, -0.15];
                let score: f32 = feature_vector
                    .iter()
                    .zip(weights.iter())
                    .map(|(f, w)| f * w)
                    .sum();

                // Apply sigmoid to get probability
                1.0 / (1.0 + (-score).exp())
            }
        };

        let is_voiced = voicing_prob > self.config.voicing_prob_threshold;
        Ok((is_voiced, voicing_prob))
    }

    /// Calculate frame energy
    fn calculate_energy(&self, frame: &[f32]) -> f32 {
        let energy = frame.iter().map(|x| x * x).sum::<f32>() / frame.len() as f32;
        if energy > 1e-10 {
            10.0 * energy.log10() // Energy in dB scale
        } else {
            -100.0 // Very low energy
        }
    }

    /// Calculate zero-crossing rate
    fn calculate_zcr(&self, frame: &[f32]) -> f32 {
        if frame.len() < 2 {
            return 0.0;
        }

        let crossings = frame.windows(2).filter(|w| w[0] * w[1] < 0.0).count();

        crossings as f32 / (frame.len() - 1) as f32
    }

    /// Calculate autocorrelation peak
    fn calculate_autocorr_peak(&self, frame: &[f32]) -> EvaluationResult<f32> {
        if frame.is_empty() {
            return Ok(0.0);
        }

        let max_lag = frame.len() / 4; // Check up to 1/4 of frame length
        let mut max_corr: f32 = 0.0;

        for lag in 1..max_lag {
            let mut correlation = 0.0;
            let mut norm1 = 0.0;
            let mut norm2 = 0.0;

            for i in 0..(frame.len() - lag) {
                correlation += frame[i] * frame[i + lag];
                norm1 += frame[i] * frame[i];
                norm2 += frame[i + lag] * frame[i + lag];
            }

            if norm1 > 0.0 && norm2 > 0.0 {
                let normalized_corr = correlation / (norm1 * norm2).sqrt();
                max_corr = max_corr.max(normalized_corr);
            }
        }

        Ok(max_corr)
    }

    /// Calculate spectral centroid
    async fn calculate_spectral_centroid(
        &self,
        frame: &[f32],
        sample_rate: f32,
    ) -> EvaluationResult<f32> {
        let spectrum = self.compute_spectrum(frame).await?;

        let mut weighted_sum = 0.0;
        let mut magnitude_sum = 0.0;

        for (i, &magnitude) in spectrum.iter().enumerate() {
            let frequency = i as f32 * sample_rate / (2.0 * spectrum.len() as f32);
            weighted_sum += frequency * magnitude;
            magnitude_sum += magnitude;
        }

        if magnitude_sum > 0.0 {
            Ok(weighted_sum / magnitude_sum)
        } else {
            Ok(0.0)
        }
    }

    /// Calculate spectral rolloff
    async fn calculate_spectral_rolloff(
        &self,
        frame: &[f32],
        sample_rate: f32,
    ) -> EvaluationResult<f32> {
        let spectrum = self.compute_spectrum(frame).await?;

        let total_energy: f32 = spectrum.iter().sum();
        let rolloff_threshold = 0.85 * total_energy;

        let mut cumulative_energy = 0.0;
        for (i, &magnitude) in spectrum.iter().enumerate() {
            cumulative_energy += magnitude;
            if cumulative_energy >= rolloff_threshold {
                let frequency = i as f32 * sample_rate / (2.0 * spectrum.len() as f32);
                return Ok(frequency / (sample_rate / 2.0)); // Normalized to Nyquist
            }
        }

        Ok(1.0) // Full spectrum
    }

    /// Calculate harmonic-to-noise ratio
    fn calculate_hnr(&self, frame: &[f32]) -> EvaluationResult<f32> {
        if frame.is_empty() {
            return Ok(0.0);
        }

        // Simplified HNR calculation
        let signal_power = frame.iter().map(|x| x * x).sum::<f32>() / frame.len() as f32;

        // Estimate noise power using high-frequency components
        let noise_estimate = if frame.len() > 10 {
            let differences: Vec<f32> = frame.windows(2).map(|w| w[1] - w[0]).collect();
            differences.iter().map(|x| x * x).sum::<f32>() / differences.len() as f32
        } else {
            signal_power * 0.1 // Assume 10% noise
        };

        if noise_estimate > 0.0 && signal_power > noise_estimate {
            let hnr = 10.0 * (signal_power / noise_estimate).log10();
            Ok(hnr.max(0.0).min(30.0)) // Clamp to reasonable range
        } else {
            Ok(0.0)
        }
    }

    /// Calculate spectral flatness
    async fn calculate_spectral_flatness(&self, frame: &[f32]) -> EvaluationResult<f32> {
        let spectrum = self.compute_spectrum(frame).await?;

        if spectrum.iter().any(|&x| x <= 0.0) {
            return Ok(0.0);
        }

        let geometric_mean = spectrum.iter().map(|x| x.ln()).sum::<f32>() / spectrum.len() as f32;
        let arithmetic_mean = spectrum.iter().sum::<f32>() / spectrum.len() as f32;

        if arithmetic_mean > 0.0 {
            Ok(geometric_mean.exp() / arithmetic_mean)
        } else {
            Ok(0.0)
        }
    }

    /// Compute power spectrum
    async fn compute_spectrum(&self, frame: &[f32]) -> EvaluationResult<Vec<f32>> {
        // Simplified power spectrum computation
        let n = frame.len();
        let mut spectrum = vec![0.0; n / 2 + 1];

        // Apply window
        let windowed: Vec<f32> = frame
            .iter()
            .enumerate()
            .map(|(i, &x)| {
                let window =
                    0.5 * (1.0 - (2.0 * std::f32::consts::PI * i as f32 / (n - 1) as f32).cos());
                x * window
            })
            .collect();

        // Simplified DFT for magnitude spectrum
        for k in 0..spectrum.len() {
            let mut real = 0.0;
            let mut imag = 0.0;

            for (i, &sample) in windowed.iter().enumerate() {
                let angle = -2.0 * std::f32::consts::PI * k as f32 * i as f32 / n as f32;
                real += sample * angle.cos();
                imag += sample * angle.sin();
            }

            spectrum[k] = (real * real + imag * imag).sqrt();
        }

        Ok(spectrum)
    }

    /// Calculate VUV statistics
    fn calculate_statistics(&self, frames: &[VuvFrame]) -> VuvStatistics {
        let total_frames = frames.len();
        let voiced_frames = frames.iter().filter(|f| f.is_voiced).count();
        let unvoiced_frames = total_frames - voiced_frames;

        let voicing_rate = if total_frames > 0 {
            voiced_frames as f32 / total_frames as f32
        } else {
            0.0
        };

        let avg_voiced_probability = if voiced_frames > 0 {
            frames
                .iter()
                .filter(|f| f.is_voiced)
                .map(|f| f.voicing_probability)
                .sum::<f32>()
                / voiced_frames as f32
        } else {
            0.0
        };

        let avg_unvoiced_probability = if unvoiced_frames > 0 {
            frames
                .iter()
                .filter(|f| !f.is_voiced)
                .map(|f| f.voicing_probability)
                .sum::<f32>()
                / unvoiced_frames as f32
        } else {
            0.0
        };

        // Calculate segment lengths and transitions
        let (longest_voiced, longest_unvoiced, transitions) = self.analyze_segments(frames);

        VuvStatistics {
            total_frames,
            voiced_frames,
            unvoiced_frames,
            voicing_rate,
            avg_voiced_probability,
            avg_unvoiced_probability,
            longest_voiced_segment: longest_voiced,
            longest_unvoiced_segment: longest_unvoiced,
            voicing_transitions: transitions,
        }
    }

    /// Analyze VUV segments and transitions
    fn analyze_segments(&self, frames: &[VuvFrame]) -> (usize, usize, usize) {
        if frames.is_empty() {
            return (0, 0, 0);
        }

        let mut longest_voiced = 0;
        let mut longest_unvoiced = 0;
        let mut transitions = 0;

        let mut current_voiced_length = 0;
        let mut current_unvoiced_length = 0;
        let mut last_state = frames[0].is_voiced;

        for frame in frames {
            if frame.is_voiced != last_state {
                transitions += 1;
                last_state = frame.is_voiced;
            }

            if frame.is_voiced {
                current_voiced_length += 1;
                longest_unvoiced = longest_unvoiced.max(current_unvoiced_length);
                current_unvoiced_length = 0;
            } else {
                current_unvoiced_length += 1;
                longest_voiced = longest_voiced.max(current_voiced_length);
                current_voiced_length = 0;
            }
        }

        // Final segments
        longest_voiced = longest_voiced.max(current_voiced_length);
        longest_unvoiced = longest_unvoiced.max(current_unvoiced_length);

        (longest_voiced, longest_unvoiced, transitions)
    }

    /// Calculate VUV accuracy metrics
    fn calculate_accuracy(
        &self,
        reference: &VuvAnalysis,
        test: &VuvAnalysis,
    ) -> EvaluationResult<VuvAccuracy> {
        let min_frames = reference.frames.len().min(test.frames.len());

        if min_frames == 0 {
            return Ok(VuvAccuracy {
                overall_accuracy: 0.0,
                voiced_accuracy: 0.0,
                unvoiced_accuracy: 0.0,
                voiced_precision: 0.0,
                voiced_recall: 0.0,
                voiced_f1: 0.0,
                false_positive_rate: 0.0,
                false_negative_rate: 0.0,
                probability_correlation: 0.0,
            });
        }

        let mut correct_total = 0;
        let mut ref_voiced = 0;
        let mut test_voiced = 0;
        let mut correct_voiced = 0;
        let mut correct_unvoiced = 0;
        let mut true_positives = 0;
        let mut false_positives = 0;
        let mut false_negatives = 0;

        let mut ref_probs = Vec::new();
        let mut test_probs = Vec::new();

        for i in 0..min_frames {
            let ref_frame = &reference.frames[i];
            let test_frame = &test.frames[i];

            ref_probs.push(ref_frame.voicing_probability);
            test_probs.push(test_frame.voicing_probability);

            if ref_frame.is_voiced == test_frame.is_voiced {
                correct_total += 1;
                if ref_frame.is_voiced {
                    correct_voiced += 1;
                    true_positives += 1;
                } else {
                    correct_unvoiced += 1;
                }
            }

            if ref_frame.is_voiced {
                ref_voiced += 1;
                if !test_frame.is_voiced {
                    false_negatives += 1;
                }
            }

            if test_frame.is_voiced {
                test_voiced += 1;
                if !ref_frame.is_voiced {
                    false_positives += 1;
                }
            }
        }

        let overall_accuracy = correct_total as f32 / min_frames as f32;

        let voiced_accuracy = if ref_voiced > 0 {
            correct_voiced as f32 / ref_voiced as f32
        } else {
            1.0
        };

        let unvoiced_accuracy = if (min_frames - ref_voiced) > 0 {
            correct_unvoiced as f32 / (min_frames - ref_voiced) as f32
        } else {
            1.0
        };

        let voiced_precision = if test_voiced > 0 {
            true_positives as f32 / test_voiced as f32
        } else {
            0.0
        };

        let voiced_recall = if ref_voiced > 0 {
            true_positives as f32 / ref_voiced as f32
        } else {
            0.0
        };

        let voiced_f1 = if voiced_precision + voiced_recall > 0.0 {
            2.0 * voiced_precision * voiced_recall / (voiced_precision + voiced_recall)
        } else {
            0.0
        };

        let false_positive_rate = if (min_frames - ref_voiced) > 0 {
            false_positives as f32 / (min_frames - ref_voiced) as f32
        } else {
            0.0
        };

        let false_negative_rate = if ref_voiced > 0 {
            false_negatives as f32 / ref_voiced as f32
        } else {
            0.0
        };

        let probability_correlation = crate::calculate_correlation(&ref_probs, &test_probs);

        Ok(VuvAccuracy {
            overall_accuracy,
            voiced_accuracy,
            unvoiced_accuracy,
            voiced_precision,
            voiced_recall,
            voiced_f1,
            false_positive_rate,
            false_negative_rate,
            probability_correlation,
        })
    }

    /// Analyze temporal alignment
    fn analyze_alignment(
        &self,
        reference: &VuvAnalysis,
        test: &VuvAnalysis,
    ) -> EvaluationResult<VuvAlignment> {
        // Simplified alignment analysis
        let min_frames = reference.frames.len().min(test.frames.len());

        if min_frames == 0 {
            return Ok(VuvAlignment {
                alignment_accuracy: 0.0,
                avg_time_shift: 0.0,
                boundary_accuracy: 0.0,
                transition_errors: Vec::new(),
            });
        }

        // Find voicing transitions in both sequences
        let ref_transitions = self.find_transitions(&reference.frames);
        let test_transitions = self.find_transitions(&test.frames);

        // Calculate boundary accuracy and timing errors
        let mut boundary_matches = 0;
        let mut time_errors = Vec::new();

        for ref_transition in &ref_transitions {
            if let Some(closest_test) = test_transitions
                .iter()
                .min_by(|a, b| (a.abs_diff(*ref_transition)).cmp(&(b.abs_diff(*ref_transition))))
            {
                let time_error =
                    (*closest_test as f32 - *ref_transition as f32) * self.config.frame_hop;
                time_errors.push(time_error.abs());

                // Consider a match if within 2 frames
                if closest_test.abs_diff(*ref_transition) <= 2 {
                    boundary_matches += 1;
                }
            }
        }

        let boundary_accuracy = if ref_transitions.is_empty() {
            1.0
        } else {
            boundary_matches as f32 / ref_transitions.len() as f32
        };

        let avg_time_shift = if time_errors.is_empty() {
            0.0
        } else {
            time_errors.iter().sum::<f32>() / time_errors.len() as f32
        };

        Ok(VuvAlignment {
            alignment_accuracy: boundary_accuracy,
            avg_time_shift,
            boundary_accuracy,
            transition_errors: time_errors,
        })
    }

    /// Find voicing transition frames
    fn find_transitions(&self, frames: &[VuvFrame]) -> Vec<usize> {
        if frames.len() < 2 {
            return Vec::new();
        }

        let mut transitions = Vec::new();

        for i in 1..frames.len() {
            if frames[i].is_voiced != frames[i - 1].is_voiced {
                transitions.push(i);
            }
        }

        transitions
    }
}

impl Default for VuvAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn create_test_audio(sample_rate: u32, duration: f32, is_voiced: bool) -> AudioBuffer {
        let num_samples = (sample_rate as f32 * duration) as usize;
        let mut samples = Vec::with_capacity(num_samples);

        for i in 0..num_samples {
            let t = i as f32 / sample_rate as f32;
            let sample = if is_voiced {
                // Voiced: harmonic signal at 200 Hz
                (2.0 * PI * 200.0 * t).sin() * 0.5
            } else {
                // Unvoiced: pseudo-random noise-like signal
                let pseudo_random =
                    ((i * 1_103_515_245 + 12345) % 2_147_483_648) as f32 / 2_147_483_648.0;
                (pseudo_random - 0.5) * 0.2
            };
            samples.push(sample);
        }

        AudioBuffer::new(samples, sample_rate, 1)
    }

    #[tokio::test]
    async fn test_vuv_analyzer_creation() {
        let analyzer = VuvAnalyzer::new();
        assert_eq!(analyzer.config.frame_length, 0.025);
        assert_eq!(analyzer.config.frame_hop, 0.01);
    }

    #[tokio::test]
    async fn test_vuv_analysis_voiced() {
        let analyzer = VuvAnalyzer::new();
        let audio = create_test_audio(16000, 0.5, true); // Voiced audio

        let analysis = analyzer
            .analyze(&audio, VuvAlgorithm::Energy)
            .await
            .unwrap();

        assert!(!analysis.frames.is_empty());
        assert_eq!(analysis.algorithm, VuvAlgorithm::Energy);
        // For a strong harmonic signal, expect at least some voicing
        assert!(
            analysis.statistics.voicing_rate > 0.3,
            "Voicing rate was {}, expected > 0.3",
            analysis.statistics.voicing_rate
        );
    }

    #[tokio::test]
    async fn test_vuv_analysis_unvoiced() {
        let analyzer = VuvAnalyzer::new();
        let audio = create_test_audio(16000, 0.5, false); // Unvoiced audio

        let analysis = analyzer
            .analyze(&audio, VuvAlgorithm::ZeroCrossing)
            .await
            .unwrap();

        assert!(!analysis.frames.is_empty());
        assert_eq!(analysis.algorithm, VuvAlgorithm::ZeroCrossing);
        assert!(analysis.statistics.voicing_rate < 0.5); // Most frames should be unvoiced
    }

    #[tokio::test]
    async fn test_vuv_comparison() {
        let analyzer = VuvAnalyzer::new();
        let voiced_audio = create_test_audio(16000, 0.5, true);
        let unvoiced_audio = create_test_audio(16000, 0.5, false);

        let comparison = analyzer
            .compare(&voiced_audio, &unvoiced_audio, VuvAlgorithm::MultiFature)
            .await
            .unwrap();

        assert!(comparison.accuracy.overall_accuracy >= 0.0);
        assert!(comparison.accuracy.overall_accuracy <= 1.0);
        assert!(comparison.accuracy.voiced_precision >= 0.0);
        assert!(comparison.accuracy.voiced_recall >= 0.0);
    }

    #[tokio::test]
    async fn test_vuv_features_extraction() {
        let analyzer = VuvAnalyzer::new();
        let frame = vec![0.1, 0.2, -0.1, 0.3, -0.2, 0.1]; // Small test frame

        let features = analyzer.extract_features(&frame, 16000.0).await.unwrap();

        assert!(features.energy.is_finite());
        assert!(features.zcr >= 0.0 && features.zcr <= 1.0);
        assert!(features.autocorr_peak >= 0.0 && features.autocorr_peak <= 1.0);
        assert!(features.spectral_centroid >= 0.0);
        assert!(features.spectral_rolloff >= 0.0 && features.spectral_rolloff <= 1.0);
    }

    #[tokio::test]
    async fn test_vuv_algorithms() {
        let analyzer = VuvAnalyzer::new();
        let audio = create_test_audio(16000, 0.2, true);

        // Test all algorithms
        let algorithms = [
            VuvAlgorithm::Energy,
            VuvAlgorithm::ZeroCrossing,
            VuvAlgorithm::Autocorrelation,
            VuvAlgorithm::SpectralFeatures,
            VuvAlgorithm::MultiFature,
            VuvAlgorithm::MachineLearning,
        ];

        for algorithm in algorithms {
            let analysis = analyzer.analyze(&audio, algorithm).await.unwrap();
            assert_eq!(analysis.algorithm, algorithm);
            assert!(!analysis.frames.is_empty());
        }
    }

    #[tokio::test]
    async fn test_vuv_statistics() {
        let analyzer = VuvAnalyzer::new();
        let audio = create_test_audio(16000, 0.3, true);

        let analysis = analyzer
            .analyze(&audio, VuvAlgorithm::Energy)
            .await
            .unwrap();
        let stats = &analysis.statistics;

        assert_eq!(
            stats.total_frames,
            stats.voiced_frames + stats.unvoiced_frames
        );
        assert!(stats.voicing_rate >= 0.0 && stats.voicing_rate <= 1.0);
        assert!(stats.avg_voiced_probability >= 0.0 && stats.avg_voiced_probability <= 1.0);
        assert!(stats.longest_voiced_segment <= stats.total_frames);
    }
}