]> git.sesse.net Git - pitch/blobdiff - pitch.cpp
Fix a possible segfault.
[pitch] / pitch.cpp
index 13efaf8a5660f9b955c7d9d29cf15e9e9ba03c0c..97238f5f05b1a99113837029199f44434b1f220e 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
@@ -234,6 +234,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],
        std::pair<double, double> peak = 
                interpolate_peak(in[best_bin - 1],
                                 in[best_bin],
@@ -248,6 +251,8 @@ std::pair<double, double> find_peak(double *in, unsigned num_samples)
 std::pair<double, double> adjust_for_overtones(std::pair<double, double> base, double *in, unsigned num_samples)
 {
        double mu = base.first, var = 1.0 / (base.second * base.second);
 std::pair<double, double> adjust_for_overtones(std::pair<double, double> base, double *in, unsigned num_samples)
 {
        double mu = base.first, var = 1.0 / (base.second * base.second);
+               
+       //printf("Base at %.2f (amp=%5.2fdB)\n", base.first, base.second);
 
        for (unsigned i = 2; i < 10; ++i) {
                unsigned middle = unsigned(floor(freq_to_bin(base.first, num_samples) * i + 0.5));
 
        for (unsigned i = 2; i < 10; ++i) {
                unsigned middle = unsigned(floor(freq_to_bin(base.first, num_samples) * i + 0.5));