October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
DocumentaryTube
Admit one · Blog

How to Generate AI Music with MusicGen: Text-to-Music Tutorial

A practical MusicGen tutorial covering the browser demo, local Transformers and AudioCraft workflows, prompt design, WAV export, melody conditioning, troubleshooting and the model’s non-commercial license.
Opened Runtime7 min Written byDocumentaryTube Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

MusicGen turns a written description into a short music sample that you can save as a WAV file. The easiest starting point is the small checkpoint, facebook/musicgen-small, run either in the official browser demo or locally with Python and Hugging Face Transformers. It is best for instrumental sketches and sound ideas—not dependable vocals, complete release-ready songs, or automatic commercial clearance.

Important license warning: AudioCraft’s source code is MIT licensed, but the released MusicGen weights are licensed CC-BY-NC 4.0. “Open source” code does not mean that generated music is automatically cleared for advertising, client work, games, films, or other commercial releases. Check the license for your exact checkpoint and use case before publishing.

What MusicGen is—and what it is not

MusicGen is Meta’s text-conditioned music-generation model in the AudioCraft project. It predicts compressed audio representations and reconstructs them into sound with a 32 kHz EnCodec tokenizer, four codebooks and a 50 Hz token-generation process. The original research paper is available at arxiv.org/abs/2306.05284, while the implementation details and model families are documented in the AudioCraft MusicGen documentation.

You describe genre, mood, instruments, tempo, arrangement and production character; the model produces an audio interpretation. In practice, it works best as a generator of short instrumental ideas, loops and background sketches. The model card warns that realistic vocals are unreliable, English descriptions generally work best, and results vary across styles and cultures. Expect repetitive structures, weak transitions, muddy high frequencies or inconsistent instrumentation rather than a finished, mixed song.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Focusrite Scarlett Solo 3rd Gen USB-C Audio Interface
  • Pro performance with great pre-amps - Achieve a brighter recording thanks to the high performing mic pre-amps of the Scarlett 3rd Gen. A switchable Air mode will add extra clarity to your acoustic instruments when recording with your Solo 3rd Gen
  • Get the perfect guitar and vocal take with - With two high-headroom instrument inputs to plug in your guitar or bass so that they shine through. Capture your voice and instruments without any unwanted clipping or distortion thanks to our Gain Halos
  • Studio quality recording for your music & podcasts - Achieve pro sounding recordings with Scarlett 3rd Gen’s high-performance converters enabling you to record and mix at up to 24-bit/192kHz. Your recordings will retain all of their sonic qualities
  • Low-noise for crystal clear listening - 2 low-noise balanced outputs provide clean audio playback with 3rd Gen. Hear all the nuances of your tracks or music from Spotify, Apple & Amazon Music. Plug-in headphones for private listening in high-fidelity
  • Everything in the box: Includes Pro Tools Intro+, Ableton Live Lite, Cubase LE, and Hitmaker Expansion: a suite of essential effects, powerful software instruments, and easy-to-use mastering tools

Melody-capable checkpoints accept an audio melody as well as text. They can be guided by a contour while changing timbre, rhythm and arrangement; they do not create an exact remake.

Choose a way to run MusicGen

Method Best for Advantages Trade-offs
Official Hugging Face Space Trying the model immediately No local installation Queues, outages, changing controls and download limits may apply
Hugging Face Transformers Developers and reproducible scripts Clear Python workflow and easy integration with other Transformers projects Requires package setup, model downloads and suitable hardware
AudioCraft API MusicGen-specific experimentation Direct MusicGen controls such as duration and AudioCraft’s WAV-writing utility Heavier dependency stack, including FFmpeg for the local workflow

Fastest trial: the browser demo

  1. Open the official MusicGen Space.
  2. Enter a description such as mellow lo-fi hip-hop instrumental, dusty drums, warm electric piano, relaxed bassline, slow tempo, no vocals.
  3. Submit the request and wait for the generated audio.
  4. Preview it, then use the interface’s download control if one is available.
  5. Generate several variations instead of treating the first result as final.

The Space is a convenient demonstration, not a guaranteed production service. Availability, queue times and interface labels can change.

Pick a checkpoint

Checkpoint Approximate size Capability Good starting audience
facebook/musicgen-small 300 million parameters Text-to-music Beginners and modest hardware
facebook/musicgen-medium 1.5 billion Text-to-music Users with enough GPU memory for higher-capacity inference
facebook/musicgen-large 3.3 billion Text-to-music High-resource experimentation
facebook/musicgen-melody 1.5 billion Text plus melody conditioning Users guiding generation with an audio melody
Stereo variants Varies Stereo output Projects where stereo is specifically required

Start with musicgen-small. The official AudioCraft documentation says medium models require at least a 16 GB GPU for its inference path; actual use varies with framework, precision, batch size and generation length. A small model can still be slow on CPU.

Prerequisites for local generation

  • Python and an isolated virtual environment.
  • PyTorch, torchaudio, Transformers and SciPy.
  • Internet access for the first model download and enough disk space for the cache.
  • A compatible CPU or GPU. CUDA is useful but not mandatory for small tests.

MusicGen support entered Transformers in version 4.31.0. Package compatibility changes, so verify the installed version and use a fresh environment rather than repeatedly mixing packages.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Generate a WAV with Transformers

1. Create and activate an environment

python -m venv musicgen-env

On macOS or Linux:

source musicgen-env/bin/activate

On Windows PowerShell:

.musicgen-envScriptsActivate.ps1

2. Install the baseline packages

pip install torch torchaudio transformers scipy

If your installed Transformers package does not contain the MusicGen classes, upgrade it. As a compatibility fallback documented by AudioCraft, install the development version:

Rank #2
Focusrite Scarlett Solo 4th Gen USB-C Audio Interface
  • The new generation of the songwriter's interface: Plug in your mic and guitar and let Scarlett Solo 4th Gen bring big studio sound to wherever you make music
  • Studio-quality sound: With a huge 120dB dynamic range, the newest generation of Scarlett uses the same converters as Focusrite’s flagship interfaces, found in the world's biggest studios
  • Find your signature sound: Scarlett 4th Gen's improved Air mode lifts vocals and guitars to the front of the mix, adding musical presence and rich harmonic drive to your recordings
  • All you need to record, mix and master your music: Includes industry-leading recording software and a full collection of record-making plugins
  • Everything in the box: Includes Pro Tools Intro+, Ableton Live Lite, Cubase LE, and Hitmaker Expansion: a suite of essential effects, powerful software instruments, and easy-to-use mastering tools
pip install -U transformers
pip install git+https://github.com/facebookresearch/transformers.git

Use the GitHub command only when the regular release is incompatible with your environment.

3. Save and run this script

import scipy.io.wavfile
import torch

from transformers import AutoProcessor, MusicgenForConditionalGeneration

model_name = "facebook/musicgen-small"
device = "cuda" if torch.cuda.is_available() else "cpu"

processor = AutoProcessor.from_pretrained(model_name)
model = MusicgenForConditionalGeneration.from_pretrained(model_name).to(device)

prompt = "A warm cinematic orchestral track with soft strings, piano, and a gradual emotional crescendo"
inputs = processor(text=[prompt], padding=True, return_tensors="pt")
inputs = {key: value.to(device) for key, value in inputs.items()}

with torch.no_grad():
    audio_values = model.generate(**inputs, max_new_tokens=256)

sampling_rate = model.config.audio_encoder.sampling_rate
audio = audio_values[0, 0].cpu().numpy()
scipy.io.wavfile.write("musicgen-output.wav", rate=sampling_rate, data=audio)
print("Saved musicgen-output.wav")

Run it with python your_script_name.py. The first run downloads model files and can take considerably longer than later runs. The resulting musicgen-output.wav uses the sampling rate reported by the model, rather than a guessed value.

Change length, device and batch size

In Transformers, max_new_tokens controls length indirectly; the exact seconds depend on the model and generation configuration. Begin around 256, then increase it for longer audio at the cost of time and memory. Passing several strings in text=[...] creates a batch, but batching also increases memory use. Generate one prompt at a time when troubleshooting.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write prompts that describe music

A broad label such as Cool music gives the model little direction. Put the most important musical characteristics first and combine concrete details:

  1. Genre or broad style.
  2. Mood.
  3. Instrumentation.
  4. Tempo or rhythmic feel.
  5. Arrangement or development.
  6. Production character.
  7. Intended context, such as a documentary scene or game menu.

For example:

A mellow lo-fi hip-hop instrumental for a late-night study session, dusty drums, warm electric piano, subtle vinyl texture, relaxed bassline, slow tempo, no vocals
Energetic 1980s-inspired synthwave instrumental with pulsing analog bass, gated drums, bright arpeggiated synthesizers, and a dramatic chorus
Minimal cinematic piano and soft strings for a reflective documentary scene, slow tempo, spacious reverb, restrained dynamics, no vocals

“No vocals” expresses an intention, not a guarantee. Avoid contradictory instructions, simplify prompts with too many competing ideas, and generate multiple short candidates so you can choose the most useful interpretation.

Rank #3
Sale
FIFINE Ampligame SC3 Gaming Audio Mixer with Indi-Fader and Volume Control
  • [XLR Mic Input] One XLR microphone input interface is set on the gaming audio mixer, which is great to up your audio quality with your XLR setup. The XLR mixer is a stepping stone to upgrade your live streaming. Audio mixer offered built-in 48V phantom power which opens up more choices for mics. Directly use it with your condenser microphone but do not solve added peripherals. (NOT available for USB mic)
  • [Individual Channel Control] Gaming audio mixer for one mic recording with smooth volume slider fader take your streaming recording to a whole new level with full pleasure. Four independent channels set on the DJ mixer give audio volume of the MICROPHONE, LINE IN, HEADPHONE, and LINE OUT channels individual control. Configurable on the PC audio mixer instead of just operating on your game or streaming software.
  • [Mute and Monitor] The front mute and monitor buttons but not at the back, make it easier to get the audio interface use. Ability to mute audio, the audio mixer for streaming prevents background noise from damaging your live broadcast. Real-time feedback between speaking and hearing will not distract your attention, which encourage you to speak more confidently. The sturdy-built control button allow you to operate freely and easily during live streaming.
  • [Sound Effects] The computer sound mixer supports four pre-recorded customized button that can be recorded and activated at the press of button to post production. 6 kinds of voice changing modes change your output style. 12 auto tune changes the tone of your voice. The podcast mixer being able to add different and fun effects is a huge bonus for your streaming or game voice.
  • [Controllable Vibrant RGB] RGB button on the audio mixer DJ meets different live streaming themes. Lights on the video mixer is vibrant but not harsh on your eyes. Flowing or frozen RGB color rotation in a decent pace presents a greatly strong impression as a "light show" to your audience. Even a streaming equipment accessory will not be dull looking when video production.

Use AudioCraft for duration control

AudioCraft follows Meta’s original API and exposes a direct duration setting. Install it separately from the Transformers route:

pip install git+https://github.com/facebookresearch/audiocraft.git

FFmpeg is also required for the original local workflow. On Debian or Ubuntu:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo apt-get update
sudo apt-get install ffmpeg

For other systems, follow the official FFmpeg installation guidance and verify:

ffmpeg -version
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write

model = MusicGen.get_pretrained("small")
model.set_generation_params(duration=8)

descriptions = [
    "happy acoustic guitar with light percussion",
    "dark electronic soundtrack with deep bass and slow drums",
]

wav = model.generate(descriptions)

for index, audio in enumerate(wav):
    audio_write(
        f"musicgen-output-{index}",
        audio.cpu(),
        model.sample_rate,
        strategy="loudness",
    )

This example requests approximately eight seconds and writes one file per description. AudioCraft’s duration parameter and Transformers’ max_new_tokens are different controls; do not substitute one for the other.

Generate from a melody

The musicgen-melody family accepts text plus an audio melody. The recording guides aspects of the melodic contour while the prompt specifies style, instruments or mood. The output may alter timing, harmony, timbre and arrangement, so it is not an exact cover.

Rank #4
Focusrite Scarlett 2i2 4th Gen USB-C Audio Interface
  • The new generation of the artist's interface: Connect your mic to Scarlett's 4th Gen mic pres. Plug in your guitar. Fire up the included software. Start making your first big hit
  • Studio-quality sound: With a huge 120dB dynamic range, the newest generation of Scarlett uses the same converters as Focusrite’s flagship interfaces, found in the world's biggest studios
  • Never lose a great take: Scarlett 4th Gen's Auto Gain sets the perfect level for your mic or guitar, and Clip Safe prevents clipping, so you can focus on the music
  • Find your signature sound: Air mode lifts vocals and guitars to the front of the mix, adding musical presence and rich harmonic drive to your recordings
  • With Scarlett 4th Gen, you have all you need to record, mix and master your music: Includes industry-leading recording software and a full collection of record-making plugins

Use only recordings and compositions you own or are licensed to use. Some AudioCraft melody workflows require additional dependencies, including Demucs-related components; consult the current MusicGen documentation before installing.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Fix common failures

MusicGen class or module is missing

Check the package and then upgrade in the active environment:

pip show transformers
pip install -U transformers

If the class remains unavailable, create a clean virtual environment and use the documented GitHub fallback rather than combining unrelated AudioCraft and Transformers imports.

CUDA out-of-memory

  1. Switch to facebook/musicgen-small.
  2. Lower max_new_tokens or AudioCraft’s duration.
  3. Generate one prompt instead of a batch.
  4. Close other GPU-heavy applications.
  5. Use CPU, accepting slower generation.
  6. Try automatic device mapping if your installed Transformers version supports it.

The model download appears stuck

The initial download can be slow. Check versions, disk space, network access, cache permissions and corporate firewall rules:

python -c "import torch; print(torch.__version__)"
python -c "import transformers; print(transformers.__version__)"

AudioCraft supports relocating its cache with the AUDIOCRAFT_CACHE_DIR environment variable; use it when the default drive is full or not writable. See the AudioCraft README.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
M-AUDIO M-Track Duo USB Audio Interface
  • Podcast, Record, Live Stream, This Portable Audio Interface Covers it All - USB sound card for Mac or PC delivers 48kHz audio resolution for pristine recording every time
  • Be ready for anything with this versatile M-AUDIO interface - Record guitar, vocals or line input signals with two combo XLR / Line / Instrument Inputs with phantom power
  • Everything you Demand from an Audio Interface for Fuss-Free Monitoring - 1/4" headphone output and stereo 1/4" outputs for total monitoring flexibility; USB/Direct switch for zero latency monitoring
  • Get the best out of your Microphones - M-Track Duo’s transparent Crystal Preamps guarantee optimal sound from all your microphones including condenser mics
  • The MPC Production Experience - Includes MPC Beats Software complete with the essential production tools from Akai Professional

FFmpeg errors

This primarily affects AudioCraft. Confirm that FFmpeg is installed and visible on the system path with ffmpeg -version. The Transformers-plus-SciPy example avoids many AudioCraft-specific FFmpeg issues.

Silent, distorted or unreadable WAV

  • Use the model’s configured sampling rate.
  • Move the tensor to CPU before calling NumPy.
  • Check the tensor shape and selected channel.
  • Write a floating-point or compatible PCM array.
  • Ensure the extension matches the actual encoding.

The prompt is poorly followed

Move the key traits to the beginning, name specific instruments and arrangement terms, remove contradictions, simplify overloaded prompts and compare several generations. Do not assume a seed exists unless the interface documents one.

Licensing and responsible use

The AudioCraft repository licenses source code under MIT, while the MusicGen model card identifies the released weights as CC-BY-NC 4.0. Review the current license for the exact checkpoint, wrapper and hosted provider before monetizing output. The software may be downloadable without a purchase, but compute, hosting, storage and usage rights can still create costs.

Do not describe MusicGen output as automatically royalty-free or commercially cleared. For a business project, obtain qualified legal advice and retain prompts, source recordings, edits and post-production records. Copyright treatment of AI-generated music varies by jurisdiction and depends on human contribution and contract terms. The model card describes training sources including the Meta Music Initiative Sound Collection, Shutterstock and Pond5 collections; that statement does not resolve every downstream copyright question.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When MusicGen is the right tool

Choose MusicGen when you want local experimentation, short instrumental sketches, programmable generation or melody-guided variation. A hosted service such as Suno, Udio or Stable Audio may be more convenient for users seeking longer arrangements, vocals or explicit plan-based commercial terms, but verify each provider’s current terms before relying on it.

For a dependable first result, use the official Space or run facebook/musicgen-small with Transformers, start around five to eight seconds, and iterate over several precise prompts. Treat the files as editable ideas rather than finished records.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.