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

Polyphonic Random Patterns

This demo extends the Random Patterns demo in creating polyphonic random patterns.
It is essentially the same process repeated three times using three different percussion voices.
However, a number of interesting structural programming changes in the class are worth noting.

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 required by this class.

public final class PolyPatterns implements JMC{

static double[] pattern = new double[5];
// Set up the rhythm patterns

static double[] pattern0 = {1.0, 0.5, 0.5, 1.5, 0.5};
static double[] pattern1 = {0.5, 0.5, 1.5, 0.5, 1.0};
static double[] pattern2 = {2.0, 0.5, 0.5, 0.5, 0.5};
static double[] pattern3 = {1.5, 0.5, 1.0, 0.5, 0.5};

public static void main(String[] args){
Score Score = new Score("JMDemo - Polyphonic Rhythm Patterns");
Part inst = new Part("Drums", 0, 9);
Phrase phr = new Phrase(0.0);
Phrase phr2 = new Phrase(0.25);
Phrase phr3 = new Phrase(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
. A phrase is created for each of the three percussion voices.
Notice that the start times for each phrase is offset by a sixteenth note (semiquaver) to add musical interest and excitement.

// create three phrases of eight patterns of four beats each.
for(short i=0;i<8;i++){
// snare patterns

setPattern();

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

//hihat patterns

setPattern();

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

//clap patterns

setPattern();

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


Here one of the four patterns is randomly selected eight times over.
Each time through the snare, hi hat, and clap patterns are added to their respective phrases.
Notice that a new random pattern is selected before each part is rendered using the 'setPattern()' method - see below.
The percussive sounds (clap etc.) are selected by choosing the appropriate pitch (39 for clap, and so on).

		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);
inst.addPhrase(phr2);
inst.addPhrase(phr3);
score.addPart(inst);

System.out.println(score.toString());


Each phrase is added to the same Part and the part to a Score.
The score is printed to the screen when the class file is run.
This allows the user to see the score data and to enable error checking.

       Write.midi(score, "PolyPatterns.mid");
}


As with other jMusic tutorial files, this code creates a MIDI file from the score.
Once created the polyPatterns.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.

public static void setPattern() {
int x = (int)(Math.random()*4);

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("Switch out of range");
System.exit(0); // end the program
}
}


This class contains a second method which chooses one of the patterns at random.
It is efficient to isolate this routine as a separate method because it is called many times during the program execution and would otherwise
need to be repeated within the main method.
The pattern arrays are declared earlier as class variables to allow access from both the main and setPattern methods.

 

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