X-Git-Url: https://git.sesse.net/?p=pitch;a=blobdiff_plain;f=pitchdetector.h;fp=pitchdetector.h;h=016cea639b4d54613526ab0ba1871e3fe187bb83;hp=0000000000000000000000000000000000000000;hb=66156de8eb723c104b616223f8ca7cc20ea55d05;hpb=cbf0d7c43e3aa06886d425c4d26c99592eb35ba6 diff --git a/pitchdetector.h b/pitchdetector.h new file mode 100644 index 0000000..016cea6 --- /dev/null +++ b/pitchdetector.h @@ -0,0 +1,34 @@ +#ifndef _PITCHDETECTOR_H +#define _PITCHDETECTOR_H 1 + +class PitchDetector { +public: + PitchDetector(unsigned sample_rate, unsigned fft_length, unsigned pad_factor, unsigned overlap); + ~PitchDetector(); + std::pair detect_pitch(short *buf); + +private: + // parameters + unsigned sample_rate; + unsigned fft_length; + unsigned pad_factor; + unsigned overlap; + + // buffers + double *in, *in_windowed; + std::complex *out; + double *bins; + fftw_plan plan; + double *window_data; + + // utility functions + void apply_window(double *in, double *out, unsigned num_samples); + void find_peak_magnitudes(std::complex *in, double *out, unsigned num_samples); + std::pair find_peak(double *in, 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::pair interpolate_peak(double ym1, double y0, double y1); +}; + +#endif /* !defined(_PITCHDETECTOR_H) */