voirs-sdk 0.1.0-rc.1

Unified SDK and public API 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
//! Voice information and metadata utilities.

use crate::types::{
    Gender, LanguageCode, ModelConfig, QualityLevel, SpeakingStyle, VoiceCharacteristics,
    VoiceConfig,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

/// Detailed voice information with computed metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceInfo {
    /// Basic voice configuration
    pub config: VoiceConfig,

    /// Computed voice metrics
    pub metrics: VoiceMetrics,

    /// Voice compatibility information
    pub compatibility: VoiceCompatibility,

    /// Model file information
    pub model_info: ModelInfo,

    /// Usage statistics
    pub usage_stats: Option<VoiceUsageStats>,
}

impl VoiceInfo {
    /// Create voice info from configuration
    pub fn from_config(config: VoiceConfig) -> Self {
        let metrics = VoiceMetrics::from_voice(&config);
        let compatibility = VoiceCompatibility::from_voice(&config);
        let model_info = ModelInfo::from_config(&config.model_config);

        Self {
            config,
            metrics,
            compatibility,
            model_info,
            usage_stats: None,
        }
    }

    /// Get voice ID
    pub fn id(&self) -> &str {
        &self.config.id
    }

    /// Get voice name
    pub fn name(&self) -> &str {
        &self.config.name
    }

    /// Get voice language
    pub fn language(&self) -> LanguageCode {
        self.config.language
    }

    /// Get voice characteristics
    pub fn characteristics(&self) -> &VoiceCharacteristics {
        &self.config.characteristics
    }

    /// Check if voice supports specific feature
    pub fn supports_feature(&self, feature: VoiceFeature) -> bool {
        match feature {
            VoiceFeature::EmotionSupport => self.config.characteristics.emotion_support,
            VoiceFeature::GpuAcceleration => {
                self.config.model_config.device_requirements.gpu_support
            }
            VoiceFeature::LowMemory => {
                self.config.model_config.device_requirements.min_memory_mb <= 512
            }
            VoiceFeature::HighQuality => matches!(
                self.config.characteristics.quality,
                QualityLevel::High | QualityLevel::Ultra
            ),
        }
    }

    /// Get voice summary
    pub fn summary(&self) -> VoiceSummary {
        VoiceSummary {
            id: self.config.id.clone(),
            name: self.config.name.clone(),
            language: self.config.language,
            gender: self.config.characteristics.gender,
            style: self.config.characteristics.style,
            quality: self.config.characteristics.quality,
            emotion_support: self.config.characteristics.emotion_support,
            memory_requirement: self.config.model_config.device_requirements.min_memory_mb,
            gpu_support: self.config.model_config.device_requirements.gpu_support,
        }
    }

    /// Export voice info as JSON
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Import voice info from JSON
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }
}

/// Voice metrics and computed properties
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceMetrics {
    /// Estimated quality score (0.0 - 1.0)
    pub quality_score: f32,

    /// Estimated naturalness score (0.0 - 1.0)
    pub naturalness_score: f32,

    /// Performance score (0.0 - 1.0, higher is faster)
    pub performance_score: f32,

    /// Memory efficiency score (0.0 - 1.0)
    pub memory_efficiency: f32,

    /// Overall rating (0.0 - 1.0)
    pub overall_rating: f32,

    /// Complexity level
    pub complexity: VoiceComplexity,
}

impl VoiceMetrics {
    /// Compute metrics from voice configuration
    pub fn from_voice(voice: &VoiceConfig) -> Self {
        let quality_score = Self::compute_quality_score(&voice.characteristics);
        let naturalness_score = Self::compute_naturalness_score(&voice.characteristics);
        let performance_score = Self::compute_performance_score(&voice.model_config);
        let memory_efficiency = Self::compute_memory_efficiency(&voice.model_config);
        let complexity = Self::determine_complexity(voice);

        let overall_rating =
            (quality_score + naturalness_score + performance_score + memory_efficiency) / 4.0;

        Self {
            quality_score,
            naturalness_score,
            performance_score,
            memory_efficiency,
            overall_rating,
            complexity,
        }
    }

    fn compute_quality_score(characteristics: &VoiceCharacteristics) -> f32 {
        match characteristics.quality {
            QualityLevel::Low => 0.25,
            QualityLevel::Medium => 0.5,
            QualityLevel::High => 0.75,
            QualityLevel::Ultra => 1.0,
        }
    }

    fn compute_naturalness_score(characteristics: &VoiceCharacteristics) -> f32 {
        let mut score: f32 = 0.6; // Base score

        // Emotion support increases naturalness
        if characteristics.emotion_support {
            score += 0.2;
        }

        // Certain styles are more natural
        match characteristics.style {
            SpeakingStyle::Neutral | SpeakingStyle::Calm => score += 0.15,
            SpeakingStyle::Conversational => score += 0.2,
            SpeakingStyle::News | SpeakingStyle::Formal => score += 0.05,
            _ => {}
        }

        score.min(1.0)
    }

    fn compute_performance_score(model_config: &ModelConfig) -> f32 {
        let base_score: f32 = if model_config.device_requirements.gpu_support {
            0.8 // GPU acceleration gives better performance
        } else {
            0.5
        };

        // Lower memory requirement = better performance score
        let memory_factor: f32 = if model_config.device_requirements.min_memory_mb <= 512 {
            1.0
        } else if model_config.device_requirements.min_memory_mb <= 1024 {
            0.8
        } else {
            0.6
        };

        (base_score * memory_factor).min(1.0)
    }

    fn compute_memory_efficiency(model_config: &ModelConfig) -> f32 {
        // Inverse relationship with memory requirement
        let memory_mb = model_config.device_requirements.min_memory_mb as f32;
        (2048.0 - memory_mb.min(2048.0)) / 2048.0
    }

    fn determine_complexity(voice: &VoiceConfig) -> VoiceComplexity {
        let memory_mb = voice.model_config.device_requirements.min_memory_mb;
        let has_emotion = voice.characteristics.emotion_support;
        let high_quality = matches!(
            voice.characteristics.quality,
            QualityLevel::High | QualityLevel::Ultra
        );

        match (memory_mb, has_emotion, high_quality) {
            (mb, _, _) if mb > 1536 => VoiceComplexity::High,
            (mb, true, true) if mb > 768 => VoiceComplexity::High,
            (mb, _, true) if mb > 512 => VoiceComplexity::Medium,
            (mb, true, _) if mb > 512 => VoiceComplexity::Medium,
            _ => VoiceComplexity::Low,
        }
    }
}

/// Voice complexity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum VoiceComplexity {
    /// Simple voice, low resource requirements
    Low,
    /// Moderate complexity and resource usage
    Medium,
    /// Complex voice with high resource requirements
    High,
}

impl fmt::Display for VoiceComplexity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Low => write!(f, "Low"),
            Self::Medium => write!(f, "Medium"),
            Self::High => write!(f, "High"),
        }
    }
}

/// Voice compatibility information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceCompatibility {
    /// Supported platforms
    pub platforms: Vec<String>,

    /// Required compute capabilities
    pub compute_capabilities: Vec<String>,

    /// Compatible languages
    pub compatible_languages: Vec<LanguageCode>,

    /// Minimum system requirements
    pub min_requirements: SystemRequirements,

    /// Recommended system requirements
    pub recommended_requirements: SystemRequirements,
}

impl VoiceCompatibility {
    /// Determine compatibility from voice configuration
    pub fn from_voice(voice: &VoiceConfig) -> Self {
        let platforms = vec![
            "linux".to_string(),
            "macos".to_string(),
            "windows".to_string(),
        ];

        let compute_capabilities = voice
            .model_config
            .device_requirements
            .compute_capabilities
            .clone();

        // Compatible languages based on voice characteristics
        let compatible_languages = vec![voice.language];

        let min_requirements = SystemRequirements {
            memory_mb: voice.model_config.device_requirements.min_memory_mb,
            storage_mb: 1024, // Estimated
            cpu_cores: 1,
            gpu_memory_mb: if voice.model_config.device_requirements.gpu_support {
                Some(512)
            } else {
                None
            },
        };

        let recommended_requirements = SystemRequirements {
            memory_mb: voice.model_config.device_requirements.min_memory_mb * 2,
            storage_mb: 2048,
            cpu_cores: 4,
            gpu_memory_mb: if voice.model_config.device_requirements.gpu_support {
                Some(2048)
            } else {
                None
            },
        };

        Self {
            platforms,
            compute_capabilities,
            compatible_languages,
            min_requirements,
            recommended_requirements,
        }
    }
}

/// System requirements specification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemRequirements {
    /// Required memory in MB
    pub memory_mb: u32,

    /// Required storage in MB
    pub storage_mb: u32,

    /// Minimum CPU cores
    pub cpu_cores: u32,

    /// Required GPU memory in MB (if applicable)
    pub gpu_memory_mb: Option<u32>,
}

/// Model file information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
    /// G2P model file info
    pub g2p_model: Option<ModelFileInfo>,

    /// Acoustic model file info
    pub acoustic_model: ModelFileInfo,

    /// Vocoder model file info
    pub vocoder_model: ModelFileInfo,

    /// Total estimated size in MB
    pub total_size_mb: u32,

    /// Model format
    pub format: String,
}

impl ModelInfo {
    /// Create model info from configuration
    pub fn from_config(config: &ModelConfig) -> Self {
        let g2p_model = config
            .g2p_model
            .as_ref()
            .map(|path| ModelFileInfo::from_path(path));

        let acoustic_model = ModelFileInfo::from_path(&config.acoustic_model);
        let vocoder_model = ModelFileInfo::from_path(&config.vocoder_model);

        let total_size_mb = g2p_model.as_ref().map(|m| m.estimated_size_mb).unwrap_or(0)
            + acoustic_model.estimated_size_mb
            + vocoder_model.estimated_size_mb;

        Self {
            g2p_model,
            acoustic_model,
            vocoder_model,
            total_size_mb,
            format: format!("{:?}", config.format),
        }
    }
}

/// Individual model file information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelFileInfo {
    /// File path
    pub path: String,

    /// Estimated file size in MB
    pub estimated_size_mb: u32,

    /// Model type
    pub model_type: String,
}

impl ModelFileInfo {
    /// Create model file info from path
    pub fn from_path(path: &str) -> Self {
        let model_type = if path.contains("g2p") {
            "G2P"
        } else if path.contains("acoustic") {
            "Acoustic"
        } else if path.contains("vocoder") {
            "Vocoder"
        } else {
            "Unknown"
        }
        .to_string();

        // Estimate size based on model type
        let estimated_size_mb = match model_type.as_str() {
            "G2P" => 50,
            "Acoustic" => 200,
            "Vocoder" => 100,
            _ => 150,
        };

        Self {
            path: path.to_string(),
            estimated_size_mb,
            model_type,
        }
    }
}

/// Voice usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceUsageStats {
    /// Number of times voice has been used
    pub usage_count: u64,

    /// Total synthesis time in seconds
    pub total_synthesis_time: f64,

    /// Average synthesis time per request
    pub avg_synthesis_time: f64,

    /// Last used timestamp
    pub last_used: Option<std::time::SystemTime>,

    /// Most common text lengths
    pub common_text_lengths: Vec<usize>,

    /// Performance metrics
    pub performance_metrics: PerformanceMetrics,
}

/// Performance metrics for voice usage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Average real-time factor (lower is better)
    pub avg_rtf: f32,

    /// Peak memory usage in MB
    pub peak_memory_mb: u32,

    /// Average memory usage in MB
    pub avg_memory_mb: u32,

    /// Error rate (0.0 - 1.0)
    pub error_rate: f32,
}

/// Voice feature enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VoiceFeature {
    /// Supports emotion in synthesis
    EmotionSupport,
    /// Supports GPU acceleration
    GpuAcceleration,
    /// Low memory usage
    LowMemory,
    /// High quality output
    HighQuality,
}

/// Compact voice summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceSummary {
    /// Voice ID
    pub id: String,

    /// Voice name
    pub name: String,

    /// Language
    pub language: LanguageCode,

    /// Gender (if specified)
    pub gender: Option<Gender>,

    /// Speaking style
    pub style: SpeakingStyle,

    /// Quality level
    pub quality: QualityLevel,

    /// Emotion support
    pub emotion_support: bool,

    /// Memory requirement in MB
    pub memory_requirement: u32,

    /// GPU support
    pub gpu_support: bool,
}

impl fmt::Display for VoiceSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} ({}) - {:?} {:?} {:?} [{}MB{}]",
            self.name,
            self.id,
            self.language,
            self.gender
                .map(|g| format!("{g:?}"))
                .unwrap_or_else(|| "Unknown".to_string()),
            self.quality,
            self.memory_requirement,
            if self.emotion_support {
                ", Emotion"
            } else {
                ""
            }
        )
    }
}

/// Voice comparison utility
pub struct VoiceComparator;

impl VoiceComparator {
    /// Compare two voices and return a comparison result
    pub fn compare(voice1: &VoiceInfo, voice2: &VoiceInfo) -> VoiceComparison {
        VoiceComparison {
            voice1_id: voice1.id().to_string(),
            voice2_id: voice2.id().to_string(),
            quality_diff: voice1.metrics.quality_score - voice2.metrics.quality_score,
            performance_diff: voice1.metrics.performance_score - voice2.metrics.performance_score,
            memory_diff: voice1.config.model_config.device_requirements.min_memory_mb as i32
                - voice2.config.model_config.device_requirements.min_memory_mb as i32,
            features_diff: Self::compare_features(voice1, voice2),
        }
    }

    /// Find best voice for specific criteria
    pub fn find_best_voice<'a>(
        voices: &'a [VoiceInfo],
        criteria: &VoiceSelectionCriteria,
    ) -> Option<&'a VoiceInfo> {
        voices
            .iter()
            .filter(|voice| Self::matches_criteria(voice, criteria))
            .max_by(|a, b| {
                Self::score_voice(a, criteria)
                    .partial_cmp(&Self::score_voice(b, criteria))
                    .expect("value should be present")
            })
    }

    fn compare_features(
        voice1: &VoiceInfo,
        voice2: &VoiceInfo,
    ) -> HashMap<String, FeatureComparison> {
        let mut features = HashMap::new();

        features.insert(
            "emotion_support".to_string(),
            FeatureComparison {
                voice1_has: voice1.config.characteristics.emotion_support,
                voice2_has: voice2.config.characteristics.emotion_support,
            },
        );

        features.insert(
            "gpu_support".to_string(),
            FeatureComparison {
                voice1_has: voice1.config.model_config.device_requirements.gpu_support,
                voice2_has: voice2.config.model_config.device_requirements.gpu_support,
            },
        );

        features
    }

    fn matches_criteria(voice: &VoiceInfo, criteria: &VoiceSelectionCriteria) -> bool {
        if let Some(max_memory) = criteria.max_memory_mb {
            if voice.config.model_config.device_requirements.min_memory_mb > max_memory {
                return false;
            }
        }

        if let Some(min_quality) = criteria.min_quality_score {
            if voice.metrics.quality_score < min_quality {
                return false;
            }
        }

        if let Some(require_emotion) = criteria.require_emotion_support {
            if voice.config.characteristics.emotion_support != require_emotion {
                return false;
            }
        }

        true
    }

    fn score_voice(voice: &VoiceInfo, criteria: &VoiceSelectionCriteria) -> f32 {
        let mut score = voice.metrics.overall_rating;

        // Apply criteria-specific weights
        if criteria.prioritize_quality {
            score += voice.metrics.quality_score * 0.3;
        }

        if criteria.prioritize_performance {
            score += voice.metrics.performance_score * 0.3;
        }

        if criteria.prioritize_memory_efficiency {
            score += voice.metrics.memory_efficiency * 0.2;
        }

        score
    }
}

/// Voice comparison result
#[derive(Debug, Clone)]
pub struct VoiceComparison {
    pub voice1_id: String,
    pub voice2_id: String,
    pub quality_diff: f32,
    pub performance_diff: f32,
    pub memory_diff: i32,
    pub features_diff: HashMap<String, FeatureComparison>,
}

/// Feature comparison for two voices
#[derive(Debug, Clone)]
pub struct FeatureComparison {
    pub voice1_has: bool,
    pub voice2_has: bool,
}

/// Voice selection criteria for finding best voice
#[derive(Debug, Clone, Default)]
pub struct VoiceSelectionCriteria {
    pub max_memory_mb: Option<u32>,
    pub min_quality_score: Option<f32>,
    pub require_emotion_support: Option<bool>,
    pub prioritize_quality: bool,
    pub prioritize_performance: bool,
    pub prioritize_memory_efficiency: bool,
}

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

    fn create_test_voice() -> VoiceConfig {
        VoiceConfig {
            id: "test-voice".to_string(),
            name: "Test Voice".to_string(),
            language: LanguageCode::EnUs,
            characteristics: VoiceCharacteristics {
                gender: Some(Gender::Female),
                age: Some(AgeRange::Adult),
                style: SpeakingStyle::Neutral,
                emotion_support: true,
                quality: QualityLevel::High,
            },
            model_config: ModelConfig {
                g2p_model: Some("g2p.bin".to_string()),
                acoustic_model: "acoustic.bin".to_string(),
                vocoder_model: "vocoder.bin".to_string(),
                format: ModelFormat::Candle,
                device_requirements: DeviceRequirements {
                    min_memory_mb: 1024,
                    gpu_support: true,
                    compute_capabilities: vec!["cpu".to_string(), "cuda".to_string()],
                },
            },
            metadata: HashMap::new(),
        }
    }

    #[test]
    fn test_voice_info_creation() {
        let voice_config = create_test_voice();
        let voice_info = VoiceInfo::from_config(voice_config);

        assert_eq!(voice_info.id(), "test-voice");
        assert_eq!(voice_info.language(), LanguageCode::EnUs);
        assert!(voice_info.supports_feature(VoiceFeature::EmotionSupport));
        assert!(voice_info.supports_feature(VoiceFeature::GpuAcceleration));
        assert!(voice_info.supports_feature(VoiceFeature::HighQuality));
    }

    #[test]
    fn test_voice_metrics() {
        let voice_config = create_test_voice();
        let metrics = VoiceMetrics::from_voice(&voice_config);

        assert!(metrics.quality_score > 0.0);
        assert!(metrics.naturalness_score > 0.0);
        assert!(metrics.overall_rating > 0.0);
        assert_eq!(metrics.complexity, VoiceComplexity::High);
    }

    #[test]
    fn test_voice_summary() {
        let voice_config = create_test_voice();
        let voice_info = VoiceInfo::from_config(voice_config);
        let summary = voice_info.summary();

        assert_eq!(summary.id, "test-voice");
        assert_eq!(summary.name, "Test Voice");
        assert_eq!(summary.language, LanguageCode::EnUs);
        assert_eq!(summary.gender, Some(Gender::Female));
        assert!(summary.emotion_support);
    }

    #[test]
    fn test_voice_comparison() {
        let voice1 = VoiceInfo::from_config(create_test_voice());

        let mut voice2_config = create_test_voice();
        voice2_config.id = "test-voice-2".to_string();
        voice2_config.characteristics.quality = QualityLevel::Medium;
        voice2_config.model_config.device_requirements.min_memory_mb = 512;
        let voice2 = VoiceInfo::from_config(voice2_config);

        let comparison = VoiceComparator::compare(&voice1, &voice2);

        assert!(comparison.quality_diff > 0.0); // voice1 has higher quality
        assert!(comparison.memory_diff > 0); // voice1 uses more memory
    }

    #[test]
    fn test_voice_selection() {
        let voice1 = VoiceInfo::from_config(create_test_voice());

        let mut voice2_config = create_test_voice();
        voice2_config.id = "test-voice-2".to_string();
        voice2_config.characteristics.emotion_support = false;
        let voice2 = VoiceInfo::from_config(voice2_config);

        let voices = vec![voice1, voice2];

        let criteria = VoiceSelectionCriteria {
            require_emotion_support: Some(true),
            ..Default::default()
        };

        let best = VoiceComparator::find_best_voice(&voices, &criteria);
        assert!(best.is_some());
        assert_eq!(best.unwrap().id(), "test-voice");
    }

    #[test]
    fn test_json_serialization() {
        let voice_config = create_test_voice();
        let voice_info = VoiceInfo::from_config(voice_config);

        let json = voice_info.to_json().unwrap();
        assert!(json.contains("test-voice"));

        let restored = VoiceInfo::from_json(&json).unwrap();
        assert_eq!(restored.id(), voice_info.id());
    }

    #[test]
    fn test_model_info() {
        let voice_config = create_test_voice();
        let model_info = ModelInfo::from_config(&voice_config.model_config);

        assert!(model_info.g2p_model.is_some());
        assert_eq!(model_info.acoustic_model.model_type, "Acoustic");
        assert_eq!(model_info.vocoder_model.model_type, "Vocoder");
        assert!(model_info.total_size_mb > 0);
    }
}