Learning goals

What to do

The only task in this lab is to experiment with randomly-created rhythms and melodies.

Start by creating a new Processing sketch and copying the code from the following program:

RandomMusic.pde

The Random music section below explains what this program is doing.

Here are some things you can change:

You can also add some new figures.

See what you can come up with!

Note that each time you click to start playing, the code for the generated rhythms and melodies used in all randomly generated melodic and percussion figures is printed to the program output view. If you like the way that any of the generated figures sound, you can copy them. This could be a source of material for your composition.

Random music

As we’ve seen in the previous labs, music can be reduced to patterns: start beats, durations, and note numbers. There is no reason why these patterns can’t be created by a program rather than a person. Music created by a program can be considered generative art in the same sense as fractals in visual art.

There are essentially an infinite number of ways we could generate music. Here is the very simple approach we will use in this lab:

Random rhythms

Random rhythms are specified by specifiying a series of note start intervals and probabilities. A note start interval is one of the following numbers: 8, 4, 2, 1, .5, .25, .125. Think of these as representing a whole note, half note, quarter note, eighth note, etc. Associated with each note start interval is a probability, which is a number from 0 to 1, 0 meaning “impossible”, .5 meaning “a 50/50 chance”, 1 meaning “absolutely certain”, etc.

For example, here is one possible series of start intervals and probabilities:

double[][] slowProb = {
  { 4, 0.8 },   // 80% chance of creating a half note
  { 2, 0.3 },   // 30% chance of creating a quarter note
};

In the example above, half notes (4) occur with .8 (80%) probability, and quarter notes (2) occur with .3 (30%) probability.

Random melodies

A random melody is created by choosing notes from the composition’s scale. For each note in the melody, a random number from 0 to 6 is generated: these correspond to the numbers of the 7 notes in the root octave of the scale. Usually, this note will be directly added to the melody. However, with some probability, the note’s octave may be moved up or down.

Random melodic figures

The randomMelodicFigure method creates a random melodic figure. For example:

Figure slow1f = randomMelodicFigure(slowProb, true, .2, 8, celesta);

In the example above, a random melodic figure is created which

Here is what slow1f sounds like on one run of the program:

Let’s create a second random melodic figure, and then use the gf command to combine them so they play at the same time:

Figure slow2f = randomMelodicFigure(slowProb, true, .2, 8, marimba);
Figure slowf = gf(slow1f, slow2f);

Here’s what slowf sounds like (which is slow1f and slow2f playing at the same time):

Random percussion figures

The randomPercussionFigure method creates a random percussion figure. For example:

double[][] hihatProb = {
  { 1, .6, },   // 60% chance of creating an eighth note
  { .5, .4 },   // 40% chance of creating a sixteenth note
  { .25, .1 },  // 10% chance of creating a thirty-second note
};
Figure hihatf = randomPercussionFigure(hihatProb, 2, drumkit, 42);

In the example above, a percussion figure is created which

Note that the random percussion figures will have only a single percussion sound.

Here’s what this figure sounds like:

Putting it together

We can assemble a “composition” out of random figures in much the same way that we would assemble a composition out of figures created from scratch. Here’s a complete “composition”:

Controlling the randomness

You will notice that the example composition sounds the same every time you play it. To get a different random composition, change the following line in the mouseClicked method:

randomSeed(123); // <-- change this to get a different "random" composition

Try specifying different numbers (other than 123): this will change the sequence of random numbers generated by the program, leading to different random rhythms and melodies.