]> git.sesse.net Git - pitch/blob - pitchdetector.h
Make an SDL/OpenGL-frontend; might be more intuitive to read, although I'm not sure...
[pitch] / pitchdetector.h
1 #ifndef _PITCHDETECTOR_H
2 #define _PITCHDETECTOR_H 1
3
4 #include <fftw3.h>
5
6 #include <complex>
7 #include <vector>
8 #include <utility>
9
10 class PitchDetector {
11 public:
12         PitchDetector(unsigned sample_rate, unsigned fft_length, unsigned pad_factor, unsigned overlap);
13         ~PitchDetector();
14         std::pair<double, double> detect_pitch(short *buf);
15
16 private:
17         // parameters
18         unsigned sample_rate;
19         unsigned fft_length;
20         unsigned pad_factor;
21         unsigned overlap;
22
23         // buffers
24         double *in, *in_windowed;
25         std::complex<double> *out;
26         double *bins;
27         fftw_plan plan;
28         double *window_data;
29
30         // utility functions
31         void apply_window(double *in, double *out, unsigned num_samples);
32         void find_peak_magnitudes(std::complex<double> *in, double *out, unsigned num_samples);
33         std::pair<double, double> find_peak(double *in, unsigned num_samples);
34         std::pair<double, double> adjust_for_overtones(std::pair<double, double> base, double *in, unsigned num_samples);
35         double bin_to_freq(double bin, unsigned num_samples);
36         double freq_to_bin(double freq, unsigned num_samples);
37         std::pair<double, double> interpolate_peak(double ym1, double y0, double y1);
38 };
39
40 #endif /* !defined(_PITCHDETECTOR_H) */