]> git.sesse.net Git - pitch/blobdiff - pitch.cpp
Don't run adjust_for_overtones if we found no peak.
[pitch] / pitch.cpp
index 1c272f1080b734bb7ae891611660edf6c915d2c0..72e956961b1e75a188d0a2083671757aee221aad 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
@@ -46,7 +46,7 @@ int main()
        in = reinterpret_cast<double *> (fftw_malloc(sizeof(double) * FFT_LENGTH / PAD_FACTOR));
        in_windowed = reinterpret_cast<double *> (fftw_malloc(sizeof(double) * FFT_LENGTH));
        out = reinterpret_cast<std::complex<double> *> (fftw_malloc(sizeof(std::complex<double>) * (FFT_LENGTH / 2 + 1)));
-       bins = reinterpret_cast<double *> (fftw_malloc(sizeof(double) * FFT_LENGTH / 2 + 1));
+       bins = reinterpret_cast<double *> (fftw_malloc(sizeof(double) * (FFT_LENGTH / 2 + 1)));
 
        memset(in, 0, sizeof(double) * FFT_LENGTH / PAD_FACTOR);
 
@@ -60,7 +60,8 @@ int main()
                fftw_execute(p);
                find_peak_magnitudes(out, bins, FFT_LENGTH);
                std::pair<double, double> peak = find_peak(bins, FFT_LENGTH);
-               peak = adjust_for_overtones(peak, bins, FFT_LENGTH);
+               if (beak.first > 0.0)
+                       peak = adjust_for_overtones(peak, bins, FFT_LENGTH);
 
                if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) {
 #if TUNING == WELL_TEMPERED_GUITAR
@@ -234,6 +235,9 @@ std::pair<double, double> find_peak(double *in, unsigned num_samples)
                }
        }
 
+       if (best_bin == 0 || best_bin == num_samples / 2) {
+               return std::make_pair(-1.0, 0.0);
+       }
        std::pair<double, double> peak = 
                interpolate_peak(in[best_bin - 1],
                                 in[best_bin],