]> git.sesse.net Git - pitch/blobdiff - pitch.cpp
Make the ALSA reader handle and recover from underruns.
[pitch] / pitch.cpp
index 1ab4696c5b9794c8c24dc1bc0a4e077eeec7c90c..421c2f24cea126da2dc786f4a918624dd459ac41 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
@@ -3,34 +3,21 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include "config.h"
+#include "notes.h"
 #include "linux_audio.h"
 #include "pitchdetector.h"
 
-#define BASE_PITCH      440.0
-#define SAMPLE_RATE     22050
-#define FFT_LENGTH      4096     /* in samples */
-#define PAD_FACTOR      2        /* 1/pf of the FFT samples are real samples, the rest are padding */
-#define OVERLAP         4        /* 1/ol samples will be replaced in the buffer every frame. Should be
-                                 * a multiple of 2 for the Hamming window (see
-                                 * http://www-ccrma.stanford.edu/~jos/parshl/Choice_Hop_Size.html).
-                                 */
-
-#define EQUAL_TEMPERAMENT     0
-#define WELL_TEMPERED_GUITAR  1
-
-#define TUNING WELL_TEMPERED_GUITAR
-
 void print_spectrogram(double freq, double amp);
-void write_sine(int dsp_fd, double freq, unsigned num_samples);
 
 int main()
 {
        PitchDetector pd(SAMPLE_RATE, FFT_LENGTH, PAD_FACTOR, OVERLAP);
-       int fd = get_dsp_fd(SAMPLE_RATE, FFT_LENGTH, OVERLAP);
+       snd_pcm_t *pcm = get_dsp_handle(SAMPLE_RATE);
        for ( ;; ) {
                short buf[FFT_LENGTH / PAD_FACTOR / OVERLAP];
 
-               read_chunk(fd, buf, FFT_LENGTH / PAD_FACTOR / OVERLAP);
+               read_chunk(pcm, buf, FFT_LENGTH / PAD_FACTOR / OVERLAP);
                std::pair<double, double> peak = pd.detect_pitch(buf);
 
                if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) {
@@ -90,19 +77,6 @@ void print_spectrogram(double freq, double amp)
 
 }
 #else
-struct note {
-       char notename[16];
-       double freq;
-};
-static note notes[] = {
-       { "E-3", BASE_PITCH/4.0 * (3.0/4.0) },
-       { "A-3", BASE_PITCH/4.0 },
-       { "D-4", BASE_PITCH/4.0 * (4.0/3.0) },
-       { "G-4", BASE_PITCH/4.0 * (4.0/3.0)*(4.0/3.0) },
-       { "B-4", BASE_PITCH * (3.0/4.0)*(3.0/4.0) },
-       { "E-5", BASE_PITCH * (3.0/4.0) }
-};
-
 void print_spectrogram(double freq, double amp)
 {
        double best_away = 999999999.9;