Using Random Walk Principle to Generate Music

Written by on October 24, 2020 in LISP, Music, Programming with 0 Comments

In mathematics, the simplest example of Random Walk is a random process along a one-dimensional plane of integers, starting at 0 and moving in the positive or negative direction in steps of +1 or -1, with equal probability. The Random Walk theory has been applied in many domains including Physics and even the Stock Market.

Here is a nice article on the principle of Random Walk in Mathematics. A more detailed video lecture is available here.

The following shows a rendering of the Random Walk process in Mathematica.

Random Walk Example in Mathematica

Random Walk Example in Mathematica

My focus today is on using the Random Walk idea to generate music in Opusmodus, my preferred platform for algorithmic music composition. Opusmodus has many functions that help mimic randomness, and one such function is “gen-walk”, an implementation of the Random Walk idea on a 1-dimensional plane.

The following shows three evaluations of the “gen-walk” function (executed in the Listener pane):

gen-walk in Opusmodus

gen-walk in Opusmodus

The first example shows 10 successive integer values starting from 0 with steps of +1 or -1, with equal probability.

The second example shows how we can specify the step size. In this case, it can be +/-1, +/-2 and +/-3.

The third example is a very useful extension, where instead of working with integers, we work with pitches directly. In this case, we specify a starting pitch (on the chromatic scale) and let the function generate other pitches based on the given intervals. This is the idea that we will be using today.

It is also possible to specify the forward and backward probabilities. Please go through the documentation to understand its entire functionality.

Just as I did in Mathematica, we can plot the generated integral values in Opusmodus. See this image:

gen-walk Plot

gen-walk Plot

The above plot was generated by the following expression:

Code to Plot gen-walk Output

Code to Plot gen-walk Output

It is now time to look at the Opusmodus code for music generation:

Using gen-walk to Synthesize Music

Using gen-walk to Synthesize Music

The “create-track” function does the main work. First, we select the rhythm randomly from the given set. Then, required number of pitches are generated using the gen-walk function. The number of pitches depends on the corresponding rhythm (we exclude rests). These two are then combined to create the OMN. Just to make it interesting, I am using three channels and each of then will be associated with a different OMN structure.

The generated music is far from perfect, of course. The idea is just to show how we can use “gen-walk” function to create music. One can apply other filters to come up with better music.

Note that every time we run this code, a different piece of music will be generated, and that is the idea! Here is the musicxml rendering during one execution of the above code:

Generated Music - MusicXML

Generated Music – MusicXML

Here is another run:

Another Run

Another Run

As you can see for yourself, a simple function such as gen-walk can lay the foundation for some interesting musical structure. You can use my code as a starting point and experiment further by adding your own logic.

You can download the Opusmodus code from here. You can find my other articles on Opusmodus here.

Have a nice weekend!

Tags: , ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *