Music Composition with Markov Chains

Timeframe: Fall 2024

Scope: MATH351 Graph Theory

Problem: To use Markov Chains of existing music to compose new music

For my final project in Graph Theory, I decided to use graphs to algorithmically compose music based on existing music. I decided to use Bach’s Prelude 1 from The Well-Tempered Clavier as my basis, as it is a well known piano piece (seen right).

To convert music into a graph, I represented each note as a node, labeled as N-v where N is the note name given by pitch and octave number, and v is the denominator of the note value (ex. C4-16 is a 16th note middle C). Edges are directed, representing transitions between notes, and weighted based on the number of times the transition occurs. The resulting graph is seen below.

Sheet music for Prelude 1.

Graph of Prelude 1. Each node is a note and each edge is a transition. Size indicates frequency of occurrence.

The actual creation of the graph was done by hand with an excel sheet of transitions. Once this was complete I created the visualization of the graph in Gephi, the open source graphing software. Once this was done, I used MATLAB to convert the table of edges into a transition matrix for the graph. I then converted this matrix into the transition matrix for a Markov Chain of the same form as the original graph, which can be seen below.

Transition matrix for a Markov Chain created from the graph of Prelude 1.

Using this transition matrix I wrote a further MATLAB script to perform a walk on the Markov Chain. I then used it to generate a walk of 100 notes. I then wrote the resulting array of notes into sheet music, as seen to the right.

The resulting music had some musical quality (about as much as could be expected from a statistically generated piece of music), but suffered from one major issue - the discrepancy between how the left hand notes were written in Prelude 1 and in the Walk. In Prelude 1, the left hand plays longer notes which start the musical phrase and then continue on underneath the right hand as it continues the melody. In the Walk, however, I merely incorporated those longer notes linearly as they appeared in the resulting array, meaning that the flow of the Walk is interrupted for the majority of left hand notes.

However, despite this one major discrepancy, the resulting Walk was for the most part interesting to listen to, and had some very musically pleasing measures. But you can form your own opinion from listening to the Walk below.

Sheet music generated by a 100-note walk on the Markov Chain of Prelude 1.

Listen to the generated music here!

Previous
Previous

Bat Frequency Audio Equalizer

Next
Next

Simulation of a UAV Launching from a USV