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
use axum::{
    extract::{
        ws::{Message, WebSocket, WebSocketUpgrade},
        Extension, Query,
    },
    response::Response,
    routing::get,
    Router,
};
use futures::{sink::SinkExt, stream::StreamExt};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc, time::Instant};
use tokio::sync::{mpsc, RwLock};
use uuid::Uuid;

use super::SharedPipeline;
use crate::{
    config::PipelineConfig,
    streaming::{StreamingConfig, StreamingState},
    types::{LanguageCode, QualityLevel, VoiceConfig},
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebSocketConfig {
    pub session_id: String,
    pub voice_id: Option<String>,
    pub language: Option<LanguageCode>,
    pub quality: Option<QualityLevel>,
    pub streaming_config: StreamingConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ClientMessage {
    Connect {
        config: WebSocketConfig,
    },
    StreamText {
        text: String,
        sequence_id: Option<u64>,
    },
    Configure {
        config: Box<PipelineConfig>,
    },
    SwitchVoice {
        voice_id: String,
    },
    GetVoices,
    GetStatus,
    Ping,
    Disconnect,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ServerMessage {
    Connected {
        session_id: String,
        capabilities: HashMap<String, serde_json::Value>,
    },
    AudioChunk {
        data: Vec<u8>,
        sequence_id: Option<u64>,
        metadata: AudioChunkMetadata,
    },
    TextProcessed {
        text: String,
        sequence_id: Option<u64>,
        processing_time: f64,
    },
    StreamingComplete {
        total_chunks: usize,
        total_duration: f64,
        quality_metrics: QualityMetrics,
    },
    VoicesList {
        voices: Vec<VoiceConfig>,
    },
    VoiceSwitched {
        voice_id: String,
    },
    Status {
        streaming_active: bool,
        current_voice: Option<String>,
        queue_size: usize,
        performance: PerformanceMetrics,
    },
    Error {
        message: String,
        code: u16,
    },
    Pong,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AudioChunkMetadata {
    pub sample_rate: u32,
    pub channels: u16,
    pub duration: f64,
    pub format: String,
    pub chunk_index: usize,
    pub timestamp: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityMetrics {
    pub latency_ms: f64,
    pub throughput_mbps: f64,
    pub quality_score: f32,
    pub compression_ratio: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    pub cpu_usage: f32,
    pub memory_usage: f64,
    pub processing_latency: f64,
    pub queue_latency: f64,
}

#[derive(Debug, Deserialize)]
pub struct WebSocketQuery {
    pub session_id: Option<String>,
    pub voice_id: Option<String>,
    pub language: Option<LanguageCode>,
}

pub struct WebSocketSession {
    pub id: String,
    pub config: WebSocketConfig,
    pub sender: mpsc::UnboundedSender<ServerMessage>,
    pub pipeline: SharedPipeline,
    pub streaming_state: Arc<RwLock<StreamingState>>,
    pub start_time: Instant,
}

pub type SessionMap = Arc<RwLock<HashMap<String, WebSocketSession>>>;

pub fn create_websocket_routes() -> Router {
    Router::new().route("/ws", get(websocket_handler))
}

pub async fn websocket_handler(
    ws: WebSocketUpgrade,
    Extension(pipeline): Extension<SharedPipeline>,
    Query(query): Query<WebSocketQuery>,
) -> Response {
    let session_id = query
        .session_id
        .clone()
        .unwrap_or_else(|| Uuid::new_v4().to_string());

    ws.on_upgrade(move |socket| handle_socket(socket, pipeline, session_id, query))
}

async fn handle_socket(
    socket: WebSocket,
    pipeline: SharedPipeline,
    session_id: String,
    query: WebSocketQuery,
) {
    let (mut sender, mut receiver) = socket.split();
    let (tx, mut rx) = mpsc::unbounded_channel::<ServerMessage>();

    let session = WebSocketSession {
        id: session_id.clone(),
        config: WebSocketConfig {
            session_id: session_id.clone(),
            voice_id: query.voice_id,
            language: query.language,
            quality: None,
            streaming_config: StreamingConfig::default(),
        },
        sender: tx,
        pipeline: pipeline.clone(),
        streaming_state: Arc::new(RwLock::new(StreamingState::default())),
        start_time: Instant::now(),
    };

    // Send initial connection message
    let capabilities = get_capabilities();
    let _ = session.sender.send(ServerMessage::Connected {
        session_id: session_id.clone(),
        capabilities,
    });

    // Spawn task to handle outgoing messages
    let mut send_task = tokio::spawn(async move {
        while let Some(msg) = rx.recv().await {
            match serde_json::to_string(&msg) {
                Ok(json) => {
                    if sender.send(Message::Text(json.into())).await.is_err() {
                        break;
                    }
                }
                Err(e) => {
                    tracing::error!("Failed to serialize message: {}", e);
                    break;
                }
            }
        }
    });

    // Spawn task to handle incoming messages
    let session_arc = Arc::new(RwLock::new(session));
    let session_clone = session_arc.clone();

    let mut recv_task = tokio::spawn(async move {
        while let Some(msg) = receiver.next().await {
            match msg {
                Ok(Message::Text(text)) => {
                    if let Err(e) =
                        handle_client_message(text.to_string(), session_clone.clone()).await
                    {
                        tracing::error!("Error handling message: {}", e);
                        break;
                    }
                }
                Ok(Message::Binary(data)) => {
                    tracing::debug!("Received binary data: {} bytes", data.len());
                }
                Ok(Message::Close(_)) => {
                    tracing::info!("WebSocket connection closed");
                    break;
                }
                Err(e) => {
                    tracing::error!("WebSocket error: {}", e);
                    break;
                }
                _ => {}
            }
        }
    });

    // Wait for either task to finish
    tokio::select! {
        _ = (&mut send_task) => {
            recv_task.abort();
        }
        _ = (&mut recv_task) => {
            send_task.abort();
        }
    }

    tracing::info!("WebSocket connection ended for session: {}", session_id);
}

async fn handle_client_message(
    text: String,
    session: Arc<RwLock<WebSocketSession>>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let message: ClientMessage = serde_json::from_str(&text)?;

    match message {
        ClientMessage::Connect { config } => {
            let mut session_guard = session.write().await;
            session_guard.config = config;

            // Initialize streaming if configured
            if let Some(voice_id) = &session_guard.config.voice_id {
                let pipeline = session_guard.pipeline.read().await;
                if let Err(e) = pipeline.set_voice(voice_id).await {
                    let _ = session_guard.sender.send(ServerMessage::Error {
                        message: format!("Failed to switch voice: {e}"),
                        code: 400,
                    });
                }
            }
        }

        ClientMessage::StreamText { text, sequence_id } => {
            let session_guard = session.read().await;
            let pipeline = session_guard.pipeline.read().await;

            let start_time = Instant::now();

            match pipeline.synthesize(&text).await {
                Ok(audio_buffer) => {
                    let processing_time = start_time.elapsed().as_secs_f64();

                    // Send text processed confirmation
                    let _ = session_guard.sender.send(ServerMessage::TextProcessed {
                        text: text.clone(),
                        sequence_id,
                        processing_time,
                    });

                    // Send audio as single chunk
                    let chunk = &audio_buffer;
                    let index = 0;
                    let metadata = AudioChunkMetadata {
                        sample_rate: chunk.sample_rate(),
                        channels: chunk.channels() as u16,
                        duration: chunk.duration() as f64,
                        format: "wav".to_string(),
                        chunk_index: index,
                        timestamp: start_time.elapsed().as_secs_f64(),
                    };

                    let audio_data = chunk.to_wav_bytes().unwrap_or_else(|_| vec![]);
                    let _ = session_guard.sender.send(ServerMessage::AudioChunk {
                        data: audio_data,
                        sequence_id,
                        metadata,
                    });

                    // Send completion message
                    let quality_metrics = QualityMetrics {
                        latency_ms: processing_time * 1000.0,
                        throughput_mbps: 0.0, // Calculate actual throughput
                        quality_score: 0.95,
                        compression_ratio: 0.8,
                    };

                    let _ = session_guard.sender.send(ServerMessage::StreamingComplete {
                        total_chunks: 1,
                        total_duration: audio_buffer.duration() as f64,
                        quality_metrics,
                    });
                }
                Err(e) => {
                    let _ = session_guard.sender.send(ServerMessage::Error {
                        message: format!("Streaming failed: {e}"),
                        code: 500,
                    });
                }
            }
        }

        ClientMessage::Configure { config } => {
            let session_guard = session.read().await;
            let pipeline = session_guard.pipeline.write().await;

            match pipeline.update_config(*config).await {
                Ok(()) => {
                    // Configuration updated successfully
                }
                Err(e) => {
                    let _ = session_guard.sender.send(ServerMessage::Error {
                        message: format!("Configuration update failed: {e}"),
                        code: 400,
                    });
                }
            }
        }

        ClientMessage::SwitchVoice { voice_id } => {
            let session_guard = session.read().await;
            let pipeline = session_guard.pipeline.write().await;

            match pipeline.set_voice(&voice_id).await {
                Ok(()) => {
                    let _ = session_guard
                        .sender
                        .send(ServerMessage::VoiceSwitched { voice_id });
                }
                Err(e) => {
                    let _ = session_guard.sender.send(ServerMessage::Error {
                        message: format!("Voice switch failed: {e}"),
                        code: 400,
                    });
                }
            }
        }

        ClientMessage::GetVoices => {
            let session_guard = session.read().await;
            let pipeline = session_guard.pipeline.read().await;

            match pipeline.list_voices().await {
                Ok(voices) => {
                    let _ = session_guard
                        .sender
                        .send(ServerMessage::VoicesList { voices });
                }
                Err(e) => {
                    let _ = session_guard.sender.send(ServerMessage::Error {
                        message: format!("Failed to get voices: {e}"),
                        code: 500,
                    });
                }
            }
        }

        ClientMessage::GetStatus => {
            let session_guard = session.read().await;
            let streaming_state = session_guard.streaming_state.read().await;

            let performance = PerformanceMetrics {
                cpu_usage: 0.0,    // Would need actual CPU monitoring
                memory_usage: 0.0, // Would need actual memory monitoring
                processing_latency: 0.0,
                queue_latency: 0.0,
            };

            let _ = session_guard.sender.send(ServerMessage::Status {
                streaming_active: streaming_state.is_realtime(),
                current_voice: None, // Would get from pipeline
                queue_size: 0,
                performance,
            });
        }

        ClientMessage::Ping => {
            let session_guard = session.read().await;
            let _ = session_guard.sender.send(ServerMessage::Pong);
        }

        ClientMessage::Disconnect => {
            // Client requesting disconnection
            tracing::info!("Client requested disconnection");
        }
    }

    Ok(())
}

fn get_capabilities() -> HashMap<String, serde_json::Value> {
    let mut capabilities = HashMap::new();

    capabilities.insert("streaming".to_string(), serde_json::json!(true));
    capabilities.insert("voice_switching".to_string(), serde_json::json!(true));
    capabilities.insert("real_time".to_string(), serde_json::json!(true));
    capabilities.insert(
        "audio_formats".to_string(),
        serde_json::json!(["wav", "raw"]),
    );
    capabilities.insert("max_text_length".to_string(), serde_json::json!(10000));
    capabilities.insert("max_chunk_size".to_string(), serde_json::json!(1024));
    capabilities.insert(
        "languages".to_string(),
        serde_json::json!(["en", "es", "fr", "de", "it", "pt", "ru", "zh", "ja", "ko"]),
    );

    capabilities
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::VoirsPipelineBuilder;
    use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};

    #[tokio::test]
    async fn test_websocket_capabilities() {
        let capabilities = get_capabilities();
        assert!(capabilities.contains_key("streaming"));
        assert!(capabilities.contains_key("voice_switching"));
        assert!(capabilities.contains_key("real_time"));
    }

    #[tokio::test]
    async fn test_client_message_serialization() {
        let msg = ClientMessage::Connect {
            config: WebSocketConfig {
                session_id: "test-session".to_string(),
                voice_id: Some("test-voice".to_string()),
                language: Some(LanguageCode::EnUs),
                quality: Some(QualityLevel::High),
                streaming_config: StreamingConfig::default(),
            },
        };

        let json = serde_json::to_string(&msg).unwrap();
        let parsed: ClientMessage = serde_json::from_str(&json).unwrap();

        match parsed {
            ClientMessage::Connect { config } => {
                assert_eq!(config.session_id, "test-session");
                assert_eq!(config.voice_id, Some("test-voice".to_string()));
            }
            _ => panic!("Wrong message type"),
        }
    }

    #[tokio::test]
    async fn test_server_message_serialization() {
        let msg = ServerMessage::Connected {
            session_id: "test-session".to_string(),
            capabilities: get_capabilities(),
        };

        let json = serde_json::to_string(&msg).unwrap();
        let parsed: ServerMessage = serde_json::from_str(&json).unwrap();

        match parsed {
            ServerMessage::Connected {
                session_id,
                capabilities,
            } => {
                assert_eq!(session_id, "test-session");
                assert!(!capabilities.is_empty());
            }
            _ => panic!("Wrong message type"),
        }
    }
}