//Magnus Pfleghar
import processing.sound.*;
SoundFile file;
int bands =64;
FFT fft;
AudioIn audio;
PVector[] points=new PVector[bands];
PVector[] peaks=new PVector[bands];
PVector[] alltimePeaks=new PVector[bands];
PShape s;
float freq[];
void setup() {
frameRate(60);
size(1920, 1080);
surface.setResizable(true);
//File for Input
/*file = new SoundFile(this, "drums.mp3");
file.loop();
file.rate(1);*/
//Mic for Input
audio = new AudioIn(this, 0);
audio.start();
for (int i=0; i<points.length; i++) {
points[i]=new PVector(width/bands*i+((width/bands)), (height/4)*3);
}
for (int i=0; i<peaks.length; i++) {
peaks[i]=new PVector(width/bands*i+((width/bands)), (height/4)*3);
}
for (int i=0; i<alltimePeaks.length; i++) {
alltimePeaks[i]=new PVector(width/bands*i+((width/bands)), (height/4)*3);
}
fft = new FFT(this, points.length);
fft.input(audio);
}
void draw() {
background(0);
if (frameCount%4==0) {
fft.analyze();
}
s = createShape();
s.beginShape();
for (int i=0; i<points.length-1; i++) {
peaks[i].y=peaks[i].y+1;
stroke(0, 255, 0);
strokeWeight(8);
println(fft.spectrum[i]);
points[i].y=constrain(map(fft.spectrum[i]*50, 0, 10, (height/4)*3, -(height/4)*3), 0, height);
if (points[i].y<peaks[i].y) {
peaks[i].y=points[i].y;
}
if (points[i].y<alltimePeaks[i].y) {
alltimePeaks[i].y=points[i].y;
}
s.vertex(points[i].x, points[i].y);
color(0, 0, 255);
//circle(points[i].x, points[i].y,25);
}
for (int i=0; i<peaks.length-1; i++) {
noStroke();
fill(210, 0, 0);
if(peaks[i].y<(height/4)*3-20){
rect(peaks[i].x-5, peaks[i].y,10,6);
}
}
for (int i=0; i<alltimePeaks.length-1; i++) {
noStroke();
fill(220, 150, 0);
if(alltimePeaks[i].y<(height/4)*3-20){
rect(alltimePeaks[i].x-5, alltimePeaks[i].y,10,6);
}
}
noFill();
stroke(255, 0, 0);
shape(s, 0, 0);
}
void mousePressed(){
for (int i=0; i<points.length; i++) {
points[i]=new PVector(width/bands*i+((width/bands)), (height/4)*3);
}
for (int i=0; i<peaks.length; i++) {
peaks[i]=new PVector(width/bands*i+((width/bands)), (height/4)*3);
}
for (int i=0; i<alltimePeaks.length; i++) {
alltimePeaks[i]=new PVector(width/bands*i+((width/bands)), (height/4)*3);
}
}