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

3 thoughts on “Generating Mozart from Markov Chains

  1. Hey Nikolaj – great stuff!
    I had a similar idea generating music, but I initially wanted to play with Evolutionary Computation algorithms for that. See: https://github.com/niieani/nandu/
    Instead of the “Mozart seed” I wanted to use Facebook users to choose what sounds better to them (hence the usage of PHP not C# or Java), and see where that leads.
    Unfortunately I did that as an university excersise, and I haven’t had the time to continue working on it, and the app server is now down (I’ll try to put it up online again sometime).
    But I also had the idea of using pre-existant music as a seed and I’m considering writing a PhD on the subject in the future. Through research, my dream is to create software able to generate music from a seed library (thousands of musical pieces), weighted probability factors for all components of music (harmony, melody, rhythm, instrumentation, volumes).
    I think that analysing relationships between notes, groups of notes and entire parts, but aware of the musical system constraints, could be the way to go. I already have ideas for the algorhythms.
    With multiple seeds is where this really starts to get interesting – imagine generating music which is a combination of Bach, Mozart, The Beatles and Lady Gaga. Or, better yet – a combination of thousands of the best composers that ever lived. How would that sound?
    Since I’m also a music composer, I think that composing, or composers creativity is really just a matter of mathematical probability – and the composer’s choice when composing is always something between these three:
    far left: using something familiar (even if subconciously)
    center: using something acording to the rules of Western music
    far right: using something unpredictable, which breakes the rules

    With that knowledge, simulating a real composer translates to:
    far left: using the seed, aware of the Western music rules;
    center: using a random value, but compliant to the music rules;
    far right: using something totally random (i.e. ‘creative’).

    I’d love to chat about it with you if you’d be interested in continuing your research of the topic.

Comment on this article

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s