voirs-cli 0.1.0-beta.1

Command-line interface for VoiRS speech synthesis
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
//! Model benchmarking command implementation.

use crate::GlobalOptions;
use std::collections::HashMap;
use std::time::{Duration, Instant};
use voirs_g2p::{
    accuracy::{AccuracyBenchmark, TestCase},
    LanguageCode,
};
use voirs_sdk::config::AppConfig;
use voirs_sdk::types::SynthesisConfig;
use voirs_sdk::VoirsPipeline;
use voirs_sdk::{QualityLevel, Result};

/// Benchmark results for a model
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    pub model_id: String,
    pub avg_synthesis_time: Duration,
    pub avg_audio_duration: Duration,
    pub real_time_factor: f64,
    pub memory_usage_mb: f64,
    pub quality_score: f64,
    pub success_rate: f64,
    pub phoneme_accuracy: Option<f64>,
    pub word_accuracy: Option<f64>,
    pub accuracy_target_met: Option<bool>,
    pub english_accuracy_target_met: Option<bool>,
    pub japanese_accuracy_target_met: Option<bool>,
    pub latency_target_met: bool,
    pub memory_target_met: bool,
}

/// Run benchmark models command
pub async fn run_benchmark_models(
    model_ids: &[String],
    iterations: u32,
    include_accuracy: bool,
    config: &AppConfig,
    global: &GlobalOptions,
) -> Result<()> {
    if !global.quiet {
        println!("Benchmarking TTS Models");
        println!("=======================");
        println!("Iterations: {}", iterations);
        println!("Models: {}", model_ids.len());
        if include_accuracy {
            println!("Accuracy Testing: Enabled (CMU Test Set)");
        }
        println!();
    }

    let test_sentences = get_test_sentences();
    let mut results = Vec::new();

    // Load accuracy benchmark if requested
    let accuracy_benchmark = if include_accuracy {
        if !global.quiet {
            println!("Loading CMU accuracy test data...");
        }
        Some(load_cmu_accuracy_benchmark()?)
    } else {
        None
    };

    for model_id in model_ids {
        if !global.quiet {
            println!("Benchmarking model: {}", model_id);
        }

        let result = benchmark_model(
            model_id,
            &test_sentences,
            iterations,
            accuracy_benchmark.as_ref(),
            config,
            global,
        )
        .await?;
        results.push(result);

        if !global.quiet {
            println!("  ✓ Completed\n");
        }
    }

    // Display results
    display_benchmark_results(&results, global);

    // Generate comparison report
    if results.len() > 1 {
        generate_comparison_report(&results, global);
    }

    Ok(())
}

/// Benchmark a single model
async fn benchmark_model(
    model_id: &str,
    test_sentences: &[String],
    iterations: u32,
    accuracy_benchmark: Option<&AccuracyBenchmark>,
    config: &AppConfig,
    global: &GlobalOptions,
) -> Result<BenchmarkResult> {
    // Load specific model by ID
    if !global.quiet {
        println!("    Loading model: {}", model_id);
    }

    let pipeline = load_model_pipeline(model_id, config, global).await?;

    let synth_config = SynthesisConfig {
        quality: QualityLevel::High,
        ..Default::default()
    };

    let mut total_synthesis_time = Duration::from_secs(0);
    let mut total_audio_duration = 0.0;
    let mut successful_runs = 0;
    let mut memory_samples = Vec::new();

    // Run benchmark iterations
    for i in 0..iterations {
        if !global.quiet && iterations > 1 {
            print!("  Progress: {}/{}\r", i + 1, iterations);
        }

        for sentence in test_sentences {
            let start_time = Instant::now();

            // Measure memory before synthesis
            let memory_before = get_memory_usage();

            // Attempt synthesis
            match pipeline
                .synthesize_with_config(sentence, &synth_config)
                .await
            {
                Ok(audio) => {
                    let synthesis_time = start_time.elapsed();
                    total_synthesis_time += synthesis_time;
                    total_audio_duration += audio.duration();
                    successful_runs += 1;

                    // Measure memory after synthesis
                    let memory_after = get_memory_usage();
                    memory_samples.push(memory_after - memory_before);
                }
                Err(e) => {
                    tracing::warn!("Synthesis failed for '{}': {}", sentence, e);
                }
            }
        }
    }

    // Calculate metrics
    let total_runs = iterations as usize * test_sentences.len();
    let avg_synthesis_time = total_synthesis_time / total_runs as u32;
    let avg_audio_duration =
        Duration::from_secs_f64(total_audio_duration as f64 / successful_runs as f64);
    let real_time_factor = avg_synthesis_time.as_secs_f64() / avg_audio_duration.as_secs_f64();
    let success_rate = successful_runs as f64 / total_runs as f64;
    let avg_memory_usage = memory_samples.iter().sum::<f64>() / memory_samples.len() as f64;

    // Calculate quality score (placeholder - would need actual quality metrics)
    let quality_score = calculate_quality_score(model_id, &real_time_factor, &success_rate);

    // Check performance targets
    let latency_target_met = check_latency_target(&avg_synthesis_time);
    let memory_target_met = check_memory_target(avg_memory_usage);

    if !global.quiet {
        println!("    Performance Targets:");
        println!(
            "      Latency (<1ms): {} - {:.2}ms",
            if latency_target_met {
                "✅ PASSED"
            } else {
                "❌ FAILED"
            },
            avg_synthesis_time.as_millis()
        );
        println!(
            "      Memory (<100MB): {} - {:.1}MB",
            if memory_target_met {
                "✅ PASSED"
            } else {
                "❌ FAILED"
            },
            avg_memory_usage
        );
    }

    // Run accuracy benchmark if provided
    let (
        phoneme_accuracy,
        word_accuracy,
        accuracy_target_met,
        english_accuracy_target_met,
        japanese_accuracy_target_met,
    ) = if let Some(benchmark) = accuracy_benchmark {
        if !global.quiet {
            println!("    Running accuracy tests...");
        }

        // For TTS, we would need to extract phonemes from the synthesized audio
        // This is a placeholder implementation that would need integration with
        // a speech recognizer or forced alignment system
        match run_accuracy_test(&pipeline, benchmark, global).await {
            Ok((phoneme_acc, word_acc, target_met, en_target_met, ja_target_met)) => {
                if !global.quiet {
                    println!("    Phoneme Accuracy: {:.2}%", phoneme_acc * 100.0);
                    println!("    Word Accuracy: {:.2}%", word_acc * 100.0);
                    println!(
                        "    Overall Target Met: {}",
                        if target_met { "" } else { "" }
                    );
                    println!(
                        "    English Target (>95%): {}",
                        if en_target_met { "" } else { "" }
                    );
                    println!(
                        "    Japanese Target (>90%): {}",
                        if ja_target_met { "" } else { "" }
                    );
                }
                (
                    Some(phoneme_acc),
                    Some(word_acc),
                    Some(target_met),
                    Some(en_target_met),
                    Some(ja_target_met),
                )
            }
            Err(e) => {
                if !global.quiet {
                    println!("    Accuracy test failed: {}", e);
                }
                (None, None, None, None, None)
            }
        }
    } else {
        (None, None, None, None, None)
    };

    Ok(BenchmarkResult {
        model_id: model_id.to_string(),
        avg_synthesis_time,
        avg_audio_duration,
        real_time_factor,
        memory_usage_mb: avg_memory_usage,
        quality_score,
        success_rate,
        phoneme_accuracy,
        word_accuracy,
        accuracy_target_met,
        english_accuracy_target_met,
        japanese_accuracy_target_met,
        latency_target_met,
        memory_target_met,
    })
}

/// Get test sentences for benchmarking
fn get_test_sentences() -> Vec<String> {
    vec![
        "The quick brown fox jumps over the lazy dog.".to_string(),
        "Hello, this is a test of the text-to-speech system.".to_string(),
        "Artificial intelligence is transforming the way we communicate.".to_string(),
        "The weather today is absolutely beautiful with clear skies.".to_string(),
        "Machine learning models require careful tuning and validation.".to_string(),
    ]
}

/// Get current memory usage in MB
fn get_memory_usage() -> f64 {
    // Try multiple methods to get memory usage

    // Method 1: Try reading /proc/self/status on Linux
    if let Ok(status) = std::fs::read_to_string("/proc/self/status") {
        for line in status.lines() {
            if line.starts_with("VmRSS:") {
                if let Some(kb_str) = line.split_whitespace().nth(1) {
                    if let Ok(kb) = kb_str.parse::<f64>() {
                        return kb / 1024.0; // Convert KB to MB
                    }
                }
            }
        }
    }

    // Method 2: Use rusage on Unix systems
    #[cfg(unix)]
    {
        unsafe {
            let mut usage = std::mem::MaybeUninit::<libc::rusage>::uninit();
            if libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) == 0 {
                let usage = usage.assume_init();
                // On Linux, ru_maxrss is in KB; on macOS, it's in bytes
                #[cfg(target_os = "linux")]
                return usage.ru_maxrss as f64 / 1024.0; // KB to MB

                #[cfg(target_os = "macos")]
                return usage.ru_maxrss as f64 / (1024.0 * 1024.0); // bytes to MB
            }
        }
    }

    // Method 3: Try reading /proc/meminfo for available memory on Linux
    if let Ok(meminfo) = std::fs::read_to_string("/proc/meminfo") {
        let mut total_kb = None;
        let mut available_kb = None;

        for line in meminfo.lines() {
            if line.starts_with("MemTotal:") {
                if let Some(kb_str) = line.split_whitespace().nth(1) {
                    if let Ok(kb) = kb_str.parse::<f64>() {
                        total_kb = Some(kb);
                    }
                }
            } else if line.starts_with("MemAvailable:") {
                if let Some(kb_str) = line.split_whitespace().nth(1) {
                    if let Ok(kb) = kb_str.parse::<f64>() {
                        available_kb = Some(kb);
                    }
                }
            }
        }

        if let (Some(total), Some(available)) = (total_kb, available_kb) {
            let used_mb = (total - available) / 1024.0; // Convert KB to MB
            return used_mb;
        }
    }

    // Fallback: Return a placeholder value if all methods fail
    // This ensures the benchmark can still run even if memory monitoring fails
    50.0 // Default 50MB estimate
}

/// Calculate quality score based on various metrics
fn calculate_quality_score(model_id: &str, real_time_factor: &f64, success_rate: &f64) -> f64 {
    // Performance score: Logarithmic scale for better granularity
    // RTF < 0.05 is exceptional, 0.05-0.1 is excellent, 0.1-0.5 is good, 0.5-1.0 is acceptable
    let performance_score = if *real_time_factor < 0.05 {
        5.0
    } else if *real_time_factor < 0.1 {
        4.5 + 0.5 * (0.1 - real_time_factor) / 0.05 // 4.5-5.0
    } else if *real_time_factor < 0.25 {
        3.5 + 1.0 * (0.25 - real_time_factor) / 0.15 // 3.5-4.5
    } else if *real_time_factor < 0.5 {
        2.5 + 1.0 * (0.5 - real_time_factor) / 0.25 // 2.5-3.5
    } else if *real_time_factor < 1.0 {
        1.5 + 1.0 * (1.0 - real_time_factor) / 0.5 // 1.5-2.5
    } else if *real_time_factor < 2.0 {
        0.5 + 1.0 * (2.0 - real_time_factor) / 1.0 // 0.5-1.5
    } else {
        (5.0 / real_time_factor).min(0.5) // Decreasing score for very slow models
    };

    // Reliability score: Non-linear scaling emphasizing high success rates
    let reliability_score = if *success_rate >= 0.99 {
        5.0
    } else if *success_rate >= 0.95 {
        4.0 + 1.0 * (success_rate - 0.95) / 0.04 // 4.0-5.0
    } else if *success_rate >= 0.90 {
        3.0 + 1.0 * (success_rate - 0.90) / 0.05 // 3.0-4.0
    } else if *success_rate >= 0.75 {
        1.5 + 1.5 * (success_rate - 0.75) / 0.15 // 1.5-3.0
    } else {
        success_rate * 2.0 // 0.0-1.5
    };

    // Model-specific adjustments based on architecture characteristics
    // Vocoder models: HiFi-GAN (fast, high quality), WaveGlow (slower, very high quality)
    // Acoustic models: Tacotron2 (stable, good quality), FastSpeech2 (fast, good quality)
    let (model_quality_baseline, model_speed_expectation) = if model_id.contains("hifigan") {
        (0.6, 0.15) // High quality vocoder, expect RTF ~0.15
    } else if model_id.contains("waveglow") || model_id.contains("wavernn") {
        (0.8, 0.5) // Very high quality but slower
    } else if model_id.contains("melgan") || model_id.contains("parallel-wavegan") {
        (0.5, 0.1) // Fast but lower quality
    } else if model_id.contains("tacotron") {
        (0.5, 0.3) // Stable acoustic model
    } else if model_id.contains("fastspeech") {
        (0.6, 0.2) // Fast acoustic model
    } else if model_id.contains("vits") {
        (0.7, 0.25) // End-to-end high quality
    } else if model_id.contains("diffwave") || model_id.contains("diffusion") {
        (0.9, 1.0) // Highest quality but slowest
    } else {
        (0.3, 0.5) // Unknown model, neutral expectations
    };

    // Bonus for meeting speed expectations
    let speed_bonus = if *real_time_factor <= model_speed_expectation {
        model_quality_baseline
    } else if *real_time_factor <= model_speed_expectation * 2.0 {
        // Linear decay for slightly slower than expected
        model_quality_baseline
            * (1.0 - (real_time_factor - model_speed_expectation) / model_speed_expectation)
    } else {
        0.0 // No bonus if significantly slower than expected
    };

    // Weighted average: 40% performance, 40% reliability, 20% model-specific
    let total_score = performance_score * 0.4 + reliability_score * 0.4 + speed_bonus * 0.2;

    total_score.clamp(0.0, 5.0)
}

/// Display benchmark results
fn display_benchmark_results(results: &[BenchmarkResult], global: &GlobalOptions) {
    if global.quiet {
        return;
    }

    println!("Benchmark Results:");
    println!("==================");

    for result in results {
        println!("\nModel: {}", result.model_id);
        println!(
            "  Avg Synthesis Time: {:.2}ms",
            result.avg_synthesis_time.as_millis()
        );
        println!(
            "  Avg Audio Duration: {:.2}ms",
            result.avg_audio_duration.as_millis()
        );
        println!("  Real-time Factor: {:.2}x", result.real_time_factor);
        println!("  Memory Usage: {:.1} MB", result.memory_usage_mb);
        println!("  Quality Score: {:.1}/5.0", result.quality_score);
        println!("  Success Rate: {:.1}%", result.success_rate * 100.0);

        // Display accuracy metrics if available
        if let Some(phoneme_acc) = result.phoneme_accuracy {
            println!("  Phoneme Accuracy: {:.2}%", phoneme_acc * 100.0);
        }
        if let Some(word_acc) = result.word_accuracy {
            println!("  Word Accuracy: {:.2}%", word_acc * 100.0);
        }
        if let Some(target_met) = result.accuracy_target_met {
            println!("  Accuracy Targets:");
            if let Some(en_target) = result.english_accuracy_target_met {
                println!(
                    "    English (>95%): {}",
                    if en_target {
                        "✅ PASSED"
                    } else {
                        "❌ FAILED"
                    }
                );
            }
            if let Some(ja_target) = result.japanese_accuracy_target_met {
                println!(
                    "    Japanese (>90%): {}",
                    if ja_target {
                        "✅ PASSED"
                    } else {
                        "❌ FAILED"
                    }
                );
            }
            println!(
                "    Overall: {}",
                if target_met {
                    "✅ PASSED"
                } else {
                    "❌ FAILED"
                }
            );
        }

        // Display performance targets
        println!(
            "  Latency Target (<1ms): {}",
            if result.latency_target_met {
                "✅ PASSED"
            } else {
                "❌ FAILED"
            }
        );
        println!(
            "  Memory Target (<100MB): {}",
            if result.memory_target_met {
                "✅ PASSED"
            } else {
                "❌ FAILED"
            }
        );
    }
}

/// Generate comparison report
fn generate_comparison_report(results: &[BenchmarkResult], global: &GlobalOptions) {
    if global.quiet {
        return;
    }

    println!("\n\nComparison Report:");
    println!("==================");

    // Find best performers
    let fastest = results.iter().min_by(|a, b| {
        a.real_time_factor
            .partial_cmp(&b.real_time_factor)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let most_reliable = results.iter().max_by(|a, b| {
        a.success_rate
            .partial_cmp(&b.success_rate)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let highest_quality = results.iter().max_by(|a, b| {
        a.quality_score
            .partial_cmp(&b.quality_score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let most_efficient = results.iter().min_by(|a, b| {
        a.memory_usage_mb
            .partial_cmp(&b.memory_usage_mb)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    if let Some(model) = fastest {
        println!(
            "🏃 Fastest Model: {} ({:.2}x real-time)",
            model.model_id, model.real_time_factor
        );
    }

    if let Some(model) = most_reliable {
        println!(
            "🎯 Most Reliable: {} ({:.1}% success rate)",
            model.model_id,
            model.success_rate * 100.0
        );
    }

    if let Some(model) = highest_quality {
        println!(
            "⭐ Highest Quality: {} ({:.1}/5.0)",
            model.model_id, model.quality_score
        );
    }

    if let Some(model) = most_efficient {
        println!(
            "💾 Most Memory Efficient: {} ({:.1} MB)",
            model.model_id, model.memory_usage_mb
        );
    }

    // Performance target analysis
    println!("\n📊 Performance Target Analysis:");
    let models_meeting_latency = results.iter().filter(|r| r.latency_target_met).count();
    let models_meeting_memory = results.iter().filter(|r| r.memory_target_met).count();
    let models_meeting_all_targets = results
        .iter()
        .filter(|r| {
            r.latency_target_met && r.memory_target_met && r.accuracy_target_met.unwrap_or(false)
        })
        .count();

    println!(
        "  🚀 Models meeting latency target (<1ms): {}/{}",
        models_meeting_latency,
        results.len()
    );
    println!(
        "  🧠 Models meeting memory target (<100MB): {}/{}",
        models_meeting_memory,
        results.len()
    );

    if results.iter().any(|r| r.accuracy_target_met.is_some()) {
        println!(
            "  🎯 Models meeting all targets: {}/{}",
            models_meeting_all_targets,
            results.len()
        );

        if models_meeting_all_targets > 0 {
            println!("  ✅ Production-ready models found!");
        } else {
            println!("  ⚠️  No models currently meet all production targets");
        }
    }
}

/// Load a pipeline configured for a specific model
async fn load_model_pipeline(
    model_id: &str,
    config: &AppConfig,
    global: &GlobalOptions,
) -> Result<VoirsPipeline> {
    // Check if model exists in cache
    let cache_dir = config.pipeline.effective_cache_dir();
    let model_path = cache_dir.join("models").join(model_id);

    if !model_path.exists() {
        return Err(voirs_sdk::VoirsError::config_error(format!(
            "Model '{}' not found in cache. Please download it first using 'voirs download-model {}'",
            model_id, model_id
        )));
    }

    // Load model configuration
    let model_config_path = model_path.join("config.json");
    let model_config = if model_config_path.exists() {
        let config_content = std::fs::read_to_string(&model_config_path).map_err(|e| {
            voirs_sdk::VoirsError::IoError {
                path: model_config_path.clone(),
                operation: voirs_sdk::error::IoOperation::Read,
                source: e,
            }
        })?;

        serde_json::from_str::<ModelMetadata>(&config_content).map_err(|e| {
            voirs_sdk::VoirsError::config_error(format!(
                "Invalid model config for '{}': {}",
                model_id, e
            ))
        })?
    } else {
        // Create default model metadata if config doesn't exist
        ModelMetadata {
            id: model_id.to_string(),
            name: model_id.to_string(),
            description: format!("Model {}", model_id),
            model_type: "neural".to_string(),
            quality: QualityLevel::High,
            requires_gpu: false,
            memory_requirements_mb: 512,
            acoustic_model: "model.safetensors".to_string(),
            vocoder_model: "vocoder.safetensors".to_string(),
            g2p_model: None,
        }
    };

    if !global.quiet {
        println!(
            "      Model: {} ({})",
            model_config.name, model_config.description
        );
        println!(
            "      Type: {}, Quality: {:?}",
            model_config.model_type, model_config.quality
        );
        println!(
            "      Memory required: {} MB",
            model_config.memory_requirements_mb
        );
    }

    // Check memory requirements
    let available_memory = get_memory_usage(); // This gets current usage, we need available
    if model_config.memory_requirements_mb as f64 > available_memory {
        tracing::warn!(
            "Model '{}' requires {} MB but only {:.1} MB may be available",
            model_id,
            model_config.memory_requirements_mb,
            available_memory
        );
    }

    // Verify model files exist
    let acoustic_path = model_path.join(&model_config.acoustic_model);
    let vocoder_path = model_path.join(&model_config.vocoder_model);

    if !acoustic_path.exists() {
        return Err(voirs_sdk::VoirsError::config_error(format!(
            "Acoustic model file not found: {}",
            acoustic_path.display()
        )));
    }

    if !vocoder_path.exists() {
        return Err(voirs_sdk::VoirsError::config_error(format!(
            "Vocoder model file not found: {}",
            vocoder_path.display()
        )));
    }

    // Build pipeline with model-specific configuration
    let mut builder = VoirsPipeline::builder().with_quality(model_config.quality);

    // Configure GPU usage based on model requirements and system capabilities
    if model_config.requires_gpu && (config.pipeline.use_gpu || global.gpu) {
        builder = builder.with_gpu_acceleration(true);
        if !global.quiet {
            println!("      GPU acceleration: enabled (required by model)");
        }
    } else if config.pipeline.use_gpu || global.gpu {
        builder = builder.with_gpu_acceleration(true);
        if !global.quiet {
            println!("      GPU acceleration: enabled");
        }
    } else if !global.quiet {
        println!("      GPU acceleration: disabled");
    }

    // Set thread count based on configuration
    if let Some(threads) = config.pipeline.num_threads {
        builder = builder.with_threads(threads);
        if !global.quiet {
            println!("      Threads: {}", threads);
        }
    } else {
        let default_threads = config.pipeline.effective_thread_count();
        builder = builder.with_threads(default_threads);
        if !global.quiet {
            println!("      Threads: {} (auto)", default_threads);
        }
    }

    // Build the pipeline
    let pipeline = builder.build().await.map_err(|e| {
        voirs_sdk::VoirsError::config_error(format!("Failed to load model '{}': {}", model_id, e))
    })?;

    if !global.quiet {
        println!("      ✓ Model loaded successfully");
    }

    Ok(pipeline)
}

/// Load CMU accuracy benchmark test data
fn load_cmu_accuracy_benchmark() -> Result<AccuracyBenchmark> {
    let mut benchmark = AccuracyBenchmark::new();

    // Add comprehensive CMU test set data for English
    let cmu_test_cases = vec![
        // Basic phoneme coverage
        ("hello", vec!["h", "ə", "ˈl", ""], LanguageCode::EnUs),
        ("world", vec!["w", "ɜːr", "l", "d"], LanguageCode::EnUs),
        ("cat", vec!["k", "æ", "t"], LanguageCode::EnUs),
        ("dog", vec!["d", "ɔː", "ɡ"], LanguageCode::EnUs),
        ("house", vec!["h", "", "s"], LanguageCode::EnUs),
        ("tree", vec!["t", "r", ""], LanguageCode::EnUs),
        ("water", vec!["ˈw", "ɔː", "t", "ər"], LanguageCode::EnUs),
        ("phone", vec!["f", "", "n"], LanguageCode::EnUs),
        // Vowel patterns
        ("beat", vec!["b", "", "t"], LanguageCode::EnUs),
        ("bit", vec!["b", "ɪ", "t"], LanguageCode::EnUs),
        ("bet", vec!["b", "ɛ", "t"], LanguageCode::EnUs),
        ("bat", vec!["b", "æ", "t"], LanguageCode::EnUs),
        ("bot", vec!["b", "ɑː", "t"], LanguageCode::EnUs),
        ("boat", vec!["b", "", "t"], LanguageCode::EnUs),
        ("boot", vec!["b", "", "t"], LanguageCode::EnUs),
        ("but", vec!["b", "ʌ", "t"], LanguageCode::EnUs),
        // Consonant clusters
        ("street", vec!["s", "t", "r", "", "t"], LanguageCode::EnUs),
        ("spring", vec!["s", "p", "r", "ɪ", "ŋ"], LanguageCode::EnUs),
        ("school", vec!["s", "k", "", "l"], LanguageCode::EnUs),
        ("throw", vec!["θ", "r", ""], LanguageCode::EnUs),
        ("three", vec!["θ", "r", ""], LanguageCode::EnUs),
        // Irregular words
        ("one", vec!["w", "ʌ", "n"], LanguageCode::EnUs),
        ("two", vec!["t", ""], LanguageCode::EnUs),
        ("eight", vec!["", "t"], LanguageCode::EnUs),
        ("through", vec!["θ", "r", ""], LanguageCode::EnUs),
        ("though", vec!["ð", ""], LanguageCode::EnUs),
        ("rough", vec!["r", "ʌ", "f"], LanguageCode::EnUs),
        // Complex multisyllabic words
        (
            "computer",
            vec!["k", "ə", "m", "ˈp", "j", "", "t", "ər"],
            LanguageCode::EnUs,
        ),
        (
            "beautiful",
            vec!["ˈb", "j", "", "t", "ɪ", "f", "ə", "l"],
            LanguageCode::EnUs,
        ),
        (
            "restaurant",
            vec!["ˈr", "ɛ", "s", "t", "ər", "ɑː", "n", "t"],
            LanguageCode::EnUs,
        ),
        (
            "university",
            vec!["j", "", "n", "ɪ", "ˈv", "ɜːr", "s", "ə", "t", "i"],
            LanguageCode::EnUs,
        ),
        (
            "pronunciation",
            vec!["p", "r", "ə", "n", "ʌ", "n", "s", "i", "ˈeɪ", "ʃ", "ə", "n"],
            LanguageCode::EnUs,
        ),
        // Names and proper nouns
        (
            "california",
            vec!["k", "æ", "l", "ɪ", "ˈf", "ɔːr", "n", "j", "ə"],
            LanguageCode::EnUs,
        ),
        (
            "washington",
            vec!["ˈw", "ɑː", "ʃ", "ɪ", "ŋ", "t", "ə", "n"],
            LanguageCode::EnUs,
        ),
        (
            "america",
            vec!["ə", "ˈm", "ɛr", "ɪ", "k", "ə"],
            LanguageCode::EnUs,
        ),
        // Technical/scientific terms
        (
            "technology",
            vec!["t", "ɛ", "k", "ˈn", "ɑː", "l", "ə", "", "i"],
            LanguageCode::EnUs,
        ),
        (
            "artificial",
            vec!["ɑːr", "t", "ɪ", "ˈf", "ɪ", "ʃ", "ə", "l"],
            LanguageCode::EnUs,
        ),
        (
            "intelligence",
            vec!["ɪ", "n", "ˈt", "ɛ", "l", "ɪ", "", "ə", "n", "s"],
            LanguageCode::EnUs,
        ),
        (
            "synthesis",
            vec!["ˈs", "ɪ", "n", "θ", "ə", "s", "ɪ", "s"],
            LanguageCode::EnUs,
        ),
        // Japanese test cases (using romaji for simplicity)
        (
            "こんにちは",
            vec!["k", "o", "n", "n", "i", "ch", "i", "w", "a"],
            LanguageCode::Ja,
        ),
        (
            "ありがとう",
            vec!["a", "r", "i", "g", "a", "t", "o", "u"],
            LanguageCode::Ja,
        ),
        (
            "おはよう",
            vec!["o", "h", "a", "y", "o", "u"],
            LanguageCode::Ja,
        ),
        (
            "さよなら",
            vec!["s", "a", "y", "o", "n", "a", "r", "a"],
            LanguageCode::Ja,
        ),
        (
            "コンピュータ",
            vec!["k", "o", "n", "p", "y", "u", "u", "t", "a"],
            LanguageCode::Ja,
        ),
        (
            "テクノロジー",
            vec!["t", "e", "k", "u", "n", "o", "r", "o", "j", "i", "i"],
            LanguageCode::Ja,
        ),
        (
            "アニメーション",
            vec!["a", "n", "i", "m", "e", "e", "sh", "o", "n"],
            LanguageCode::Ja,
        ),
        (
            "大学",
            vec!["d", "a", "i", "g", "a", "k", "u"],
            LanguageCode::Ja,
        ),
        (
            "東京",
            vec!["t", "o", "u", "k", "y", "o", "u"],
            LanguageCode::Ja,
        ),
        (
            "日本語",
            vec!["n", "i", "h", "o", "n", "g", "o"],
            LanguageCode::Ja,
        ),
    ];

    for (word, phonemes, lang) in cmu_test_cases {
        benchmark.add_test_case(TestCase {
            word: word.to_string(),
            expected_phonemes: phonemes.into_iter().map(|p| p.to_string()).collect(),
            language: lang,
        });
    }

    Ok(benchmark)
}

/// Run accuracy test using the TTS pipeline and G2P system
async fn run_accuracy_test(
    _pipeline: &VoirsPipeline,
    benchmark: &AccuracyBenchmark,
    _global: &GlobalOptions,
) -> Result<(f64, f64, bool, bool, bool)> {
    // This is a simplified implementation. In a real TTS accuracy test, we would:
    // 1. Synthesize audio for each test word
    // 2. Use a speech recognizer to extract phonemes from the audio
    // 3. Compare extracted phonemes with expected phonemes
    //
    // For now, we'll simulate this by using the G2P system directly
    // which tests the phoneme prediction accuracy component of TTS

    // Create a dummy G2P system for testing
    // In a real implementation, this would be the G2P component of the TTS pipeline
    let g2p = create_test_g2p_system();

    let metrics = benchmark
        .evaluate(&g2p)
        .await
        .map_err(|e| voirs_sdk::VoirsError::config_error(format!("Accuracy test failed: {}", e)))?;

    // Check if accuracy targets are met:
    // English: >95%, Japanese: >90%
    let english_target_met =
        if let Some(en_metrics) = metrics.language_metrics.get(&LanguageCode::EnUs) {
            en_metrics.accuracy >= 0.95
        } else {
            false
        };

    let japanese_target_met =
        if let Some(ja_metrics) = metrics.language_metrics.get(&LanguageCode::Ja) {
            ja_metrics.accuracy >= 0.90
        } else {
            false
        };

    // Overall target is met if at least one language meets its target
    // (or we could require all languages to meet targets - depending on requirements)
    let target_met = english_target_met || japanese_target_met;

    Ok((
        metrics.phoneme_accuracy,
        metrics.word_accuracy,
        target_met,
        english_target_met,
        japanese_target_met,
    ))
}

/// Create a test G2P system for accuracy evaluation
fn create_test_g2p_system() -> impl voirs_g2p::G2p {
    // This is a placeholder. In a real implementation, this would be
    // the actual G2P system used by the TTS pipeline
    voirs_g2p::DummyG2p::new()
}

/// Check if latency target is met (<1ms for typical sentences)
fn check_latency_target(avg_synthesis_time: &Duration) -> bool {
    avg_synthesis_time.as_millis() < 1
}

/// Check if memory target is met (<100MB)
fn check_memory_target(memory_usage_mb: f64) -> bool {
    memory_usage_mb < 100.0
}

/// Model metadata structure
#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct ModelMetadata {
    pub id: String,
    pub name: String,
    pub description: String,
    pub model_type: String,
    pub quality: QualityLevel,
    pub requires_gpu: bool,
    pub memory_requirements_mb: u32,
    pub acoustic_model: String,
    pub vocoder_model: String,
    pub g2p_model: Option<String>,
}

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

    #[test]
    fn test_get_test_sentences() {
        let sentences = get_test_sentences();
        assert!(!sentences.is_empty());
        assert!(sentences.len() >= 3);
    }

    #[test]
    fn test_calculate_quality_score() {
        let score = calculate_quality_score("hifigan-v1", &0.5, &1.0);
        assert!(score >= 0.0 && score <= 5.0);
    }

    #[test]
    fn test_get_memory_usage() {
        let usage = get_memory_usage();
        assert!(usage >= 0.0);
    }

    #[test]
    fn test_check_latency_target() {
        // Test passing latency (under 1ms)
        let fast_duration = Duration::from_micros(500);
        assert!(check_latency_target(&fast_duration));

        // Test failing latency (over 1ms)
        let slow_duration = Duration::from_millis(2);
        assert!(!check_latency_target(&slow_duration));

        // Test edge case (exactly 1ms)
        let edge_duration = Duration::from_millis(1);
        assert!(!check_latency_target(&edge_duration));
    }

    #[test]
    fn test_check_memory_target() {
        // Test passing memory (under 100MB)
        assert!(check_memory_target(50.0));

        // Test failing memory (over 100MB)
        assert!(!check_memory_target(150.0));

        // Test edge case (exactly 100MB)
        assert!(!check_memory_target(100.0));
    }
}