forked from bsondermann/Blackbox2Sound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectrum.pde
43 lines (42 loc) · 1.21 KB
/
Spectrum.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Spectrum{
FFT fft;
float scl=5;
int sampleSize,wid,posx,posy;
int speclength=200;
LinkedList<float[]>spectrum = new LinkedList<float[]>();
Spectrum(int sampleSize, int w, int px,int py){
fft = new FFT(sampleSize,rate);
this.sampleSize=sampleSize;
wid=w;
posx=px;
posy=py;
}
void show(){
stroke(200);
fill(0);
rect(posx,posy,speclength*wid+6,(sampleSize/2)*wid+6);
rectMode(CORNER);
colorMode(HSB);
for(int i = 0;i<spectrum.size(); i++){
for(int j = 0; j<spectrum.get(i).length;j++){
noStroke();
fill(map(spectrum.get(i)[j],0,scl,200,0),255,map(constrain(spectrum.get(i)[j],0,scl),0,scl/2,0,255));
rect(posx+(-i+speclength)*wid,(spectrum.get(i).length-j)*wid+posy,wid,wid);
}
}
colorMode(RGB);
fill(0);
textAlign(LEFT,CENTER);
for(int i = 0; i<=10;i++){
text(int(i*rate/20)+" Hz",posx+(speclength)*wid+20,map(i,0,10,(sampleSize/2)*wid+posy,posy));
}
}
void setScale(float scale){scl=scale;}
void update(float[]data){
fft.forward(data);
float[]spec=new float[sampleSize/2];
arrayCopy(fft.getSpectrum(),0,spec,0,sampleSize/2);
spectrum.push(spec);
if(spectrum.size()>speclength){spectrum.removeLast();}
}
}