Skip to main content

Embedder

Trait Embedder 

Source
pub trait Embedder: Send + Sync {
    // Required methods
    fn dim(&self) -> usize;
    fn embed(&self, audio: &[f32]) -> Result<Vec<f32>, EmbedderError>;

    // Provided method
    fn embed_batch(
        &self,
        audios: &[&[f32]],
    ) -> Result<Vec<Vec<f32>>, EmbedderError> { ... }
}
Expand description

Speaker embedding extractor — turns a slice of 16 kHz mono audio into a fixed-dimension embedding vector. Implementations are expected to L2-normalize their output so cosine similarity is a meaningful metric downstream.

This is the supported library injection API for crate::pipeline::LegacyPipeline and crate::streaming::StreamingPipeline. Implement it on an external encoder (Candle, tract, custom) without enabling onnx:

use polyvoice::{Embedder, EmbedderError};

struct ConstantEmbedder { dim: usize }

impl Embedder for ConstantEmbedder {
    fn dim(&self) -> usize { self.dim }
    fn embed(&self, _audio: &[f32]) -> Result<Vec<f32>, EmbedderError> {
        let mut v = vec![0.0f32; self.dim];
        if let Some(first) = v.first_mut() { *first = 1.0; }
        Ok(v)
    }
}

Required Methods§

Source

fn dim(&self) -> usize

Output dimension of this embedder. Constant per instance.

Source

fn embed(&self, audio: &[f32]) -> Result<Vec<f32>, EmbedderError>

Compute an embedding for one audio segment.

Requires: audio is 16 kHz mono PCM. Guarantees on Ok: result.len() == self.dim() and the vector is L2-normalized (|sum(x²)¹ᐟ² − 1.0| < 1e-3).

Provided Methods§

Source

fn embed_batch(&self, audios: &[&[f32]]) -> Result<Vec<Vec<f32>>, EmbedderError>

Compute embeddings for a batch of audio segments. Default implementation is sequential; impls may override with a true batched ONNX call.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§