SQCodec: A nerual audio codec with one quantizer
Project description
SQCodec
This repository contains the implementation of SQCodec, a lightweight audio codec based on a single quantizer, introduced in the paper titled "One Quantizer is Enough: Toward a Lightweight Audio Codec".
install
pip install sq_codec
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 SQCodec 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 sq_codec
all_models = sq_codec.list_models()
print(f"Available models: {all_models}")
MODEL_USED = '6kbps'
codec = sq_codec.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)
generated_audio = audio_out[:, :audio_length].detach().cpu().numpy()
mse = ((sample_audio - generated_audio) ** 2).mean().item()
print(f"codec({MODEL_USED}) mse: {mse}")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sq_codec-0.0.1.tar.gz.
File metadata
- Download URL: sq_codec-0.0.1.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5fb71975b1a2d50b4a1cb5fd548251eca70bb1878fb1e9fd045d340a9db0e132
|
|
| MD5 |
937a26bbc5b91b938761655761fcac16
|
|
| BLAKE2b-256 |
c5e7be2ca225a0c59cc6c603a7c0699f720ac3e2416e207ed3d1e7abc6e41240
|
File details
Details for the file sq_codec-0.0.1-py3-none-any.whl.
File metadata
- Download URL: sq_codec-0.0.1-py3-none-any.whl
- Upload date:
- Size: 25.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96afb3d16f7c6399e65d7447f4191c14cb91b9f7fca1223c5529944f3fe92258
|
|
| MD5 |
11c93c268d76c8e4242159f714958300
|
|
| BLAKE2b-256 |
d18c41254c45a2e503540274c5a85e3f3693b7f703f5ad5c6acbeee4c75b7d3f
|