← All models

text-to-audio Β· audio (music) Β· WebAssembly Β· ~656 MB

MusicGen β€” turn a text prompt into music, on your device

MusicGen composes short, original music from a text description β€” "lo-fi hip hop, chill" or "90s rock, loud guitars" β€” right in your browser. This is not text-to-speech: the model predicts a stream of audio tokens and decodes them into a 32 kHz waveform. No server, no upload, no per-clip bill. It is also genuinely compute-heavy, so keep clips short and expect a real wait.

At a glance

ModelXenova/musicgen-small (Meta MusicGen, ONNX export)
Task / pipelinetext-to-audio (music generation)
ArchitectureT5 text encoder β†’ autoregressive Transformer decoder over EnCodec audio tokens (4 codebooks, 50 Hz) β†’ EnCodec decoder β†’ 32 kHz waveform
Quantizationq8 text-encoder + q8 decoder, fp32 EnCodec decode (quantising the decoder garbles the audio)
BackendWebAssembly β€” no reliable WebGPU path for this model; runs on CPU and is slow
Download~656 MB (text encoder + decoder + fp32 EnCodec), cached after first load
LicenseCC-BY-NC-4.0 (research / non-commercial β€” the weights, not your prompts)
Web APIsWebAssembly, Web Workers, Web Audio (playback), Blob URLs

Run it

Describe some music, choose a length and a couple of sampling controls, then hit Generate. Generation runs in a Web Worker with a real token-level progress bar β€” the model is decoding audio frames one step at a time, and the bar reflects that, not a spinner.

Heads up β€” this is compute-heavy. MusicGen runs on the CPU (WASM). Even a few seconds of music can take a while to generate, and generation time grows with the length. Start with a short clip (3–5 s). The wait and the timing readout below are real; nothing here is pre-baked.

See inside

MusicGen doesn't produce sound directly β€” it predicts a sequence of EnCodec audio tokens (about 50 per second of music), then a neural codec decodes those tokens into the waveform below (the bar fills as it plays). The stats are all measured on this device: a real-time factor above 1.0Γ— means it took longer to generate than the clip's own length β€” expected on CPU.

Generate a clip to see its waveform and token stats.

Why it matters

Generative music used to mean a cloud API, a licence, and your prompt leaving the building. MusicGen makes short original music a local capability of your page β€” the prompt never leaves the tab, and there's no per-clip bill. It's slow on CPU today, but it proves the shape of what's coming to the browser.

Use cases

Basics

A prompt in, a music clip out β€” presets, length, and the core move.

Practical

Royalty-free background loop & UI-sound generator you can download.

Wild

Mood-to-music: blend two vibes into one prompt and hear the mix.

Multi-model

A tiny LLM writes the music prompt from a one-word mood β†’ MusicGen plays it.

How the API works

MusicGen is a multi-part model, so we use the low-level classes (that's also the only way to get token-level progress). Load once, then generate:

import {
  MusicgenForConditionalGeneration,
  AutoTokenizer,
} from "@huggingface/transformers";

const MODEL = "Xenova/musicgen-small";
const model = await MusicgenForConditionalGeneration.from_pretrained(MODEL, {
  dtype: {                         // the known-good config
    text_encoder: "q8",
    decoder_model_merged: "q8",
    encodec_decode: "fp32",        // MUST stay fp32 β€” q8 here garbles the audio
  },
  device: "wasm",                  // no reliable WebGPU path for this model
});
const tokenizer = await AutoTokenizer.from_pretrained(MODEL);

const inputs = tokenizer("lo-fi hip hop, chill");
const out = await model.generate({
  ...inputs,
  max_length: 5 * 50 + 4,          // ~50 audio tokens per second of music
  guidance_scale: 3,
  temperature: 1,
  do_sample: true,
  // streamer: a BaseStreamer whose put() fires once per decoded token β†’ real progress
});
// out.data β†’ Float32Array waveform
// model.config.audio_encoder.sampling_rate β†’ 32000

This page runs all of that inside a Web Worker so the long decode never blocks the interface, attaches a BaseStreamer for the real progress bar, encodes the Float32 samples to a WAV blob for the <audio> element, and draws the same samples as the waveform.

References