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
//! Comprehensive batch processing example for VoiRS SDK.
//!
//! This example demonstrates:
//! - Basic batch synthesis with multiple requests
//! - Priority-based processing
//! - Custom speed and pitch parameters per request
//! - Voice switching in batch mode
//! - Progress tracking
//! - Error handling and retry logic
//! - Different scheduling strategies
//! - Performance metrics and statistics
//!
//! ## Usage
//!
//! ```bash
//! cargo run --example batch_processing --features emotion,cloning
//! ```

use std::sync::Arc;
use std::time::Instant;
use voirs_sdk::batch::{
    BatchConfig, BatchProcessor, BatchRequest, ProcessingMetrics, SchedulingStrategy,
};
use voirs_sdk::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize tracing for logging
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .init();

    println!("=== VoiRS SDK - Advanced Batch Processing Example ===\n");

    // Create pipeline with test mode for demonstration
    println!("Creating VoiRS pipeline...");
    let pipeline = Arc::new(
        VoirsPipelineBuilder::new()
            .with_test_mode(true)
            .with_quality(QualityLevel::High)
            .build()
            .await?,
    );
    println!("Pipeline created successfully!\n");

    // Example 1: Basic batch processing
    println!("Example 1: Basic Batch Processing");
    println!("{}", "-".repeat(60));
    basic_batch_processing(&pipeline).await?;
    println!();

    // Example 2: Priority-based processing
    println!("Example 2: Priority-Based Processing");
    println!("{}", "-".repeat(60));
    priority_batch_processing(&pipeline).await?;
    println!();

    // Example 3: Custom speed and pitch parameters
    println!("Example 3: Custom Speed and Pitch Parameters");
    println!("{}", "-".repeat(60));
    custom_parameters_batch(&pipeline).await?;
    println!();

    // Example 4: Voice switching in batch mode
    println!("Example 4: Voice Switching in Batch Mode");
    println!("{}", "-".repeat(60));
    voice_switching_batch(&pipeline).await?;
    println!();

    // Example 5: Different scheduling strategies
    println!("Example 5: Scheduling Strategy Comparison");
    println!("{}", "-".repeat(60));
    scheduling_strategies_comparison(&pipeline).await?;
    println!();

    // Example 6: Progress tracking
    println!("Example 6: Real-Time Progress Tracking");
    println!("{}", "-".repeat(60));
    progress_tracking_batch(&pipeline).await?;
    println!();

    // Example 7: Large-scale batch processing
    println!("Example 7: Large-Scale Batch Processing");
    println!("{}", "-".repeat(60));
    large_scale_batch(&pipeline).await?;
    println!();

    println!("All batch processing examples completed successfully!");

    Ok(())
}

/// Example 1: Basic batch processing with multiple requests
async fn basic_batch_processing(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Processing a batch of simple text requests...");

    // Create batch configuration
    let config = BatchConfig::default();

    // Create batch processor
    let processor = BatchProcessor::new(Arc::clone(pipeline), config);

    // Create batch requests
    let requests = vec![
        BatchRequest::new("Hello, world!", None),
        BatchRequest::new("How are you today?", None),
        BatchRequest::new("This is a batch processing demonstration.", None),
        BatchRequest::new("VoiRS SDK makes speech synthesis easy!", None),
        BatchRequest::new("Goodbye and have a great day!", None),
    ];

    let start = Instant::now();

    // Process the batch
    let results = processor.process(requests).await?;

    let duration = start.elapsed();

    // Display results
    println!("\nResults:");
    println!("  Total requests: {}", results.len());
    println!(
        "  Successful: {}",
        results.iter().filter(|r| r.is_success()).count()
    );
    println!(
        "  Failed: {}",
        results.iter().filter(|r| !r.is_success()).count()
    );
    println!("  Total time: {:.2}s", duration.as_secs_f64());
    println!(
        "  Average time per request: {:.2}ms",
        duration.as_millis() as f64 / results.len() as f64
    );

    // Display individual results
    for (idx, result) in results.iter().enumerate() {
        if let Some(audio) = result.audio() {
            println!(
                "  Request {}: {} samples, {:.2}s audio, processed in {:.2}ms",
                idx + 1,
                audio.len(),
                audio.duration(),
                result.processing_time.as_millis()
            );
        } else if let Some(error) = result.error() {
            println!("  Request {}: Failed - {}", idx + 1, error);
        }
    }

    // Get statistics
    let stats = processor.statistics().await;
    println!("\nStatistics:");
    println!("{}", stats.summary());

    Ok(())
}

/// Example 2: Priority-based batch processing
async fn priority_batch_processing(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Processing batch with different priorities...");

    let config = BatchConfig {
        scheduling_strategy: SchedulingStrategy::PriorityBased,
        ..Default::default()
    };

    let processor = BatchProcessor::new(Arc::clone(pipeline), config);

    // Create requests with different priorities
    let requests = vec![
        BatchRequest::new("Low priority request", None).with_priority(1),
        BatchRequest::new("Critical urgent request!", None).with_priority(100),
        BatchRequest::new("Normal priority", None).with_priority(50),
        BatchRequest::new("Another low priority", None).with_priority(1),
        BatchRequest::new("High priority request", None).with_priority(75),
    ];

    println!("Request priorities:");
    for (idx, req) in requests.iter().enumerate() {
        println!(
            "  Request {}: priority {} - \"{}\"",
            idx + 1,
            req.priority,
            &req.text[..req.text.len().min(30)]
        );
    }

    let results = processor.process(requests).await?;

    println!("\nProcessing order (by worker assignment):");
    for (idx, result) in results.iter().enumerate() {
        println!(
            "  Processed request {}: {:.2}ms",
            idx + 1,
            result.processing_time.as_millis()
        );
    }

    Ok(())
}

/// Example 3: Custom speed and pitch parameters
async fn custom_parameters_batch(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Processing batch with custom speed and pitch parameters...");

    let config = BatchConfig::default();
    let processor = BatchProcessor::new(Arc::clone(pipeline), config);

    // Create requests with different speed/pitch settings
    let requests = vec![
        BatchRequest::new("Normal speed and pitch", None),
        BatchRequest::new("Speaking very quickly now", None).with_speed(1.5),
        BatchRequest::new("Speaking very slowly now", None).with_speed(0.7),
        BatchRequest::new("Higher pitch voice here", None).with_pitch(3.0),
        BatchRequest::new("Lower pitch voice here", None).with_pitch(-3.0),
        BatchRequest::new("Fast and high pitched!", None)
            .with_speed(1.3)
            .with_pitch(2.0),
        BatchRequest::new("Slow and low pitched", None)
            .with_speed(0.8)
            .with_pitch(-2.0),
    ];

    println!("Request parameters:");
    for (idx, req) in requests.iter().enumerate() {
        print!("  Request {}: ", idx + 1);
        if let Some(speed) = req.speed {
            print!("speed={:.1}x ", speed);
        }
        if let Some(pitch) = req.pitch {
            print!("pitch={:+.1} semitones ", pitch);
        }
        if req.speed.is_none() && req.pitch.is_none() {
            print!("default parameters");
        }
        println!();
    }

    let results = processor.process(requests).await?;

    println!("\nResults:");
    for (idx, result) in results.iter().enumerate() {
        if let Some(audio) = result.audio() {
            println!(
                "  Request {}: {:.2}s audio, {:.2}ms processing",
                idx + 1,
                audio.duration(),
                result.processing_time.as_millis()
            );
        }
    }

    Ok(())
}

/// Example 4: Voice switching in batch mode
async fn voice_switching_batch(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Processing batch with different voices...");

    let config = BatchConfig::default();
    let processor = BatchProcessor::new(Arc::clone(pipeline), config);

    // Create requests with different voices
    let requests = vec![
        BatchRequest::new("Default voice speaking", None),
        BatchRequest::new("Female voice speaking", Some("en-US-female-calm")),
        BatchRequest::new("Back to default voice", None),
        BatchRequest::new("Male voice speaking", Some("en-US-male-energetic")),
        BatchRequest::new("Female voice again", Some("en-US-female-calm")),
    ];

    println!("Voice assignments:");
    for (idx, req) in requests.iter().enumerate() {
        println!(
            "  Request {}: {} - \"{}\"",
            idx + 1,
            req.voice.as_deref().unwrap_or("default"),
            &req.text[..req.text.len().min(30)]
        );
    }

    let results = processor.process(requests).await?;

    println!("\nResults:");
    for (idx, result) in results.iter().enumerate() {
        println!(
            "  Request {}: {} (worker: {})",
            idx + 1,
            if result.is_success() {
                "Success"
            } else {
                "Failed"
            },
            result
                .worker_id
                .map(|id| id.to_string())
                .unwrap_or_else(|| "N/A".to_string())
        );
    }

    Ok(())
}

/// Example 5: Compare different scheduling strategies
async fn scheduling_strategies_comparison(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Comparing different scheduling strategies...\n");

    // Test texts of varying lengths
    let test_requests: Vec<_> = vec![
        ("Short", "Hi!"),
        ("Medium", "This is a medium length sentence for testing."),
        ("Long", "This is a much longer sentence that will take more time to process and synthesize into speech audio."),
        ("Short", "Bye!"),
        ("Medium", "Another medium sentence here for variety."),
    ]
    .into_iter()
    .map(|(len, text)| {
        let mut req = BatchRequest::new(text, None);
        req.priority = if len == "Short" { 10 } else if len == "Medium" { 5 } else { 1 };
        req
    })
    .collect();

    let strategies = vec![
        ("FIFO (First In, First Out)", SchedulingStrategy::FIFO),
        ("Priority-Based", SchedulingStrategy::PriorityBased),
        ("Load Balanced", SchedulingStrategy::LoadBalanced),
        ("Shortest Job First", SchedulingStrategy::ShortestFirst),
        ("Adaptive", SchedulingStrategy::Adaptive),
    ];

    for (name, strategy) in strategies {
        println!("Testing {}", name);

        let config = BatchConfig {
            scheduling_strategy: strategy,
            ..Default::default()
        };

        let processor = BatchProcessor::new(Arc::clone(pipeline), config);
        let start = Instant::now();
        let results = processor.process(test_requests.clone()).await?;
        let duration = start.elapsed();

        let stats = processor.statistics().await;

        println!("  Total time: {:.2}ms", duration.as_millis());
        println!("  Throughput: {:.2} req/s", stats.throughput());
        println!("  Success rate: {:.1}%", stats.success_rate() * 100.0);
        println!();
    }

    Ok(())
}

/// Example 6: Progress tracking
async fn progress_tracking_batch(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Processing batch with real-time progress tracking...\n");

    let config = BatchConfig {
        track_progress: true,
        ..Default::default()
    };

    // Create a larger batch for visible progress
    let requests: Vec<_> = (0..20)
        .map(|i| BatchRequest::new(format!("Request number {}", i + 1), None))
        .collect();

    println!("Starting batch of {} requests...", requests.len());

    // Process with progress callback
    let processor_with_progress = BatchProcessor::with_progress(
        Arc::clone(pipeline),
        config,
        |completed: usize, total: usize| {
            let percent = (completed as f64 / total as f64) * 100.0;
            println!("  Progress: {}/{} ({:.1}%)", completed, total, percent);
        },
    );
    let results = processor_with_progress.process(requests).await?;

    println!("\nBatch completed!");
    println!(
        "  Successful: {}",
        results.iter().filter(|r| r.is_success()).count()
    );
    println!(
        "  Total audio duration: {:.2}s",
        results
            .iter()
            .filter_map(|r| r.audio())
            .map(|a| a.duration() as f64)
            .sum::<f64>()
    );

    Ok(())
}

/// Example 7: Large-scale batch processing
async fn large_scale_batch(pipeline: &Arc<VoirsPipeline>) -> Result<()> {
    println!("Processing large-scale batch (100 requests)...");

    let config = BatchConfig {
        max_concurrency: num_cpus::get() * 2, // Use more workers for large batch
        max_batch_size: 100,
        scheduling_strategy: SchedulingStrategy::LoadBalanced,
        ..Default::default()
    };

    let processor = BatchProcessor::new(Arc::clone(pipeline), config);

    // Create 100 requests with varying complexity
    let requests: Vec<_> = (0..100)
        .map(|i| {
            let text = match i % 3 {
                0 => format!("Short request {}", i),
                1 => format!("This is a medium length request number {} with more text to process", i),
                _ => format!("This is a longer request number {} that contains significantly more text and will require more processing time to synthesize into speech audio", i),
            };
            BatchRequest::new(text, None)
                .with_priority(fastrand::i32(1..100))
        })
        .collect();

    let start = Instant::now();
    let results = processor.process(requests).await?;
    let duration = start.elapsed();

    let stats = processor.statistics().await;

    println!("\nLarge-Scale Batch Results:");
    println!("  Total requests: {}", results.len());
    println!(
        "  Successful: {}",
        results.iter().filter(|r| r.is_success()).count()
    );
    println!(
        "  Failed: {}",
        results.iter().filter(|r| !r.is_success()).count()
    );
    println!("  Total time: {:.2}s", duration.as_secs_f64());
    println!("  Throughput: {:.2} requests/second", stats.throughput());
    println!(
        "  Average processing time: {:.2}ms",
        stats.avg_time_per_request.as_secs_f64() * 1000.0
    );
    println!("  Success rate: {:.1}%", stats.success_rate() * 100.0);

    // Worker metrics
    println!("\nWorker Performance:");
    for (worker_id, metrics) in &stats.worker_metrics {
        println!(
            "  Worker {}: {} requests, {:.2}ms avg time",
            worker_id,
            metrics.requests_processed,
            metrics.total_time.as_secs_f64() * 1000.0 / metrics.requests_processed.max(1) as f64
        );
    }

    // Total audio produced
    let total_audio_duration: f64 = results
        .iter()
        .filter_map(|r| r.audio())
        .map(|a| a.duration() as f64)
        .sum();
    println!(
        "\n  Total audio produced: {:.2} seconds",
        total_audio_duration
    );

    Ok(())
}