A Chaotic Melody

This demo creates a melody by mapping pitch values to the output of a chaotic system.

Fractals and Chaos theory are often mentioned along side each other. They are similar in both being iterative, such that the output of one cycle of the process feeds into the next cycle. This means that their 'history' affects their future rather than being random on each iteration. However, there is a significant difference between the two. Fractal systems are probabilistic, hence stochastic or indeterminate, while chaotic systems are deterministic or predictable - to a point. A chaotic system which starts with the same values will follow the same predictable path, even though it will be a complex path with no discernible repetition. An important feature of chaotic systems is that a very small variation in the starting values will result, over time, in widely divergent paths. Thus chaotic systems are very sensitive to even small changes.

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.midi.*;
	 

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 -> PACKAGES).

public final class Chaos implements JMC{
	public static void main(String[] args){
		Score Score = new Score("JMDemo - Chaos");
		Part inst = new Part("Piano", 0, PIANO);
		Phrase phr = 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 the usual suspects of Score, Part, and Phrase. 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;

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 already mentioned even very small changes to these values will produce widely different results - try it!

	    for(short 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;
		}

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.

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

By this stage of the tutorial this final section should be quite familiar. The phrase is 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