Chaotic Counterpoint

This demo produces a two part chaotic counterpoint. The initially small changes in starting value gradually bifurcate (go their own ways) and become a feature of the piece. The two parts start together and stay within a few semitones of each other for a while then separate such that from about halfway through little connection between the two is evident - they become chaotic : )

If you have music notation software which will show parts on the same stave in different colours, then try importing the MIDI file from this class and visually see the increasing bifurcation.

For an overview of chaotic systems and their musical uses read The Computer Music Tutorial (Roads 1996) from p.886. Also read Computer Music (Dodge & Jerse 1997) from p.371.

Click here to view source.

To hear the result play the MIDI file below.

Let's have a closer look.
import jm.JMC;
import jm.music.data.*;
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 used for error messages. The rest of the statements import the jMusic classes (take another look at the package documentation to work out what gets imported from where by looking here).

public final class muchChaos implements JMC{
public static void main(String[] args){
Score score = new Score("JMDemo - Chaos");
Part inst = new Part("Guitar", NYLON_GUITAR, 0);
Part inst2 = new Part("Piano", PIANO, 1);
Phrase phr = new Phrase(0.0);
Phrase phr2 = new Phrase(0.0);
double xold = 0.0;
// initial x position double x, y; // temp variables double yold = 0.0; // initial y position
All the work is done in the main method which starts here. This section of the method sets up the attributes to be used. First are the usual suspects of Score, Part, and Phrase. There are two phrases (of course) - one for each line. Then a number of variables which will be used by the fractal algorithm are set up. XOLD is the past x value, and YOLD is the past y value, which are later given starting values. The details of the algorithm will not be discussed here - refer to the references above (or others) for details on the math.

	double a = 1.4;
double b = 0.3;

for(int i=0;i<48;i++){
x = 1 + yold - a * xold * xold;
y = b * xold;
 
Note note = new Note((int)(x*36)+48, Q);
phr.addNote(note);
xold = x;
yold = y;
}
The initial values for the system are set with these two lines. Some initial values will result in oscillating (repeating) output while others result in never-ending change. These values are of the latter sort (they come from the Dodge and Jerse book, Computer Music).

As you can see above the maths for chaos theory is really quite straight forward - especially compared to Fractal theory. Just two lines. The x value only is used in the output and is mapped to pitch, each note is a quaver [eighth note] in length. Notice that because the math calculates in floating point numbers the result is cast (converted) to an integer - this is what the '(int)' does. The value is multiplied by 36 then has 48 added to it to bring it into a 'reasonable' range for MIDI notes from the C below middle C up three octaves. After the note is created and added to the phrase the values of x and y are stored as previous values and the loop continues until 48 notes are created.

         a = 1.401;
b = 0.3;
xold = 0.0;
yold = 0.0;
// create a phrase of chaotically pitched quavers
// over a limited MIDI range.
for(short i=0;i<100;i++){ x = 1 + yold - a * xold * xold; y = b * xold; // map the x value across a few octaves Note note = new Note((int)((x*24)+48), C); phr2.addNote(note); xold = x; yold = y; }

This code is exactly the same as the previous section except that the starting value for x (a) is 0.001 greater, and notes are added to phrase 2.

		inst.addPhrase(phr);
inst2.addPhrase(phr2);
// add the part that to a score score.addPart(inst); score.addPart(inst2); // create a MIDI file of the score Write.midi(score, "chaos.mid"); } }

The phrases are added to a part which is added to the score. Then the score is converted to a standard MIDI file (SMF) and written to disk with the name 'chaos.mid'.



jMusic Tutorial Index