# MusicLM-Pytorch music generator

> Sets up and trains an open-source tool that turns text into music.

- **What it does:** Sets up free, open-source code that turns a written description into a piece of music, and guides you through training it. It needs some technical setup and a good graphics card to run well.
- **Agents:** Claude Code and Codex (through orbit-connect), Claude and ChatGPT (through the Orbit connector)
- **Install:** `npx -y --loglevel=error orbit-connect@0.3.33 install skl_7a6bc09e598ccf8d0206037f`
- **Creator:** @orbit.creates on Instagram
- **Safety:** Checked by Orbit: nothing risky found in what it runs or reads
- **Last updated:** 2026-11-14
- **Page:** https://orbitinbox.app/s/musiclm-pytorch-music-generator

## Skill

---
name: musiclm-pytorch-music-generator
description: Use when you want the music-generation code behind an AI-generated song; it installs lucidrains/musiclm-pytorch and walks Claude through training MuLaN and the semantic, coarse, and fine transformers to turn text into music.
---

# MusicLM music-generation code

## What it does
This is lucidrains' open-source PyTorch rebuild of Google's MusicLM, a model that turns text prompts into real music. It works by pairing two pieces:

- **MuLaN**: a text-audio contrastive model that learns how words and sound line up, made of an `AudioSpectrogramTransformer` and a `TextTransformer`.
- **AudioLM** (from the separate `audiolm-pytorch` project): text-conditioned audio generation, made of three transformers trained in sequence - semantic, coarse, and fine.

Once MuLaN is trained, its embeddings (via `MuLaNEmbedQuantizer`) condition the three AudioLM transformers. The trained AudioLM plus MuLaN are wrapped together as `MusicLM`, which takes a text prompt, generates several audio samples, and uses MuLaN to pick the best match. The repo has 3,294 stars, 262 forks, and is MIT licensed.

## What you need
- Python with `pip`
- The `musiclm-pytorch` package (installs `torch` as a dependency)
- The separate `audiolm-pytorch` package for training the semantic, coarse, and fine transformers (the README directs you to its training instructions)
- A GPU is used in the README's training example (`.cuda()`); CPU may work but will be slow
- Pretrained Hubert checkpoint files (`hubert_base_ls960.pt` and `hubert_base_ls960_L9_km500.bin`) for the `HubertWithKmeans` wav2vec model used in the AudioLM training example
- Your own dataset of `<sound, text>` pairs for MuLaN, and a folder of audio files for AudioLM training

## Setup
```bash
pip install musiclm-pytorch
```

Train MuLaN first (adapt the example in the README with your own `<sound, text>` data):

```python
import torch
from musiclm_pytorch import MuLaN, AudioSpectrogramTransformer, TextTransformer

audio_transformer = AudioSpectrogramTransformer(
    dim = 512, depth = 6, heads = 8, dim_head = 64,
    spec_n_fft = 128, spec_win_length = 24, spec_aug_stretch_factor = 0.8
)

text_transformer = TextTransformer(
    dim = 512, depth = 6, heads = 8, dim_head = 64
)

mulan = MuLaN(audio_transformer = audio_transformer, text_transformer = text_transformer)

wavs = torch.randn(2, 1024)
texts = torch.randint(0, 20000, (2, 256))

loss = mulan(wavs, texts)
loss.backward()
```

Then set up the quantizer and train the three AudioLM transformers (semantic, coarse, fine), following `audiolm-pytorch`'s training instructions and passing the quantizer in as `audio_conditioner`. See the full code in the README: https://github.com/lucidrains/musiclm-pytorch

Finally, wrap your trained `audio_lm` and `quantizer` into `MusicLM` and generate:

```python
from musiclm_pytorch import MusicLM

musiclm = MusicLM(audio_lm = audio_lm, mulan_embed_quantizer = quantizer)
music = musiclm('the crystalline sounds of the piano in a ballroom', num_samples = 4)
```

## How to use it with Claude
- "Help me set up a Python environment and install musiclm-pytorch."
- "Walk me through training MuLaN on my own sound-and-text dataset using the README example."
- "Set up the MuLaNEmbedQuantizer with semantic, coarse, and fine namespaces for my AudioLM transformers."
- "Help me follow audiolm-pytorch's trainer instructions to train the SemanticTransformer with this quantizer as the audio_conditioner."
- "Once I have a trained AudioLM and MuLaN, show me how to wrap them in MusicLM and generate music from a text prompt."
- "Explain what each of the three transformers (semantic, coarse, fine) does in this pipeline."

Training these models from scratch on real data requires substantial audio datasets, compute, and time; the README's code examples use tiny random tensors only to show the API shape.

## Credits
Made by lucidrains (MusicLM-Pytorch), packaged by Orbit.
https://github.com/lucidrains/musiclm-pytorch, license: MIT

Shared by @orbit.creates, credited on Orbit.