# Algorithmic Counterpoint: How I Wrote a Computer Program to Generate Music at Mathcamp 2011

Hello all; I have just returned from Mathcamp, a five week math program for high school students. I’m not going to write in detail about life at Mathcamp, other than that it was amazing in every way.

This year at Mathcamp, I chose to do a computer programming project to ‘teach’ a computer to write ‘music’. The word ‘music’ is in quotes because what it’s expected to generate is more or less random and nothing like actual music by Bach or Mozart; on the other hand, the ‘music’ generated resembles actual music closely enough that it can still be called music, not a random string of notes. Anyways, here’s a short sample of what it can generate:

What can be considered ‘music’ in some sense here is really just a set of rules backed by a random number generator. Hence what it does is really the following:

1. Generate a main melody using a bunch of rules: ie, what notes are allowed to go with each other, what rhythms are allowed to go with what notes, etc.
2. Using the main melody as a sort of a baseline, generate one or more counterpoint melodies: a counterpoint melody is constrained by the same rules that the main melody must follow, but also has to harmonize with the existing melody, thus being constrained by another set of rules.
3. Once all the notes are generated, the program simply uses some library — in our case JFugue — to play the notes through the speakers.

The bulk of our time was spent implementing, tweaking, and debugging the rules and the various other substeps in the above process.

The source code, mostly written by myself over about two weeks, is available as a Google Code project.

## 7 thoughts on “Algorithmic Counterpoint: How I Wrote a Computer Program to Generate Music at Mathcamp 2011”

1. Anonymous says:

I’m the best partner ever!!!

Like

Like

2. Anonymous says:

BAI LI FORGOT YOUR NAMEE ❤

-lol it's me, Surya, and you're a boss at programming. ^__^

Like

3. Pedro says:

Is this an actual program I can use?

Like

1. The source code is available on the Google Code link. I don’t know if it’s still possible to run it.

Like

4. Wesley Griffin says:

Is the source code for this still available anywhere? The Google Code link doesn’t seem to be working.

Like

1. You can download the source code as a zip file if you click the “source” tab, then click on the “source” link again. However, be aware that this project hasn’t really been maintained for several years.

Like