iTranslated by AI
Transforming Claude Code into a 16-Step Beat Sequencer
Introduction
I built a 16-step beat sequencer using Claude Code extensions (skills / agents).
Based on the mechanics of Mastra AI Beats Lab, a web-based beat maker using the AI agent framework Mastra, I reproduced this in Claude Code to allow beat generation directly from the terminal.
/beat "lofi, jazzy, dusty, 92bpm"
With just this command, JSON, MIDI, WAV, and MP3 files are generated.
What I Built
Demo
An example of generating a "sleepy beat":
BPM: 68 Swing: 0.15
| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|
------------+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Kick | X| | | | | | | | X| | | | | | | |
Snare | | | | | | | | | | | | | X| | | |
HiHat | | | | | | | X| | | | | | | | X| |
------------+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
C4 | O| | | | | | | | O| | | | | | | |
B3 | | | | | O| | | | | | | | | | | |
A3 | | | | | | | | | | | | | O| | | |
------------+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Beat |1 | | | |2 | | | |3 | | | |4 | | | |
Main Features
- Text-to-beat generation: Generate beats using style prompts like "lofi, jazzy" or "sleepy"
- Multi-format output: JSON / MIDI / WAV / MP3
- ASCII grid display: Visual confirmation in the terminal
- DAW integration: MIDI output compatible with any DAW
Architecture
A configuration that makes full use of Claude Code's extension system:
/beat "<prompt>"
│
▼
┌─────────────────────────────────┐
│ music-reference-agent (haiku) │ ← Style interpretation
│ prompt → BeatSpec YAML │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ music-generation-agent (sonnet)│ ← Beat generation
│ BeatSpec → Beat JSON │
└─────────────────────────────────┘
│
▼
┌─────────────────────────────────┐
│ beatlab-pipeline (skill) │ ← Validation & Output
│ validate → render → export │
└─────────────────────────────────┘
Extension Configuration
| Type | Role | File |
|---|---|---|
| Skill | User command /beat
|
.claude/skills/beat/SKILL.md |
| Skill | JSON contract definition | .claude/skills/beatlab-contract/SKILL.md |
| Skill | Pipeline script | .claude/skills/beatlab-pipeline/ |
| Agent | Style interpretation (haiku) | .claude/agents/music-reference-agent.md |
| Agent | Beat generation (sonnet) | .claude/agents/music-generation-agent.md |
| Hook | Auto-playback after generation | .claude/hooks/play-audio.sh |
Usage
Setup
git clone https://github.com/ryok/claude-beatlab
cd claude-beatlab
uv sync # Python environment setup
Beat Generation
claude # Launch Claude Code
# Generate a beat with a style prompt
/beat "lofi, jazzy, dusty, 92bpm, minimal melody"
# Japanese input is also supported
/beat "眠くなるビート"
/beat "激しいビート"
Output Files
beats/20260127_043543/
├── beat.json # Beat data
├── beat.mid # MIDI (for DAW)
├── beat.wav # Audio
└── beat.mp3 # Compressed audio
Implementation Highlights
1. Two-Stage Agent Configuration
Why split it into two agents?:
# music-reference-agent (haiku) - Lightweight and fast
# Specialized in style interpretation
bpm: 68
swing: 0.15
drum_plan: "Kick on 0, 8; soft snare on 12"
melody_plan: "Single repeating motif (C4, B3)"
density: low
// music-generation-agent (sonnet) - High precision
// Actual beat data generation
{
"bpm": 68,
"swing": 0.15,
"drumSequence": { "Kick": [0, 8], "Snare": [12] },
"pianoSequence": { "C4": [0, 8], "B3": [4] }
}
Benefits:
- Fast style interpretation with Haiku → Low cost
- Accurate JSON generation with Sonnet → High quality
- Easier debugging due to separation of concerns
2. Contract Definition via Skills
The beatlab-contract skill strictly defines the JSON format:
Allowed piano notes (fixed):
C5, B4, A4, G4, F4, E4, D4, C4, B3, A3, G3
Allowed drum sounds (fixed):
Kick, Snare, HiHat, Clap, OpenHat, Tom, Crash, Ride, Shaker, Cowbell
Steps: 0-15 only
This ensures:
- The agent generates accurate JSON by referencing the contract
- Verification via validation scripts
- Quality assurance with a maximum of 3 retries
3. Deterministic Pipeline
AI outputs are processed reliably using Python scripts:
# validate.py - JSON validation & normalization
# render_grid.py - ASCII grid generation
# export_midi.py - MIDI output
# render_wav.py - Audio synthesis
Scripts are executed with uv run python to eliminate environment dependencies.
4. Automatic Playback via Hooks
Using Claude Code's Hooks feature, audio can be played automatically after beat generation is complete:
// .claude/settings.json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [{ "type": "command", "command": "bash .claude/hooks/play-audio.sh" }]
}
]
}
}
The PostToolUse hook is triggered after Bash command execution. It detects the completion of MP3 conversion by ffmpeg and plays it using afplay (macOS):
# .claude/hooks/play-audio.sh
if [[ "$command" == *"ffmpeg"* ]] && [[ "$command" == *"beat.mp3"* ]]; then
mp3_path=$(echo "$command" | grep -oE 'beats/[0-9_]+/beat\.mp3')
afplay "$mp3_path" & # Play in background
fi
This allows the generated beat to play automatically after executing /beat.
Beat JSON Format
It adopts a JSON format fully compatible with the Mastra AI Beats Lab. Generated beats can also be opened, edited, and played in the Mastra AI Beats Lab Web UI:
{
"bpm": 68,
"swing": 0.15,
"pianoSequence": {
"C4": [0, 8],
"B3": [4],
"A3": [12]
},
"drumSequence": {
"Kick": [0, 8],
"Snare": [12],
"HiHat": [6, 14]
}
}
Future Outlook
- Visual editing in Web UI
- Loop playback and real-time preview
- Chained playback of multiple patterns
- Support for custom sound sources
Summary
By combining Claude Code's extension system (skills / agents), I was able to realize a text-based beat sequencer.
Key takeaways:
- Skills: User command definitions and bundling reusable knowledge
- Agents: AI task execution leveraging different models (haiku / sonnet)
- Pipeline: Reliable processing of AI output with Python scripts
- Hooks: Automating events after tool execution
Claude Code can be utilized not just as an AI coding assistant, but as an extensible platform.
Discussion