r/algorithmicmusic • u/manuelhe • Oct 21 '26
I built a rule-based MIDI composition engine in Python: one JSON file in, multi-track MIDI out
I wrote these two songs with the rule-based MIDI composition engine
I’ve been building Forma Composition, a Python engine that generates multi-track MIDI from a single JSON file. It’s rule-based and seeded, not a neural net, so the same file and seed always give the same MIDI.
Repo: https://github.com/ConnectedReasoning/FormaComposition
It runs from the command line:
python forma/main.py compositions/piece_name.jsonCaveats
- The code is as is. It started as a hand-written legacy script, and I extended it over the past year with big support from Claude
- It’s a work in progress. Some files are large, some comments are historical notes, and I’m cleaning things up over the next few weeks.
- Output is MIDI only. I assign instruments in Logic with Arturia synths. any DAW should work
How a piece is built
You write one JSON file with:
- key, mode, tempo range, and a short motif (diatonic scale steps plus rhythm)
- sections, each with a Roman-numeral progression (tensions like
V7(9,13)work), melody behavior, bass style, groove, swing, drums and a dynamic arc - optional extra voices with counterpoint species settings
Then run python main.py piece.json. The engine runs in this order:
- Validate (Pydantic) and lint, which warns about settings that won’t be heard.
- For each section, generate bass, then melody, then counterpoint/peer voices, then harmony, then drums. Each voice leaves a snapshot (register, contour, density, last transform) that later voices and the next section can read.
- Write a type 1 MIDI file: melody, counterpoint, harmony, bass, drums, plus a chord-symbol marker track.
I recommend setting up a Python virtual environment first, then installing the requirements from the repo.
The theory side
- Motifs develop through 12 transforms (inversion, retrograde, augmentation, diminution, sequence and others). You can script the order, or let the engine pick with form-aware weighting: statement early, development in the middle, return near the end.
- Counterpoint supports species 1–5 and free. Parallel fifths and octaves, hidden fifths and octaves, and strong-beat dissonance are hard-forbidden. Voice crossing and parallel motion are penalized.
- Harmony works in all 7 modes, with per-chord lengths, voicings (closed, open, drop2), and key/mode changes per section.
- Drums lock to the bass onsets and follow section density and swing.
- Song form lets you define verse and chorus once, with an
exact_repeatoption for identical repeats. rhythm_extract.pyturns a rhythm you play in your DAW into a JSON pattern you can paste into a section.
Limits
- Output is MIDI only. I assign instruments in Logic with Arturia synths.
- Sections aren’t fully isolated. Melody openings and transform choice depend on earlier sections, so one edit can ripple forward unless you pin a
transform_sequence. - The rules keep the voice leading clean, but they don’t make a piece memorable. That part is still my ear and the JSON choices.
I'm interested in your feedback
(updated to include repo and guidelines for building)
2
u/manuelhe Oct 22 '26
Repo: https://github.com/ConnectedReasoning/FormaComposition
It runs from the command line:
I recommend setting up a Python virtual environment first, then installing the requirements from the repo.
Caveats
As usual, feedback and questions welcome.
2
u/burnt-store-studio Oct 21 '26
The songs sound decent; I definitely scrubbed through the one that’s 30 minutes long.
I’d be really curious to see your code and JSON samples if you’re willing to share?
Congrats!
2
u/bhagatriks Oct 21 '26
interesting concept. eerily similar to notebin.fm/generate
2
u/Dependent-Sorbet8867 Oct 22 '26
ai added json in a command line host that I built.. seemed rather odd and it could be an ai bias of some kind... I don't see your project though but the music is intresting.
4
u/danja Oct 21 '26
Link? Source code?
I've been been doing something similar using VST plugins. It's been rather more ad hoc, I've let an AI assistant figure out the rules. Lots of smaller plugins rather than a unified system. https://danja.github.io/downspout/algorithms/ I am curious about your JSON notation. My setup uses random seeds for the motifs, it would be nice to have more control.
2
u/Pretend_Bread_437 Oct 22 '26
this is cool
1
u/IWasGettingThePaper Oct 21 '26
yes i can get ai to make me one too