How facebookresearch/encodec works
Purpose and Entry Points
EnCodec is a PyTorch neural audio codec that converts waveforms into discrete residual-vector-quantized codes and reconstructs audio from those codes. It ships pretrained causal 24 kHz mono and non-causal 48 kHz stereo models. Primary entry points are the Python package API in encodec/__init__.py, CLI launcher encodec/__main__.py, and file compression workflow encodec/compress.py.
Model Orchestration
EncodecModel owns codec configuration, pretrained model construction, target-bandwidth selection, chunking/overlap behavior, normalization scales, and encode/decode execution. It is the boundary between waveform-facing callers and learned codec internals.
• encodec/model.py
• encodec/__init__.py
Neural Analysis/Synthesis Path
The codec’s encoder maps audio waveforms to latent frames; its decoder reconstructs waveforms from quantized latents. Both are built from SEANet convolutional blocks and LSTM-based temporal modeling, with normalization and convolution primitives factored into module files.
• encodec/modules/seanet.py
• encodec/modules/lstm.py
• encodec/modules/conv.py
• encodec/modules/norm.py
Discrete Latent Representation
Residual vector quantization transforms continuous encoder latents into ordered codebook indices, with the selected bitrate determining the number of active quantizers/codebooks. Quantization code is separated from the main model so codes can be extracted for downstream language-model or storage workflows.
• encodec/quantization/vq.py
• encodec/quantization/core_vq.py
• encodec/quantization/__init__.py
Compression Container and Entropy Coding
The file-oriented pipeline serializes codec metadata and discrete frames into .ecdc artifacts, then reverses that process for decompression. Optional arithmetic coding uses pretrained language-model probabilities to reduce encoded representation size without changing reconstruction codes.
• encodec/compress.py
• encodec/binary.py
• encodec/quantization/ac.py
CLI and Audio I/O Flow
The CLI accepts an input audio file, chooses 24/48 kHz model and bandwidth, converts sample rate/channel layout to model requirements, encodes it, and either writes .ecdc or immediately decodes to WAV. Decompression reads .ecdc, restores codes/scales, invokes model decoding, and writes audio. Audio loading/resampling relies on torchaudio; model weights are fetched lazily through PyTorch Torch Hub.
• encodec/__main__.py
• encodec/compress.py
• encodec/utils.py
Training-Support Components
Although the repository primarily exposes inference/compression, it includes core training components: a multi-scale STFT discriminator for perceptual adversarial audio training and a balancer for combining reconstruction/adversarial objectives. Distributed gradient and buffer synchronization utilities provide an alternative to direct DDP interaction for RVQ-related training.
• encodec/msstftd.py
• encodec/balancer.py
• encodec/distrib.py
Runtime and External Boundaries
The package targets Python 3.8+ with PyTorch (ideally 1.11+) and torchaudio. Runtime inputs are tensors or torchaudio-supported audio files; outputs are tensors, discrete codes, WAV audio, or the project-specific .ecdc binary container. Pretrained checkpoints are external Torch Hub downloads; Hugging Face Transformers integration is documented but implemented outside this repository.
Main entry points: compress.py Compress / decompress · model.py EncodecModel · seanet.py SEANet encoder / decoder · vq.py Residual vector quantizer
Components 19
Entry & I/O
- Python API · Waveform-facing package API.
encodec/__init__.py - CLI
encodec/__main__.py - Audio I/O · Loads, resamples, and writes audio.
encodec/utils.py
Codec Runtime
- EncodecModel · Selects bandwidth, chunks audio, and runs encode/decode.
encodec/model.py - SEANet encoder / decoder · Convolutional waveform-to-latent and latent-to-waveform networks.
encodec/modules/seanet.py - Temporal modeling
encodec/modules/lstm.py - Convolution & normalization
encodec/modules/conv.py - Normalization layers
encodec/modules/norm.py
Discrete Codes
- Residual vector quantizer · Maps latents to ordered codebook indices; active codebooks follow bitrate.
encodec/quantization/vq.py - Codebook core
encodec/quantization/core_vq.py - Quantization API
encodec/quantization/__init__.py
Compression
- Compress / decompress · Coordinates model codes, metadata, and audio-file conversion.
encodec/compress.py - ECDC container · Serializes codec metadata and discrete frames.
encodec/binary.py - Arithmetic coding · Uses language-model probabilities without changing reconstruction codes.
encodec/quantization/ac.py - .ecdc artifact
Training Support
- Multi-scale STFT discriminator · Perceptual adversarial audio-training component.
encodec/msstftd.py - Loss balancer · Combines reconstruction and adversarial objectives.
encodec/balancer.py - Distributed synchronization · Synchronizes gradients and buffers for RVQ-related training.
encodec/distrib.py
Other components
- Torch Hub checkpoints · Pretrained weights are fetched lazily.
Connections 18
- Python API → EncodecModel: exposes
- CLI → Compress / decompress: runs file workflow
- CLI → Audio I/O: loads input / writes WAV
- Compress / decompress → Audio I/O: converts audio files
- Compress / decompress → EncodecModel: encodes and decodes
- Compress / decompress → ECDC container: reads and writes
- ECDC container → .ecdc artifact: serializes
- Compress / decompress → Arithmetic coding: optionally entropy-codes
- EncodecModel → Torch Hub checkpoints: loads pretrained weights
- EncodecModel → SEANet encoder / decoder: analysis / synthesis
- EncodecModel → Residual vector quantizer: quantizes latents
- SEANet encoder / decoder → Temporal modeling: uses
- SEANet encoder / decoder → Convolution & normalization: uses
- SEANet encoder / decoder → Normalization layers: uses
- Residual vector quantizer → Codebook core: stacks residual codebooks
- Quantization API → Residual vector quantizer: exports
- Loss balancer → Multi-scale STFT discriminator: balances adversarial objective
- Distributed synchronization → Residual vector quantizer: synchronizes training state