22 sampleRate = newSampleRate;
23 bufferSize = newBufferSize;
25 mcLeodBuffer.resize(bufferSize);
26 peakPositions.resize(bufferSize);
27 periodEstimates.resize(bufferSize);
28 amplitudeEstimates.resize(bufferSize);
29 window.resize(bufferSize);
31 for (
int i = 0; i < bufferSize; ++i)
33 mcLeodBuffer[i] = 0.0f;
35 periodEstimates[i] = 0.0f;
36 amplitudeEstimates[i] = 0.0f;
37 window[i] =
static_cast<float>(0.5 * (1.0 - std::cos(TWO_PI *
double(i) /
double(bufferSize))));
63 int tauEstimation = -1;
64 float pitchEstimation = -1.0f;
66 float maxAmplitude = -FLT_MAX;
68 float maxAmplitude = -__FLT_MAX__;
71 peakPositionsIndex = 0;
72 periodEstimatesIndex = 0;
73 amplitudeEstimatesIndex = 0;
75 calculateNormalisedSquareDifferenceFunction(mcLeodBuffer);
79 for (
int i = 0; i < peakPositionsIndex; ++i)
81 tauEstimation = peakPositions[i];
82 maxAmplitude = jmax(maxAmplitude, mcLeodBuffer[i]);
84 if (mcLeodBuffer[i] > SMALL_CUTOFF)
86 parabolicInterpolation(tauEstimation);
87 amplitudeEstimates[amplitudeEstimatesIndex++] = interpolatedY;
88 periodEstimates[periodEstimatesIndex++] = interpolatedX;
89 maxAmplitude = jmax(maxAmplitude, interpolatedY);
93 if (periodEstimatesIndex != 0)
95 const float threshold = float(CUTOFF) * maxAmplitude;
98 for (
int i = 0; i < amplitudeEstimatesIndex; ++i)
100 if (amplitudeEstimates[i] > threshold)
106 pitchEstimation = float(sampleRate) / periodEstimates[periodIndex];
107 if (pitchEstimation < LOWER_PITCH_CUTOFF)
108 pitchEstimation = -1;
111 return pitchEstimation;
117 for (
int tau = 0; tau < bufferSize; ++tau)
119 float autocorrelationSum = 0;
120 float squareDifferenceSum = 0;
122 for (
int index = 0; index < bufferSize - tau; ++index)
124 autocorrelationSum += buffer[index] * buffer[index + tau];
125 squareDifferenceSum += buffer[index] * buffer[index] + buffer[index + tau] * buffer[index + tau];
128 mcLeodBuffer[tau] = 2 * autocorrelationSum / squareDifferenceSum;
135 int currentMaxPosition = 0;
137 while (position < (bufferSize - 1) / 3 && mcLeodBuffer[position] > 0.0f)
143 while (position < (bufferSize - 1) &&
144 mcLeodBuffer[position] <= 0.0f)
152 while (position < bufferSize - 1)
155 if (mcLeodBuffer[position] > mcLeodBuffer[position - 1] &&
156 mcLeodBuffer[position] >= mcLeodBuffer[position + 1])
158 if (currentMaxPosition == 0)
159 currentMaxPosition = position;
160 else if (mcLeodBuffer[position] > mcLeodBuffer[currentMaxPosition])
161 currentMaxPosition = position;
167 if (position < bufferSize - 1 && mcLeodBuffer[position] <= 0.0f)
169 if (currentMaxPosition > 0)
173 peakPositions[peakPositionsIndex++] = currentMaxPosition;
174 currentMaxPosition = 0;
177 while (position < bufferSize - 1 &&
178 mcLeodBuffer[position] <= 0.0f)
186 if (currentMaxPosition > 0)
187 peakPositions[peakPositionsIndex++] = currentMaxPosition;
197 float previous = mcLeodBuffer[tauEstimation - 1];
198 float current = mcLeodBuffer[tauEstimation];
199 float next = mcLeodBuffer[tauEstimation + 1];
201 float divisor = next + previous - 2 * current;
205 interpolatedX = float(tauEstimation);
206 interpolatedY = mcLeodBuffer[tauEstimation];
210 interpolatedX = tauEstimation + (previous - next) / (2 * divisor);
211 interpolatedY = mcLeodBuffer[tauEstimation] - ((previous - next) * (previous - next)) / (8 * divisor);