aprender-core 0.30.0

Next-generation machine learning library in pure Rust
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
//! Speaker embedding extraction.
//!
//! Provides speaker embedding models and utilities:
//! - X-vector embeddings (Snyder et al., 2018)
//! - ECAPA-TDNN embeddings (Desplanques et al., 2020)
//! - Embedding comparison and clustering support
//!
//! # Architecture
//!
//! ```text
//! Audio Frames → TDNN → Statistics Pooling → FC → Embedding
//!//!                  Mean + Std aggregation
//! ```
//!
//! # Example
//!
//! ```rust
//! use aprender::voice::embedding::{EmbeddingConfig, SpeakerEmbedding, cosine_similarity};
//!
//! let emb1 = SpeakerEmbedding::from_vec(vec![1.0, 0.0, 0.0]);
//! let emb2 = SpeakerEmbedding::from_vec(vec![0.0, 1.0, 0.0]);
//! let sim = cosine_similarity(&emb1, &emb2);
//! assert!((sim - 0.0).abs() < 1e-6); // Orthogonal vectors
//! ```
//!
//! # References
//!
//! - Snyder, D., et al. (2018). X-Vectors: Robust DNN Embeddings for Speaker Recognition.
//! - Desplanques, B., et al. (2020). ECAPA-TDNN: Emphasized Channel Attention.
//!
//! # PMAT Compliance
//!
//! - Zero `unwrap()` calls
//! - All public APIs return `Result<T, E>` where fallible

use super::{VoiceError, VoiceResult};

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for speaker embedding extraction
#[derive(Debug, Clone)]
pub struct EmbeddingConfig {
    /// Output embedding dimension (typically 192 or 512)
    pub embedding_dim: usize,
    /// Input sample rate (Hz)
    pub sample_rate: u32,
    /// Frame length in milliseconds
    pub frame_length_ms: u32,
    /// Frame shift in milliseconds
    pub frame_shift_ms: u32,
    /// Number of mel filterbank channels
    pub n_mels: usize,
    /// Normalize embeddings to unit length
    pub normalize: bool,
}

impl Default for EmbeddingConfig {
    fn default() -> Self {
        Self {
            embedding_dim: 192,
            sample_rate: 16000,
            frame_length_ms: 25,
            frame_shift_ms: 10,
            n_mels: 80,
            normalize: true,
        }
    }
}

impl EmbeddingConfig {
    /// Configuration for ECAPA-TDNN model
    #[must_use]
    pub fn ecapa_tdnn() -> Self {
        Self {
            embedding_dim: 192,
            n_mels: 80,
            ..Self::default()
        }
    }

    /// Configuration for X-vector model
    #[must_use]
    pub fn x_vector() -> Self {
        Self {
            embedding_dim: 512,
            n_mels: 30,
            ..Self::default()
        }
    }

    /// Configuration for ResNet-based embeddings
    #[must_use]
    pub fn resnet() -> Self {
        Self {
            embedding_dim: 256,
            n_mels: 64,
            ..Self::default()
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> VoiceResult<()> {
        if self.embedding_dim == 0 {
            return Err(VoiceError::InvalidConfig(
                "embedding_dim must be > 0".to_string(),
            ));
        }
        if self.sample_rate == 0 {
            return Err(VoiceError::InvalidConfig(
                "sample_rate must be > 0".to_string(),
            ));
        }
        if self.frame_length_ms == 0 {
            return Err(VoiceError::InvalidConfig(
                "frame_length_ms must be > 0".to_string(),
            ));
        }
        if self.n_mels == 0 {
            return Err(VoiceError::InvalidConfig("n_mels must be > 0".to_string()));
        }
        Ok(())
    }
}

// ============================================================================
// Speaker Embedding
// ============================================================================

/// A speaker embedding vector.
///
/// Represents a fixed-dimensional representation of a speaker's voice
/// characteristics, suitable for comparison and clustering.
#[derive(Debug, Clone)]
pub struct SpeakerEmbedding {
    /// The embedding vector
    vector: Vec<f32>,
    /// Whether the embedding is normalized
    normalized: bool,
}

impl SpeakerEmbedding {
    /// Create embedding from vector
    #[must_use]
    pub fn from_vec(vector: Vec<f32>) -> Self {
        Self {
            vector,
            normalized: false,
        }
    }

    /// Create zero embedding of given dimension
    #[must_use]
    pub fn zeros(dim: usize) -> Self {
        Self {
            vector: vec![0.0; dim],
            normalized: false,
        }
    }

    /// Get embedding dimension
    #[must_use]
    pub fn dim(&self) -> usize {
        self.vector.len()
    }

    /// Get embedding vector as slice
    #[must_use]
    pub fn as_slice(&self) -> &[f32] {
        &self.vector
    }

    /// Get mutable embedding vector
    #[must_use]
    pub fn as_mut_slice(&mut self) -> &mut [f32] {
        &mut self.vector
    }

    /// Convert to owned vector
    #[must_use]
    pub fn into_vec(self) -> Vec<f32> {
        self.vector
    }

    /// Check if embedding is normalized
    #[must_use]
    pub fn is_normalized(&self) -> bool {
        self.normalized
    }

    /// Normalize embedding to unit length (in-place)
    pub fn normalize(&mut self) {
        let norm = self.l2_norm();
        if norm > f32::EPSILON {
            for x in &mut self.vector {
                *x /= norm;
            }
        }
        self.normalized = true;
    }

    /// Get L2 norm of embedding
    #[must_use]
    pub fn l2_norm(&self) -> f32 {
        self.vector.iter().map(|x| x * x).sum::<f32>().sqrt()
    }

    /// Compute dot product with another embedding
    ///
    /// # Errors
    /// Returns error if dimensions don't match.
    pub fn dot(&self, other: &Self) -> VoiceResult<f32> {
        if self.dim() != other.dim() {
            return Err(VoiceError::DimensionMismatch {
                expected: self.dim(),
                got: other.dim(),
            });
        }
        Ok(self
            .vector
            .iter()
            .zip(other.vector.iter())
            .map(|(a, b)| a * b)
            .sum())
    }

    /// Compute Euclidean distance to another embedding
    ///
    /// # Errors
    /// Returns error if dimensions don't match.
    /// ONE PATH: Core computation delegates to `nn::functional::euclidean_distance` (UCBD §4).
    pub fn euclidean_distance(&self, other: &Self) -> VoiceResult<f32> {
        if self.dim() != other.dim() {
            return Err(VoiceError::DimensionMismatch {
                expected: self.dim(),
                got: other.dim(),
            });
        }
        Ok(crate::nn::functional::euclidean_distance(
            &self.vector,
            &other.vector,
        ))
    }
}

// ============================================================================
// Embedding Extractor Trait
// ============================================================================

/// Trait for speaker embedding extraction models.
///
/// Implementations include:
/// - ECAPA-TDNN (best quality)
/// - X-Vector (classic)
/// - ResNet-based (fast)
pub trait EmbeddingExtractor {
    /// Extract speaker embedding from audio samples.
    ///
    /// # Arguments
    /// * `audio` - Audio samples (f32, mono, at configured sample rate)
    ///
    /// # Returns
    /// Speaker embedding vector
    fn extract(&self, audio: &[f32]) -> VoiceResult<SpeakerEmbedding>;

    /// Get the embedding dimension
    fn embedding_dim(&self) -> usize;

    /// Get the expected sample rate
    fn sample_rate(&self) -> u32;
}

// ============================================================================
// Stub Implementations
// ============================================================================

/// ECAPA-TDNN speaker embedding extractor.
///
/// Emphasized Channel Attention, Propagation and Aggregation in TDNN.
/// State-of-the-art for speaker verification (2020+).
///
/// # Note
/// This is a stub - actual implementation requires model weights.
#[derive(Debug)]
pub struct EcapaTdnn {
    config: EmbeddingConfig,
}

impl EcapaTdnn {
    /// Create new ECAPA-TDNN extractor
    ///
    /// # Note
    /// This is a stub - returns `NotImplemented` on `extract()`.
    #[must_use]
    pub fn new(config: EmbeddingConfig) -> Self {
        Self { config }
    }

    /// Create with default ECAPA configuration
    #[must_use]
    pub fn default_config() -> Self {
        Self::new(EmbeddingConfig::ecapa_tdnn())
    }
}

impl EmbeddingExtractor for EcapaTdnn {
    fn extract(&self, audio: &[f32]) -> VoiceResult<SpeakerEmbedding> {
        if audio.is_empty() {
            return Err(VoiceError::InvalidAudio("empty audio".to_string()));
        }
        // Stub: Real implementation requires ECAPA-TDNN model
        Err(VoiceError::NotImplemented(
            "ECAPA-TDNN requires model weights (use from_apr to load)".to_string(),
        ))
    }

    fn embedding_dim(&self) -> usize {
        self.config.embedding_dim
    }

    fn sample_rate(&self) -> u32 {
        self.config.sample_rate
    }
}

/// X-Vector speaker embedding extractor.
///
/// Classic TDNN-based speaker embedding (Snyder et al., 2018).
/// Good balance of quality and speed.
///
/// # Note
/// This is a stub - actual implementation requires model weights.
#[derive(Debug)]
pub struct XVector {
    config: EmbeddingConfig,
}

impl XVector {
    /// Create new X-Vector extractor
    #[must_use]
    pub fn new(config: EmbeddingConfig) -> Self {
        Self { config }
    }

    /// Create with default X-Vector configuration
    #[must_use]
    pub fn default_config() -> Self {
        Self::new(EmbeddingConfig::x_vector())
    }
}

impl EmbeddingExtractor for XVector {
    fn extract(&self, audio: &[f32]) -> VoiceResult<SpeakerEmbedding> {
        if audio.is_empty() {
            return Err(VoiceError::InvalidAudio("empty audio".to_string()));
        }
        // Stub: Real implementation requires X-Vector model
        Err(VoiceError::NotImplemented(
            "X-Vector requires model weights (use from_apr to load)".to_string(),
        ))
    }

    fn embedding_dim(&self) -> usize {
        self.config.embedding_dim
    }

    fn sample_rate(&self) -> u32 {
        self.config.sample_rate
    }
}

// ============================================================================
// Utility Functions
// ============================================================================

/// Compute cosine similarity between two embeddings.
///
/// Returns value in [-1.0, 1.0] where:
/// - 1.0 = identical direction
/// - 0.0 = orthogonal
/// - -1.0 = opposite direction
///
/// For speaker verification, same speaker pairs typically have
/// cosine similarity > 0.7.
///
/// ONE PATH: Delegates to `nn::functional::cosine_similarity_slice` (UCBD §4).
#[must_use]
pub fn cosine_similarity(a: &SpeakerEmbedding, b: &SpeakerEmbedding) -> f32 {
    if a.dim() != b.dim() || a.dim() == 0 {
        return 0.0;
    }
    crate::nn::functional::cosine_similarity_slice(a.as_slice(), b.as_slice())
}

/// Normalize embedding to unit length.
///
/// Returns a new normalized embedding without modifying the original.
#[must_use]
pub fn normalize_embedding(embedding: &SpeakerEmbedding) -> SpeakerEmbedding {
    let mut normalized = embedding.clone();
    normalized.normalize();
    normalized
}

/// Compute average embedding from multiple embeddings.
///
/// Useful for creating speaker templates from multiple utterances.
///
/// # Errors
/// Returns error if embeddings have different dimensions or list is empty.
pub fn average_embeddings(embeddings: &[SpeakerEmbedding]) -> VoiceResult<SpeakerEmbedding> {
    if embeddings.is_empty() {
        return Err(VoiceError::InvalidConfig(
            "cannot average empty list".to_string(),
        ));
    }

    let dim = embeddings[0].dim();
    for emb in embeddings.iter().skip(1) {
        if emb.dim() != dim {
            return Err(VoiceError::DimensionMismatch {
                expected: dim,
                got: emb.dim(),
            });
        }
    }

    let mut avg = vec![0.0_f32; dim];
    let count = embeddings.len() as f32;

    for emb in embeddings {
        for (i, &val) in emb.as_slice().iter().enumerate() {
            avg[i] += val / count;
        }
    }

    Ok(SpeakerEmbedding::from_vec(avg))
}

/// Compute pairwise similarity matrix for embeddings.
///
/// Returns `NxN` matrix where entry `[i][j]` is cosine similarity
/// between embedding `i` and embedding `j`.
#[must_use]
pub fn similarity_matrix(embeddings: &[SpeakerEmbedding]) -> Vec<Vec<f32>> {
    let n = embeddings.len();
    let mut matrix = vec![vec![0.0_f32; n]; n];

    for i in 0..n {
        matrix[i][i] = 1.0; // Self-similarity
        for j in (i + 1)..n {
            let sim = cosine_similarity(&embeddings[i], &embeddings[j]);
            matrix[i][j] = sim;
            matrix[j][i] = sim; // Symmetric
        }
    }

    matrix
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
#[path = "embedding_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "tests_embedding_contract.rs"]
mod tests_embedding_contract;