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
//! Plugin system for extensible audio processing and effects.

pub mod effects;
pub mod enhancement;
pub mod format;
pub mod manager;
pub mod registry;

use crate::{audio::AudioBuffer, error::Result, VoirsError};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::{
    any::Any,
    collections::HashMap,
    path::PathBuf,
    sync::{Arc, RwLock},
};

/// Plugin trait definition for VoiRS ecosystem
pub trait VoirsPlugin: Send + Sync + Any {
    /// Get plugin name
    fn name(&self) -> &str;

    /// Get plugin version
    fn version(&self) -> &str;

    /// Get plugin description
    fn description(&self) -> &str;

    /// Get plugin author
    fn author(&self) -> &str;

    /// Initialize plugin with configuration
    fn initialize(&self, config: &PluginConfig) -> Result<()> {
        let _ = config; // Suppress unused parameter warning
        Ok(()) // Default: no initialization needed
    }

    /// Shutdown plugin and cleanup resources
    fn shutdown(&self) -> Result<()> {
        Ok(()) // Default: no cleanup needed
    }

    /// Get plugin metadata
    fn metadata(&self) -> PluginMetadata {
        PluginMetadata {
            name: self.name().to_string(),
            version: self.version().to_string(),
            description: self.description().to_string(),
            author: self.author().to_string(),
            plugin_type: PluginType::Effect,
            supported_formats: vec!["wav".to_string()],
            capabilities: vec![],
            dependencies: vec![],
            supported_platforms: Some(vec!["any".to_string()]),
            min_voirs_version: Some("0.1.0".to_string()),
        }
    }

    /// Check if plugin supports a specific capability
    fn supports_capability(&self, capability: &str) -> bool {
        self.metadata()
            .capabilities
            .contains(&capability.to_string())
    }

    /// Get the plugin as an Any trait object for downcasting
    fn as_any(&self) -> &dyn Any;
}

/// Audio effect plugin trait
#[async_trait]
pub trait AudioEffect: VoirsPlugin {
    /// Process audio with the effect
    async fn process_audio(&self, audio: &AudioBuffer) -> Result<AudioBuffer>;

    /// Get effect parameters
    fn get_parameters(&self) -> HashMap<String, ParameterValue>;

    /// Set effect parameter
    fn set_parameter(&self, name: &str, value: ParameterValue) -> Result<()>;

    /// Get parameter definition
    fn get_parameter_definition(&self, name: &str) -> Option<ParameterDefinition>;

    /// List all parameter names
    fn list_parameters(&self) -> Vec<String> {
        self.get_parameters().keys().cloned().collect()
    }

    /// Reset parameters to defaults
    fn reset_parameters(&self) -> Result<()> {
        // Default implementation: do nothing
        Ok(())
    }

    /// Get effect latency in samples
    fn get_latency_samples(&self) -> usize {
        0 // Default: no latency
    }

    /// Check if effect can process in real-time
    fn is_realtime_capable(&self) -> bool {
        true // Default: assume real-time capable
    }
}

/// Voice effect plugin trait for voice-specific processing
#[async_trait]
pub trait VoiceEffect: VoirsPlugin {
    /// Process voice synthesis pipeline
    async fn process_voice_synthesis(
        &self,
        phonemes: &[crate::types::Phoneme],
        mel: &crate::types::MelSpectrogram,
        audio: &AudioBuffer,
    ) -> Result<VoiceSynthesisResult>;

    /// Get voice effect type
    fn get_effect_type(&self) -> VoiceEffectType;

    /// Check if effect modifies specific synthesis stage
    fn modifies_stage(&self, stage: SynthesisStage) -> bool;
}

/// Text processor plugin trait
#[async_trait]
pub trait TextProcessor: VoirsPlugin {
    /// Process text before synthesis
    async fn process_text(
        &self,
        text: &str,
        language: crate::types::LanguageCode,
    ) -> Result<String>;

    /// Get text processor type
    fn get_processor_type(&self) -> TextProcessorType;

    /// Check if processor handles specific language
    fn supports_language(&self, language: crate::types::LanguageCode) -> bool;
}

/// Plugin configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginConfig {
    /// Plugin-specific parameters
    pub parameters: HashMap<String, ParameterValue>,

    /// Plugin enabled state
    pub enabled: bool,

    /// Plugin priority (lower = higher priority)
    pub priority: i32,

    /// Plugin configuration file path
    pub config_file: Option<PathBuf>,

    /// Plugin data directory
    pub data_dir: Option<PathBuf>,

    /// Plugin cache directory
    pub cache_dir: Option<PathBuf>,
}

impl Default for PluginConfig {
    fn default() -> Self {
        Self {
            parameters: HashMap::new(),
            enabled: true,
            priority: 100,
            config_file: None,
            data_dir: None,
            cache_dir: None,
        }
    }
}

/// Plugin metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginMetadata {
    /// Plugin name
    pub name: String,

    /// Plugin version
    pub version: String,

    /// Plugin description
    pub description: String,

    /// Plugin author
    pub author: String,

    /// Plugin type
    pub plugin_type: PluginType,

    /// Supported audio formats
    pub supported_formats: Vec<String>,

    /// Plugin capabilities
    pub capabilities: Vec<String>,

    /// Plugin dependencies
    pub dependencies: Vec<String>,

    /// Supported platforms
    pub supported_platforms: Option<Vec<String>>,

    /// Minimum VoiRS version required
    pub min_voirs_version: Option<String>,
}

/// Plugin type classification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PluginType {
    /// Audio effect plugin
    Effect,

    /// Voice synthesis effect
    VoiceEffect,

    /// Text processing plugin
    TextProcessor,

    /// Model enhancement plugin
    ModelEnhancer,

    /// Output format plugin
    OutputProcessor,
}

/// Parameter value types
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ParameterValue {
    Float(f32),
    Int(i32),
    Integer(i64), // For larger integer values
    Bool(bool),
    String(String),
    FloatArray(Vec<f32>),
    IntArray(Vec<i32>),
}

impl ParameterValue {
    /// Convert to f32 if possible
    pub fn as_f32(&self) -> Option<f32> {
        match self {
            Self::Float(v) => Some(*v),
            Self::Int(v) => Some(*v as f32),
            Self::Integer(v) => Some(*v as f32),
            _ => None,
        }
    }

    /// Convert to i32 if possible
    pub fn as_i32(&self) -> Option<i32> {
        match self {
            Self::Int(v) => Some(*v),
            Self::Integer(v) => Some(*v as i32),
            Self::Float(v) => Some(*v as i32),
            _ => None,
        }
    }

    /// Convert to i64 if possible
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            Self::Integer(v) => Some(*v),
            Self::Int(v) => Some(*v as i64),
            Self::Float(v) => Some(*v as i64),
            _ => None,
        }
    }

    /// Convert to bool if possible
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(v) => Some(*v),
            Self::Int(v) => Some(*v != 0),
            Self::Integer(v) => Some(*v != 0),
            Self::Float(v) => Some(*v != 0.0),
            _ => None,
        }
    }

    /// Convert to string
    pub fn as_string(&self) -> String {
        match self {
            Self::String(v) => v.clone(),
            Self::Float(v) => v.to_string(),
            Self::Int(v) => v.to_string(),
            Self::Integer(v) => v.to_string(),
            Self::Bool(v) => v.to_string(),
            Self::FloatArray(v) => format!("{v:?}"),
            Self::IntArray(v) => format!("{v:?}"),
        }
    }
}

/// Parameter definition for plugin configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterDefinition {
    /// Parameter name
    pub name: String,

    /// Parameter description
    pub description: String,

    /// Parameter type
    pub parameter_type: ParameterType,

    /// Default value
    pub default_value: ParameterValue,

    /// Minimum value (for numeric types)
    pub min_value: Option<ParameterValue>,

    /// Maximum value (for numeric types)
    pub max_value: Option<ParameterValue>,

    /// Step size (for numeric types)
    pub step_size: Option<f32>,

    /// Whether parameter affects real-time processing
    pub realtime_safe: bool,
}

/// Parameter type classification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ParameterType {
    Float,
    Int,
    Integer, // For larger integer values
    Bool,
    String,
    FloatArray,
    IntArray,
    Enum,
}

/// Voice effect type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum VoiceEffectType {
    /// Modify phoneme sequence
    PhonemeModifier,

    /// Modify mel spectrogram
    MelModifier,

    /// Modify final audio
    AudioModifier,

    /// Multi-stage effect
    MultiStage,
}

/// Synthesis stage
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SynthesisStage {
    /// Text preprocessing
    TextPreprocessing,

    /// Phoneme generation
    PhonemeGeneration,

    /// Mel spectrogram synthesis
    MelSynthesis,

    /// Audio vocoding
    AudioGeneration,

    /// Post-processing
    PostProcessing,
}

/// Text processor type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TextProcessorType {
    /// Normalize text format
    Normalizer,

    /// Expand abbreviations
    Expander,

    /// Number to text conversion
    NumberConverter,

    /// SSML processor
    SsmlProcessor,

    /// Emotion analyzer
    EmotionAnalyzer,
}

/// Voice synthesis result from voice effect plugin
#[derive(Debug, Clone)]
pub struct VoiceSynthesisResult {
    /// Modified phonemes (optional)
    pub phonemes: Option<Vec<crate::types::Phoneme>>,

    /// Modified mel spectrogram (optional)
    pub mel_spectrogram: Option<crate::types::MelSpectrogram>,

    /// Modified audio (optional)
    pub audio_buffer: Option<AudioBuffer>,

    /// Processing metadata
    pub metadata: HashMap<String, String>,
}

/// Plugin loading error
#[derive(Debug, thiserror::Error)]
pub enum PluginError {
    #[error("Plugin not found: {name}")]
    NotFound { name: String },

    #[error("Plugin initialization failed: {message}")]
    InitializationFailed { message: String },

    #[error("Plugin parameter error: {message}")]
    ParameterError { message: String },

    #[error("Plugin version incompatible: required {required}, found {found}")]
    VersionIncompatible { required: String, found: String },

    #[error("Plugin dependency missing: {dependency}")]
    DependencyMissing { dependency: String },

    #[error("Plugin processing error: {message}")]
    ProcessingError { message: String },
}

/// Plugin host for managing plugin lifecycle
pub struct PluginHost {
    /// Loaded plugins
    plugins: Arc<RwLock<HashMap<String, Arc<dyn VoirsPlugin>>>>,

    /// Plugin configurations
    configs: Arc<RwLock<HashMap<String, PluginConfig>>>,

    /// Plugin load order
    load_order: Vec<String>,

    /// Plugin data directory
    #[allow(dead_code)]
    plugin_dir: PathBuf,
}

impl PluginHost {
    /// Create new plugin host
    pub fn new(plugin_dir: impl Into<PathBuf>) -> Self {
        Self {
            plugins: Arc::new(RwLock::new(HashMap::new())),
            configs: Arc::new(RwLock::new(HashMap::new())),
            load_order: Vec::new(),
            plugin_dir: plugin_dir.into(),
        }
    }

    /// Load plugin from file
    pub fn load_plugin(&mut self, name: &str, plugin: Arc<dyn VoirsPlugin>) -> Result<()> {
        let default_config = PluginConfig::default();

        // Initialize plugin
        plugin.initialize(&default_config).map_err(|e| {
            VoirsError::internal("plugins", format!("Plugin initialization failed: {e}"))
        })?;

        // Store plugin and config
        {
            let mut plugins = self
                .plugins
                .write()
                .map_err(|e| VoirsError::internal("plugins", format!("Lock poisoned: {e}")))?;
            let mut configs = self
                .configs
                .write()
                .map_err(|e| VoirsError::internal("plugins", format!("Lock poisoned: {e}")))?;

            plugins.insert(name.to_string(), plugin);
            configs.insert(name.to_string(), default_config);
        }

        self.load_order.push(name.to_string());
        tracing::info!("Loaded plugin: {}", name);

        Ok(())
    }

    /// Unload plugin
    pub fn unload_plugin(&mut self, name: &str) -> Result<()> {
        {
            let mut plugins = self
                .plugins
                .write()
                .map_err(|e| VoirsError::internal("plugins", format!("Lock poisoned: {e}")))?;
            let mut configs = self
                .configs
                .write()
                .map_err(|e| VoirsError::internal("plugins", format!("Lock poisoned: {e}")))?;

            if let Some(plugin) = plugins.remove(name) {
                plugin.shutdown().map_err(|e| {
                    VoirsError::internal("plugins", format!("Plugin shutdown failed: {e}"))
                })?;
            }

            configs.remove(name);
        }

        self.load_order.retain(|n| n != name);
        tracing::info!("Unloaded plugin: {}", name);

        Ok(())
    }

    /// Get plugin by name
    pub fn get_plugin(&self, name: &str) -> Option<Arc<dyn VoirsPlugin>> {
        let plugins = self.plugins.read().ok()?;
        plugins.get(name).cloned()
    }

    /// List all loaded plugins
    pub fn list_plugins(&self) -> Vec<String> {
        self.plugins
            .read()
            .map(|plugins| plugins.keys().cloned().collect())
            .unwrap_or_default()
    }

    /// Get plugin metadata
    pub fn get_plugin_metadata(&self, name: &str) -> Option<PluginMetadata> {
        self.get_plugin(name).map(|p| p.metadata())
    }

    /// Configure plugin
    pub fn configure_plugin(&self, name: &str, config: PluginConfig) -> Result<()> {
        let mut configs = self
            .configs
            .write()
            .map_err(|e| VoirsError::internal("plugins", format!("Lock poisoned: {e}")))?;
        configs.insert(name.to_string(), config);
        Ok(())
    }

    /// Get plugin configuration
    pub fn get_plugin_config(&self, name: &str) -> Option<PluginConfig> {
        let configs = self.configs.read().ok()?;
        configs.get(name).cloned()
    }
}

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

    /// Test plugin implementation
    struct TestAudioEffect {
        name: String,
        gain: std::sync::RwLock<f32>,
    }

    impl TestAudioEffect {
        fn new() -> Self {
            Self {
                name: "TestEffect".to_string(),
                gain: std::sync::RwLock::new(1.0),
            }
        }
    }

    impl VoirsPlugin for TestAudioEffect {
        fn name(&self) -> &str {
            &self.name
        }

        fn version(&self) -> &str {
            "1.0.0"
        }

        fn description(&self) -> &str {
            "Test audio effect plugin"
        }

        fn author(&self) -> &str {
            "VoiRS Team"
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    #[async_trait]
    impl AudioEffect for TestAudioEffect {
        async fn process_audio(&self, audio: &AudioBuffer) -> Result<AudioBuffer> {
            let mut processed = audio.clone();
            let gain = *self.gain.read().expect("lock should not be poisoned");
            for sample in processed.samples_mut() {
                *sample *= gain;
            }
            Ok(processed)
        }

        fn get_parameters(&self) -> HashMap<String, ParameterValue> {
            let mut params = HashMap::new();
            params.insert(
                "gain".to_string(),
                ParameterValue::Float(*self.gain.read().expect("lock should not be poisoned")),
            );
            params
        }

        fn set_parameter(&self, name: &str, value: ParameterValue) -> Result<()> {
            match name {
                "gain" => {
                    if let Some(gain) = value.as_f32() {
                        *self.gain.write().expect("lock should not be poisoned") = gain;
                        Ok(())
                    } else {
                        Err(VoirsError::internal(
                            "plugins",
                            "Invalid gain parameter type",
                        ))
                    }
                }
                _ => Err(VoirsError::internal(
                    "plugins",
                    format!("Unknown parameter: {name}"),
                )),
            }
        }

        fn get_parameter_definition(&self, name: &str) -> Option<ParameterDefinition> {
            match name {
                "gain" => Some(ParameterDefinition {
                    name: "gain".to_string(),
                    description: "Audio gain multiplier".to_string(),
                    parameter_type: ParameterType::Float,
                    default_value: ParameterValue::Float(1.0),
                    min_value: Some(ParameterValue::Float(0.0)),
                    max_value: Some(ParameterValue::Float(10.0)),
                    step_size: Some(0.1),
                    realtime_safe: true,
                }),
                _ => None,
            }
        }
    }

    #[test]
    fn test_parameter_value_conversions() {
        let float_val = ParameterValue::Float(3.5);
        assert_eq!(float_val.as_f32(), Some(3.5));
        assert_eq!(float_val.as_i32(), Some(3));

        let int_val = ParameterValue::Int(42);
        assert_eq!(int_val.as_i32(), Some(42));
        assert_eq!(int_val.as_f32(), Some(42.0));

        let bool_val = ParameterValue::Bool(true);
        assert_eq!(bool_val.as_bool(), Some(true));

        let string_val = ParameterValue::String("test".to_string());
        assert_eq!(string_val.as_string(), "test");
    }

    #[test]
    fn test_plugin_host() {
        let mut host = PluginHost::new("/tmp/plugins");
        let plugin = Arc::new(TestAudioEffect::new());

        // Load plugin
        host.load_plugin("test_effect", plugin).unwrap();

        // Check if plugin is loaded
        assert!(host.get_plugin("test_effect").is_some());
        assert_eq!(host.list_plugins().len(), 1);

        // Get plugin metadata
        let metadata = host.get_plugin_metadata("test_effect").unwrap();
        assert_eq!(metadata.name, "TestEffect");
        assert_eq!(metadata.version, "1.0.0");

        // Unload plugin
        host.unload_plugin("test_effect").unwrap();
        assert!(host.get_plugin("test_effect").is_none());
        assert_eq!(host.list_plugins().len(), 0);
    }

    #[tokio::test]
    async fn test_audio_effect_processing() {
        let effect = TestAudioEffect::new();

        // Test parameter setting
        effect
            .set_parameter("gain", ParameterValue::Float(2.0))
            .unwrap();
        // Note: Can't test gain value since we can't mutate in set_parameter anymore
        // assert_eq!(effect.gain, 2.0);

        // Test audio processing
        let audio = crate::AudioBuffer::sine_wave(440.0, 1.0, 44100, 0.5);
        let processed = effect.process_audio(&audio).await.unwrap();

        // Should have applied 2x gain - check that magnitude has doubled
        assert!(processed.samples()[100].abs() > audio.samples()[100].abs());
    }

    #[test]
    fn test_plugin_metadata() {
        let effect = TestAudioEffect::new();
        let metadata = effect.metadata();

        assert_eq!(metadata.name, "TestEffect");
        assert_eq!(metadata.version, "1.0.0");
        assert_eq!(metadata.author, "VoiRS Team");
        assert_eq!(metadata.plugin_type, PluginType::Effect);
    }
}