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
//! Voice switching and management functionality.

use super::discovery::{VoiceRegistry, VoiceSearchCriteria};
use crate::{
    error::Result,
    traits::VoiceManager,
    types::{LanguageCode, VoiceConfig},
    VoirsError,
};
use async_trait::async_trait;
use futures::StreamExt;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::io::AsyncWriteExt;
use tokio::sync::RwLock;

/// Default voice manager implementation
pub struct DefaultVoiceManager {
    registry: VoiceRegistry,
    models_dir: PathBuf,
    current_voice: Option<String>,
    download_enabled: bool,
    switching_history: Vec<VoiceSwitch>,
    test_mode: bool,
}

impl DefaultVoiceManager {
    /// Create new voice manager
    pub fn new(models_dir: impl Into<PathBuf>) -> Self {
        Self {
            registry: VoiceRegistry::new(),
            models_dir: models_dir.into(),
            current_voice: None,
            download_enabled: !cfg!(test), // Disable downloads in test mode
            switching_history: Vec::new(),
            test_mode: cfg!(test), // Automatically enable test mode when running tests
        }
    }

    /// Create voice manager with custom registry
    pub fn with_registry(models_dir: impl Into<PathBuf>, registry: VoiceRegistry) -> Self {
        Self {
            registry,
            models_dir: models_dir.into(),
            current_voice: None,
            download_enabled: !cfg!(test), // Disable downloads in test mode
            switching_history: Vec::new(),
            test_mode: cfg!(test), // Automatically enable test mode when running tests
        }
    }

    /// Add custom voice
    pub fn add_voice(&mut self, voice: VoiceConfig) {
        self.registry.register_voice(voice);
    }

    /// Search voices
    pub fn search(&self, criteria: &VoiceSearchCriteria) -> Vec<&VoiceConfig> {
        self.registry.find_voices(criteria)
    }

    /// Get current voice ID
    pub fn current_voice(&self) -> Option<&str> {
        self.current_voice.as_deref()
    }

    /// Set current voice
    pub fn set_current_voice(&mut self, voice_id: Option<String>) -> Result<()> {
        if let Some(ref id) = voice_id {
            // Validate that the voice exists
            if self.registry.get_voice(id).is_none() {
                return Err(VoirsError::voice_not_found(
                    id.clone(),
                    self.registry
                        .list_voices()
                        .iter()
                        .map(|v| v.id.clone())
                        .collect(),
                ));
            }
        }

        self.current_voice = voice_id;
        Ok(())
    }

    /// Switch to voice by ID
    pub async fn switch_to_voice(&mut self, voice_id: &str) -> Result<()> {
        // Check if voice exists
        if self.registry.get_voice(voice_id).is_none() {
            return Err(VoirsError::voice_not_found(
                voice_id.to_string(),
                self.registry
                    .list_voices()
                    .iter()
                    .map(|v| v.id.clone())
                    .collect(),
            ));
        }

        // Check if voice is available
        if !self.is_voice_available(voice_id) {
            if self.download_enabled {
                tracing::info!(
                    "Voice {} not available locally, attempting download",
                    voice_id
                );
                self.download_voice(voice_id).await?;
            } else {
                return Err(VoirsError::voice_not_found(
                    voice_id.to_string(),
                    self.get_available_voices(),
                ));
            }
        }

        // Track the voice switch in history
        let voice_switch = VoiceSwitch {
            timestamp: std::time::SystemTime::now(),
            from_voice: self.current_voice.clone(),
            to_voice: voice_id.to_string(),
            successful: true,
            error: None,
        };
        self.switching_history.push(voice_switch);

        self.current_voice = Some(voice_id.to_string());
        tracing::info!("Switched to voice: {}", voice_id);
        Ok(())
    }

    /// Switch to default voice for language
    pub async fn switch_to_language(&mut self, language: LanguageCode) -> Result<()> {
        if let Some(default_voice_id) = self.default_voice_for_language(language) {
            self.switch_to_voice(&default_voice_id).await
        } else {
            Err(VoirsError::voice_not_found(
                format!("default voice for {language:?}"),
                self.registry
                    .list_voices()
                    .iter()
                    .map(|v| v.id.clone())
                    .collect(),
            ))
        }
    }

    /// Get available voice IDs (voices that have model files present)
    pub fn get_available_voices(&self) -> Vec<String> {
        self.registry
            .list_voices()
            .iter()
            .filter(|voice| self.is_voice_available(&voice.id))
            .map(|voice| voice.id.clone())
            .collect()
    }

    /// Get unavailable voice IDs (voices missing model files)
    pub fn get_unavailable_voices(&self) -> Vec<String> {
        self.registry
            .list_voices()
            .iter()
            .filter(|voice| !self.is_voice_available(&voice.id))
            .map(|voice| voice.id.clone())
            .collect()
    }

    /// Check if any voice is available for a language
    pub fn has_available_voice_for_language(&self, language: LanguageCode) -> bool {
        self.registry
            .voices_for_language(language)
            .iter()
            .any(|voice| self.is_voice_available(&voice.id))
    }

    /// Get first available voice for language
    pub fn get_available_voice_for_language(&self, language: LanguageCode) -> Option<String> {
        self.registry
            .voices_for_language(language)
            .iter()
            .find(|voice| self.is_voice_available(&voice.id))
            .map(|voice| voice.id.clone())
    }

    /// Enable or disable automatic downloading
    pub fn set_download_enabled(&mut self, enabled: bool) {
        self.download_enabled = enabled;
    }

    /// Check if automatic downloading is enabled
    pub fn is_download_enabled(&self) -> bool {
        self.download_enabled
    }

    /// Set test mode (skips expensive operations for fast testing)
    pub fn set_test_mode(&mut self, enabled: bool) {
        self.test_mode = enabled;
        if enabled {
            self.download_enabled = false;
        }
    }

    /// Validate voice models exist and are accessible
    pub fn validate_voice_models(&self, voice_id: &str) -> Result<VoiceValidationResult> {
        let voice = self.registry.get_voice(voice_id).ok_or_else(|| {
            VoirsError::voice_not_found(
                voice_id.to_string(),
                self.registry
                    .list_voices()
                    .iter()
                    .map(|v| v.id.clone())
                    .collect(),
            )
        })?;

        let mut result = VoiceValidationResult {
            voice_id: voice_id.to_string(),
            valid: true,
            missing_files: Vec::new(),
            invalid_files: Vec::new(),
            warnings: Vec::new(),
        };

        // Check acoustic model
        let acoustic_path = self.models_dir.join(&voice.model_config.acoustic_model);
        if !acoustic_path.exists() {
            result
                .missing_files
                .push(voice.model_config.acoustic_model.clone());
            result.valid = false;
        }

        // Check vocoder model
        let vocoder_path = self.models_dir.join(&voice.model_config.vocoder_model);
        if !vocoder_path.exists() {
            result
                .missing_files
                .push(voice.model_config.vocoder_model.clone());
            result.valid = false;
        }

        // Check G2P model if specified
        if let Some(ref g2p_model) = voice.model_config.g2p_model {
            let g2p_path = self.models_dir.join(g2p_model);
            if !g2p_path.exists() {
                result.missing_files.push(g2p_model.clone());
                result.valid = false;
            }
        }

        // Add warnings for potential issues
        if voice.model_config.device_requirements.min_memory_mb > 2048 {
            result
                .warnings
                .push("Voice requires more than 2GB of memory".to_string());
        }

        if !voice
            .model_config
            .device_requirements
            .compute_capabilities
            .contains(&"cpu".to_string())
        {
            result
                .warnings
                .push("Voice may not support CPU-only inference".to_string());
        }

        Ok(result)
    }

    /// Download all models for a voice
    async fn download_voice_models(&self, voice: &VoiceConfig) -> Result<()> {
        let models = [
            &voice.model_config.acoustic_model,
            &voice.model_config.vocoder_model,
        ];

        let mut all_models = models.to_vec();
        if let Some(ref g2p_model) = voice.model_config.g2p_model {
            all_models.push(g2p_model);
        }

        for model_path in all_models {
            let full_path = self.models_dir.join(model_path);

            // Create parent directories if needed
            if let Some(parent) = full_path.parent() {
                tokio::fs::create_dir_all(parent)
                    .await
                    .map_err(|e| VoirsError::IoError {
                        path: parent.to_path_buf(),
                        operation: crate::error::types::IoOperation::Create,
                        source: e,
                    })?;
            }

            // Skip download in test mode - create dummy file
            if cfg!(test) {
                tracing::debug!("Test mode: creating dummy file for {}", model_path);
                tokio::fs::write(&full_path, b"dummy model file")
                    .await
                    .map_err(|e| VoirsError::IoError {
                        path: full_path.clone(),
                        operation: crate::error::types::IoOperation::Write,
                        source: e,
                    })?;
                continue;
            }

            // Actual model download
            tracing::info!("Downloading model: {}", model_path);

            // Determine download URL based on model path
            let download_url = self.get_model_download_url(&voice.id, model_path)?;

            // Download the model file
            self.download_file(&download_url, &full_path).await?;

            tracing::info!("Successfully downloaded: {}", model_path);
        }

        Ok(())
    }

    /// Get download URL for a model file
    fn get_model_download_url(&self, voice_id: &str, model_path: &str) -> Result<String> {
        // Check if the model path is already a URL
        if model_path.starts_with("http://") || model_path.starts_with("https://") {
            return Ok(model_path.to_string());
        }

        // Check if it's a HuggingFace Hub reference (e.g., "microsoft/DialoGPT-medium")
        if model_path.contains('/') && !model_path.starts_with("models/") {
            return Ok(format!(
                "https://huggingface.co/{voice_id}/resolve/main/{model_path}"
            ));
        }

        // Default: construct URL from voice repository
        let base_url = std::env::var("VOIRS_MODEL_REPOSITORY_URL")
            .unwrap_or_else(|_| "https://github.com/voirs-ai/models/releases/download".to_string());

        let model_filename = std::path::Path::new(model_path)
            .file_name()
            .and_then(|f| f.to_str())
            .unwrap_or(model_path);

        Ok(format!("{base_url}/{voice_id}/{model_filename}"))
    }

    /// Download a file from URL to local path
    async fn download_file(&self, url: &str, local_path: &std::path::Path) -> Result<()> {
        tracing::debug!("Downloading {} to {}", url, local_path.display());

        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(300)) // 5 minute timeout
            .build()
            .map_err(|e| VoirsError::DownloadFailed {
                url: url.to_string(),
                reason: format!("Failed to create HTTP client: {e}"),
                bytes_downloaded: 0,
                total_bytes: None,
            })?;

        let response = client
            .get(url)
            .send()
            .await
            .map_err(|e| VoirsError::DownloadFailed {
                url: url.to_string(),
                reason: format!("Request failed: {e}"),
                bytes_downloaded: 0,
                total_bytes: None,
            })?;

        if !response.status().is_success() {
            return Err(VoirsError::DownloadFailed {
                url: url.to_string(),
                reason: format!("HTTP error: {}", response.status()),
                bytes_downloaded: 0,
                total_bytes: response.content_length(),
            });
        }

        let total_size = response.content_length();
        let mut bytes_downloaded = 0u64;

        // Create the file
        let mut file =
            tokio::fs::File::create(local_path)
                .await
                .map_err(|e| VoirsError::IoError {
                    path: local_path.to_path_buf(),
                    operation: crate::error::types::IoOperation::Create,
                    source: e,
                })?;

        // Stream the download
        let mut stream = response.bytes_stream();
        while let Some(chunk) = stream.next().await {
            let chunk = chunk.map_err(|e| VoirsError::DownloadFailed {
                url: url.to_string(),
                reason: format!("Stream error: {e}"),
                bytes_downloaded,
                total_bytes: total_size,
            })?;

            use tokio::io::AsyncWriteExt;
            file.write_all(&chunk)
                .await
                .map_err(|e| VoirsError::IoError {
                    path: local_path.to_path_buf(),
                    operation: crate::error::types::IoOperation::Write,
                    source: e,
                })?;

            bytes_downloaded += chunk.len() as u64;

            // Log progress for large files
            if let Some(total) = total_size {
                if total > 10 * 1024 * 1024 {
                    // > 10MB
                    let percent = (bytes_downloaded as f64 / total as f64) * 100.0;
                    if bytes_downloaded.is_multiple_of(1024 * 1024) {
                        // Log every MB
                        tracing::debug!(
                            "Download progress: {:.1}% ({}/{})",
                            percent,
                            bytes_downloaded,
                            total
                        );
                    }
                }
            }
        }

        file.flush().await.map_err(|e| VoirsError::IoError {
            path: local_path.to_path_buf(),
            operation: crate::error::types::IoOperation::Write,
            source: e,
        })?;

        tracing::debug!(
            "Downloaded {} bytes to {}",
            bytes_downloaded,
            local_path.display()
        );
        Ok(())
    }

    /// Get voice switching history
    pub fn get_switching_history(&self) -> Vec<VoiceSwitch> {
        self.switching_history.clone()
    }

    /// Clear voice switching history
    pub fn clear_switching_history(&mut self) {
        self.switching_history.clear();
    }
}

#[async_trait]
impl VoiceManager for DefaultVoiceManager {
    async fn list_voices(&self) -> Result<Vec<VoiceConfig>> {
        Ok(self.registry.list_voices().into_iter().cloned().collect())
    }

    async fn get_voice(&self, voice_id: &str) -> Result<Option<VoiceConfig>> {
        Ok(self.registry.get_voice(voice_id).cloned())
    }

    async fn download_voice(&self, voice_id: &str) -> Result<()> {
        let voice = self.registry.get_voice(voice_id).ok_or_else(|| {
            VoirsError::voice_not_found(
                voice_id.to_string(),
                self.registry
                    .list_voices()
                    .iter()
                    .map(|v| v.id.clone())
                    .collect(),
            )
        })?;

        self.download_voice_models(voice).await?;

        tracing::info!("Voice '{}' downloaded successfully", voice_id);
        Ok(())
    }

    fn is_voice_available(&self, voice_id: &str) -> bool {
        // In test mode, assume all voices are available to avoid file system checks
        if self.test_mode {
            return self.registry.get_voice(voice_id).is_some();
        }

        if let Some(voice) = self.registry.get_voice(voice_id) {
            // Check if model files exist
            let models_exist = [
                &voice.model_config.acoustic_model,
                &voice.model_config.vocoder_model,
            ]
            .iter()
            .chain(voice.model_config.g2p_model.as_ref().iter())
            .all(|model_path| {
                let full_path = self.models_dir.join(model_path);
                full_path.exists()
            });

            models_exist
        } else {
            false
        }
    }

    fn default_voice_for_language(&self, lang: LanguageCode) -> Option<String> {
        self.registry
            .default_voice_for_language(lang)
            .map(|voice| voice.id.clone())
    }
}

/// Thread-safe voice manager wrapper
pub struct ConcurrentVoiceManager {
    inner: Arc<RwLock<DefaultVoiceManager>>,
}

impl ConcurrentVoiceManager {
    /// Create new concurrent voice manager
    pub fn new(models_dir: impl Into<PathBuf>) -> Self {
        Self {
            inner: Arc::new(RwLock::new(DefaultVoiceManager::new(models_dir))),
        }
    }

    /// Get read access to voice manager
    pub async fn read(&self) -> tokio::sync::RwLockReadGuard<'_, DefaultVoiceManager> {
        self.inner.read().await
    }

    /// Get write access to voice manager
    pub async fn write(&self) -> tokio::sync::RwLockWriteGuard<'_, DefaultVoiceManager> {
        self.inner.write().await
    }

    /// Switch voice (convenience method)
    pub async fn switch_to_voice(&self, voice_id: &str) -> Result<()> {
        self.inner.write().await.switch_to_voice(voice_id).await
    }

    /// Get current voice (convenience method)
    pub async fn current_voice(&self) -> Option<String> {
        self.inner
            .read()
            .await
            .current_voice()
            .map(|s| s.to_string())
    }
}

/// Voice validation result
#[derive(Debug, Clone)]
pub struct VoiceValidationResult {
    /// Voice ID that was validated
    pub voice_id: String,
    /// Whether the voice is valid and usable
    pub valid: bool,
    /// List of missing model files
    pub missing_files: Vec<String>,
    /// List of invalid or corrupted files
    pub invalid_files: Vec<String>,
    /// List of warnings
    pub warnings: Vec<String>,
}

/// Voice switch record for history tracking
#[derive(Debug, Clone)]
pub struct VoiceSwitch {
    /// Timestamp of the switch
    pub timestamp: std::time::SystemTime,
    /// Previous voice ID (if any)
    pub from_voice: Option<String>,
    /// New voice ID
    pub to_voice: String,
    /// Whether the switch was successful
    pub successful: bool,
    /// Error message if switch failed
    pub error: Option<String>,
}

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

    #[tokio::test]
    async fn test_voice_manager_creation() {
        let temp_dir = tempdir().unwrap();
        let manager = DefaultVoiceManager::new(temp_dir.path());

        // Should have voices from registry
        let voices = manager.list_voices().await.unwrap();
        assert!(!voices.is_empty());
    }

    #[tokio::test]
    async fn test_voice_availability() {
        let temp_dir = tempdir().unwrap();
        let mut manager = DefaultVoiceManager::new(temp_dir.path());
        // Disable test mode to test actual file system behavior
        manager.set_test_mode(false);

        // Voice should not be available (no model files)
        assert!(!manager.is_voice_available("en-US-female-calm"));

        // Get available voices should be empty
        let available = manager.get_available_voices();
        assert!(available.is_empty());

        // Get unavailable voices should not be empty
        let unavailable = manager.get_unavailable_voices();
        assert!(!unavailable.is_empty());
    }

    #[tokio::test]
    async fn test_voice_switching() {
        let temp_dir = tempdir().unwrap();
        let mut manager = DefaultVoiceManager::new(temp_dir.path());

        // Initially no current voice
        assert!(manager.current_voice().is_none());

        // Download and switch to voice
        manager.set_download_enabled(true);
        let result = manager.switch_to_voice("en-US-female-calm").await;
        assert!(result.is_ok());

        // Should now have current voice
        assert_eq!(manager.current_voice(), Some("en-US-female-calm"));

        // Voice should now be available
        assert!(manager.is_voice_available("en-US-female-calm"));
    }

    #[tokio::test]
    async fn test_language_switching() {
        let temp_dir = tempdir().unwrap();
        let mut manager = DefaultVoiceManager::new(temp_dir.path());
        manager.set_download_enabled(true);

        // Switch to English
        let result = manager.switch_to_language(LanguageCode::EnUs).await;
        assert!(result.is_ok());

        let current = manager.current_voice().unwrap();
        let voice = manager.get_voice(current).await.unwrap().unwrap();
        assert_eq!(voice.language, LanguageCode::EnUs);
    }

    #[tokio::test]
    async fn test_voice_validation() {
        let temp_dir = tempdir().unwrap();
        let manager = DefaultVoiceManager::new(temp_dir.path());

        // Validate non-existent voice
        let result = manager.validate_voice_models("non-existent");
        assert!(result.is_err());

        // Validate existing voice (should show missing files)
        let result = manager.validate_voice_models("en-US-female-calm").unwrap();
        assert!(!result.valid);
        assert!(!result.missing_files.is_empty());
    }

    #[tokio::test]
    async fn test_concurrent_voice_manager() {
        let temp_dir = tempdir().unwrap();
        let manager = ConcurrentVoiceManager::new(temp_dir.path());

        // Test concurrent access
        let current = manager.current_voice().await;
        assert!(current.is_none());

        // Test write access
        {
            let mut write_guard = manager.write().await;
            write_guard.set_download_enabled(true);
        }

        // Test read access
        {
            let read_guard = manager.read().await;
            assert!(read_guard.is_download_enabled());
        }
    }

    #[tokio::test]
    async fn test_voice_search() {
        let temp_dir = tempdir().unwrap();
        let manager = DefaultVoiceManager::new(temp_dir.path());

        // Search for female voices
        let criteria = VoiceSearchCriteria::new().gender(Gender::Female);
        let results = manager.search(&criteria);
        assert!(!results.is_empty());

        for voice in results {
            assert_eq!(voice.characteristics.gender, Some(Gender::Female));
        }
    }

    #[test]
    fn test_voice_validation_result() {
        let result = VoiceValidationResult {
            voice_id: "test-voice".to_string(),
            valid: false,
            missing_files: vec!["model1.bin".to_string(), "model2.bin".to_string()],
            invalid_files: vec![],
            warnings: vec!["High memory usage".to_string()],
        };

        assert!(!result.valid);
        assert_eq!(result.missing_files.len(), 2);
        assert_eq!(result.warnings.len(), 1);
    }
}