PyTheory: Music Theory for Humans¶
PyTheory is a Python library for exploring music theory, composing multi-part arrangements, and exporting them to MIDI for your DAW.
Use it to learn theory by doing — build chords from intervals and hear the result. Use it to sketch song ideas faster than clicking through a DAW. Use it with Claude Code to prototype music from natural language. Or just use it to answer “what chords are in G major?” without opening a browser.
$ pip install pytheory
Theory¶
The theory layer works everywhere Python runs — no audio setup needed. Tones, scales, chords, keys, intervals, harmony, 16 musical systems, 60+ instruments:
>>> from pytheory import Key, Chord, Tone
>>> Key("C", "major").chords
['C major', 'D minor', 'E minor', 'F major', 'G major', 'A minor', 'B diminished']
>>> [c.symbol for c in Key("G", "major").progression("I", "V", "vi", "IV")]
['G', 'D', 'Em', 'C']
>>> Chord.from_symbol("F#m7b5").identify()
'F# half-diminished 7th'
>>> Tone.from_string("C4").interval_to(Tone.from_string("G4"))
'perfect 5th'
Composition¶
When you’re ready to make noise, the composition layer adds drums, synths, effects, and multi-part arrangements. Sketch an idea, hear it through your speakers, export MIDI, finish in your DAW:
from pytheory import Score, Key, Duration
from pytheory.play import play_score
score = Score("4/4", bpm=120)
score.drums("rock", repeats=8, fill="rock", fill_every=4)
piano = score.part("piano", instrument="piano", reverb=0.3)
lead = score.part("lead", synth="saw", envelope="pluck",
delay=0.2, reverb=0.2, lowpass=4000)
bass = score.part("bass", synth="triangle", lowpass=900)
for chord in Key("G", "major").progression("I", "V", "vi", "IV") * 2:
piano.add(chord, Duration.WHOLE)
lead.add("D5", 1).add("B4", 0.5).add("D5", 0.5)
lead.add("G5", 1).add("E5", 1)
lead.add("D5", 0.5).add("B4", 0.5).add("A4", 1)
lead.add("G4", 2).rest(2)
for n in ["G2", "G2", "D2", "D2", "E2", "E2", "C2", "C2"] * 2:
bass.add(n, Duration.HALF)
play_score(score)
Or hear a randomly generated track from the command line — different every time:
$ pytheory demo
What’s Inside¶
Theory — tones, scales (40+ across 16 systems), chords (17 types), keys, Roman numeral analysis, figured bass, pitch class sets (Forte numbers), scale recommendation, modulation, voice leading, enharmonic support (Cb, Fb, E#, B#, double sharps/flats, unicode symbols)
Sequencing — Score, Parts, arpeggiator, legato/glide, velocity, swing, humanize, tempo changes, song sections with repeat, strumming, pitch bends (3 types), rolls, tuning systems (TET factory, 4 temperaments, reference_pitch)
Synthesis — 41 waveforms (including Karplus-Strong pluck, Hammond organ, bowed string, granular, vocal/formant, and 31 dedicated instrument synths), 10 envelopes, 60+ instrument presets, configurable FM, sub-oscillator, noise layer, filter envelope, velocity-to-brightness, analog oscillator drift, detune, stereo pan/spread, 80+ drum patterns (stereo panned, including world percussion and cajón), 21 fills, 11 microtonal systems
Effects — reverb (algorithmic + 7 convolution IRs, stereo), delay, lowpass/highpass (with resonance), distortion, guitar cabinet simulation, saturation, chorus, phaser, tremolo, analog drift, sidechain compression, automation, LFOs. Master bus compressor/limiter
Instruments — 60+ presets with fingering generation, guitar strumming, pitch bends, note choking
Output — stereo playback, WAV export, MIDI import/export
Interface — REPL with tab completion, CLI (15 commands),
pytheory demo, KeyboardInterrupt handling for clean stopAI-friendly — Claude Code can compose and play music through PyTheory from natural language
User Guide
- Quickstart
- Music Theory Fundamentals
- Working with Tones
- Working with Scales
- Working with Chords
- Chord Construction
- Inversions
- Extended Chords
- Using the Chord Chart
- Building Chords
- Intervals
- Consonance and Dissonance
- Transposition
- Chord Manipulation
- Chord Identification
- Harmonic Analysis
- Tension and Resolution
- Voice Leading
- Tritone Substitution
- The Overtone Series
- Chord Symbols
- Slash Chords
- Drop Voicings
- Chord Extensions
- Borrowed Chord Analysis
- Figured Bass
- Pitch Class Sets
- Instruments and Fingerings
- Musical Systems
- Sequencing
- Duration
- Time Signatures
- Score Basics
- Parts
- Polyphonic Hold
- Arpeggiator
- Legato and Glide
- Complete Example
- Velocity
- Articulations
- Dynamic Curves
- Part.hit() — Manual Drum Placement
- Rudiments — Flam, Diddle, Cheese
- Ensemble
- Swing and Groove
- Tempo Changes
- Fades
- Parameter Ramps
- Humanize
- Song Structure
- Guitar Strumming
- Pitch Bends
- Rolls
- Tuning Systems
- Synthesizers
- Effects
- Drums
- Playback and Export
- play() – Single Tones and Chords
- play_score() – Full Arrangements
- render_score() – Headless Rendering
- save() – WAV Export
- save_midi() – MIDI Export
- to_abc() – ABC Notation / Sheet Music
- to_lilypond() – LilyPond Export
- to_musicxml() – MusicXML Export
- to_tab() – Guitar/Bass Tablature
- play_pattern() – Drum Patterns
- play_progression() – Quick Chord Playback
- MIDI Import
- Interactive REPL
- Command-Line Interface
- Cookbook
- Analyze a Song
- Write a 12-Bar Blues
- Find Chords in a Key
- Compare Scales
- Guitar Chord Chart
- Explore an Interval
- Walk the Circle of Fifths
- Voice Leading Between Chords
- Measure Harmonic Tension
- Tritone Substitution (Jazz)
- Key Signatures and Detection
- Relative and Parallel Keys
- Borrowed Chords and Secondary Dominants
- The Overtone Series
- Enharmonic Spellings
- World Scales
- Visualize a Scale on Guitar
- Composition Recipes
API Reference
- Tones
- Scales
- Chords and Fretboard
- Chord Charts
- Audio Playback
sine_wave()sawtooth_wave()triangle_wave()square_wave()pulse_wave()fm_wave()noise_wave()supersaw_wave()pwm_wave()pwm_slow_wave()pwm_fast_wave()hard_sync_wave()ring_mod_wave()wavefold_wave()drift_wave()pluck_wave()organ_wave()strings_wave()piano_wave()rhodes_wave()wurlitzer_wave()mellotron_wave()vibraphone_wave()pipe_organ_wave()choir_wave()bass_guitar_wave()flute_wave()trumpet_wave()clarinet_wave()marimba_wave()oboe_wave()harpsichord_wave()cello_wave()harp_wave()upright_bass_wave()timpani_wave()saxophone_wave()vocal_wave()granular_wave()pedal_steel_wave()theremin_wave()kalimba_wave()steel_drum_wave()harmonium_wave()accordion_wave()didgeridoo_wave()bagpipe_wave()banjo_wave()mandolin_wave()ukulele_wave()acoustic_guitar_wave()electric_guitar_wave()sitar_wave()crotales_wave()tingsha_wave()singing_bowl_strike_wave()singing_bowl_ring_wave()EnvelopeSynthplay()save()play_progression()play_pattern()render_score()play_score()save_midi()
- Systems
Project
Music is math that makes you feel something. PyTheory gives you the math. What you feel is up to you.