Markov music generation

Here is an example of a method for generating music based on a Markov chain.

The following python code generate notes in sequence, by choosing among an octave of notes (i.e., twelve pitches) at each step. The first note is chosen entirely at random, and then each subsequent note is determined using the probabilities in a transition matrix.

The notes have a fixed length and rhythm. This would not be hard to modify if you would like to make the output have more variety: you could have note duration and amplitude controlled by other Markov chains, if you like.

The notes themselves are very simple sine wave-based oscillations, with an envelope thrown on top to give the sound a short attack and long decay.

The program requires the packages math, random and soundfile.

Example audio output (wav file)

Here is the python code.