import ddf.minim.*; import ddf.minim.signals.*; Minim minim; Organ wav; AudioOutput out; int cx1 = 200, cx2 = 600, cy1 = 210, cy2 = 210; int numPoints = 24; int[] p1 = new int[numPoints]; int[] p2 = new int[numPoints]; float s1 = 100, s2 = 100; int pt1 = 0, pt2 = 0, stPt1 = 0, stPt2 = 0, stMs1 = 0, stMs2 = 0; boolean isPlaying = false; float[] freq = new float[36]; int bottom = 6; float[] tcos = new float[numPoints]; float[] tsin = new float[numPoints]; AudioRecorder rec; //int pt = 0; void setup() { frameRate(60); size(800,400); background(255,255,0); smooth(); for (int i = 0; i < numPoints; i ++) { p1[i] = 24; p2[i] = 12; } for (int i = 0; i < 36; i ++) { freq[i] = pow(2, (float) i / 12) * 220; print(freq[i] + "/"); } createTrigBuff(); //MINIM minim = new Minim(this); // get a line out from Minim, default bufferSize is 1024, default sample rate is 44100, bit depth is 16 out = minim.getLineOut(Minim.STEREO); // create a wav wave Oscillator, set to 440 Hz, at 0.5 amplitude, sample rate from line out wav = new Organ(220, 0.5, out.sampleRate()); // set the portamento speed on the oscillator to 200 milliseconds wav.portamento(10); // add the oscillator to the line out out.addSignal(wav); wav.setFreq(freq[p1[0]]); wav.setAmp(0); } /********************************************************************/ void draw() { background(255,255,0); updatePt(); updateCircle(); updateLine(); updateUI(); wav.setFreq(freq[p1[pt1]]); if (isPlaying) { wav.setAmp(((float) p2[pt2] - bottom) / (36 - bottom)); } else { wav.setAmp(0); } //pt = (pt + 1) % numPoints; } /********************************************************************/ void stop() { out.close(); minim.stop(); super.stop(); } void mouseDragged() { checkMousePos(); } void mouseClicked() { checkMousePos(); if (mouseY >= 30 && mouseY <= 60) { if(mouseX > 360 && mouseX < 390) { startf(); } if (mouseX > 410 && mouseX < 440) { stopf(); } } } void checkMousePos() { float d; if (mouseX < 400) { d = getDistance(cx1, cy1); if (d < 180) { p1[getIndex(cx1, cy1)] = max(bottom, (int) d / 5); } } else { d = getDistance(cx2, cy2); if (d < 180) { p2[getIndex(cx2, cy2)] = max(bottom, (int) d / 5); } } if (mouseY <= 20) { if(mouseX > 140 && mouseX < 260) { s1 = constrain(mouseX - 145, 0, 100); stPt1 = pt1; stMs1 = millis(); } if(mouseX > 540 && mouseX < 660) { s2 = constrain(mouseX - 545, 0, 100); stPt2 = pt2; stMs2 = millis(); } } } float getDistance(int cx, int cy) { return sqrt((mouseX - cx) * (mouseX - cx) + (mouseY - cy) * (mouseY - cy)); } int getIndex(int cx, int cy) { double ang = atan2(mouseX - cx, cy - mouseY); double i = ( ang / PI / 2 * numPoints) + 0.5; if (i < 0) { i = numPoints + i; } print (i); return (int)i; } void startf() { if(!isPlaying) { isPlaying = true; pt1 = pt2 = 0; stPt1 = stPt2 = 0; stMs1 = stMs2 = millis(); } } void stopf() { if(isPlaying) { isPlaying = false; } } /* ****************************************************************** */ void updatePt() { if (isPlaying) { int ms = millis(); int offset1 = (int)((ms - stMs1) / (300 - s1 * 2)); int offset2 = (int)((ms - stMs2) / (300 - s2 * 2 + 100)); pt1 = (stPt1 + offset1) % numPoints; pt2 = (stPt2 + offset2) % numPoints; } } void updateCircle() { noStroke(); fill(255); ellipse(cx1,cy1,360,360); ellipse(cx2,cy2,360,360); fill(255,200,200); beginShape(); for (int i = 0; i < numPoints; i ++) { vertex(cx1 + tsin[i] * p1[i] * 5, cy1 - tcos[i] * p1[i] * 5); } vertex(cx1 + tsin[0] * p1[0] * 5, cy1 - tcos[0] * p1[0] * 5); endShape(); fill(200,200,255); beginShape(); for (int i = 0; i < numPoints; i ++) { vertex(cx2 + tsin[i] * p2[i] * 5, cy2 - tcos[i] * p2[i] * 5); } vertex(cx2 + tsin[0] * p2[0] * 5, cy2 - tcos[0] * p2[0] * 5); endShape(); } void updateLine() { stroke(255); line(cx1, cy1, cx1 + tsin[pt1] * p1[pt1] * 5, cy1 - tcos[pt1] * p1[pt1] * 5); line(cx2, cy2, cx2 + tsin[pt2] * p2[pt2] * 5, cy2 - tcos[pt2] * p2[pt2] * 5); } void updateUI() { noStroke(); fill(255); rect(140,0,120,20); rect(540,0,120,20); fill(200); rect(s1 + 140, 0, 20,20); rect(s2 + 540, 0, 20,20); if (isPlaying) { fill(0,255,0); } else { fill(200); } rect (360,20, 30,30); if (!isPlaying) { fill(255,100,0); } else { fill(200); } rect (410,20, 30,30); fill (255); beginShape(); vertex(370,30); vertex(382,35); vertex(370,40); endShape(); rect(420,30,10,10); } void createTrigBuff() { for (int i = 0; i < numPoints; i ++) { tcos[i] = cos(PI * 2 / numPoints * i); tsin[i] = sin(PI * 2 / numPoints * i); } }