r/algorithmicmusic • • 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

https://youtu.be/PgwJsWXm-Ew

https://youtu.be/dEhyCWyddKg

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.json

Caveats

  • 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:

  1. Validate (Pydantic) and lint, which warns about settings that won’t be heard.
  2. 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.
  3. 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_repeat option for identical repeats.
  • rhythm_extract.py turns 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)

13 Upvotes

12 comments sorted by

2

u/manuelhe Oct 22 '26

Repo: https://github.com/ConnectedReasoning/FormaComposition

It runs from the command line:

python forma/main.py compositions/piece_name.json

I recommend setting up a Python virtual environment first, then installing the requirements from the repo.

Caveats

  • 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

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/manuelhe Oct 22 '26

I play the 30 minute song in my sleep playlist

2

u/burnt-store-studio Oct 22 '26

I think that song is perfect for a sleep playlist! I’m not sure I was as effusive in my previous comment as I meant to be. I’m really impressed with the sensible choices your system makes. Not easy. I respect that! 🙂

2

u/bhagatriks Oct 21 '26

interesting concept. eerily similar to notebin.fm/generate

1

u/manuelhe Oct 21 '26

That’s a very good interface. Mine is a simpler command line interface. Will post repo later today. Similarity isn’t surprising if we are both working from the same Western musical tradition

2

u/bhagatriks Oct 21 '26

love it! good luck on your project. i have a world section too, non-western music is incredibly interesting to work through

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.

2

u/manuelhe Oct 22 '26

I had posted code in a separate response but I merged it into the original post.

The JSON format was my idea.

There is a legacy folder in the repo that contains the first iteration of the script that I wrote before ChatGPT and Claude were a thing.

It used an array of dictionaries containing MIDI scales to drive the voices across registers using random walks.

That's what gave me the idea that JSON would work in the larger project.

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.

1

u/IWasGettingThePaper Oct 21 '26

yes i can get ai to make me one too