CoolFace
Apppublic

softwareweaver/MusicGen

sourceHugging Facecc-by-nc-4.0updated 11mo agoView on Hugging Face
0likes
MBD.md118 linesDownload Raw Back to docs
1# MultiBand Diffusion2 3AudioCraft provides the code and models for MultiBand Diffusion, [From Discrete Tokens to High Fidelity Audio using MultiBand Diffusion][arxiv].4MultiBand diffusion is a collection of 4 models that can decode tokens from5<a href="https://github.com/facebookresearch/encodec">EnCodec tokenizer</a> into waveform audio. You can listen to some examples on the <a href="https://ai.honu.io/papers/mbd/">sample page</a>.6 7<a target="_blank" href="https://colab.research.google.com/drive/1JlTOjB-G0A2Hz3h8PK63vLZk4xdCI5QB?usp=sharing">8  <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>9</a>10<br>11 12 13## Installation14 15Please follow the AudioCraft installation instructions from the [README](../README.md).16 17 18## Usage19 20We offer a number of way to use MultiBand Diffusion:211. The MusicGen demo includes a toggle to try diffusion decoder. You can use the demo locally by running [`python -m demos.musicgen_app --share`](../demos/musicgen_app.py), or through the [MusicGen Colab](https://colab.research.google.com/drive/1JlTOjB-G0A2Hz3h8PK63vLZk4xdCI5QB?usp=sharing).222. You can play with MusicGen by running the jupyter notebook at [`demos/musicgen_demo.ipynb`](../demos/musicgen_demo.ipynb) locally (if you have a GPU).23 24## API25 26We provide a simple API and pre-trained models for MusicGen and for EnCodec at 24 khz for 3 bitrates (1.5 kbps, 3 kbps and 6 kbps).27 28See after a quick example for using MultiBandDiffusion with the MusicGen API:29 30```python31import torchaudio32from audiocraft.models import MusicGen, MultiBandDiffusion33from audiocraft.data.audio import audio_write34 35model = MusicGen.get_pretrained('facebook/musicgen-melody')36mbd = MultiBandDiffusion.get_mbd_musicgen()37model.set_generation_params(duration=8)  # generate 8 seconds.38wav, tokens = model.generate_unconditional(4, return_tokens=True)    # generates 4 unconditional audio samples and keep the tokens for MBD generation39descriptions = ['happy rock', 'energetic EDM', 'sad jazz']40wav_diffusion = mbd.tokens_to_wav(tokens)41wav, tokens = model.generate(descriptions, return_tokens=True)  # generates 3 samples and keep the tokens.42wav_diffusion = mbd.tokens_to_wav(tokens)43melody, sr = torchaudio.load('./assets/bach.mp3')44# Generates using the melody from the given audio and the provided descriptions, returns audio and audio tokens.45wav, tokens = model.generate_with_chroma(descriptions, melody[None].expand(3, -1, -1), sr, return_tokens=True)46wav_diffusion = mbd.tokens_to_wav(tokens)47 48for idx, one_wav in enumerate(wav):49    # Will save under {idx}.wav and {idx}_diffusion.wav, with loudness normalization at -14 db LUFS for comparing the methods.50    audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)51    audio_write(f'{idx}_diffusion', wav_diffusion[idx].cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)52```53 54For the compression task (and to compare with [EnCodec](https://github.com/facebookresearch/encodec)):55 56```python57import torch58from audiocraft.models import MultiBandDiffusion59from encodec import EncodecModel60from audiocraft.data.audio import audio_read, audio_write61 62bandwidth = 3.0  # 1.5, 3.0, 6.063mbd = MultiBandDiffusion.get_mbd_24khz(bw=bandwidth)64encodec = EncodecModel.encodec_model_24khz()65 66somepath = ''67wav, sr = audio_read(somepath)68with torch.no_grad():69    compressed_encodec = encodec(wav)70    compressed_diffusion = mbd.regenerate(wav, sample_rate=sr)71 72audio_write('sample_encodec', compressed_encodec.squeeze(0).cpu(), mbd.sample_rate, strategy="loudness", loudness_compressor=True)73audio_write('sample_diffusion', compressed_diffusion.squeeze(0).cpu(), mbd.sample_rate, strategy="loudness", loudness_compressor=True)74```75 76 77## Training78 79The [DiffusionSolver](../audiocraft/solvers/diffusion.py) implements our diffusion training pipeline.80It generates waveform audio conditioned on the embeddings extracted from a pre-trained EnCodec model81(see [EnCodec documentation](./ENCODEC.md) for more details on how to train such model).82 83Note that **we do NOT provide any of the datasets** used for training our diffusion models.84We provide a dummy dataset containing just a few examples for illustrative purposes.85 86### Example configurations and grids87 88One can train diffusion models as described in the paper by using this [dora grid](../audiocraft/grids/diffusion/4_bands_base_32khz.py).89```shell90# 4 bands MBD trainning91dora grid diffusion.4_bands_base_32khz92```93 94### Learn more95 96Learn more about AudioCraft training pipelines in the [dedicated section](./TRAINING.md).97 98 99## Citation100 101```102@article{sanroman2023fromdi,103  title={From Discrete Tokens to High-Fidelity Audio Using Multi-Band Diffusion},104  author={San Roman, Robin and Adi, Yossi and Deleforge, Antoine and Serizel, Romain and Synnaeve, Gabriel and Défossez, Alexandre},105  journal={arXiv preprint arXiv:},106  year={2023}107}108```109 110 111## License112 113See license information in the [README](../README.md).114 115 116[arxiv]: https://arxiv.org/abs/2308.02560117[mbd_samples]: https://ai.honu.io/papers/mbd/118