Skip to content

A Python application that uses the Claude Code Python SDK to generate unique MIDI music files with the Music21 library from MIT.

License

Notifications You must be signed in to change notification settings

bryancraven/midi-gen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

midi-gen

A standalone Python application that uses the Claude Code Python SDK to generate unique MIDI music files. Each request creates fresh, custom Python code using the music21 library - no templates or pre-made patterns.

Built with music21 - A toolkit for computer-aided musicology developed by MIT. This project gratefully acknowledges the music21 project and its contributors for providing the comprehensive music theory and MIDI generation capabilities that power this agent.

Powered by Claude Code SDK - Anthropic's Python SDK for building AI agents. This project gratefully acknowledges Anthropic for providing the Claude Code SDK that enables the AI-powered code generation at the heart of this application.

Features

  • 🎡 Unique Every Time: Writes fresh Python code for each music request
  • πŸ€– AI-Powered: Uses Claude Code SDK for intelligent music generation
  • 🎼 Professional Music Theory: Full music21 library capabilities
  • ⏱️ Smart Duration: Automatically determines appropriate length based on context
  • 🎹 Multiple Genres: Classical, jazz, pop, electronic, and more
  • 🎨 Creative Interpretation: Handles abstract and playful requests

Installation

Prerequisites

Setup

  1. Clone the repository:
git clone git@github.com:bryancraven/midi-gen.git
cd midi-gen
  1. Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install claude-code-sdk music21
  1. Set your Anthropic API key:
export ANTHROPIC_API_KEY="sk-ant-..."  # Mac/Linux
# or
set ANTHROPIC_API_KEY="sk-ant-..."     # Windows

Usage

Run the application:

python creative_midi_agent.py

Then enter your music request when prompted. Examples:

Simple Requests

  • "Create a happy birthday tune"
  • "Generate a spooky Halloween melody"
  • "Make a relaxing piano piece"

Creative Requests

  • "Generate music for two kittens playing together"
  • "Create a robot learning to dance"
  • "Make a victory fanfare for completing a puzzle"

Technical Requests

  • "Generate a 12-bar blues in B flat"
  • "Create a fugue in the style of Bach"
  • "Make a jazz piece with ii-V-I progressions"

Duration-Specific

  • "Create a 5-second doorbell chime"
  • "Generate a 3-minute classical sonata"
  • "Make a 30-second ringtone"

How It Works

  1. Input: You describe the music you want
  2. AI Processing: Claude writes custom Python code using music21
  3. Code Generation: Fresh code is created (no templates)
  4. Execution: The code runs to generate your MIDI file
  5. Output: MIDI file saved with descriptive name

Music Duration Intelligence

The agent automatically determines appropriate duration:

  • General music: 2-3 minutes (default)
  • Sound effects: 2-10 seconds
  • Classical pieces: 3-8 minutes
  • Jingles: 5-15 seconds
  • Game loops: 1-2 minutes
  • Ringtones: 20-30 seconds

Advanced Features

Music Theory Capabilities

  • Scales: Major, Minor, Modes, Pentatonic, Blues, Chromatic
  • Chords: Triads to extended jazz harmony
  • Progressions: Genre-specific patterns
  • Articulations: Staccato, legato, accents
  • Ornaments: Trills, mordents, grace notes
  • Dynamics: Full range with crescendo/diminuendo

Technical Features

  • Voice leading and counterpoint
  • Complex rhythms and tuplets
  • Key changes and modulation
  • Multiple instrument parts
  • Humanization (timing/velocity variations)
  • Proper MIDI velocity for audible output

Output

Generated MIDI files are saved in the current directory with descriptive names:

  • playful_kittens_waltz_[timestamp].mid
  • epic_orchestral_fanfare_[timestamp].mid
  • mysterious_ambient_loop_[timestamp].mid

Project Structure

midi-gen/
β”œβ”€β”€ creative_midi_agent.py     # Main application
β”œβ”€β”€ CLAUDE.md                  # Project context documentation
β”œβ”€β”€ README.md                  # This file
β”œβ”€β”€ requirements.txt           # Python dependencies
└── *.mid                      # Generated MIDI files

Configuration

Edit creative_midi_agent.py to modify:

  • Model version (default: claude-sonnet-4-20250514)
  • Max turns (default: 10)
  • Output directory
  • File naming pattern

Troubleshooting

Common Issues

No sound in MIDI file:

  • The agent sets proper velocities, but check your MIDI player volume

Generation takes too long:

  • Request shorter pieces or simpler arrangements

Import errors:

  • Ensure both claude-code-sdk and music21 are installed
  • Check your Python version (3.8+ required)

API key issues:

  • Ensure ANTHROPIC_API_KEY environment variable is set
  • Check that your API key is valid and has sufficient credits

Best Practices

  1. Be specific about genre for better results
  2. Mention mood or energy level
  3. Specify duration if you have requirements
  4. Request specific instruments when needed

Examples of Generated Music

The agent can create:

  • Classical pieces (sonatas, fugues, Γ©tudes)
  • Jazz standards (with walking bass, chord changes)
  • Pop songs (verse-chorus structure)
  • Electronic music (loops, drops, builds)
  • Sound effects (stingers, transitions)
  • Children's music (playful, simple melodies)
  • Ambient backgrounds (atmospheric, evolving)

Compatible Software

Generated MIDI files work with:

  • DAWs: Ableton Live, FL Studio, Logic Pro, GarageBand, Reaper
  • Notation: MuseScore, Sibelius, Finale
  • Players: VLC, Windows Media Player, QuickTime
  • Online: BandLab, Soundtrap

Dependencies

  • claude-code-sdk: Claude Code Python SDK for AI integration
  • music21: MIT's music theory and MIDI generation library

Requirements

Create a requirements.txt file:

claude-code-sdk
music21

License

MIT License - See LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Acknowledgments

  • music21 - MIT's powerful toolkit for computer-aided musicology (BSD 3-Clause License)
    • Created by Michael Scott Asato Cuthbert and contributors
    • Provides comprehensive music theory, analysis, and generation capabilities
  • Claude Code SDK - Anthropic's SDK for Claude integration
  • Anthropic Claude - AI model powering the creative generation

Support

For issues or suggestions, please open an issue on GitHub.

About

A Python application that uses the Claude Code Python SDK to generate unique MIDI music files with the Music21 library from MIT.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages