metronome by minim and controlP5
@author
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
import controlP5.*;
Minim minim;
AudioOutput out;
ControlP5 cp5;
int bpm;
int desiredFrameRate = 60;
int count = 0;
void setup(){
size(300, 300);
frameRate(desiredFrameRate);
minim = new Minim(this);
out = minim.getLineOut();
cp5 = new ControlP5(this);
cp5.addKnob("bpm")
.setRange(60, 200)
.setValue(120)
.setPosition(100, 100)
.setRadius(50);
}
void draw(){
if(frameCount % int(desiredFrameRate / (bpm / 60.0)) == 0){
if(count == 0){
out.playNote(0.0, 0.3, new MyInstrument(880));
} else {
out.playNote(0.0, 0.3, new MyInstrument(440));
}
count++;
if(count == 4) count = 0;
}
}
class MyInstrument implements Instrument {
Oscil osc;
Line ampEnv;
MyInstrument(float frequency){
osc = new Oscil(frequency, 0.0, Waves.SINE);
ampEnv = new Line();
ampEnv.patch(osc.amplitude);
}
void noteOn(float duration){
ampEnv.activate(duration, 0.5, 0);
osc.patch(out);
}
void noteOff(){
osc.unpatch(out);
}
}