Claude Code for AudioCraft: AI Music and Audio Generation — Claude Skills 360 Blog
Blog / AI / Claude Code for AudioCraft: AI Music and Audio Generation
AI

Claude Code for AudioCraft: AI Music and Audio Generation

Published: October 22, 2027
Read time: 5 min read
By: Claude Skills 360

AudioCraft generates music and audio from text descriptions. pip install audiocraft. from audiocraft.models import MusicGen. Load: model = MusicGen.get_pretrained("facebook/musicgen-small") — sizes: small (300M), medium (1.5B), large (3.3B), melody (1.5B), stereo variants. Set params: model.set_generation_params(duration=15, temperature=1.0, top_k=250, cfg_coef=3.0). Generate: wav = model.generate(["upbeat jazz piano with walking bass"]) — returns (batch, channels, samples) tensor. Multiple: wavs = model.generate(["classical piano", "electronic dance music", "acoustic guitar folk"]). Save: from audiocraft.utils.audio import audio_write, audio_write("output", wav[0].cpu(), model.sample_rate, strategy="loudness"). Melody conditioning: melody_waveform, sr = torchaudio.load("melody.wav"), model = MusicGen.get_pretrained("facebook/musicgen-melody"), wavs = model.generate_with_chroma(["orchestral version"], melody_waveform[None], sr). Continuation: model.generate_continuation(prompt_waveform, prompt_sample_rate, ["continues with bridge section"]). AudioGen: from audiocraft.models import AudioGen, model = AudioGen.get_pretrained("facebook/audiogen-medium"), model.set_generation_params(duration=5), wav = model.generate(["dog barking", "thunder storm", "office keyboard typing"]). EnCodec compression: from audiocraft.models import EncodecModel, codec = EncodecModel.get_pretrained("facebook/encodec_24khz"), codec.set_target_bandwidth(6.0). Stereo: model = MusicGen.get_pretrained("facebook/musicgen-stereo-medium") — returns 2-channel audio. Claude Code generates AudioCraft music generation scripts, batch audio pipelines, melody-conditioned generation, and sound effect synthesis code.

CLAUDE.md for AudioCraft

## AudioCraft Stack
- Version: audiocraft >= 1.3
- MusicGen: MusicGen.get_pretrained("facebook/musicgen-small|medium|large|melody|stereo-*")
- AudioGen: AudioGen.get_pretrained("facebook/audiogen-medium")
- Params: model.set_generation_params(duration, temperature, top_k, cfg_coef)
- Generate: model.generate(["text prompt 1", "text prompt 2"]) → (B, C, T) tensor
- Melody: model.generate_with_chroma(descriptions, melody_wavs, melody_sr)
- Continue: model.generate_continuation(prompt_wav, prompt_sr, descriptions)
- Save: audio_write(stem, wav.cpu(), model.sample_rate, strategy="loudness")
- EnCodec: EncodecModel.get_pretrained("facebook/encodec_24khz") for compression

AudioCraft Generation Pipeline

# audio/audiocraft_pipeline.py — AI music and audio generation with AudioCraft
from __future__ import annotations
import os
from pathlib import Path
from typing import Optional

import torch
import torchaudio

from audiocraft.utils.audio import audio_write


# ── 1. Model loading ──────────────────────────────────────────────────────────

def load_musicgen(
    model_size: str = "small",    # small | medium | large | melody | stereo-small | stereo-medium
    device:     str = "cuda" if torch.cuda.is_available() else "cpu",
):
    """
    Load MusicGen model. Auto-detects GPU.
    Memory: small=~2GB, medium=~5GB, large=~10GB.
    """
    from audiocraft.models import MusicGen

    model_name = f"facebook/musicgen-{model_size}"
    print(f"Loading {model_name} on {device}...")
    model = MusicGen.get_pretrained(model_name)
    model.to(device)
    print(f"MusicGen-{model_size} ready | sample_rate={model.sample_rate}Hz")
    return model


def load_audiogen(
    model_size: str = "medium",   # medium
    device:     str = "cuda" if torch.cuda.is_available() else "cpu",
):
    """Load AudioGen for sound effects generation."""
    from audiocraft.models import AudioGen

    model = AudioGen.get_pretrained(f"facebook/audiogen-{model_size}")
    model.to(device)
    print(f"AudioGen-{model_size} ready")
    return model


# ── 2. Basic music generation ─────────────────────────────────────────────────

def generate_music(
    model,
    descriptions: list[str],
    duration:     float = 15.0,   # seconds
    temperature:  float = 1.0,
    top_k:        int   = 250,
    cfg_coef:     float = 3.0,    # Classifier-free guidance scale
    output_dir:   str   = "./generated_music",
) -> list[str]:
    """
    Generate music from text descriptions.
    Returns list of output file paths.
    """
    Path(output_dir).mkdir(parents=True, exist_ok=True)

    model.set_generation_params(
        duration=duration,
        temperature=temperature,
        top_k=top_k,
        cfg_coef=cfg_coef,
        two_step_cfg=False,
    )

    print(f"Generating {len(descriptions)} music clips ({duration}s each)...")
    with torch.no_grad():
        wavs = model.generate(descriptions)   # (B, C, T)

    output_paths = []
    for i, (wav, desc) in enumerate(zip(wavs, descriptions)):
        stem     = os.path.join(output_dir, f"music_{i:04d}")
        out_path = stem + ".wav"
        audio_write(
            stem,
            wav.cpu(),
            model.sample_rate,
            strategy="loudness",      # Normalize loudness
            loudness_compressor=True,
        )
        output_paths.append(out_path)
        short_desc = desc[:50] + "..." if len(desc) > 50 else desc
        print(f"  [{i+1}] '{short_desc}' → {out_path}")

    return output_paths


# ── 3. Melody-conditioned generation ─────────────────────────────────────────

def generate_from_melody(
    model,
    descriptions:  list[str],
    melody_path:   str,
    duration:      float = 20.0,
    output_dir:    str   = "./generated_music",
) -> list[str]:
    """
    Generate music that follows a melody from an audio file.
    Model must be 'melody' variant: MusicGen.get_pretrained("facebook/musicgen-melody").
    """
    Path(output_dir).mkdir(parents=True, exist_ok=True)

    # Load melody audio
    melody_wav, melody_sr = torchaudio.load(melody_path)
    if melody_wav.shape[0] > 1:
        melody_wav = melody_wav.mean(dim=0, keepdim=True)   # Mono

    # Replicate melody for each description in batch
    melody_batch = melody_wav.unsqueeze(0).repeat(len(descriptions), 1, 1)

    model.set_generation_params(duration=duration, cfg_coef=3.0)

    print(f"Melody-conditioned generation from '{melody_path}'...")
    with torch.no_grad():
        wavs = model.generate_with_chroma(
            descriptions=descriptions,
            melody_wavs=melody_batch.to(model.device),
            melody_sample_rate=melody_sr,
        )

    output_paths = []
    for i, (wav, desc) in enumerate(zip(wavs, descriptions)):
        stem = os.path.join(output_dir, f"melody_cond_{i:04d}")
        audio_write(stem, wav.cpu(), model.sample_rate, strategy="loudness")
        output_paths.append(stem + ".wav")

    return output_paths


# ── 4. Music continuation ─────────────────────────────────────────────────────

def continue_music(
    model,
    prompt_path:   str,
    descriptions:  list[str],
    prompt_dur:    float = 5.0,   # Use first N seconds as prompt
    total_dur:     float = 20.0,
    output_dir:    str   = "./generated_music",
) -> list[str]:
    """
    Continue music from an existing audio prompt.
    Useful for extending or transforming existing music.
    """
    Path(output_dir).mkdir(parents=True, exist_ok=True)

    # Load and trim prompt audio
    prompt_wav, prompt_sr = torchaudio.load(prompt_path)
    prompt_samples = int(prompt_dur * prompt_sr)
    prompt_wav     = prompt_wav[:, :prompt_samples]

    model.set_generation_params(duration=total_dur)

    # Replicate prompt for batch
    prompt_batch = prompt_wav.unsqueeze(0).repeat(len(descriptions), 1, 1)

    print(f"Music continuation ({prompt_dur}s prompt → {total_dur}s total)...")
    with torch.no_grad():
        wavs = model.generate_continuation(
            prompt=prompt_batch.to(model.device),
            prompt_sample_rate=prompt_sr,
            descriptions=descriptions,
        )

    output_paths = []
    for i, (wav, desc) in enumerate(zip(wavs, descriptions)):
        stem = os.path.join(output_dir, f"continuation_{i:04d}")
        audio_write(stem, wav.cpu(), model.sample_rate, strategy="loudness")
        output_paths.append(stem + ".wav")

    return output_paths


# ── 5. Sound effects generation ──────────────────────────────────────────────

def generate_sound_effects(
    model,
    descriptions: list[str],
    duration:     float = 5.0,
    temperature:  float = 1.0,
    output_dir:   str   = "./generated_sfx",
) -> list[str]:
    """
    Generate sound effects from text with AudioGen.
    Best for: foley, ambient sounds, UI sounds, nature sounds.
    """
    Path(output_dir).mkdir(parents=True, exist_ok=True)

    model.set_generation_params(
        duration=duration,
        temperature=temperature,
        top_k=250,
    )

    print(f"Generating {len(descriptions)} sound effects ({duration}s each)...")
    with torch.no_grad():
        wavs = model.generate(descriptions)

    output_paths = []
    for i, (wav, desc) in enumerate(zip(wavs, descriptions)):
        stem = os.path.join(output_dir, f"sfx_{i:04d}")
        audio_write(stem, wav.cpu(), model.sample_rate, strategy="loudness")
        output_paths.append(stem + ".wav")
        print(f"  [{i+1}] '{desc[:50]}' → {stem}.wav")

    return output_paths


# ── 6. Prompt engineering for music ──────────────────────────────────────────

STYLE_TEMPLATES = {
    "cinematic":    "{mood} cinematic score, orchestral, {instruments}, film music",
    "electronic":   "{bpm}bpm {subgenre} electronic, synthesizer, {energy} energy",
    "acoustic":     "acoustic {instruments}, {mood}, {setting}, no drums",
    "jazz":         "{tempo} jazz, {instruments}, improvisation, swing rhythm",
    "ambient":      "ambient {mood} soundscape, {setting}, atmospheric, no lyrics",
    "game_music":   "{game_type} video game music, {mood}, 8-bit inspired, looping",
}


def build_music_prompt(
    template: str = "cinematic",
    **kwargs,
) -> str:
    """Build a structured music generation prompt."""
    template_str = STYLE_TEMPLATES.get(template, "{mood} {genre} music")
    # Fill in any provided kwargs, leave rest as-is
    try:
        return template_str.format(**kwargs)
    except KeyError as e:
        # Return partially filled template
        for k, v in kwargs.items():
            template_str = template_str.replace(f"{{{k}}}", v)
        return template_str


def generate_music_set(
    model,
    style:      str = "cinematic",
    variations: int = 4,
    duration:   float = 15.0,
    output_dir: str   = "./music_set",
    **style_kwargs,
) -> list[str]:
    """Generate a set of variations on a style."""
    base_prompt  = build_music_prompt(style, **style_kwargs)
    # Create slight variations
    descriptions = [f"{base_prompt}, variation {i+1}" for i in range(variations)]
    return generate_music(model, descriptions, duration=duration, output_dir=output_dir)


# ── 7. EnCodec audio compression ─────────────────────────────────────────────

def compress_audio(
    audio_path: str,
    bandwidth:  float = 6.0,   # kbps: 1.5, 3.0, 6.0, 12.0, 24.0
    output_path: str = None,
) -> tuple[torch.Tensor, int]:
    """
    Compress audio with EnCodec neural codec.
    Lower bandwidth = more compression, lower quality.
    """
    from audiocraft.models import EncodecModel

    if bandwidth <= 12.0:
        model = EncodecModel.get_pretrained("facebook/encodec_24khz")
    else:
        model = EncodecModel.get_pretrained("facebook/encodec_48khz")

    model.set_target_bandwidth(bandwidth)

    waveform, sr = torchaudio.load(audio_path)
    if sr != model.sample_rate:
        waveform = torchaudio.functional.resample(waveform, sr, model.sample_rate)

    with torch.no_grad():
        encoded   = model.encode(waveform.unsqueeze(0))
        codes     = encoded[0]   # Compressed tokens
        decoded   = model.decode(encoded)

    decoded_wav = decoded.squeeze(0)
    if output_path:
        torchaudio.save(output_path, decoded_wav.cpu(), model.sample_rate)
        print(f"Compressed ({bandwidth}kbps) → {output_path}")

    return decoded_wav, model.sample_rate


# ── Demo ──────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    # Load small model for demo
    model = load_musicgen("small")

    # Basic music generation
    descriptions = [
        "upbeat jazz piano with walking bass and light drums, 120bpm",
        "ambient electronic meditation music, slow tempo, peaceful atmosphere",
        "energetic rock guitar riff, distorted electric guitar, heavy drums",
    ]
    paths = generate_music(model, descriptions, duration=10.0, output_dir="./demo_music")
    print(f"\nGenerated {len(paths)} music clips")
    for p in paths:
        print(f"  {p}")

    # Structured prompt
    cinematic_prompt = build_music_prompt(
        "cinematic",
        mood="tense",
        instruments="strings and brass",
    )
    print(f"\nCinematic prompt: {cinematic_prompt}")

    # Sound effects with AudioGen
    sfx_model = load_audiogen("medium")
    sfx_descriptions = [
        "heavy rain on a rooftop with distant thunder",
        "busy coffee shop ambience with background chatter",
        "notification ping, clean digital sound",
    ]
    sfx_paths = generate_sound_effects(sfx_model, sfx_descriptions, duration=5.0)
    print(f"\nGenerated {len(sfx_paths)} sound effects")

For the Suno API alternative when needing AI music generation with lyrics and full song structure (verse, chorus, bridge) via a managed cloud service — Suno handles vocal synthesis and song structure while AudioCraft runs fully on local hardware with no API costs, enabling unlimited generation for training data creation, game audio production, and research applications where cloud API rate limits and per-generation costs are prohibitive. For the Stable Audio alternative when needing high-quality 44.1kHz stereo music generation with longer durations (up to 3+ minutes) and more consistent genre fidelity — Stable Audio excels at quality for consumer music while AudioCraft’s open-source Apache 2.0 license, melody conditioning, and AudioGen sound effects generation make it the complete open-source toolkit for developers building audio generation into products without licensing restrictions. The Claude Skills 360 bundle includes AudioCraft skill sets covering MusicGen text-to-music, melody conditioning, music continuation, AudioGen sound effects, prompt engineering templates, EnCodec compression, and batch generation pipelines. Start with the free tier to try AI music generation code.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free