X-Git-Url: https://git.sesse.net/?p=pitch;a=blobdiff_plain;f=pitch.cpp;h=4e0e3eba09fda41c971c9289ebb831662a6faa58;hp=92c7b085cf026ce6a9aa9a2f5ff070bb875e7115;hb=ed42daa790cfe2942c8c83a729bbce3744bae89b;hpb=b81a44fa57306d7e2b1376419a5448f5d8c72291 diff --git a/pitch.cpp b/pitch.cpp index 92c7b08..4e0e3eb 100644 --- a/pitch.cpp +++ b/pitch.cpp @@ -20,14 +20,16 @@ #define EQUAL_TEMPERAMENT 0 #define WELL_TEMPERED_GUITAR 1 -#define TUNING EQUAL_TEMPERAMENT +#define TUNING WELL_TEMPERED_GUITAR int get_dsp_fd(); void read_chunk(int fd, double *in, unsigned num_samples); void apply_window(double *in, double *out, unsigned num_samples); std::pair find_peak(double *in, unsigned num_samples); void find_peak_magnitudes(std::complex *in, double *out, unsigned num_samples); +std::pair adjust_for_overtones(std::pair base, double *in, unsigned num_samples); double bin_to_freq(double bin, unsigned num_samples); +double freq_to_bin(double freq, unsigned num_samples); std::string freq_to_tonename(double freq); std::pair interpolate_peak(double ym1, double y0, double y1); void print_spectrogram(double freq, double amp); @@ -58,6 +60,7 @@ int main() fftw_execute(p); find_peak_magnitudes(out, bins, FFT_LENGTH); std::pair peak = find_peak(bins, FFT_LENGTH); + 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 @@ -87,9 +90,12 @@ int get_dsp_fd() int chan = 1; ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &chan); - int rate = 22050; + int rate = SAMPLE_RATE; ioctl(fd, SOUND_PCM_WRITE_RATE, &rate); + int fragments = 0x00020002; + ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fragments); + ioctl(3, SNDCTL_DSP_SYNC, 0); return fd; @@ -234,10 +240,66 @@ std::pair find_peak(double *in, unsigned num_samples) return std::make_pair(bin_to_freq(double(best_bin) + peak.first, num_samples), peak.second); } +// it's perhaps not ideal to _first_ find the peak and _then_ the harmonics -- +// ideally, one should find the set of all peaks and then determine the likely +// base from that... something for later. :-) +std::pair adjust_for_overtones(std::pair base, double *in, unsigned num_samples) +{ + double mu = base.first, var = 1.0 / (base.second * base.second); + printf("mu=%f, var=%f\n", mu, var); + + for (unsigned i = 2; i < 10; ++i) { + 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 (upper >= num_samples) + upper = num_samples - 2; + + // printf("Searching in [%u,%u] = %f..%f\n", lower, upper, bin_to_freq(lower, num_samples), bin_to_freq(upper, num_samples)); + + // search for a peak in this interval + double best_harmonics_freq = -1.0; + double best_harmonics_amp = -1.0; + for (unsigned j = lower; j <= upper; ++j) { + if (in[j] > in[j-1] && in[j] > in[j+1]) { + std::pair peak = + interpolate_peak(in[j - 1], + in[j], + in[j + 1]); + + if (peak.second > best_harmonics_amp) { + best_harmonics_freq = bin_to_freq(j + peak.first, num_samples); + best_harmonics_amp = peak.second; + } + } + } + + if (best_harmonics_amp <= 0.0) + continue; + + //printf("Found overtone %u at %.2f (amp=%5.2fdB)\n", i, best_harmonics_freq, + // best_harmonics_amp); + + double this_mu = best_harmonics_freq / double(i); + double this_var = 1.0 / (best_harmonics_amp * best_harmonics_amp); + + double k = var / (var + this_var); + mu = (1.0 - k) * mu + k * this_mu; + var *= (1.0 - k); + + printf("mu=%f, var=%f\n", mu, var); + } + return std::make_pair(mu, base.second); +} + double bin_to_freq(double bin, unsigned num_samples) { return bin * SAMPLE_RATE / double(num_samples); } +double freq_to_bin(double freq, unsigned num_samples) +{ + return freq * double(num_samples) / double(SAMPLE_RATE); +} /* * Given three bins, find the interpolated real peak based