]> git.sesse.net Git - pitch/blobdiff - pitchdetector.h
Split the pitch logic into its own class.
[pitch] / pitchdetector.h
diff --git a/pitchdetector.h b/pitchdetector.h
new file mode 100644 (file)
index 0000000..016cea6
--- /dev/null
@@ -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<double, double> detect_pitch(short *buf);
+
+private:
+       // parameters
+       unsigned sample_rate;
+       unsigned fft_length;
+       unsigned pad_factor;
+       unsigned overlap;
+
+       // buffers
+       double *in, *in_windowed;
+       std::complex<double> *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<double> *in, double *out, unsigned num_samples);
+       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 bin_to_freq(double bin, unsigned num_samples);
+       double freq_to_bin(double freq, unsigned num_samples);
+       std::pair<double, double> interpolate_peak(double ym1, double y0, double y1);
+};
+
+#endif /* !defined(_PITCHDETECTOR_H) */