@ BenHem & blindfish
Kai Tracid is a DJ, and i have tested it with his Music
@ all
can anybody tell me why the beatdetection has a delay??
what's the problem with my code??
mfg maik
Code:import processing.opengl.*;
import krister.Ess.*;
import controlP5.*;
AudioInput myInput;
FFT myFFT;
ControlP5 controlP5;
int bpmMin=120;
int bpmMax=140;
int bufferSize;
int averages;
int msecMin;
float msecMax;
float eRadius;
long p=500;
int q=500;
float a;
float b;
float a2;
float b2;
float avg;
float avg2;
float avg3;
float avg4=500;
float total;
float total2;
float total3;
float total4;
int numReadings = (100);
int numReadings2 = (100);
int numReadings3 = (1000);
int numReadings4 = (200);
int index = 0;
int index2 = 0;
int index3 = 0;
int index4 = 0;
float average = 0;
float average2 = 0;
float averageAll = 0;
float average4 =500;
float[] readings= new float[numReadings];
float[] readings2= new float[numReadings2];
float[] readings3= new float[numReadings3];
float[] readings4= new float[numReadings4];
boolean [] beat;
int mydelay;
int milidif;
int time ;
float Volume = 1;
void setup(){
size(400, 400);
controlP5 = new ControlP5(this);
controlP5.addRange("range",100,160,120,140,0,0,400,30);
controlP5.addKnob("Volume",0.01,2,1,180,40,40);
PFont fontB = loadFont("Georgia-Bold-48.vlw");
textFont(fontB, 15);
time = 0;
milidif = 500;
mydelay = 300;
Ess.start(this);
bufferSize = 2048;
averages = 512;
myInput=new AudioInput(bufferSize);
myFFT = new FFT(averages);
myInput.start();
ellipseMode(CENTER_RADIUS);
eRadius = 20;
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++)
readings2[thisReading2] = 0;
for (int thisReading3 = 0; thisReading3 < numReadings3; thisReading3++)
readings3[thisReading3] = 0;
for (int thisReading4 = 0; thisReading4 < numReadings4; thisReading4++)
readings4[thisReading4] = 0;
beat = new boolean[1];
}
void draw(){
background(0);
smooth();
int q = int(avg4);
fill(255);
text(q+" msec",10, 50);
text((60000/q)+" BPM",320, 50);
a();
b();
time = millis();
milidif=time-mydelay;
stroke(0,0,255);
line(0,height-b,400,height-a);
stroke(0,255,0);
line(0,height-avg,400,height-avg2);
stroke(255,0,0);
line(0,height-avg/2.1,400,height-avg2/2.1);
stroke(255,255,0);
line(0,height-avg3,400,height-avg3);
eRadius = beat[0] ? 80 : eRadius > 20 ? eRadius - 2 : 20;
float c = map(eRadius, 20, 80, 60, 255);
fill(60, 255, 0, c);
ellipse(width/2, height/2, eRadius, eRadius);
if ( eRadius < 20 ){
eRadius = 20;
}
average();
average2();
averageAll();
averageBPM();
beat();
fill(0);
msecMin = 60000/bpmMin;
msecMax = 60000/bpmMax;
/*println("msecMin : "+msecMin);
println("bpmMin : "+bpmMin);
println("msecMax : "+msecMax);
println("bpmMax : "+bpmMax);*/
println(Volume);
}
void controlEvent(ControlEvent theControlEvent) {
if(theControlEvent.controller().name().equals("range")) {
// min and max values are stored in an array.
// access this array with controller().arrayValue().
// min is at index 0, max is at index 1.
println(theControlEvent.controller().arrayValue());
bpmMin = int(theControlEvent.controller().arrayValue()[0]);
bpmMax = int(theControlEvent.controller().arrayValue()[1]);
}
}
public void audioInputData(AudioInput theInput) {
myFFT.getSpectrum(myInput);
}
boolean beat() {
if((b>avg)&&(b>avg3)&&(!beat[0])&&(milidif>msecMax)){
beat[0] = true;
mydelay=time;
p = milidif>msecMin ? msecMin : milidif;
}
else if((a>avg2)&&(a>avg3)&&(!beat[0])&&(milidif>msecMax)){
beat[0] = true;
mydelay=time;
p = milidif>msecMin ? msecMin : milidif;
}
else {
beat[0] = false;
}
return beat[0];
}
float a(){
a= ((myFFT.spectrum[0])*height*2*Volume);
return a;
}
float b(){
b= ((myFFT.spectrum[1])*height*2*Volume);
return b;
}
float average(){
total= total - readings[index];
readings[index] = b;
total= total + readings[index];
index = index + 1;
if (index >= numReadings)
index = 0;
float m = map((total / numReadings), 0, height/3*2, 3.5,.9);
m = m<1 ? 1 : m;
avg = total / numReadings*m;
return avg;
}
float average2(){
total2= total2 - readings2[index2];
readings2[index2] = a;
total2= total2 + readings2[index2];
index2 = index2 + 1;
if (index2 >= numReadings2)
index2 = 0;
float m2 = map((total2 / numReadings2), 0, height/3*2, 3.5,.9);
m2 = m2<1 ? 1 : m2;
avg2 = total2 / numReadings2*m2;
return avg2;
}
float averageAll(){
total3= total3 - readings3[index3];
readings3[index3] = b+a;
total3= total3 + readings3[index3];
index3 = index3 + 1;
if (index3 >= numReadings3)
index3 = 0;
float m3 = map((total3 / numReadings3), 0, height/3*2, 3,1);
m3 = m3<1 ? 1 : m3;
avg3 = total3 / numReadings3*m3;
return avg3;
}
float averageBPM(){
total4= total4 - readings4[index4];
readings4[index4] = p;
total4= total4 + readings4[index4];
index4 = index4 + 1;
if (index4 >= numReadings4)
index4 = 0;
avg4 = total4 / numReadings4;
return avg4;
}
void stop() {
Ess.stop();
super.stop();
}