class PulseHandler extends Bead { int currpulse = -1, marimbahistoryindex = 0; int currmeasure = 0; int currmeasurelength = 2 * pulsesper8th; int[] marimbahistory = new int[200], marimbaaccomphistory = new int[200]; Vector cuedpatterns = new Vector(); Vector dcuedkeys = new Vector(); DisplayObject currdkey; int keyr = 180, keyg = 180, keyb = 180; float[] currpattern; int currpc = 21, targetpc = 21; int currpcchange = 0, lastpcchange = 0; int currbasspc = -1; int bass_min_pc = -6, bass_max_pc = 14; int marimba_max_pc = 65, marimba_min_pc = 7; private int noteindex = -1; private float notetime = 0, notevel = 0; float p = 220; int hadpattern = 0; int patternid = -1; int pattmeasure = 0; int currbeat = -1; int currbeat2 = -1; int[] measurelenpatt = {3, 3, 2, 3, 2, 2, 3, 2}; int currmeasure8th = 0; int keyxspace = 30; int currkeyoffset = 30; Boolean cuePattern(float[][] newpatts, String label) { if(dcuedkeys.size() < 12) { for(int i = 0; i < newpatts.length; i++) { float[] newpatt = newpatts[i]; cuedpatterns.add(newpatt); } int k = dcuedkeys.size() * keyxspace; DisplayText newkey = new DisplayText(label, smallfont, CENTER, 60 + currkeyoffset + k, 0, keyr, keyg, keyb, 0); newkey.setCTrans(.3, keyr, keyg, keyb, 1); newkey.setPTrans(.4, k + currkeyoffset, 0, 1, 2); dcuedkeys.add(newkey); dkeys.add(newkey); return false; } else return true; } void loadNextNote() { if(currpattern != null) { noteindex++; int ni = noteindex * 5 + 3; if(currpattern.length - ni < 6) { notetime = 0; } else { notetime = currpattern[ni] * pulsesper8th; } currpc = currscale.calcNoteFromInterval(currpc, (int)currpattern[ni + 1]); //println("currpc: " + oldpc + " +" + (int)currpattern[ni + 1] + " ->" + currpc); if(currpc < marimba_min_pc) currpc += currscale.getOctaveDivisor(); else if(currpc >= marimba_max_pc) currpc -= currscale.getOctaveDivisor(); notevel = currpattern[ni + 4]; //println(noteindex + ": " + notetime + ", " + currpc + ", " + notevel); } } void loadNextPatt() { currbeat++; if(currmeasure8th > 11 || currbeat >= 5) { currbeat = 0; currmeasure8th = -1; } currbeat2 = (currbeat2 + 1) % measurelenpatt.length; if(cuedpatterns.size() > 0) { currpattern = (float[])cuedpatterns.firstElement(); cuedpatterns.removeElementAt(0); notevel = currpattern[2]; currmeasurelength = (int)currpattern[1] * pulsesper8th; int pattpcchange = 0; for(int i = 4; i < currpattern.length; i += 5) { pattpcchange += (int)currpattern[i]; } targetpc = currscale.calcNoteFromInterval(currpc, pattpcchange); lastpcchange = currpcchange; currpcchange = targetpc - currpc; noteindex = -1; if(patternid != currpattern[0]) { patternid = (int)currpattern[0]; pattmeasure = 0; processkeys(); } else { pattmeasure++; } } else { currpattern = null; processkeys(); currmeasurelength = pulsesper8th * measurelenpatt[currbeat2]; lastpcchange = currpcchange; currpcchange = targetpc - currpc; } } void processkeys() { if(currdkey != null) { currdkey.cuePTrans(3, -400, 0, .75, 1, 1); currdkey.cueCTrans(.3, keyr, keyg, keyb, .9); currdkey.cueCTrans(1, keyr, keyg, keyb, .3); currdkey.cueCTrans(1.5, keyr, keyg, keyb, 0); } int s = dcuedkeys.size(); if(s > 0) { currdkey = (DisplayObject)dcuedkeys.firstElement(); dcuedkeys.removeElementAt(0); currdkey.setPTrans(.1, 0, 0, 1, 2); currdkey.setCTrans(.2, 220, 80, 80, 1); for(int i = 0; i < s - 1; i++) { DisplayObject dob = (DisplayObject)dcuedkeys.elementAt(i); dob.setPTrans(.3, currkeyoffset + i * keyxspace, 0, 2, 2); } } else currdkey = null; } void messageReceived(Bead message) { currpulse++; currpulse %= currmeasurelength; marimbahistoryindex++; marimbahistoryindex %= marimbahistory.length; //println("currpulse: " + currpulse); float d = notetime - currpulse; if(currpulse == 0) { loadNextPatt(); } execute_drums_logic(); execute_bass_logic(); // marimba handler //clear the histories marimbahistory[marimbahistoryindex] = 0; marimbaaccomphistory[marimbahistoryindex] = 0; //figure out if we're playing note this time - if the time until playing is less than 1 pulse, we are if(d < 1 && d >= 0) { // and if the velocity is greater than 0, we'll actually spend time making sound if(notevel > 0) { float del = d * pulseduration; marimba.playNote(currpc, notevel, del); //record the history marimbahistory[marimbahistoryindex] = currpc + 1000; //println((currpc + 1000) + ", " + marimbahistoryindex); //play an accompaniament note a fourth below when we're on the beat if(currpulse == 0) { int accpc = currscale.calcNoteFromInterval(currpc, -4); if(accpc >= 0) { float nv = notevel * random(.95, 1.02); marimba.playNote(accpc, nv, del + random(5, 20)); //record the history marimbaaccomphistory[marimbahistoryindex] = accpc + 1000; //println((accpc + 1000) + ", " + marimbahistoryindex); } } } if(notevel >= 0) { loadNextNote(); } } //no note this time else {} if(currpattern == null) notevel = -1; } // drums logic void execute_drums_logic() { if(currsection == 0) { if(currpulse == 0) { bd.hit(.9); } } else if(currsection == 1) { if(currpulse == 0) { bd.hit(.9); hh.hit(.3); } else if(currpulse % pulsesper8th == 0) { hh.hit(.15); } } else if(currsection == 2) { if(currpulse == 0) { bd.hit(.9); sd.hit(.9); } else if(currpulse % pulsesper8th == 0) { hh.hit(.2); } } else if(currsection == 3) { if(currpulse == 0) { if(currbeat == 0 || currbeat == 2) bd.hit(.9); else sd.hit(.9); currmeasure8th++; hh.hit(.5); } else if(currpulse % pulsesper8th == 0) { hh.hit(.2); currmeasure8th++; } else if(currpulse == pulsesper8th * 1.5) { hh.hit(.15, 5); } } } // the bass logic void execute_bass_logic() { if(currsection == 0) { } else if(currsection >= 1 && currsection <= 2) { if(currbeat == 0 && currpulse == 0) { int change = currpcchange; int n1 = currscale.calcNoteFromInterval(currpc, -1) % currscale.getOctaveDivisor(); int n2 = currscale.calcNoteFromInterval(currpc, -3) % currscale.getOctaveDivisor(); int i1 = (n1 - currbasspc + currscale.getOctaveDivisor() * 4) % currscale.getOctaveDivisor(); int i2 = (n2 - currbasspc + currscale.getOctaveDivisor() * 4) % currscale.getOctaveDivisor(); int ai1 = i1 - currscale.getOctaveDivisor(); int ai2 = i2 - currscale.getOctaveDivisor(); if(change == 0) { if(abs(ai1) <= abs(i1)) i1 = ai1; if(abs(ai2) <= abs(i1)) i2 = ai2; } else if(change > 0) { if(i1 != 0) i1 = ai1; if(i2 != 0) i2 = ai2; } //println("n1: " + n1 + ", i1: " + i1 + ", n2: " + n2 + ", i2: " + i2); Boolean doswap = false; if(abs(i1) < abs(i2)) doswap = true; else if(i1 == 0) doswap = true; else if(i2 == 0) doswap = false; if(doswap) { int t = i1; i1 = i2; i2 = i1; } int bpc = currbasspc + i1; if(bpc < bass_min_pc) bpc += currscale.getOctaveDivisor(); else if(bpc > bass_max_pc) bpc -= currscale.getOctaveDivisor(); //println("bpc: " + bpc); currbasspc = bpc; bass.playNoteAtPitch(basepitch * pow(2, (float)bpc / currscale.getOctaveDivisor()), 1); basswah.wah(1); } } else if(currsection >= 3) { Boolean playbass = false; int melnote = 0, change = 0; //if(currpulse % pulsesper8th == 0) { // println(currmeasurelength - pulsesper8th + ", " + currpulse); //} if(currmeasurelength - pulsesper8th == currpulse && (currbeat != 2 && currbeat != 3)) { playbass = true; melnote = targetpc; change = currpcchange; } else if(currpulse == 0 && (currbeat == 3 || currbeat == 4)) { playbass = true; melnote = currpc; change = lastpcchange; } if(playbass) { //println(currpc + ", " + targetpc + ", " + change + " melnote: " + melnote); //println(currbeat); int n1 = currscale.calcNoteFromInterval(melnote, -1) % currscale.getOctaveDivisor(); int n2 = currscale.calcNoteFromInterval(melnote, -3) % currscale.getOctaveDivisor(); int i1 = (n1 - currbasspc + currscale.getOctaveDivisor() * 4) % currscale.getOctaveDivisor(); int i2 = (n2 - currbasspc + currscale.getOctaveDivisor() * 4) % currscale.getOctaveDivisor(); int ai1 = i1 - currscale.getOctaveDivisor(); int ai2 = i2 - currscale.getOctaveDivisor(); if(change == 0) { if(abs(ai1) <= abs(i1)) i1 = ai1; if(abs(ai2) <= abs(i1)) i2 = ai2; } else if(change > 0) { if(i1 != 0) i1 = ai1; if(i2 != 0) i2 = ai2; } //println("n1: " + n1 + ", i1: " + i1 + ", n2: " + n2 + ", i2: " + i2); Boolean doswap = false; if(abs(i1) < abs(i2)) doswap = true; if(currbeat == 0) doswap = !doswap; else if(i1 == 0) doswap = true; else if(i2 == 0) doswap = false; if(doswap) { int t = i1; i1 = i2; i2 = i1; } int bpc = currbasspc + i1; if(bpc < bass_min_pc) bpc += currscale.getOctaveDivisor(); else if(bpc > bass_max_pc) bpc -= currscale.getOctaveDivisor(); //println("bpc: " + bpc); currbasspc = bpc; bass.playNoteAtPitch(basepitch * pow(2, (float)bpc / currscale.getOctaveDivisor()), 1); basswah.wah(1); } } else if(currsection == 2) { } } }