audio MIDI Music Algorithms Interfaces Programming Acoustics Context
> Music Algorithms > Generating > Random Patterns    
 
   

Random Patterns

Because totally random durations make little musical sense to us, this demo adds some certainty of pulse to an uncertain rhythmic outcome.
It does this by choosing randomly from a number of four beat rhythms.
Because each pattern is itself musically coherent, the result is guaranteed to be reasonable even if highly unpredictable.
 It is a recombinatorial algorithm, similar to Mozart's Dice Game.

To hear the result play this MIDI file

Click here to view source.

Lets have a closer look.

import jm.JMC;
import jm.music.data.*;
import jm.midi.*;
import jm.util.*;


Lines 1-4 import useful packages that we need to use in the program.
The first import statement gets a standard Java class. The rest of the statements import the jMusic classes.

public final class RandomPatterns implements JMC{
public static void main(String[] args){
Score score = new Score("JMDemo - Random Rhythm");
Part inst = new Part("Snare", 0, 9);
Phrase phr = new Phrase(0.0);
int x;
double[] pattern = new double[5];
double[] pattern0 = {1.0, 0.5, 0.5, 1.5, 0.5};
double[] pattern1 = {0.5, 0.5, 1.5, 0.5, 1.0};
double[] pattern2 = {2.0, 0.5, 0.5, 0.5, 0.5};
double[] pattern3 = {1.5, 0.5, 1.0, 0.5, 0.5};


In this section Score, Part, and Phrase objects are created.
As well, arrays of duration are set up, each one 4 beats long.
The array called 'pattern' is later used to store one of the randomly selected patterns 0 through 3.

               for(short i=0;i<8;i++){                        
// choose one of the patterns at random

x = (int)(Math.random()*4);
System.out.println("x = " + x);

switch (x) {

case 0:
pattern = pattern0;
break;
case 1:
pattern = pattern1;
break;
case 2:
pattern = pattern2;
break;
case 3:
pattern = pattern3;
break;
default:
System.out.println("Random number out of range");
}


Here one of the four patterns is randomly selected eight times over.
Each time the code below is executed before the next pattern is chosen.
The switch statement is used with the random number 'x' to select a different outcome in each possible random case.

The case statement consistes of a switch value, in this example the number x, and cases for all possible values of the switch value. case 0, case 1, case 2, etc. This is used in this tutorial code to choose a pattern array depending on a random number allocated to x.
The default case is used if all others fail - it is good programming practice to use a default case even if (as in this example) it is considered impossible that the default statement would be required.

		for (short j=0; j<pattern.length; j++) {
Note note = new Note((int)(38), pattern[j]);
phr.addNote(note);
}


Each time a pattern is selected, each rhythmic value in the pattern array is converted into a note on MIDI channel 10 (9 with Java's zero offset) and added to the phrase.

    		Note note = new Note((int)(49), 4.0);
phr.addNote(note);


Just for musical coherence a cymbal crash is added to the end of the phrase, otherwise the score does not finish on a down beat.

    		inst.addPhrase(phr);
score.addPart(inst);


The phrase is added to a Part and the part to a Score.

    		Write.midi(score, "RandomPatterns.mid");


As with other jMusic demo files, this code creates a MIDI file from the score. Once created the randomPatterns.mid file can be played by any MIDI file player and will sound correctly using a General MIDI sound source, such as most PC sound cards, or Quicktime. The final line provides feedback during execution that the MIDI file has been successfully written.

 

jMusic Australia Council Queensland University of Technology Sitemap Contact Home Home http://www.qut.com http://explodingart.com/jmusic http://www.ozco.gov.au

Digital Instrument making Home