jiaqili3 commited on
Commit
82fc022
·
verified ·
1 Parent(s): f400e3c

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +89 -0
README.md ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: text-to-speech
3
+ ---
4
+ # FlexiCodec: A Dynamic Neural Audio Codec for Low Frame Rates
5
+
6
+ [![Demo Page](https://img.shields.io/badge/GitHub.io-Demo_Page-blue?logo=Github&style=flat-square)](https://flexicodec.github.io/)
7
+ [![ArXiv](https://img.shields.io/badge/arxiv-PDF-green?logo=arxiv&style=flat-square)](https://arxiv.org/abs/2510.00981)
8
+
9
+ ## Abstract
10
+ Neural audio codecs are foundational to speech language models. It is expected to have a low frame rate and decoupled semantic and acoustic information. A lower frame rate codec can reduce the computational cost of speech language models by shortening the sequence length. Recent studies have developed 12.5Hz low-frame-rate audio codecs, but even lower frame rate codecs remain underexplored. We find that a major challenge for very low frame rate tokens is missing semantic information. This paper introduces FlexiCodec to address this limitation. FlexiCodec improves semantic preservation with a dynamic frame rate approach and introduces a novel architecture featuring an ASR feature-assisted dual stream encoding and Transformer bottlenecks. With dynamic frame rates, it uses less frames at information-sparse regions through adaptively merging semantically similar frames. A dynamic frame rate also allows FlexiCodec to support inference-time controllable frame rates between 3Hz and 12.5Hz. Experiments on 6.25Hz, 8.3Hz and 12.5Hz average frame rates confirm that FlexiCodec excels over baseline systems in semantic information preservation and delivers a high audio reconstruction quality. We also validate the effectiveness of FlexiCodec in language model-based TTS.
11
+ ![](.github/flexicodec.png)
12
+
13
+ ## Installation
14
+ ```bash
15
+ git clone https://github.com/amphionspace/FlexiCodec.git
16
+ cd FlexiCodec
17
+ pip install -r requirements.txt
18
+ ```
19
+ <!-- # pip install -e . -->
20
+
21
+ ## FlexiCodec
22
+ Code is available under [`flexicodec/modeling_flexicodec.py`](flexicodec/modeling_flexicodec.py).
23
+
24
+ To run inference (automatically downloads checkpoint from huggingface):
25
+ ```python
26
+ import torch
27
+ import torchaudio
28
+ from flexicodec.infer import prepare_model, encode_flexicodec
29
+
30
+ model_dict = prepare_model()
31
+
32
+ # Load a real audio file
33
+ audio_path = "YOUR_WAV.wav"
34
+ audio, sample_rate = torchaudio.load(audio_path)
35
+ with torch.no_grad():
36
+ encoded_output = encode_flexicodec(audio, model_dict, sample_rate, num_quantizers=8, merging_threshold=0.91)
37
+
38
+ reconstructed_audio = model_dict['model'].decode_from_codes(
39
+ semantic_codes=encoded_output['semantic_codes'],
40
+ acoustic_codes=encoded_output['acoustic_codes'],
41
+ token_lengths=encoded_output['token_lengths'],
42
+ )
43
+
44
+ duration = audio.shape[-1] / sample_rate
45
+ output_path = 'decoded_audio.wav'
46
+ torchaudio.save(output_path, reconstructed_audio.cpu().squeeze(1), 16000)
47
+
48
+ print(f"Saved decoded audio to {output_path}")
49
+ print(f"This sample avg frame rate: {encoded_output['token_lengths'].shape[-1] / duration:.4f} frames/sec")
50
+ ```
51
+ For Chinese users, you might need to execute `export HF_ENDPOINT=https://hf-mirror.com` in terminal, before running the code. If you don't want to automatically download from huggingface, you can manually specify your downloaded checkpoint paths in `prepare_model`.
52
+
53
+
54
+ Batched input is supported. You can directly pass audios shaped [B,T] to the script above, but the audio length information will be unavailable.
55
+ To resolve this, you can additionally pass an `audio_lens` parameter to `encode_flexicodec`, and you can crop the output for each audio in `encoded_output[speech_token_len]`.
56
+
57
+ If you want to use the above code elsewhere, you might want to add `sys.path.append('PATH_TO_FLEXICODEC_REPOSITORY')` to find the code.
58
+
59
+ To extract continuous features from the semantic tokens, use:
60
+ ```python
61
+ feat = model_dict['model'].get_semantic_feature(encoded_output['semantic_codes'])
62
+ ```
63
+
64
+ ## FlexiCodec-TTS
65
+ Our code for Flexicodec-based AR TTS is available at [`flexicodec/ar_tts/modeling_artts.py`](flexicodec/ar_tts/modeling_artts.py). The training step is inside `training_forward` method. It receives a `dl_output` dictionary containing `x` (the [`feature_extractor`](flexicodec/infer.py#L50) output), `x_lens` (length of each x before padding), `audio` (the 16khz audio tensor). The inference is at the `inference` method in the same file.
66
+
67
+ Our code for Flow matching-based NAR TTS is based on the voicebox-based implementation [here](https://github.com/jiaqili3/DualCodec/tree/main/dualcodec/model_tts/voicebox).
68
+ We plan to release TTS trained models and TTS training examples.
69
+
70
+ ## Acknowledgements & Citation
71
+ - Our codebase setup is based on [DualCodec](https://github.com/jiaqili3/DualCodec)
72
+ - We thank the [Mimi Codec](https://github.com/kyutai-labs/moshi) for transformer implementations
73
+
74
+ If you find our works useful, please consider citing as:
75
+ ```biblatex
76
+ @article{li2025flexicodec,
77
+ title={FlexiCodec: A Dynamic Neural Audio Codec for Low Frame Rates},
78
+ author={Li, Jiaqi and Qian, Yao and Hu, Yuxuan and Zhang, Leying and Wang, Xiaofei and Lu, Heng and Thakker, Manthan and Li, Jinyu and Zhao, Shang and Wu, Zhizheng},
79
+ journal={arXiv preprint arXiv:2510.00981},
80
+ year={2025}
81
+ }
82
+
83
+ @article{li2025dualcodec,
84
+ title={Dualcodec: A low-frame-rate, semantically-enhanced neural audio codec for speech generation},
85
+ author={Li, Jiaqi and Lin, Xiaolong and Li, Zhekai and Huang, Shixi and Wang, Yuancheng and Wang, Chaoren and Zhan, Zhenpeng and Wu, Zhizheng},
86
+ journal={Interspeech 2025},
87
+ year={2025}
88
+ }
89
+ ```