The Sex Machine: A Genetic Music Generator

Ramanujan Srinivasan

Weiwei Yang

EE/CS 107 Winter Term

 

Try Our Program!

We don't have certification, but please trust us, this is not a virus.

Left click once to select a song.

Right click once to play selected song.

Double left click to add or remove a song to the preferred list.

You can only select two songs (put them in the preferred list) for mating.

Click the 'HaveSex' button to mate two songs in the preferred list. A new generation of 10 songs will appear afterwards.

Over the course of this term, the two of us (henceforth RS and WY) wrote a program to generate simple melodies using a genetic algorithm.  After generating an initial set of random songs (the first generation), the program allows the user to select which two melodies have the most pleasing characteristics.  These are taken as the parents for a new generation; the program combines the two melodies of the user’s choice together in a random way (simulating mating) to generate a new set of child melodies.  This process is repeated ad infinitum, with pleasing melodies typically being generated by the third or fourth generation. 

The issue of  melodic representation is of key importance; by defining melodies as tree structures of operators and operands, there is a simple way of mating two trees together or mutating an existing tree.   Typical operators include “play twice”, “combine two sections”, “reverse”, and “play and reverse.”  Typical operands include “C-chord”, “A-sharp, 4th octave”, and “rest.”  To mate trees together, the program creates a new child tree using a random combination of branches from each parent tree (it will be possible in a future version to have more than 2 parents).  To mutate trees, the program randomly replaces a branch from a child tree with a new, randomly generated branch.

We completed the program by splitting the task into two parts.  RS was responsible for the user-interface and MIDI-interface.  WY was responsible for the tree structure and the code to mutate, mate, and generate songs from trees.  The two sections of code are simple to interface together.  WY’s code generates a string of notes for RS’s MIDI-interface code to play and RS’s code uses the user input to control the mating and crossing.

Our results were better than expected.  Typically, it takes only a few generations (and thus only a few minutes of operation) to get interesting melodic snippets.  Because of the use of minor and major chords in the operand set, it is possible to get more than simple monophonic tunes.

We intend to continue this project into next term, implementing a neural network and a more precise rating system.  Instead of simply choosing two songs out of a generation of 10, we would like for the user to be able to rate each song individually on a scale of 1-5, and then generate new songs based on characteristics from all of the highly rated ones.  The neural network would let the program learn which traits the user liked the most.  This would allow the user to rate only a few songs before letting the system take over and evolve the music over many more generations without the tedious task of listening to hundreds of generated melodies.

If someone else were to do a similar project in the future, our only advice would be to use principles of music theory to generate melodies.  Since both of us lacked formal music education, we took a slightly different approach, counting on the user to differentiate between pleasing and dissonant songs.  However, if additional types of chords were implemented along with keys and time signatures that could also be combined and evolved, one might use this system to produce extremely interesting snippets of music with very little trial and error.