X-Git-Url: https://git.sesse.net/?p=pitch;a=blobdiff_plain;f=pitch.cpp;h=eac947c775c4001e558dac01e156f1928fb0c0e9;hp=72e956961b1e75a188d0a2083671757aee221aad;hb=67bcecfdd519329d303d315e08733d1d27db6225;hpb=c151532df8989049915955d1df127225753855ea diff --git a/pitch.cpp b/pitch.cpp index 72e9569..eac947c 100644 --- a/pitch.cpp +++ b/pitch.cpp @@ -60,7 +60,7 @@ int main() fftw_execute(p); find_peak_magnitudes(out, bins, FFT_LENGTH); std::pair peak = find_peak(bins, FFT_LENGTH); - if (beak.first > 0.0) + if (peak.first > 0.0) peak = adjust_for_overtones(peak, bins, FFT_LENGTH); if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) { @@ -225,9 +225,9 @@ std::pair find_peak(double *in, unsigned num_samples) best_bin /= i; // consider sliding one bin up or down - if (best_bin > 0 && in[best_bin - 1] > in[best_bin] && in[best_bin - 1] > in[best_bin - 2]) { + if (best_bin > 1 && in[best_bin - 1] > in[best_bin] && in[best_bin - 1] > in[best_bin - 2]) { --best_bin; - } else if (best_bin < num_samples / 2 && in[best_bin + 1] > in[best_bin] && in[best_bin + 1] > in[best_bin + 2]) { + } else if (best_bin < num_samples / 2 - 1 && in[best_bin + 1] > in[best_bin] && in[best_bin + 1] > in[best_bin + 2]) { ++best_bin; } @@ -259,6 +259,8 @@ std::pair adjust_for_overtones(std::pair base, d unsigned middle = unsigned(floor(freq_to_bin(base.first, num_samples) * i + 0.5)); unsigned lower = middle - (i+1)/2, upper = middle + (i+1)/2; + if (lower < 1) + lower = 1; if (upper >= num_samples) upper = num_samples - 2;