Inspiration

Music-theory tools check your work after the fact. ConstraintLab flips it: you declare the voice-leading rules, and a constraint solver searches for a piece that obeys every one of them - then shows its work. No accounts, no server, no network calls: the whole thing runs in your browser.

What it does

ConstraintLab composes a 16-bar AABA piece in four-part harmony (root-position triads, harmonic minor or major keys). You pick the key, mode, and seed, and switch rules on or off: no parallel fifths, no parallel octaves, no voice crossing, the soprano's leading tone resolves to the tonic, a maximum leap for the upper voices, and a dominant-function chord before the final tonic. The solver searches eight-chord phrases and four-voice voicings and keeps only candidates that obey every rule you switched on - rules you switch off are shown as OFF, never silently passed. The proof panel lists every enforced rule with how many candidates were rejected along the way, and the same rules hold across the phrase seams. The piano roll plays the result with Web Audio; the same seed and same rules always give the same piece, and share links encode only the constraints and the seed.

How we built it

A backtracking constraint solver in plain JavaScript generates candidate chord progressions and voicings, enforcing each rule during the search, including across the A-A, A-B and B-A phrase seams. The front end is a single static page: a canvas piano roll, Web Audio playback, and a WAV export that renders the identical piece offline. Built with AI assistance (design and code), disclosed per hackathon rules.

Challenges we ran into

The hardest part was the seams: rules like "no parallel fifths" must hold across phrase boundaries, not just inside one phrase. A post-hoc rejection pass biased the search toward its first solution, so the seam checks were moved into the backtracking search itself. We also kept the rule set honest: when a rule is off, the UI says OFF instead of implying it passed.

What we learned

Constraint programming is a practical fit for rule-based composition: the same machinery that checks harmony assignments can generate them. Stating limits plainly (root-position triads, four voices, one 16-bar form) made the tool more useful, not less.

What's next for ConstraintLab

First-inversion and seventh chords, more forms, melodic constraints in addition to harmonic ones, and counterpoint species rules.

Built With

Share this project:

Updates

Submission history