zhai-lw/L3AC

A lightweight audio codec based on a single quantizer

Python

36

3 commits

updated Sep 4, 2025

See the code

README

L3AC

This repository contains the implementation of L3AC, a lightweight audio codec based on a single quantizer, introduced in the paper titled "L3AC: Towards a Lightweight and Lossless Audio Codec".

Paper

Model Weights

Training Code

Comparison of various audio codec Comparison of various audio codec

install

pip install l3ac

demo

Firstly, make sure you have installed the librosa package to load the example audio file. You can install it using pip:

pip install librosa

Then, you can use the following code to load a sample audio file, encode it using the L3AC model, and decode it back to audio. The code also calculates the mean squared error (MSE) between the original and generated audio.

import librosa
import torch
import l3ac

all_models = l3ac.list_models()
print(f"Available models: {all_models}")

MODEL_USED = '1kbps'
codec = l3ac.get_model(MODEL_USED)
print(f"loaded codec({MODEL_USED}) and codec sample rate: {codec.config.sample_rate}")

sample_audio, sample_rate = librosa.load(librosa.example("libri1"))
sample_audio = sample_audio[None, :]
print(f"loaded sample audio and audio sample_rate :{sample_rate}")

sample_audio = librosa.resample(sample_audio, orig_sr=sample_rate, target_sr=codec.config.sample_rate)

codec.network.cuda()
codec.network.eval()
with torch.inference_mode():
    audio_in = torch.tensor(sample_audio, dtype=torch.float32, device='cuda')
    _, audio_length = audio_in.shape
    print(f"{audio_in.shape=}")
    q_feature, indices = codec.encode_audio(audio_in)
    audio_out = codec.decode_audio(q_feature)  # or
    # audio_out = codec.decode_audio(indices=indices['indices'])
    generated_audio = audio_out[:, :audio_length].detach().cpu().numpy()

mse = ((sample_audio - generated_audio) ** 2).mean().item()
print(f"codec({MODEL_USED}) mse: {mse}")

available models

config_nameSample rate(Hz)tokens/sCodebook sizeBitrate(bps)
0k75bps16,00044.44117,649748.6
1kbps16,00059.26117,649998.2
1k5bps16,00088.89117,6491497.3
3kbps16,000166.67250,0472988.6

Contributors

zhai-lw

3 commits

zhai-lw/L3AC

A lightweight audio codec based on a single quantizer

Python

36

3 commits

updated Sep 4, 2025

See the code

README

L3AC

This repository contains the implementation of L3AC, a lightweight audio codec based on a single quantizer, introduced in the paper titled "L3AC: Towards a Lightweight and Lossless Audio Codec".

Paper

Model Weights

Training Code

Comparison of various audio codec Comparison of various audio codec

install

pip install l3ac

demo

Firstly, make sure you have installed the librosa package to load the example audio file. You can install it using pip:

pip install librosa

Then, you can use the following code to load a sample audio file, encode it using the L3AC model, and decode it back to audio. The code also calculates the mean squared error (MSE) between the original and generated audio.

import librosa
import torch
import l3ac

all_models = l3ac.list_models()
print(f"Available models: {all_models}")

MODEL_USED = '1kbps'
codec = l3ac.get_model(MODEL_USED)
print(f"loaded codec({MODEL_USED}) and codec sample rate: {codec.config.sample_rate}")

sample_audio, sample_rate = librosa.load(librosa.example("libri1"))
sample_audio = sample_audio[None, :]
print(f"loaded sample audio and audio sample_rate :{sample_rate}")

sample_audio = librosa.resample(sample_audio, orig_sr=sample_rate, target_sr=codec.config.sample_rate)

codec.network.cuda()
codec.network.eval()
with torch.inference_mode():
    audio_in = torch.tensor(sample_audio, dtype=torch.float32, device='cuda')
    _, audio_length = audio_in.shape
    print(f"{audio_in.shape=}")
    q_feature, indices = codec.encode_audio(audio_in)
    audio_out = codec.decode_audio(q_feature)  # or
    # audio_out = codec.decode_audio(indices=indices['indices'])
    generated_audio = audio_out[:, :audio_length].detach().cpu().numpy()

mse = ((sample_audio - generated_audio) ** 2).mean().item()
print(f"codec({MODEL_USED}) mse: {mse}")

available models

config_nameSample rate(Hz)tokens/sCodebook sizeBitrate(bps)
0k75bps16,00044.44117,649748.6
1kbps16,00059.26117,649998.2
1k5bps16,00088.89117,6491497.3
3kbps16,000166.67250,0472988.6

Contributors

zhai-lw

3 commits

Languages

Python

100.0%