import jm.JMC;import jm.music.data.*;import jm.midi.*;import jm.util.*;/** * A short example which generates a chaotic chromatic melody * and writes to a MIDI file called chaos.mid * Algorithm taken from "Computer Music" by Dodge and Jerse, P.373. * @author Andrew R. Brown */public final class ShowChaos implements JMC{	public static void main(String[] args){		Score score = new Score("JMDemo - Chaos");		Part inst = new Part("Piano", PIANO, 0);		Part inst2 = new Part("Guitar", GUITAR, 1);		Part inst3 = new Part("Flute", FLUTE, 2);		Phrase phr = new Phrase(0.0);				//---------------		// Do chaotic things		//----------------		for (double i=0.0;i<Math.random()*100;i+=Math.random()*2.0+                                                                       8.0) {			phr = makePhrase(i);			inst.addPhrase(phr);		}				for (double i=0.0;i<Math.random()*100;i+=Math.random()*2.0+                                                                       8.0) {			phr = makePhrase(i);			inst2.addPhrase(phr);		}				for (double i=0.0;i<Math.random()*100;i+=Math.random()*2.0+                                                                       8.0) {			phr = makePhrase(i);			inst3.addPhrase(phr);		}				// add the part that to a score		score.addPart(inst);		score.addPart(inst2);		score.addPart(inst3);				//use show routines		View.show(phr);		View.show(inst, 20, 20);		View.show(inst2, 40, 40);		View.show(inst3, 60, 60);		View.show(score, 80, 80);				//create a MIDI file of the score		Write.midi(score, "ShowChaos.mid");	}		public static Phrase makePhrase(double startTime) {                // first constant. For oscillation try 1.04                double a = Math.random()* 2.0; //1.4;                                 //second constant. For oscillation try 0.3		double b = 0.3;                                 // initial x position		double xold = 0.0;                                 // temp variables		double x, y;                                 // initial y position		double yold = 0.0; 		                int pitch;				Phrase phrase = new Phrase(startTime);		                // create a phrase of chaotically pitched quavers over a                 // limited MIDI range.		for(short i=0;i<(int)Math.random()*10+15;i++){			x = 1 + yold - a * xold * xold;			y = b * xold;			// map the x value across a few octaves			pitch = (int)(x*24)+60;                        // make black notes into white notes			if (pitch%12==1 | pitch%12==3 | pitch%12== 6 |                             pitch%12==8 | pitch%12==10) pitch -= 1;                                                 //if pitch is negative make it positive                        if(pitch <0) pitch *= -1;                        			Note note = new Note(pitch, Q);			phrase.addNote(note);			xold = x;			yold = y;		}		return phrase;	}}