-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitchDetector.pde
49 lines (46 loc) · 1.09 KB
/
pitchDetector.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
44
45
46
47
48
49
class PitchDetectorHPS{
FFT fft;
int sampleRate;
int fftLength;
int harmonicSize;
float[][] step;
PitchDetectorHPS(int fftLength,int sampleRate,int harmonicSize){
fft=new FFT(fftLength,sampleRate);
this.sampleRate=sampleRate;
//this.fftLength=fftLength;
this.fftLength=fft.specSize();
this.harmonicSize=harmonicSize;
step=new float[harmonicSize][];
for(int i=0;i<harmonicSize;i++){
step[i]=new float[this.fftLength];
}
//fft.window(fft.HAMMING);
}
float detect(float[] frame){
fft.forward(frame);
//downsample
for(int i=0;i<fftLength;i++){
for(int j=1;j<=harmonicSize;j++){
if(i%j==0)( step[j-1])[i/j]=fft.getBand(i);
}
}
//HSP
int index=0;
float max;
max=0;
float tmp;;
for(int i=0;i<fftLength;i++){
tmp=1;
for(int j=0;j<harmonicSize;j++){
tmp*=step[j][i];
}
//println(tmp);
if(tmp>max){
max=tmp;
index=i;
}
}
if(index==0)return 1.0;
return index*fft.getBandWidth();
}
}