]> git.sesse.net Git - pitch/blobdiff - pitch.cpp
Added support for guitar-specific tuning.
[pitch] / pitch.cpp
index 8080748e17a01d29fa0b8f414c131e4173489176..92c7b085cf026ce6a9aa9a2f5ff070bb875e7115 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
                                  * http://www-ccrma.stanford.edu/~jos/parshl/Choice_Hop_Size.html).
                                  */
 
                                  * http://www-ccrma.stanford.edu/~jos/parshl/Choice_Hop_Size.html).
                                  */
 
+#define EQUAL_TEMPERAMENT     0
+#define WELL_TEMPERED_GUITAR  1
+
+#define TUNING EQUAL_TEMPERAMENT
+
 int get_dsp_fd();
 void read_chunk(int fd, double *in, unsigned num_samples);
 void apply_window(double *in, double *out, unsigned num_samples);
 int get_dsp_fd();
 void read_chunk(int fd, double *in, unsigned num_samples);
 void apply_window(double *in, double *out, unsigned num_samples);
@@ -55,7 +60,11 @@ int main()
                std::pair<double, double> peak = find_peak(bins, FFT_LENGTH);
 
                if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) {
                std::pair<double, double> peak = find_peak(bins, FFT_LENGTH);
 
                if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) {
+#if TUNING == WELL_TEMPERED_GUITAR
+                       printf("......\n");
+#else          
                        printf("............\n");
                        printf("............\n");
+#endif
                } else {
                        print_spectrogram(peak.first, peak.second - log10(FFT_LENGTH));
                }
                } else {
                        print_spectrogram(peak.first, peak.second - log10(FFT_LENGTH));
                }
@@ -301,6 +310,7 @@ std::string freq_to_tonename(double freq)
        return buf;
 }
 
        return buf;
 }
 
+#if TUNING == EQUAL_TEMPERAMENT
 void print_spectrogram(double freq, double amp)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
 void print_spectrogram(double freq, double amp)
 {
        std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
@@ -332,3 +342,52 @@ void print_spectrogram(double freq, double amp)
        printf("]\n");
 
 }
        printf("]\n");
 
 }
+#else
+struct note {
+       char notename[16];
+       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) }
+};
+
+void print_spectrogram(double freq, double amp)
+{
+       double best_away = 999999999.9;
+       unsigned best_away_ind = 0;
+       
+       for (unsigned i = 0; i < sizeof(notes)/sizeof(note); ++i) {
+               double half_notes_away = 12.0 * log2(freq / notes[i].freq);
+               if (fabs(half_notes_away) < fabs(best_away)) {
+                       best_away = half_notes_away;
+                       best_away_ind = i;
+               }
+       }
+       
+       for (unsigned i = 0; i < sizeof(notes)/sizeof(note); ++i)
+               if (i == best_away_ind)
+                       printf("#");
+               else
+                       printf(".");
+
+       printf(" (%s %+.2f, %5.2fHz) [%5.2fdB]  [", notes[best_away_ind].notename, best_away, freq, amp);
+
+       for (int i = -10; i <= 10; ++i) {
+               if (best_away >= (i-0.5) * 0.05 && best_away < (i+0.5) * 0.05) {
+                       printf("#");
+               } else {
+                       if (i == 0) {
+                               printf("|");
+                       } else {
+                               printf("-");
+                       }
+               }
+       }
+       printf("]\n");
+}
+#endif