Skip to main content

eredu_codec/
lib.rs

1//! Backend-neutral neural audio codec architectures.
2//!
3//! This crate keeps codec implementations optional and separate from
4//! `eredu`. Realtime language models can operate on discrete codec tokens,
5//! while applications that need audio encode/decode can depend on this crate.
6
7#![warn(missing_docs)]
8
9/// Mimi neural audio tokenizer support.
10pub mod mimi;
11
12use eredu_nn::Tensor;
13
14/// Common interface for neural audio tokenizers.
15pub trait AudioTokenizer {
16    /// Backend-native tensor handle.
17    type Tensor: Tensor;
18
19    /// Codec configuration.
20    fn config(&self) -> AudioTokenizerConfig;
21
22    /// Encodes mono PCM shaped `[batch, channels, samples]` into codec tokens.
23    fn encode(
24        &mut self,
25        pcm: &Self::Tensor,
26        context: &<Self::Tensor as Tensor>::Context,
27    ) -> Result<Self::Tensor, Error>;
28
29    /// Decodes codec tokens shaped `[batch, codebooks, frames]` into PCM.
30    fn decode(
31        &mut self,
32        codes: &Self::Tensor,
33        context: &<Self::Tensor as Tensor>::Context,
34    ) -> Result<Self::Tensor, Error>;
35}
36
37/// Static metadata for pairing an audio tokenizer with a realtime model.
38#[derive(Debug, Clone, Copy, PartialEq)]
39pub struct AudioTokenizerConfig {
40    /// Audio sample rate in Hz.
41    pub sample_rate: f64,
42    /// Codec frame rate in Hz.
43    pub frame_rate: f64,
44    /// Number of audio channels supported by the codec.
45    pub channels: i32,
46    /// Number of active codebooks used for encode/decode.
47    pub codebooks: i32,
48    /// Codebook cardinality.
49    pub cardinality: i32,
50}
51
52/// Errors returned by codec construction and tokenization operations.
53#[derive(Debug, thiserror::Error)]
54pub enum Error {
55    /// Invalid input or checkpoint shape.
56    #[error("{0}")]
57    InvalidShape(String),
58
59    /// Underlying neural-compute backend error.
60    #[error(transparent)]
61    Compute(#[from] eredu_nn::Error),
62}