---
title: "Algorithmic Drum Pattern Generation"
author: "MML Team"
description: "Demonstrates algorithmic drum programming using random() for velocity and @loop for patterns"
midi_format: 1
ppq: 480
tempo: 120
time_signature: [4, 4]
---

# ============================================
# MIDI Markup Language - Algorithmic Drums
# ============================================
#
# This example demonstrates professional drum pattern generation
# using algorithmic techniques:
#
# Techniques covered:
#   1. Kick drum patterns with velocity humanization
#   2. Snare drum with ghost notes
#   3. Hi-hat patterns (closed and open)
#   4. Layered polyrhythmic patterns
#   5. Dynamic drum fill variations
#   6. Practical drum programming practices
#
# Drum note mappings (GM standard):
#   - Kick: C2 (36)
#   - Snare: D2 (38)
#   - Closed Hi-Hat: F#2 (42)
#   - Open Hi-Hat: A#2 (46)
#   - Crash: C#3 (49)

# ============================================
# Section 1: Basic Kick Drum Pattern
# ============================================

[00:00.000]
- marker "Kick Pattern"
- text "Four-on-the-floor kick with humanized velocity"

# Simple four-on-the-floor pattern with velocity variation
# Kicks on beats 1, 2, 3, 4 with slightly different dynamics
[00:00.000]
- note_on 10.C2 random(100,115) 0.2b  # Beat 1 - strong
[00:01.000]
- note_on 10.C2 random(90,105) 0.2b   # Beat 2 - medium
[00:02.000]
- note_on 10.C2 random(95,110) 0.2b   # Beat 3 - strong
[00:03.000]
- note_on 10.C2 random(85,100) 0.2b   # Beat 4 - weak

# Repeat pattern with accent on downbeat
[00:04.000]
- text "Kick with accent pattern"
@loop 3 times at [00:04.000] every 4b
  - note_on 10.C2 random(105,120) 0.2b  # Downbeat accent
  - text "Kick pattern repeat"
@end

[00:04.000]
- note_on 10.C2 random(105,120) 0.2b
[00:05.000]
- note_on 10.C2 random(85,100) 0.2b
[00:06.000]
- note_on 10.C2 random(85,100) 0.2b
[00:07.000]
- note_on 10.C2 random(80,95) 0.2b

[00:08.000]
- note_on 10.C2 random(105,120) 0.2b
[00:09.000]
- note_on 10.C2 random(85,100) 0.2b
[00:10.000]
- note_on 10.C2 random(85,100) 0.2b
[00:11.000]
- note_on 10.C2 random(80,95) 0.2b

[00:12.000]
- note_on 10.C2 random(105,120) 0.2b
[00:13.000]
- note_on 10.C2 random(85,100) 0.2b
[00:14.000]
- note_on 10.C2 random(85,100) 0.2b
[00:15.000]
- note_on 10.C2 random(80,95) 0.2b

# ============================================
# Section 2: Snare Drum with Ghost Notes
# ============================================

[00:16.000]
- marker "Snare Pattern"
- text "Snare with ghost notes for depth"

# Main snare hits on beats 2 and 4 (backbeat)
# Ghost notes in between for pocket
[00:16.000]
- note_on 10.D2 random(105,120) 0.2b  # Strong snare on 2
[00:16.480]
- note_on 10.D2 random(20,35) 0.15b   # Ghost note between 2-3
[00:17.000]
- note_on 10.D2 random(20,35) 0.15b   # Ghost note between 3-4
[00:18.000]
- note_on 10.D2 random(105,120) 0.2b  # Strong snare on 4

# Expanded snare pattern with multiple ghost notes
[00:20.000]
- text "Dense ghost note pattern"
- note_on 10.D2 random(110,125) 0.2b    # Main hit
[00:20.480]
- note_on 10.D2 random(15,30) 0.1b      # Ghost note 1
[00:20.730]
- note_on 10.D2 random(10,25) 0.1b      # Ghost note 2
[00:20.980]
- note_on 10.D2 random(15,30) 0.1b      # Ghost note 3

[00:22.000]
- note_on 10.D2 random(110,125) 0.2b
[00:22.480]
- note_on 10.D2 random(15,30) 0.1b
[00:22.730]
- note_on 10.D2 random(10,25) 0.1b
[00:22.980]
- note_on 10.D2 random(15,30) 0.1b

# ============================================
# Section 3: Hi-Hat Patterns
# ============================================

[00:24.000]
- marker "Hi-Hat Patterns"
- text "Closed and open hi-hat dynamics"

# Straight eighth-note hi-hat pattern (closed)
# Alternating velocity for pocket
[00:24.000]
- text "Eighth-note closed hi-hat pattern"
@loop 8 times at [00:24.000] every 0.5b
  - note_on 10.F#2 random(45,55) 0.2b
@end

# Sixteenth-note pattern with accents
[00:28.000]
- text "Sixteenth-note hi-hat with accents"
@loop 16 times at [00:28.000] every 0.25b
  - note_on 10.F#2 random(35,50) 0.15b
@end

# Open hi-hat on offbeats for swing
[00:32.000]
- text "Closed hi-hat with open accents"
- note_on 10.F#2 random(50,60) 0.2b     # On beat
[00:32.500]
- note_on 10.F#2 random(45,55) 0.2b
[00:33.000]
- note_on 10.A#2 random(55,70) 0.3b     # Open on &
[00:33.500]
- note_on 10.F#2 random(45,55) 0.2b

[00:34.000]
- note_on 10.F#2 random(50,60) 0.2b
[00:34.500]
- note_on 10.F#2 random(45,55) 0.2b
[00:35.000]
- note_on 10.A#2 random(55,70) 0.3b
[00:35.500]
- note_on 10.F#2 random(45,55) 0.2b

[00:36.000]
- note_on 10.F#2 random(50,60) 0.2b
[00:36.500]
- note_on 10.F#2 random(45,55) 0.2b
[00:37.000]
- note_on 10.A#2 random(55,70) 0.3b
[00:37.500]
- note_on 10.F#2 random(45,55) 0.2b

[00:38.000]
- note_on 10.F#2 random(50,60) 0.2b
[00:38.500]
- note_on 10.F#2 random(45,55) 0.2b
[00:39.000]
- note_on 10.A#2 random(55,70) 0.3b
[00:39.500]
- note_on 10.F#2 random(45,55) 0.2b

# ============================================
# Section 4: Complete Drum Groove
# ============================================

[00:40.000]
- marker "Complete Groove"
- text "Full drum kit pattern: kick, snare, hi-hats"

# Four-bar groove with all elements
# Bar 1: Basic pattern
[00:40.000]
- text "Bar 1: Basic groove"
- note_on 10.C2 random(100,115) 0.2b   # Kick 1
[00:41.000]
- note_on 10.D2 random(110,125) 0.2b   # Snare 2
- note_on 10.F#2 random(48,58) 0.15b   # Hi-hat on 2&
[00:41.500]
- note_on 10.F#2 random(48,58) 0.15b
[00:42.000]
- note_on 10.C2 random(90,105) 0.2b    # Kick 3
- note_on 10.F#2 random(48,58) 0.15b   # Hi-hat
[00:42.500]
- note_on 10.F#2 random(48,58) 0.15b
[00:43.000]
- note_on 10.D2 random(105,120) 0.2b   # Snare 4
- note_on 10.F#2 random(48,58) 0.15b
[00:43.500]
- note_on 10.A#2 random(60,75) 0.25b   # Open hat

# Bar 2: Syncopated variation
[00:44.000]
- text "Bar 2: Syncopated variation"
- note_on 10.C2 random(105,120) 0.2b
[00:44.500]
- note_on 10.C2 random(85,100) 0.15b   # Second kick
[00:45.000]
- note_on 10.D2 random(110,125) 0.2b
[00:45.500]
- note_on 10.D2 random(20,35) 0.1b     # Ghost note
[00:46.000]
- note_on 10.C2 random(95,110) 0.2b
[00:47.000]
- note_on 10.D2 random(105,120) 0.2b
- note_on 10.A#2 random(60,75) 0.25b

# Bar 3: Sparse (for contrast)
[00:48.000]
- text "Bar 3: Sparse pattern"
- note_on 10.C2 random(100,115) 0.2b
[00:49.000]
- note_on 10.D2 random(110,125) 0.2b
[00:50.000]
- note_on 10.C2 random(90,105) 0.2b

# Bar 4: Build (fill-like)
[00:52.000]
- text "Bar 4: Build to fill"
- note_on 10.C2 random(105,120) 0.2b
[00:52.500]
- note_on 10.C2 random(85,100) 0.15b
[00:53.000]
- note_on 10.D2 random(115,127) 0.2b   # Louder snare
[00:53.500]
- note_on 10.C2 random(90,105) 0.2b
[00:54.000]
- note_on 10.D2 random(115,127) 0.2b
[00:54.500]
- note_on 10.C2 random(95,110) 0.2b
[00:55.000]
- note_on 10.D2 random(110,125) 0.2b
[00:55.500]
- note_on 10.C2 random(100,115) 0.2b   # Rolling kick

# ============================================
# Section 5: Dynamic Drum Fill
# ============================================

[00:56.000]
- marker "Drum Fill"
- text "Rhythmic fill with acceleration"

# Build tension with kick roll
[00:56.000]
- text "Kick roll (8th notes)"
@loop 8 times at [00:56.000] every 0.5b
  - note_on 10.C2 random(80,100) 0.3b
@end

# Snare roll builds energy
[00:60.000]
- text "Snare roll (16th notes)"
@loop 16 times at [00:60.000] every 0.25b
  - note_on 10.D2 random(100,120) 0.15b
@end

# Crash cymbal hit to transition
[00:64.000]
- marker "Transition"
- text "Crash cymbal hit"
- note_on 10.C#3 random(110,127) 0.5b  # Crash

# ============================================
# Section 6: Polyrhythmic Experiment
# ============================================

[00:66.000]
- marker "Polyrhythm"
- text "3 against 4 kick pattern"

# 3-note kick pattern over 4 beat bar
# Creates interesting rhythmic tension
[00:66.000]
- text "Triplet kick over straight time"
- note_on 10.C2 random(100,115) 0.2b   # 1
[00:66.667]
- note_on 10.C2 random(90,105) 0.2b    # 1.67
[00:67.333]
- note_on 10.C2 random(95,110) 0.2b    # 2.33
[00:68.000]
- note_on 10.C2 random(85,100) 0.2b    # 3

# Repeat polyrhythmic pattern
[00:69.000]
- text "Repeat polyrhythm"
- note_on 10.C2 random(100,115) 0.2b
[00:69.320]
- note_on 10.C2 random(90,105) 0.2b
[00:69.640]
- note_on 10.C2 random(95,110) 0.2b
[00:69.960]
- note_on 10.C2 random(85,100) 0.2b

[00:73.000]
- note_on 10.C2 random(100,115) 0.2b
[00:73.320]
- note_on 10.C2 random(90,105) 0.2b
[00:73.640]
- note_on 10.C2 random(95,110) 0.2b
[00:73.960]
- note_on 10.C2 random(85,100) 0.2b

[00:77.000]
- note_on 10.C2 random(100,115) 0.2b
[00:77.320]
- note_on 10.C2 random(90,105) 0.2b
[00:77.640]
- note_on 10.C2 random(95,110) 0.2b
[00:77.960]
- note_on 10.C2 random(85,100) 0.2b

# ============================================
# Section 7: Best Practices & Tips
# ============================================

[00:81.000]
- marker "Best Practices"
- text "Drum programming guidelines"

# Tip 1: Velocity ranges by instrument
[00:81.000]
- text "Kick (100-120) vs Snare (105-127) vs Hi-hat (40-60)"
- note_on 10.C2 random(100,120) 0.2b
- note_on 10.D2 random(105,127) 0.2b
- note_on 10.F#2 random(40,60) 0.15b

# Tip 2: Ghost notes add musicality
[00:82.000]
- text "Ghost notes (15-35 velocity) for pocket"
- note_on 10.D2 random(110,125) 0.2b   # Main hit
[00:82.480]
- note_on 10.D2 random(15,35) 0.1b     # Ghost note

# Tip 3: Accent patterns create groove
[00:83.000]
- text "Accent every 3rd or 4th hit"
- note_on 10.F#2 random(45,55) 0.2b    # Regular
[00:83.480]
- note_on 10.F#2 random(45,55) 0.2b
[00:83.960]
- note_on 10.F#2 random(60,75) 0.2b    # Accented
[00:84.440]
- note_on 10.F#2 random(45,55) 0.2b

# Tip 4: Open hi-hats on specific beats
[00:85.000]
- text "Open hi-hat on '&' of beat 2 and 4"
- note_on 10.F#2 random(50,60) 0.2b
[00:85.500]
- note_on 10.A#2 random(65,80) 0.3b    # Open
[00:86.000]
- note_on 10.F#2 random(50,60) 0.2b
[00:86.500]
- note_on 10.A#2 random(65,80) 0.3b    # Open

# ============================================
# Conclusion
# ============================================

[00:87.000]
- marker "Summary"
- text "Algorithmic drum programming techniques demonstrated"

[00:88.000]
- marker "End"
- text "Example complete"

# Summary of techniques used:
# 1. Velocity humanization for pocket and feel
# 2. Layered instrument patterns (kick, snare, hats)
# 3. Ghost notes for musicality
# 4. @loop for repetitive patterns with variations
# 5. Accent patterns for groove
# 6. Polyrhythmic experiments
# 7. Crash cymbal for transitions
#
# Key constraints remembered:
# - random() works for velocity (2nd param) and note names
# - Use drum note names: C2 (kick), D2 (snare), F#2 (closed hat), A#2 (open hat)
# - Use beat durations (0.5b, 0.25b) for proper timing
# - Variables must be UPPERCASE (unused here, but use when needed)
#
# See also:
# - random_humanization.mml for foundational techniques
# - examples/02_simple_click_track.mml for basic drum patterns
# - examples/04_tempo_changes.mml for dynamic arrangements

[00:90.000]
- marker "EOF"
