Generating Mozart from Markov Chains


Source code: https://dl.dropboxusercontent.com/u/19633784/AlMuGe.zip 

This post will detail a method of generating music from sampling MIDI-files. We’ll introduce an algebraic structure called a Markov chain, and use it to make our own MIDI-files.

A markov chain is a system of nodes connecting by edges. Each edge has an associated probability with it, which describes the probability that the system will progress to that node next round. Consider this graphical example:

200px-Markovkate_01.svg

This is a markov chain composed of two nodes, E and A and four edges – E to EE to A, A to A, A to E. If we start on node E, there is a 0.7 probability that we will progres to node A, and a 0.3 probability that we stay on node E. Sounds simple, right?

The basic concept is simple really, and that’s all we really need to utilize this system in our program, but there is in fact a lot of advanced mathematics regarding markov chains, but I won’t detail it.

Now, consider a system with twelve nodes, one for each musical note, and 144 edges connecting all the nodes to each other, but where do we get these probabilies from?

Introducing sampling. The program contains a module called midcrawler, which goes to a website, crawls it for .mid-files and constructs a large markov chain, which is a representation of the midi-files, it’s sampled. I have also included the feature of loading/saving these samples, so it’s not necessary to sample the entire webpage for every single generated bit. From this, it starts on a random note and starts traversing the system, generating the song for steps.

The rhythm however, is not generated. Upon using the program, you need to supply it with some rhythm information. There are some obvious problems with this technique though, it is not limited to a scale, so it may sometimes use a tritone or be very dissonant, making it sound less than good.

I used the program to sample the following page containing a bunch of Mozart .mid files: http://www.midiworld.com/mozart.htm and made the following .mid file:

https://dl.dropboxusercontent.com/u/19633784/mozart.mid

Personally however, I’m not able to play this MIDI file, so I converted it to an MP3 file, which everyone can play:

https://dl.dropboxusercontent.com/u/19633784/mozart.mp3

It doesn’t sound very Mozart-ish, but it’s very interesting to listen to, and I’m surprised that it doesn’t sound worse.

Here is an explanation of how to use the program, given the source code:

 Crawler c = new Crawler();
 c.crawlPage("URL_OF_WEBPAGE_CONTAINING_MIDI_FILES");
 c.processInternalData();
 Parser p = new Parser(c.getContents());
 p.parseContents();
 Generator g = new Generator(p.getNoteMatrix());
 g.generateSequence(LENGTH_OF_MIDI_FILE, "DESTINATION_PATH");
Advertisements