Added support for guitar-specific tuning.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 27 Jul 2005 23:18:02 +0000 (23:18 +0000)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 27 Jul 2005 23:18:02 +0000 (23:18 +0000)
pitch.cpp

index 8080748..92c7b08 100644 (file)
--- a/pitch.cpp
+++ b/pitch.cpp
                                  * 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);
@@ -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) {
+#if TUNING == WELL_TEMPERED_GUITAR
+                       printf("......\n");
+#else          
                        printf("............\n");
+#endif
                } else {
                        print_spectrogram(peak.first, peak.second - log10(FFT_LENGTH));
                }
@@ -301,6 +310,7 @@ std::string freq_to_tonename(double freq)
        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" };
@@ -332,3 +342,52 @@ void print_spectrogram(double freq, double amp)
        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