From 7af91e2bc4a51670343ba3d5852c8e6b688a5a13 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Sun, 9 Apr 2017 00:57:13 +0200 Subject: Look up note corresponding to frequency --- tuner.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) (limited to 'tuner.c') diff --git a/tuner.c b/tuner.c index 67cfadb..c4e6221 100644 --- a/tuner.c +++ b/tuner.c @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include #include @@ -9,6 +11,8 @@ #define SAMPLE_RATE 8000 #define FFT_SIZE (1<<13) +#define ACCURACY 1.0 // Hz + static double *fft_in; static fftw_complex *fft_out; static fftw_plan fft_plan; @@ -74,10 +78,46 @@ static void fft_cleanup() fftw_free(fft_out); } +static void find_note(double freq) +{ + int i, index = 0, dir = 0; + char note = '?'; + + /* E, A, D, G, B, e */ + const double notes[] = { INT_MIN, 82.41, 110.00, 146.83, 196.00, 246.94, 329.63, INT_MAX }; + const char *note_str = "?EADGBe?"; + + for (i=0; i<7; i++) { + if (freq > notes[i+1]) { + continue; + } + printf("%f %f\n", freq, (notes[i] + notes[i+1])/2); + if (freq > (notes[i] + notes[i+1])/2) { + note = note_str[i+1]; + dir = -1; + index = i+1; + } else { + note = note_str[i]; + dir = +1; + index = i; + } + break; + } + + if (fabs(freq - notes[index]) < ACCURACY) { + dir = 0; + } + + if (dir < 0) printf(">"); + printf("%c", note); + if (dir > 0) printf("<"); + printf("\n"); +} + static void process_frames() { - int i, freq, index = -1; - double largest = -1; + int i, index = -1; + double largest = -1, freq; fftw_execute(fft_plan); @@ -90,15 +130,15 @@ static void process_frames() index = i; } } - freq = index * SAMPLE_RATE / FFT_SIZE; - printf("freq: %d Hz\n", freq); + freq = (float)index * SAMPLE_RATE / FFT_SIZE; + printf("freq: %f Hz\n", freq); + find_note(freq); } static void capture(snd_pcm_t *pcm_handle) { - int read; - while(capturing) { + int read; while ((read = snd_pcm_readi(pcm_handle, fft_in, FFT_SIZE)) < 0) { snd_pcm_prepare(pcm_handle); printf("overrun\n"); -- cgit v1.2.3