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.
- π΅ 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
- Python 3.8 or higher
- Anthropic API key (get one at console.anthropic.com)
- Clone the repository:
git clone git@github.com:bryancraven/midi-gen.git
cd midi-gen- Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install claude-code-sdk music21- Set your Anthropic API key:
export ANTHROPIC_API_KEY="sk-ant-..." # Mac/Linux
# or
set ANTHROPIC_API_KEY="sk-ant-..." # WindowsRun the application:
python creative_midi_agent.pyThen enter your music request when prompted. Examples:
- "Create a happy birthday tune"
- "Generate a spooky Halloween melody"
- "Make a relaxing piano piece"
- "Generate music for two kittens playing together"
- "Create a robot learning to dance"
- "Make a victory fanfare for completing a puzzle"
- "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"
- "Create a 5-second doorbell chime"
- "Generate a 3-minute classical sonata"
- "Make a 30-second ringtone"
- Input: You describe the music you want
- AI Processing: Claude writes custom Python code using music21
- Code Generation: Fresh code is created (no templates)
- Execution: The code runs to generate your MIDI file
- Output: MIDI file saved with descriptive name
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
- 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
- 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
Generated MIDI files are saved in the current directory with descriptive names:
playful_kittens_waltz_[timestamp].midepic_orchestral_fanfare_[timestamp].midmysterious_ambient_loop_[timestamp].mid
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
Edit creative_midi_agent.py to modify:
- Model version (default: claude-sonnet-4-20250514)
- Max turns (default: 10)
- Output directory
- File naming pattern
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-sdkandmusic21are 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
- Be specific about genre for better results
- Mention mood or energy level
- Specify duration if you have requirements
- Request specific instruments when needed
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)
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
claude-code-sdk: Claude Code Python SDK for AI integrationmusic21: MIT's music theory and MIDI generation library
Create a requirements.txt file:
claude-code-sdk
music21
MIT License - See LICENSE file for details
Contributions are welcome! Please feel free to submit issues or pull requests.
- 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
For issues or suggestions, please open an issue on GitHub.