// javac -source 1.3 -target 1.3 Water.java import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.sound.midi.*; public class Water extends Applet implements Runnable, ActionListener { public static final int TREBLE_LOWER = 0, TREBLE_UPPER = 14, TREBLE_AV = (TREBLE_LOWER+TREBLE_UPPER)/2, TREBLE_RANGE = TREBLE_UPPER-TREBLE_LOWER; public static final int BASS_LOWER = -14, BASS_UPPER = 0, BASS_AV = (BASS_LOWER+BASS_UPPER)/2, BASS_RANGE = BASS_UPPER-BASS_LOWER; public static final int WTREBLE_LOWER = 5, WTREBLE_UPPER = 15, WTREBLE_AV = (WTREBLE_LOWER+WTREBLE_UPPER)/2, WTREBLE_RANGE = WTREBLE_UPPER-WTREBLE_LOWER; public static final int WBASS_LOWER = -10, WBASS_UPPER = -3, WBASS_AV = (WBASS_LOWER+WBASS_UPPER)/2, WBASS_RANGE = WBASS_UPPER-WBASS_LOWER; public static final int PIANO_LOWER = -23, PIANO_UPPER = 28, PIANO_AV = (PIANO_LOWER+PIANO_UPPER)/2, PIANO_RANGE = PIANO_UPPER-PIANO_LOWER; public static final int PIANISSIMO = 32, PIANO = 40, MEZZO_FORTE = 96, FORTE = 127; Button b = new Button("Play"); boolean going; Receiver out; void message(int m, int num, int vol) throws Exception { if(num>127) num = 127; if(num<0) num = 0; if(vol>127) vol = 127; if(vol<0) vol = 0; ShortMessage sm = new ShortMessage(); sm.setMessage(m,0,num,vol); out.send(sm,0); } void noteOn(int num, int vol) throws Exception { if(going) message(ShortMessage.NOTE_ON,num,vol); } void noteOff(int num) throws Exception { message(ShortMessage.NOTE_OFF,num,0); } void note(int num, int vol, int time) throws Exception { //showStatus(num+","+vol+","+time); noteOn(num,vol); sleep(time); noteOff(num); } void sleep(int n) throws Exception { if(!going) return; n = Math.abs(n); Thread.sleep(n); } int whiteKey(int num) { int oct = num/7, note = num%7; if(note<0) { note += 7; oct--; } int[] notes = {0,2,4,5,7,9,11}; return 60 + oct*12 + notes[note]; } int blackKey(int num) { int oct = num/5, note = num%5; if(note<0) { note += 5; oct--; } int[] notes = {1,3,6,8,10}; return 60 + oct*12 + notes[note]; } int cresc(int min, int max, int where, int len) { return min+where*(max-min)/len; } void refreshPedal() throws Exception { message(ShortMessage.CONTROL_CHANGE,64,0); message(ShortMessage.CONTROL_CHANGE,64,127); } // ooh, lots of code! must be impressive! public void doMelody1(Random r) throws Exception { showStatus("Small stream"); int[] lengths = { 1+(int)Math.abs(r.nextGaussian()), 1+(int)Math.abs(r.nextGaussian()), 1+(int)Math.abs(r.nextGaussian()), 1+(int)Math.abs(r.nextGaussian()) }; int notes = (lengths[0]+lengths[1]+lengths[2]+lengths[3])*12; int where = 0; int note = TREBLE_AV + (int)(r.nextGaussian()*TREBLE_RANGE); note = Math.max(TREBLE_LOWER,Math.min(TREBLE_UPPER,note)); for(int i = 0; i < lengths[0]; i++) { note += (int)(r.nextGaussian()*2); note = Math.max(TREBLE_LOWER,Math.min(TREBLE_UPPER,note)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 1 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 2 where++; note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 3 where++; note += (int)(1+Math.abs(r.nextGaussian()*6*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 4 where++; note -= (int)(1+Math.abs(r.nextGaussian()*4*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 5 where++; note += (int)(1+Math.abs(r.nextGaussian()*1*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 6 where++; note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 7 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 8 where++; note -= (int)(1+Math.abs(r.nextGaussian()*1*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 9 where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 10 where++; note -= (int)(1+Math.abs(r.nextGaussian()*3*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 11 where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 12 where++; } int note2 = BASS_AV + (int)(r.nextGaussian()*BASS_RANGE); note2 = Math.max(BASS_LOWER,Math.min(BASS_UPPER,note)); for(int i = 0; i < lengths[1]; i++) { note += (int)(r.nextGaussian()*2); note = Math.max(TREBLE_LOWER,Math.min(TREBLE_UPPER,note)); note2 += (int)(r.nextGaussian()*2); note2 = Math.max(BASS_LOWER,Math.min(BASS_UPPER,note)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 1 where++; note -= (int)(1+Math.abs(r.nextGaussian()*4*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 2 where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 3 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 4 noteOff(whiteKey(note2)); where++; note2 += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 5 where++; note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 6 noteOff(whiteKey(note2)); where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(TREBLE_UPPER-note)/TREBLE_RANGE)); noteOn(whiteKey(note),cresc(PIANO,FORTE,where,notes)); // 7 note2 += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note2),cresc(PIANO,FORTE,where,notes),500+(int)(r.nextGaussian()*50)); note2 += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note2),cresc(PIANO,FORTE,where,notes),500+(int)(r.nextGaussian()*50)); noteOff(whiteKey(note)); where++; note2 += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note += (int)(1+Math.abs(r.nextGaussian()*1*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 8 noteOff(whiteKey(note2)); where++; note2 += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note += (int)(1+Math.abs(r.nextGaussian()*1*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 9 where++; note -= (int)(1+Math.abs(r.nextGaussian()*4*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 10 noteOff(whiteKey(note2)); where++; note2 -= (int)(1+Math.abs(r.nextGaussian()*2*(note+BASS_LOWER)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note += (int)(1+Math.abs(r.nextGaussian()*1*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 11 noteOff(whiteKey(note2)); where++; note2 -= (int)(1+Math.abs(r.nextGaussian()*2*(note+BASS_LOWER)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note += (int)(1+Math.abs(r.nextGaussian()*2*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 12 noteOff(whiteKey(note2)); where++; } for(int i = 0; i < lengths[2]; i++) { note += (int)(r.nextGaussian()*2); note = Math.max(TREBLE_LOWER,Math.min(TREBLE_UPPER,note)); note2 += (int)(r.nextGaussian()*2); note2 = Math.max(BASS_LOWER,Math.min(BASS_UPPER,note)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 1 where++; note -= (int)(1+Math.abs(r.nextGaussian()*5*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 2 noteOff(whiteKey(note2)); where++; note2 -= (int)(1+Math.abs(r.nextGaussian()*2*(note+BASS_LOWER)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note += (int)(1+Math.abs(r.nextGaussian()*5*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 3 where++; note -= (int)(1+Math.abs(r.nextGaussian()*5*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 4 noteOff(whiteKey(note2)); where++; note2 += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note += (int)(r.nextGaussian()*1); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 5 where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 6 noteOff(whiteKey(note2)); where++; note2 += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note += (int)(1+Math.abs(r.nextGaussian()*1*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 7 where++; note -= (int)(1+Math.abs(r.nextGaussian()*3*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 8 noteOff(whiteKey(note2)); where++; note2 -= (int)(1+Math.abs(r.nextGaussian()*3*(note+BASS_LOWER)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note -= (int)(1+Math.abs(r.nextGaussian()*1*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 9 where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 10 noteOff(whiteKey(note2)); where++; note2 += (int)(1+Math.abs(r.nextGaussian()*2*(BASS_UPPER-note)/BASS_RANGE)); noteOn(whiteKey(note2),cresc(PIANO,FORTE,where,notes)); note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+TREBLE_LOWER)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 11 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(TREBLE_UPPER-note)/TREBLE_RANGE)); note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); // 12 noteOff(whiteKey(note2)); where++; } showStatus("Small stream going over rocks"); for(int i = 0; i < lengths[3]*12; i++) { int oldnote = note; while(note==oldnote) { note = (int)(note*.75+TREBLE_AV*.25+r.nextGaussian()*2); note = Math.max(TREBLE_LOWER,Math.min(TREBLE_UPPER,note)); } if(r.nextInt(6)==0) { int note1 = note+4+(int)r.nextGaussian(); note2 = BASS_AV + (int)(r.nextGaussian()*BASS_RANGE); note2 = Math.max(BASS_LOWER,Math.min(BASS_UPPER,note)); int cluster = 4+(int)(r.nextGaussian()*2); for(int j = 0; j < cluster; j++) noteOn(whiteKey(note2+j),FORTE); note(whiteKey(note1),FORTE,1000+(int)(r.nextGaussian()*100)); for(int j = 0; j < cluster; j++) noteOff(whiteKey(note2+j)); } else note(whiteKey(note),cresc(PIANO,FORTE,where,notes),1000+(int)(r.nextGaussian()*100)); where++; } } public void doWaterfall(Random r) throws Exception { refreshPedal(); showStatus("Waterfall"); int[] lengths = { 1+(int)Math.abs(5+5*r.nextGaussian()), 1+(int)Math.abs(5+5*r.nextGaussian()) }; for(int i = 0; i < lengths[0]; i++) { int note1 = WTREBLE_AV + (int)(r.nextGaussian()*WTREBLE_RANGE); note1 = Math.max(WTREBLE_LOWER,Math.min(WTREBLE_UPPER,note1)); int note2 = WBASS_AV + (int)(r.nextGaussian()*WBASS_RANGE); note2 = Math.max(WBASS_LOWER,Math.min(WBASS_UPPER,note2)); for(int j = note1; j >= note2; j--) note(blackKey(j),FORTE,75); } for(int i = 0; i < lengths[0]; i++) { int note1 = WTREBLE_AV + (int)(r.nextGaussian()*WTREBLE_RANGE); note1 = Math.max(WTREBLE_LOWER,Math.min(WTREBLE_UPPER,note1)); int note2 = WBASS_AV + (int)(r.nextGaussian()*WBASS_RANGE); note2 = Math.max(WBASS_LOWER,Math.min(WBASS_UPPER,note2)); for(int j = note1; j >= note2; j--) note(blackKey(j),FORTE,75); int cluster = 4+(int)(r.nextGaussian()*2); for(int j = 0; j < cluster; j++) noteOn(blackKey(note2-j),FORTE); sleep(75); for(int j = 0; j < cluster; j++) noteOff(blackKey(note2-j)); } } public void doMelody2(Random r) throws Exception { refreshPedal(); showStatus("Fast river"); int notes = 32; int where = 0; int note = BASS_AV + (int)(r.nextGaussian()*BASS_RANGE); note = Math.max(BASS_LOWER,Math.min(BASS_UPPER,note)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 1 where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 2 where++; note -= (int)(1+Math.abs(r.nextGaussian()*4*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 3 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 4 where++; note += (int)(r.nextGaussian()*2); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 5 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 6 where++; note -= (int)(1+Math.abs(r.nextGaussian()*4*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 7 where++; note += (int)(1+Math.abs(r.nextGaussian()*2*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 8 where++; note -= (int)(1+Math.abs(r.nextGaussian()*4*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 9 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 10 where++; note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 11 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 12 where++; note -= (int)(1+Math.abs(r.nextGaussian()*3*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 13 where++; note += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 14 where++; note -= (int)(1+Math.abs(r.nextGaussian()*1*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 15 where++; note += (int)(1+Math.abs(r.nextGaussian()*3*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 16 where++; note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 17 where++; note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 18 where++; note += (int)(1+Math.abs(r.nextGaussian()*3*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 19 where++; note -= (int)(1+Math.abs(r.nextGaussian()*1*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 20 where++; note += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 21 where++; note -= (int)(1+Math.abs(r.nextGaussian()*4*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 22 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 23 where++; note -= (int)(1+Math.abs(r.nextGaussian()*2*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 24 where++; note += (int)(r.nextGaussian()*2); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 25 where++; note += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 26 where++; note -= (int)(1+Math.abs(r.nextGaussian()*3*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 27 where++; note += (int)(1+Math.abs(r.nextGaussian()*3*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 28 where++; note += (int)(r.nextGaussian()*2); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 29 where++; note += (int)(1+Math.abs(r.nextGaussian()*1*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 30 where++; note -= (int)(1+Math.abs(r.nextGaussian()*3*(note+BASS_LOWER)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 31 where++; note += (int)(1+Math.abs(r.nextGaussian()*4*(BASS_UPPER-note)/BASS_RANGE)); note(whiteKey(note),cresc(MEZZO_FORTE,FORTE,where,notes),cresc(500,250,where,notes)+(int)(r.nextGaussian()*50)); // 32 showStatus("Ocean"); double delta = 0; notes = Math.abs(48 + (int)(r.nextGaussian()*32)); where = 0; while(where=PIANO_UPPER && delta>0) delta = -delta; //showStatus("Ocean "+note+" "+delta); note = Math.max(PIANO_LOWER,Math.min(PIANO_UPPER,note)); delta = Math.max(-14,Math.min(14,delta)); note(whiteKey(note),FORTE,250+(int)(r.nextGaussian()*50)); where++; } notes = Math.abs(25 + (int)(r.nextGaussian()*18)); where = 0; while(where=PIANO_UPPER && delta>0) delta = -delta; //showStatus("Ocean "+note+" "+delta); note = Math.max(PIANO_LOWER,Math.min(PIANO_UPPER,note)); delta = Math.max(-14,Math.min(14,delta)); note(whiteKey(note),cresc(FORTE,PIANISSIMO,where,notes),cresc(250,1500,where,notes)+(int)(r.nextGaussian()*100)); where++; } sleep(2000+(int)(r.nextGaussian()*200)); showStatus("Sunset"); int note2; if(note<0) { do note = TREBLE_AV + (int)(r.nextGaussian()*TREBLE_RANGE); while(note%7==3 || note%7==-4); note2 = note + 3; } else { do note = BASS_AV + (int)(r.nextGaussian()*BASS_RANGE); while(note%7==6 && note%7==-1); note2 = note - 3; } noteOn(whiteKey(note2),PIANO); note(whiteKey(note),PIANO,5000); noteOff(note2); } public void run() { try { b.setLabel("..."); if(out==null) { Synthesizer synth = MidiSystem.getSynthesizer(); if(!synth.isOpen()) synth.open(); out = synth.getReceiver(); } b.setLabel("Stop"); message(ShortMessage.CONTROL_CHANGE,64,127); message(ShortMessage.PROGRAM_CHANGE,0,0); Random rand = new Random(); doMelody1(rand); doWaterfall(rand); doMelody2(rand); showStatus(""); b.setLabel("Play"); } catch(Exception z) { z.printStackTrace(); b.setLabel("Error"); } finally { try { message(ShortMessage.CONTROL_CHANGE,64,0); } catch(Exception z) { z.printStackTrace(); } if(b.getLabel().equals("Stop")) b.setLabel("Play"); going = false; } } public void actionPerformed(ActionEvent ae) { if(going) going = false; else { going = true; new Thread(this).start(); } } public void init() { setLayout(new BorderLayout()); b.addActionListener(this); add(b); } public void stop() { going = false; } }