Skip to main content

voirs_sdk/
lib.rs

1//! # VoiRS SDK
2//!
3//! Unified SDK and public API for VoiRS speech synthesis framework.
4//!
5//! VoiRS SDK provides a comprehensive, high-level interface for neural speech synthesis,
6//! abstracting the complexity of G2P (Grapheme-to-Phoneme), acoustic modeling, and vocoding
7//! into a simple, efficient API.
8//!
9//! ## Quick Start
10//!
11//! ```no_run
12//! use voirs_sdk::prelude::*;
13//!
14//! #[tokio::main]
15//! async fn main() -> Result<()> {
16//!     // Create a pipeline with default settings
17//!     let pipeline = VoirsPipelineBuilder::new()
18//!         .with_quality(QualityLevel::High)
19//!         .with_voice("default")
20//!         .build()
21//!         .await?;
22//!
23//!     // Synthesize speech
24//!     let audio = pipeline.synthesize("Hello, world!").await?;
25//!     
26//!     // Save to file
27//!     audio.save_wav("output.wav")?;
28//!     
29//!     Ok(())
30//! }
31//! ```
32//!
33//! ## Key Features
34//!
35//! - **Simple API**: High-level interface for speech synthesis
36//! - **Async/Concurrent**: Built for modern async Rust applications
37//! - **Streaming**: Real-time synthesis with low latency
38//! - **Plugin System**: Extensible audio effects and processing
39//! - **Caching**: Intelligent model and result caching
40//! - **Quality Control**: Comprehensive audio quality validation
41//! - **Performance**: Optimized for both speed and memory efficiency
42//!
43//! ## Architecture
44//!
45//! The VoiRS SDK consists of several key components:
46//!
47//! - [`VoirsPipeline`]: Main synthesis pipeline
48//! - [`VoirsPipelineBuilder`]: Fluent API for pipeline configuration
49//! - [`AudioBuffer`]: Audio data management and processing
50//! - [`streaming`]: Real-time synthesis capabilities
51//! - [`plugins`]: Extensible effects system
52//! - [`cache`]: Intelligent caching system
53//!
54//! ## Examples
55//!
56//! ### Basic Synthesis
57//!
58//! ```no_run
59//! use voirs_sdk::prelude::*;
60//!
61//! #[tokio::main]
62//! async fn main() -> Result<()> {
63//!     let pipeline = VoirsPipelineBuilder::new().build().await?;
64//!     let audio = pipeline.synthesize("Hello, world!").await?;
65//!     audio.save_wav("hello.wav")?;
66//!     Ok(())
67//! }
68//! ```
69//!
70//! ### Streaming Synthesis
71//!
72//! ```no_run
73//! use voirs_sdk::prelude::*;
74//! use futures::StreamExt;
75//!
76//! #[tokio::main]
77//! async fn main() -> Result<()> {
78//!     let pipeline = Arc::new(VoirsPipelineBuilder::new().build().await?);
79//!     
80//!     let mut stream = pipeline.synthesize_stream(
81//!         "This is a longer text that will be synthesized in real-time."
82//!     ).await?;
83//!     
84//!     while let Some(chunk) = stream.next().await {
85//!         let audio_chunk = chunk?;
86//!         // Process audio chunk in real-time
87//!         println!("Received {} samples", audio_chunk.len());
88//!     }
89//!     
90//!     Ok(())
91//! }
92//! ```
93//!
94//! ### Voice Management
95//!
96//! ```no_run
97//! use voirs_sdk::prelude::*;
98//!
99//! #[tokio::main]
100//! async fn main() -> Result<()> {
101//!     let pipeline = VoirsPipelineBuilder::new()
102//!         .with_voice("female_voice")
103//!         .build()
104//!         .await?;
105//!     
106//!     // List available voices
107//!     let voices = pipeline.list_voices().await?;
108//!     for voice in voices {
109//!         println!("Available voice: {} ({})", voice.name, voice.language);
110//!     }
111//!     
112//!     // Switch voice at runtime
113//!     pipeline.set_voice("male_voice").await?;
114//!     let audio = pipeline.synthesize("Speaking with a different voice").await?;
115//!     
116//!     Ok(())
117//! }
118//! ```
119//!
120//! ### Advanced Configuration
121//!
122//! ```no_run
123//! use voirs_sdk::prelude::*;
124//!
125//! #[tokio::main]
126//! async fn main() -> Result<()> {
127//!     let pipeline = VoirsPipelineBuilder::new()
128//!         .with_quality(QualityLevel::High)
129//!         .with_gpu_acceleration(true)
130//!         .with_threads(4)
131//!         .build()
132//!         .await?;
133//!     
134//!     let audio = pipeline.synthesize("High quality synthesis!").await?;
135//!     audio.save_wav("quality_output.wav")?;
136//!     
137//!     Ok(())
138//! }
139//! ```
140//!
141//! ### Configuration and Quality Control
142//!
143//! ```no_run
144//! use voirs_sdk::prelude::*;
145//! use std::path::PathBuf;
146//!
147//! #[tokio::main]
148//! async fn main() -> Result<()> {
149//!     let pipeline = VoirsPipelineBuilder::new()
150//!         .with_quality(QualityLevel::High)
151//!         .with_threads(4)
152//!         .with_cache_dir(PathBuf::from("/tmp/voirs-cache"))
153//!         .build()
154//!         .await?;
155//!     
156//!     let audio = pipeline.synthesize("High quality synthesis").await?;
157//!     
158//!     // Access audio properties
159//!     println!("Sample rate: {} Hz", audio.sample_rate());
160//!     println!("Duration: {:.2} seconds", audio.duration());
161//!     
162//!     Ok(())
163//! }
164//! ```
165//!
166//! ## Performance
167//!
168//! The VoiRS SDK is designed for high performance:
169//!
170//! - **Initialization**: ≤ 2 seconds (cold start with model download)
171//! - **Synthesis Latency**: ≤ 100ms overhead per synthesis
172//! - **Memory Usage**: ≤ 50MB SDK overhead
173//! - **Real-time Factor**: ≤ 0.5 (synthesis faster than playback)
174//! - **Concurrent Operations**: 100+ simultaneous operations supported
175//!
176//! ## Error Handling
177//!
178//! All operations return [`Result<T, VoirsError>`](VoirsError) for comprehensive error handling:
179//!
180//! ```no_run
181//! use voirs_sdk::prelude::*;
182//!
183//! #[tokio::main]
184//! async fn main() {
185//!     match VoirsPipelineBuilder::new().build().await {
186//!         Ok(pipeline) => {
187//!             match pipeline.synthesize("Hello!").await {
188//!                 Ok(audio) => println!("Success! {} samples", audio.len()),
189//!                 Err(e) => eprintln!("Synthesis error: {}", e),
190//!             }
191//!         }
192//!         Err(e) => eprintln!("Pipeline creation error: {}", e),
193//!     }
194//! }
195//! ```
196//!
197//! ## Feature Flags
198//!
199//! - `gpu`: Enable GPU acceleration for models
200//! - `onnx`: Enable ONNX runtime support
201//! - `default`: Standard CPU-based processing
202//!
203//! ## Platform Support
204//!
205//! - **Operating Systems**: Linux, macOS, Windows
206//! - **Architectures**: x86_64, ARM64
207//! - **Runtimes**: Tokio async runtime required
208
209// Allow pedantic lints that are acceptable for audio/DSP processing code
210#![allow(clippy::cast_precision_loss)] // Acceptable for audio sample conversions
211#![allow(clippy::cast_possible_truncation)] // Controlled truncation in audio processing
212#![allow(clippy::cast_sign_loss)] // Intentional in index calculations
213#![allow(clippy::missing_errors_doc)] // Many internal functions with self-documenting error types
214#![allow(clippy::missing_panics_doc)] // Panics are documented where relevant
215#![allow(clippy::unused_self)] // Some trait implementations require &self for consistency
216#![allow(clippy::must_use_candidate)] // Not all return values need must_use annotation
217#![allow(clippy::doc_markdown)] // Technical terms don't all need backticks
218#![allow(clippy::unnecessary_wraps)] // Result wrappers maintained for API consistency
219#![allow(clippy::float_cmp)] // Exact float comparisons are intentional in some contexts
220#![allow(clippy::match_same_arms)] // Pattern matching clarity sometimes requires duplication
221#![allow(clippy::module_name_repetitions)] // Type names often repeat module names
222#![allow(clippy::struct_excessive_bools)] // Config structs naturally have many boolean flags
223#![allow(clippy::too_many_lines)] // Some functions are inherently complex
224#![allow(clippy::needless_pass_by_value)] // Some functions designed for ownership transfer
225#![allow(clippy::similar_names)] // Many similar variable names in algorithms
226#![allow(clippy::unused_async)] // Public API functions may need async for consistency
227#![allow(clippy::needless_range_loop)] // Range loops sometimes clearer than iterators
228#![allow(clippy::uninlined_format_args)] // Explicit argument names can improve clarity
229#![allow(clippy::manual_clamp)] // Manual clamping sometimes clearer
230#![allow(clippy::return_self_not_must_use)] // Not all builder methods need must_use
231#![allow(clippy::cast_possible_wrap)] // Controlled wrapping in processing code
232#![allow(clippy::cast_lossless)] // Explicit casts preferred for clarity
233#![allow(clippy::wildcard_imports)] // Prelude imports are convenient and standard
234#![allow(clippy::format_push_string)] // Sometimes more readable than alternative
235#![allow(clippy::redundant_closure_for_method_calls)] // Closures sometimes needed for type inference
236#![allow(clippy::too_many_arguments)] // Some functions naturally need many parameters
237#![allow(clippy::field_reassign_with_default)] // Sometimes clearer than builder pattern
238#![allow(clippy::trivially_copy_pass_by_ref)] // API consistency more important
239#![allow(clippy::await_holding_lock)] // Controlled lock holding in async contexts
240
241pub mod adapters;
242pub mod adaptive;
243pub mod r#async;
244pub mod audio;
245pub mod batch;
246pub mod builder;
247pub mod cache;
248pub mod capabilities;
249pub mod config;
250pub mod diagnostics;
251pub mod error;
252pub mod logging;
253pub mod memory;
254pub mod model_runtime;
255pub mod performance;
256pub mod pipeline;
257pub mod plugins;
258pub mod prelude;
259pub mod profiling;
260pub mod streaming;
261pub mod traits;
262pub mod types;
263pub mod validation;
264pub mod versioning;
265pub mod voice;
266
267// Advanced voice features
268#[cfg(feature = "cloning")]
269pub mod cloning;
270#[cfg(feature = "conversion")]
271pub mod conversion;
272#[cfg(feature = "emotion")]
273pub mod emotion;
274#[cfg(feature = "singing")]
275pub mod singing;
276#[cfg(feature = "spatial")]
277pub mod spatial;
278
279// Web Integration modules
280#[cfg(feature = "http")]
281pub mod http;
282#[cfg(feature = "wasm")]
283pub mod wasm;
284
285// Cloud Integration modules
286#[cfg(feature = "cloud")]
287pub mod cloud;
288
289// Re-export core types and traits
290pub use adaptive::{AdaptiveConfig, AdaptiveController, QualityTarget};
291pub use audio::AudioBuffer;
292pub use builder::VoirsPipelineBuilder;
293pub use capabilities::CapabilityManager;
294pub use diagnostics::{ProductionReadiness, ReadinessConfig, ReadinessReport};
295pub use error::VoirsError;
296pub use performance::PerformanceMonitor;
297pub use pipeline::VoirsPipeline;
298pub use traits::{AcousticModel, G2p, Vocoder};
299pub use types::*;
300
301// Advanced voice features re-exports
302#[cfg(feature = "cloning")]
303pub use cloning::{CloningConfig, VoiceCloner};
304#[cfg(feature = "conversion")]
305pub use conversion::{ConversionConfig, VoiceConverter};
306#[cfg(feature = "emotion")]
307pub use emotion::{EmotionConfig, EmotionController};
308#[cfg(feature = "singing")]
309pub use singing::{SingingConfig, SingingController};
310#[cfg(feature = "spatial")]
311pub use spatial::{SpatialAudioConfig, SpatialAudioController};
312
313/// Result type alias for VoiRS operations
314pub type Result<T> = std::result::Result<T, VoirsError>;
315
316#[cfg(test)]
317mod tests {
318    use super::*;
319
320    #[test]
321    fn test_basic_types() {
322        // Basic compilation test
323        let _result: Result<()> = Ok(());
324    }
325}