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
//! Pipeline initialization and component loading.

use crate::{
    adapters::{G2pAdapter, VocoderAdapter},
    config::PipelineConfig,
    error::Result,
    traits::{AcousticModel, G2p, Vocoder},
    VoirsError,
};
use std::sync::Arc;
use tracing::info;

/// Information about a model file
#[derive(Debug, Clone)]
struct ModelInfo {
    /// Human-readable model name
    name: String,
    /// Filename for local storage
    filename: String,
    /// Download URL
    url: String,
    /// Expected checksum (empty if not available)
    checksum: String,
}

/// Component loading and validation
pub struct PipelineInitializer {
    config: PipelineConfig,
}

impl PipelineInitializer {
    /// Create new initializer with configuration
    pub fn new(config: PipelineConfig) -> Self {
        Self { config }
    }

    /// Get available devices for configuration validation
    fn get_available_devices(&self) -> Vec<String> {
        let mut devices = vec!["cpu".to_string()];

        // Check for GPU availability
        if self.is_gpu_available() {
            #[cfg(feature = "gpu")]
            if cfg!(target_os = "linux") || cfg!(target_os = "windows") {
                devices.push("cuda".to_string());
            }
            if cfg!(target_os = "macos") {
                devices.push("metal".to_string());
            }
            devices.push("vulkan".to_string());
        }

        devices
    }

    /// Initialize all pipeline components
    pub async fn initialize_components(
        &self,
    ) -> Result<(Arc<dyn G2p>, Arc<dyn AcousticModel>, Arc<dyn Vocoder>)> {
        info!("Initializing pipeline components");

        // Validate configuration
        self.validate_configuration().await?;

        // Detect and setup device
        self.setup_device().await?;

        // Download and cache models if needed
        self.download_models().await?;

        // Load components
        let g2p = self.load_g2p().await?;
        let acoustic = self.load_acoustic_model().await?;
        let vocoder = self.load_vocoder().await?;

        info!("Pipeline components initialized successfully");
        Ok((g2p, acoustic, vocoder))
    }

    /// Validate pipeline configuration
    async fn validate_configuration(&self) -> Result<()> {
        info!("Validating pipeline configuration");

        // Validate device configuration
        if !self.is_device_available(&self.config.device) {
            return Err(VoirsError::InvalidConfiguration {
                field: "device".to_string(),
                value: self.config.device.clone(),
                reason: "Device not available".to_string(),
                valid_values: Some(self.get_available_devices()),
            });
        }

        // Validate GPU configuration
        if self.config.use_gpu && !self.is_gpu_available() {
            return Err(VoirsError::InvalidConfiguration {
                field: "use_gpu".to_string(),
                value: "true".to_string(),
                reason: "GPU not available".to_string(),
                valid_values: Some(vec!["false".to_string()]),
            });
        }

        // Validate cache directory
        if let Some(cache_dir) = &self.config.cache_dir {
            if !cache_dir.exists() {
                std::fs::create_dir_all(cache_dir).map_err(|e| VoirsError::IoError {
                    path: cache_dir.clone(),
                    operation: crate::error::types::IoOperation::Create,
                    source: e,
                })?;
            }
        }

        Ok(())
    }

    /// Setup device for computation
    async fn setup_device(&self) -> Result<()> {
        info!("Setting up device: {}", self.config.device);

        match self.config.device.as_str() {
            "cpu" => {
                self.setup_cpu_device().await?;
            }
            "cuda" => {
                self.setup_cuda_device().await?;
            }
            "metal" => {
                self.setup_metal_device().await?;
            }
            "vulkan" => {
                self.setup_vulkan_device().await?;
            }
            "opencl" => {
                self.setup_opencl_device().await?;
            }
            _ => {
                return Err(VoirsError::UnsupportedDevice {
                    device: self.config.device.clone(),
                });
            }
        }

        info!("Device setup completed: {}", self.config.device);
        Ok(())
    }

    /// Setup CPU device
    async fn setup_cpu_device(&self) -> Result<()> {
        info!("Setting up CPU device");

        let thread_count = self.config.effective_thread_count();
        info!("Using {} CPU threads", thread_count);

        // Set thread pool size for CPU inference
        // In real implementation, would configure actual CPU inference backend
        std::env::set_var("OMP_NUM_THREADS", thread_count.to_string());
        std::env::set_var("MKL_NUM_THREADS", thread_count.to_string());

        Ok(())
    }

    /// Setup CUDA device
    async fn setup_cuda_device(&self) -> Result<()> {
        info!("Setting up CUDA device");

        if !self.is_gpu_available() {
            return Err(VoirsError::DeviceNotAvailable {
                device: self.config.device.clone(),
                alternatives: vec!["cpu".to_string()],
            });
        }

        // In real implementation, would initialize CUDA context
        info!("CUDA device initialized");
        Ok(())
    }

    /// Setup Metal device (macOS)
    async fn setup_metal_device(&self) -> Result<()> {
        info!("Setting up Metal device");

        #[cfg(not(target_os = "macos"))]
        {
            Err(VoirsError::DeviceNotAvailable {
                device: "metal".to_string(),
                alternatives: vec!["cpu".to_string(), "cuda".to_string()],
            })
        }

        #[cfg(target_os = "macos")]
        {
            // In real implementation, would initialize Metal context
            info!("Metal device initialized");
            Ok(())
        }
    }

    /// Setup Vulkan device
    async fn setup_vulkan_device(&self) -> Result<()> {
        info!("Setting up Vulkan device");

        // In real implementation, would check Vulkan availability and initialize
        info!("Vulkan device initialized");
        Ok(())
    }

    /// Setup OpenCL device
    async fn setup_opencl_device(&self) -> Result<()> {
        info!("Setting up OpenCL device");

        // In real implementation, would check OpenCL availability and initialize
        info!("OpenCL device initialized");
        Ok(())
    }

    /// Download and cache required models
    async fn download_models(&self) -> Result<()> {
        info!("Checking and downloading models");

        let cache_dir = match &self.config.cache_dir {
            Some(dir) => dir.clone(),
            None => {
                // Use default cache directory
                let mut default_cache = std::env::temp_dir();
                default_cache.push("voirs-cache");
                default_cache
            }
        };

        // Ensure cache directory exists
        if !cache_dir.exists() {
            std::fs::create_dir_all(&cache_dir).map_err(|e| VoirsError::IoError {
                path: cache_dir.clone(),
                operation: crate::error::types::IoOperation::Create,
                source: e,
            })?;
        }

        info!("Models will be cached in: {}", cache_dir.display());

        // Check for required models based on configuration
        let required_models = self.get_required_models();

        for model_info in required_models {
            let model_path = cache_dir.join(&model_info.filename);

            if !model_path.exists() {
                if self.config.model_loading.auto_download {
                    info!("Downloading model: {}", model_info.name);
                    self.download_model(&model_info, &model_path).await?;
                } else {
                    return Err(VoirsError::VoiceNotFound {
                        voice: model_info.name,
                        available: vec![],
                        suggestions: vec![],
                    });
                }
            } else {
                // Verify model integrity if checksum verification is enabled
                if self.config.model_loading.verify_checksums {
                    self.verify_model_checksum(&model_path, &model_info.checksum)
                        .await?;
                }
                info!("Model already cached: {}", model_info.name);
            }
        }

        Ok(())
    }

    /// Get list of required models based on configuration
    fn get_required_models(&self) -> Vec<ModelInfo> {
        let mut models = Vec::new();

        // Add default models based on language and quality settings
        let language = self.config.default_synthesis.language;
        let quality = &self.config.default_synthesis.quality;

        models.push(ModelInfo {
            name: format!("{language:?}-g2p"),
            filename: format!("{language:?}-g2p-{quality:?}.bin"),
            url: format!("https://huggingface.co/voirs/models/{language:?}/g2p-{quality:?}.bin"),
            checksum: "".to_string(), // In real implementation, would have actual checksums
        });

        models.push(ModelInfo {
            name: format!("{language:?}-acoustic"),
            filename: format!("{language:?}-acoustic-{quality:?}.bin"),
            url: format!(
                "https://huggingface.co/voirs/models/{language:?}/acoustic-{quality:?}.bin"
            ),
            checksum: "".to_string(),
        });

        models.push(ModelInfo {
            name: format!("{language:?}-vocoder"),
            filename: format!("{language:?}-vocoder-{quality:?}.bin"),
            url: format!(
                "https://huggingface.co/voirs/models/{language:?}/vocoder-{quality:?}.bin"
            ),
            checksum: "".to_string(),
        });

        models
    }

    /// Download a single model file
    async fn download_model(
        &self,
        model_info: &ModelInfo,
        target_path: &std::path::Path,
    ) -> Result<()> {
        info!("Downloading {} from {}", model_info.name, model_info.url);

        // Create a dummy file for now - in real implementation, would use HTTP client
        tokio::fs::write(target_path, format!("Dummy {} model data", model_info.name))
            .await
            .map_err(|e| VoirsError::IoError {
                path: target_path.to_path_buf(),
                operation: crate::error::types::IoOperation::Write,
                source: e,
            })?;

        info!("Successfully downloaded: {}", model_info.name);
        Ok(())
    }

    /// Verify model file checksum
    async fn verify_model_checksum(
        &self,
        model_path: &std::path::Path,
        expected_checksum: &str,
    ) -> Result<()> {
        if expected_checksum.is_empty() {
            // Skip verification if no checksum provided
            return Ok(());
        }

        info!("Verifying checksum for: {}", model_path.display());

        // In real implementation, would calculate actual file hash
        // For now, just log the verification
        info!("Checksum verification passed");
        Ok(())
    }

    /// Load G2P component
    async fn load_g2p(&self) -> Result<Arc<dyn G2p>> {
        info!("Loading G2P component");

        // Load actual G2P model based on configuration
        use voirs_g2p::backends::rule_based::RuleBasedG2p;
        use voirs_g2p::LanguageCode as G2pLanguageCode;

        match self.config.g2p_model.as_deref().unwrap_or("rule_based") {
            "rule_based" => {
                info!("Loading rule-based G2P model");

                // Determine language from config or use default
                let language = self
                    .config
                    .language_code
                    .and_then(|lang| match lang {
                        crate::types::LanguageCode::EnUs => Some(G2pLanguageCode::EnUs),
                        crate::types::LanguageCode::EnGb => Some(G2pLanguageCode::EnGb),
                        crate::types::LanguageCode::De => Some(G2pLanguageCode::De),
                        crate::types::LanguageCode::Fr => Some(G2pLanguageCode::Fr),
                        crate::types::LanguageCode::Es => Some(G2pLanguageCode::Es),
                        crate::types::LanguageCode::It => Some(G2pLanguageCode::It),
                        crate::types::LanguageCode::Pt => Some(G2pLanguageCode::Pt),
                        crate::types::LanguageCode::Ja => Some(G2pLanguageCode::Ja),
                        _ => None,
                    })
                    .unwrap_or(G2pLanguageCode::EnUs); // Default to English (US)

                let rule_based_g2p = Arc::new(RuleBasedG2p::new(language));
                let adapter = G2pAdapter::new(rule_based_g2p);
                Ok(Arc::new(adapter))
            }
            model_name => {
                // For other models, fall back to dummy for now but log warning
                tracing::warn!("G2P model '{}' not implemented, using dummy", model_name);
                Ok(Arc::new(crate::pipeline::DummyG2p::new()))
            }
        }
    }

    /// Load acoustic model component
    async fn load_acoustic_model(&self) -> Result<Arc<dyn AcousticModel>> {
        info!("Loading acoustic model component");

        // Load actual acoustic model based on configuration
        use voirs_acoustic::backends::candle::CandleBackend;
        use voirs_acoustic::backends::{Backend, BackendManager};
        use voirs_acoustic::config::AcousticConfig;

        match self.config.acoustic_model.as_deref().unwrap_or("candle") {
            "candle" => {
                info!("Loading Candle-based acoustic model");

                // Create acoustic configuration
                let mut acoustic_config = AcousticConfig::default();

                // Set device type based on string
                use voirs_acoustic::config::DeviceType;
                acoustic_config.runtime.device.device_type = match self.config.device.as_str() {
                    "cpu" => DeviceType::Cpu,
                    "cuda" => DeviceType::Cuda,
                    "metal" => DeviceType::Metal,
                    "opencl" => DeviceType::OpenCl,
                    _ => DeviceType::Cpu, // Default to CPU
                };

                // Set GPU usage via mixed precision if GPU is requested
                if self.config.use_gpu && self.config.device != "cpu" {
                    acoustic_config.runtime.device.mixed_precision = true;
                }

                // Set thread count in performance config
                acoustic_config.runtime.performance.num_threads =
                    self.config.num_threads.map(|t| t as u32);

                // Create backend manager
                let _backend_manager = BackendManager::new();

                // Create Candle backend with device config
                let candle_backend = CandleBackend::with_device(
                    acoustic_config.runtime.device.clone(),
                )
                .map_err(|e| VoirsError::ModelError {
                    model_type: crate::error::types::ModelType::Acoustic,
                    message: format!("Failed to create Candle backend: {e}"),
                    source: Some(Box::new(e)),
                })?;

                // Create acoustic model using the backend
                // Determine the actual model path from configuration
                let model_path = self.get_acoustic_model_path()?;
                let acoustic_model =
                    candle_backend
                        .create_model(&model_path)
                        .await
                        .map_err(|e| VoirsError::ModelError {
                            model_type: crate::error::types::ModelType::Acoustic,
                            message: format!("Failed to create acoustic model: {e}"),
                            source: Some(Box::new(e)),
                        })?;

                // Create trait adapter for the acoustic model
                let adapter = crate::adapters::AcousticAdapter::new(Arc::from(acoustic_model));
                Ok(Arc::new(adapter))
            }
            model_name => {
                // For other models, fall back to dummy for now but log warning
                tracing::warn!(
                    "Acoustic model '{}' not implemented, using dummy",
                    model_name
                );
                Ok(Arc::new(crate::pipeline::DummyAcoustic::new()))
            }
        }
    }

    /// Load vocoder component
    async fn load_vocoder(&self) -> Result<Arc<dyn Vocoder>> {
        info!("Loading vocoder component");

        // Load actual vocoder based on configuration
        match self.config.vocoder_model.as_deref().unwrap_or("hifigan") {
            "hifigan" => {
                info!("Loading HiFi-GAN vocoder");

                // Create HiFi-GAN vocoder with configuration
                use voirs_vocoder::HiFiGanVocoder;
                let mut hifigan = HiFiGanVocoder::new();

                // Initialize inference for the vocoder
                hifigan
                    .initialize_inference_for_testing()
                    .map_err(|e| VoirsError::ModelError {
                        model_type: crate::error::types::ModelType::Vocoder,
                        message: format!("Failed to initialize HiFi-GAN vocoder: {e}"),
                        source: Some(Box::new(e)),
                    })?;

                // Create trait adapter for the vocoder
                let adapter = VocoderAdapter::new(Arc::new(hifigan));
                Ok(Arc::new(adapter))
            }
            model_name => {
                // For other models, fall back to dummy for now but log warning
                tracing::warn!(
                    "Vocoder model '{}' not implemented, using dummy",
                    model_name
                );
                Ok(Arc::new(crate::pipeline::DummyVocoder::new()))
            }
        }
    }

    /// Check if device is available
    fn is_device_available(&self, device: &str) -> bool {
        match device {
            "cpu" => true,
            "cuda" => self.is_gpu_available(),
            _ => false,
        }
    }

    /// Check if GPU is available
    fn is_gpu_available(&self) -> bool {
        // Check for CUDA availability on different platforms
        #[cfg(any(target_os = "linux", target_os = "windows"))]
        {
            // Check if CUDA runtime is available
            match std::env::var("CUDA_PATH") {
                Ok(_) => true,
                Err(_) => {
                    // Try alternative checks
                    std::path::Path::new("/usr/local/cuda").exists()
                        || std::path::Path::new("/opt/cuda").exists()
                }
            }
        }

        #[cfg(target_os = "macos")]
        {
            // Check for Metal Performance Shaders
            // Metal is always available on macOS 10.11+
            true
        }

        #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
        {
            false
        }
    }

    /// Get the path to the acoustic model based on configuration
    fn get_acoustic_model_path(&self) -> Result<String> {
        // Check if there's a model override with a local path for acoustic model
        let acoustic_model_name = self.config.acoustic_model.as_deref().unwrap_or("candle");

        if let Some(override_config) = self
            .config
            .model_loading
            .model_overrides
            .get(acoustic_model_name)
        {
            if let Some(local_path) = &override_config.local_path {
                return Ok(local_path.to_string_lossy().to_string());
            }
        }

        // Otherwise, construct path from cache directory and model filename
        let cache_dir = self.config.effective_cache_dir();
        let language = self
            .config
            .language_code
            .unwrap_or(crate::types::LanguageCode::EnUs);
        let quality = &self.config.default_synthesis.quality;

        // Try different model file formats based on environment
        let model_formats = if std::env::var("VOIRS_TEST_MODE").is_ok() || cfg!(test) {
            // In test mode, prefer .bin files first, then .safetensors
            vec!["bin", "safetensors"]
        } else {
            // In production, prefer .safetensors files first, then .bin
            vec!["safetensors", "bin"]
        };

        let mut model_path = None;
        for format in model_formats {
            let model_filename = format!("{language:?}-acoustic-{quality:?}.{format}");
            let candidate_path = cache_dir.join(&model_filename);
            if candidate_path.exists() {
                model_path = Some(candidate_path);
                break;
            }
        }

        let model_path = model_path.ok_or_else(|| VoirsError::ModelError {
            model_type: crate::error::types::ModelType::Acoustic,
            message: format!("Acoustic model not found. Searched for {language:?}-acoustic-{quality:?}.{{safetensors,bin}} in {}", cache_dir.display()),
            source: None,
        })?;

        Ok(model_path.to_string_lossy().to_string())
    }
}

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

    #[tokio::test]
    async fn test_pipeline_initializer() {
        // Test configuration validation rather than actual component loading
        // since the actual loading requires real model files

        // Test with valid configuration
        let config = PipelineConfig {
            device: "cpu".to_string(),
            use_gpu: false,
            ..Default::default()
        };
        let initializer = PipelineInitializer::new(config);

        let result = initializer.validate_configuration().await;
        assert!(result.is_ok());

        // Test with invalid configuration
        let invalid_config = PipelineConfig {
            device: "unsupported".to_string(),
            ..Default::default()
        };
        let invalid_initializer = PipelineInitializer::new(invalid_config);

        let result = invalid_initializer.validate_configuration().await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_configuration_validation() {
        let config = PipelineConfig {
            device: "unsupported".to_string(),
            ..Default::default()
        };

        let initializer = PipelineInitializer::new(config);
        let result = initializer.validate_configuration().await;
        assert!(result.is_err());
    }
}