1 #ifndef __PitchDetector_h__
2 #define __PitchDetector_h__
28 init(fftSize, aSamplingRate);
36 void init(
int fftSize,
float aSamplingRate){
37 samplingRate=aSamplingRate;
41 binSize=samplingRate/fftSize;
51 samplingRate=asamplingRate;
54 minBin=(int)(aMinFrequency/binSize);
57 maxBin=(int)(aMaxFrequency/binSize+1);
63 fft.
fft(timeDomain, fd);
68 timeDomain.
insert(input, 0, writePointer, samplesToCopy);
69 writePointer+=samplesToCopy;
70 if(writePointer==fft.
getSize()){
73 fft.
fft(timeDomain, fd);
92 if(maxIndex==0||maxIndex==magnitudesSub.
getSize()-1) {
99 float alpha=log10(magnitudesSub[maxIndex-1]);
100 float beta=log10(magnitudesSub[maxIndex]);
101 float gamma=log10(magnitudesSub[maxIndex+1]);
102 float p=0.5*(alpha-gamma)/(alpha-2*beta+gamma);
104 int bin=maxIndex+minBin;
105 frequency=(bin+p)*binSize;
116 int numLowPassStages;
117 int numHighPassStages;
121 const static int POINTS_AVERAGE = 10;
124 samplingRate(aSamplingRate),
125 numLowPassStages(aNumLowPassStages),
126 numHighPassStages(aNumHighPassStages) {
140 samplingRate = aSamplingRate;
143 if(numLowPassStages<1)
148 if(numHighPassStages<1)
154 static float lastValue=0;
155 static size_t countsPointer=0;
157 filter->
process(input, filterOutput);
159 for(
size_t n=0; n<input.
getSize(); n++){
160 float currentValue=filterOutput[n];
161 if(currentValue>0 && lastValue<=0){
167 float offset=(-lastValue)/(lastValue+currentValue);
168 counts[countsPointer]=count+offset;
171 if(countsPointer==counts.
getSize())
174 lastValue=currentValue;
181 return samplingRate/mean;
void setHighPass(float fc, float q)
void setLowPass(float fc, float q)
static BiquadFilter * create(float sr, size_t stages=1)
static void destroy(BiquadFilter *filter)
void process(float *input, float *output, size_t size)
void getMagnitudeSquaredValues(FloatArray destination)
The squared magnitudes of the elements of the array.
static void destroy(ComplexFloatArray)
Destroys a ComplexFloatArray created with the create() method.
ComplexFloatArray subArray(int offset, size_t length)
A subset of the array.
static ComplexFloatArray create(size_t size)
Creates a new ComplexFloatArray.
static constexpr float BUTTERWORTH_Q
This class contains useful methods for manipulating arrays of floats.
void multiply(FloatArray operand2, FloatArray destination)
Element-wise multiplication between arrays.
int getMaxIndex()
Get the index of the maximum value in the array.
static void destroy(FloatArray array)
Destroys a FloatArray created with the create() method.
float getMean()
Mean of the array.
static FloatArray create(int size)
Creates a new FloatArray.
FloatArray subArray(int offset, size_t length)
A subset of the array.
int process(FloatArray input)
FourierPitchDetector(int fftSize, float aSamplingRate)
void setMaxFrequency(float aMaxFrequency)
void init(int fftSize, float aSamplingRate)
void setSamplingRate(float asamplingRate)
void setMinFrequency(float aMinFrequency)
void insert(SimpleArray< T > source, int destinationOffset, size_t len)
Copies the content of an array into a subset of the array.
static Window create(WindowType type, int size)
void setSamplingRate(float aSamplingRate)
void setHighPassCutoff(float fc)
void setLowPassCutoff(float fc)
ZeroCrossingPitchDetector(float aSamplingRate, int blocksize, int aNumLowPassStages=1, int aNumHighPassStages=1)
void process(FloatArray input)
BiquadFilter * getFilter()
~ZeroCrossingPitchDetector()
#define ASSERT(cond, msg)