X-Codec
This model was released on 2024-08-30 and added to Hugging Face Transformers on 2025-08-15.
X-Codec
Section titled “X-Codec”
Overview
Section titled “Overview”The X-Codec model was proposed in Codec Does Matter: Exploring the Semantic Shortcoming of Codec for Audio Language Model by Zhen Ye, Peiwen Sun, Jiahe Lei, Hongzhan Lin, Xu Tan, Zheqi Dai, Qiuqiang Kong, Jianyi Chen, Jiahao Pan, Qifeng Liu, Yike Guo, Wei Xue.
The X-Codec model is a neural audio codec that integrates semantic information from self-supervised models (e.g., HuBERT) alongside traditional acoustic information. This enables:
- Music continuation: Better modeling of musical semantics yields more coherent continuations.
- Text-to-Sound Synthesis: X-Codec captures semantic alignment between text prompts and generated audio.
- Semantic aware audio tokenization: X-Codec is used as an audio tokenizer in the YuE lyrics to song generation model.
The abstract of the paper states the following:
Recent advancements in audio generation have been significantly propelled by the capabilities of Large Language Models (LLMs). The existing research on audio LLM has primarily focused on enhancing the architecture and scale of audio language models, as well as leveraging larger datasets, and generally, acoustic codecs, such as EnCodec, are used for audio tokenization. However, these codecs were originally designed for audio compression, which may lead to suboptimal performance in the context of audio LLM. Our research aims to address the shortcomings of current audio LLM codecs, particularly their challenges in maintaining semantic integrity in generated audio. For instance, existing methods like VALL-E, which condition acoustic token generation on text transcriptions, often suffer from content inaccuracies and elevated word error rates (WER) due to semantic misinterpretations of acoustic tokens, resulting in word skipping and errors. To overcome these issues, we propose a straightforward yet effective approach called X-Codec. X-Codec incorporates semantic features from a pre-trained semantic encoder before the Residual Vector Quantization (RVQ) stage and introduces a semantic reconstruction loss after RVQ. By enhancing the semantic ability of the codec, X-Codec significantly reduces WER in speech synthesis tasks and extends these benefits to non-speech applications, including music and sound generation. Our experiments in text-to-speech, music continuation, and text-to-sound tasks demonstrate that integrating semantic information substantially improves the overall performance of language models in audio generation.
Model cards:
- xcodec-hubert-librispeech (for speech)
- xcodec-wavlm-mls (for speech)
- xcodec-wavlm-more-data (for speech)
- xcodec-hubert-general (for general audio)
- xcodec-hubert-general-balanced (for general audio)
This model was contributed by Manal El Aidouni. The original code can be found here and original checkpoints for the five different models here.
Demos can be found on this page.
Usage example
Section titled “Usage example”Here is a quick example of how to encode and decode an audio using this model:
from datasets import load_dataset, Audiofrom transformers import XcodecModel, AutoFeatureExtractordummy_dataset = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
# load model and feature extractormodel_id = "hf-audio/xcodec-hubert-librispeech"model = XcodecModel.from_pretrained(model_id)feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
# load audio sampledummy_dataset = dummy_dataset.cast_column("audio", Audio(sampling_rate=feature_extractor.sampling_rate))audio_sample = dummy_dataset[-1]["audio"]["array"]inputs = feature_extractor(raw_audio=audio_sample, sampling_rate=feature_extractor.sampling_rate, return_tensors="pt")
# encode and decodeencoder_outputs = model.encode(inputs["input_values"])decoder_outputs = model.decode(encoder_outputs.audio_codes)audio_values = decoder_outputs.audio_values
# or the equivalent with a forward passaudio_values = model(inputs["input_values"]).audio_valuesTo listen to the original and reconstructed audio, run the snippet below and then open the generated original.wav and reconstruction.wav files in your music player to compare.
import soundfile as sf
original = audio_samplereconstruction = audio_values[0].cpu().detach().numpy()sampling_rate = feature_extractor.sampling_rate
sf.write("original.wav", original, sampling_rate)sf.write("reconstruction.wav", reconstruction.T, sampling_rate)XcodecConfig
Section titled “XcodecConfig”[[autodoc]] XcodecConfig
XcodecModel
Section titled “XcodecModel”[[autodoc]] XcodecModel - decode - encode - forward