NeuCodec is a 0.8kbps neural audio codec designed for speech tokenization. It uses Finite Scalar Quantisation (FSQ) to produce a single vector for quantized output, making it ideal for Speech Language Models.
Key technical specifications:
- Input: 16kHz audio.
- Output: 24kHz reconstructed audio (via an upsampling decoder).
- Bit-rate: 0.8kbps (at 50 tokens/sec and 16 bits per token).
- Architecture: Uses both audio (BigCodec) and semantic (Wav2Vec2-BERT) encoders.
To use the codec, load the model using NeuCodec.from_pretrained, ensure your input audio is resampled to 16kHz, and use encode_code to generate FSQ codes and decode_code to reconstruct the audio.
import librosa
import torch
import torchaudio
from torchaudio import transforms as T
from neucodec import NeuCodec
model = NeuCodec.from_pretrained("neuphonic/neucodec")
model.eval().cuda()
y, sr = torchaudio.load(librosa.ex("libri1"))
if sr != 16_000:
y = T.Resample(sr, 16_000)(y)[None, ...] # (B, 1, T_16)
with torch.no_grad():
fsq_codes = model.encode_code(y)
# fsq_codes = model.encode_code(librosa.ex("libri1")) # or directly pass your filepath!
print(f"Codes shape: {fsq_codes.shape}")
recon = model.decode_code(fsq_codes).cpu() # (B, 1, T_24)
torchaudio.save("reconstructed.wav", recon[0, :, :], 24_000)