]> git.sesse.net Git - pitch/blobdiff - pitch.cpp
#define away the 440 constant, in case you want to tune with a capo.
[pitch] / pitch.cpp
index 933fef9aacc97896dc108dd2466ecbb85f23f8e3..39e20cef2b9a3a30e0fdeb600e079c2607610338 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -11,6 +12,7 @@
 
 #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 */
@@ -126,7 +128,7 @@ void write_sine(int dsp_fd, double freq, unsigned num_samples)
 std::string freq_to_tonename(double freq)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
-       double half_notes_away = 12.0 * log2(freq / 440.0) - 3.0;
+       double half_notes_away = 12.0 * log2(freq / BASE_PITCH) - 3.0;
        int hnai = int(floor(half_notes_away + 0.5));
        int octave = (hnai + 48) / 12;
 
@@ -139,7 +141,7 @@ std::string freq_to_tonename(double freq)
 void print_spectrogram(double freq, double amp)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
-       double half_notes_away = 12.0 * log2(freq / 440.0) - 3.0;
+       double half_notes_away = 12.0 * log2(freq / BASE_PITCH) - 3.0;
        int hnai = int(floor(half_notes_away + 0.5));
        int octave = (hnai + 48) / 12;
 
@@ -173,12 +175,12 @@ struct note {
        double freq;
 };
 static note notes[] = {
-       { "E-3", 110.0 * (3.0/4.0) },
-       { "A-3", 110.0 },
-       { "D-4", 110.0 * (4.0/3.0) },
-       { "G-4", 110.0 * (4.0/3.0)*(4.0/3.0) },
-       { "B-4", 440.0 * (3.0/4.0)*(3.0/4.0) },
-       { "E-5", 440.0 * (3.0/4.0) }
+       { "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)
@@ -202,6 +204,7 @@ void print_spectrogram(double freq, double amp)
 
        printf(" (%s %+.2f, %5.2fHz) [%5.2fdB]  [", notes[best_away_ind].notename, best_away, freq, amp);
 
+       // coarse tuning
        for (int i = -10; i <= 10; ++i) {
                if (best_away >= (i-0.5) * 0.05 && best_away < (i+0.5) * 0.05) {
                        printf("#");
@@ -213,6 +216,20 @@ void print_spectrogram(double freq, double amp)
                        }
                }
        }
+       printf("]  [");
+       
+       // fine tuning
+       for (int i = -10; i <= 10; ++i) {
+               if (best_away >= (i-0.5) * 0.01 && best_away < (i+0.5) * 0.01) {
+                       printf("#");
+               } else {
+                       if (i == 0) {
+                               printf("|");
+                       } else {
+                               printf("-");
+                       }
+               }
+       }
        printf("]\n");
 }
 #endif