8 #include "linux_audio.h"
9 #include "pitchdetector.h"
11 void print_spectrogram(double freq, double amp);
15 PitchDetector pd(SAMPLE_RATE, FFT_LENGTH, PAD_FACTOR, OVERLAP);
16 snd_pcm_t *pcm = get_dsp_handle(SAMPLE_RATE);
18 short buf[FFT_LENGTH / PAD_FACTOR / OVERLAP];
20 read_chunk(pcm, buf, FFT_LENGTH / PAD_FACTOR / OVERLAP);
21 std::pair<double, double> peak = pd.detect_pitch(buf);
23 if (peak.first < 50.0 || peak.second - log10(FFT_LENGTH) < 0.0) {
24 #if TUNING == WELL_TEMPERED_GUITAR
27 printf("............\n");
30 print_spectrogram(peak.first, peak.second - log10(FFT_LENGTH));
35 std::string freq_to_tonename(double freq)
37 std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
38 double half_notes_away = 12.0 * log2(freq / BASE_PITCH) - 3.0;
39 int hnai = int(floor(half_notes_away + 0.5));
40 int octave = (hnai + 48) / 12;
43 sprintf(buf, "%s%d + %.2f [%d]", notenames[((hnai % 12) + 12) % 12].c_str(), octave, half_notes_away - hnai, hnai);
47 #if TUNING == EQUAL_TEMPERAMENT
48 void print_spectrogram(double freq, double amp)
50 std::string notenames[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
51 double half_notes_away = 12.0 * log2(freq / BASE_PITCH) - 3.0;
52 int hnai = int(floor(half_notes_away + 0.5));
53 int octave = (hnai + 48) / 12;
55 for (int i = 0; i < 12; ++i)
56 if (i == ((hnai % 12) + 12) % 12)
61 printf(" (%-2s%d %+.2f, %5.2fHz) [%5.2fdB] [", notenames[((hnai % 12) + 12) % 12].c_str(), octave, half_notes_away - hnai,
64 double off = half_notes_away - hnai;
65 for (int i = -10; i <= 10; ++i) {
66 if (off >= (i-0.5) * 0.05 && off < (i+0.5) * 0.05) {
80 void print_spectrogram(double freq, double amp)
82 double best_away = 999999999.9;
83 unsigned best_away_ind = 0;
85 for (unsigned i = 0; i < sizeof(notes)/sizeof(note); ++i) {
86 double half_notes_away = 12.0 * log2(freq / notes[i].freq);
87 if (fabs(half_notes_away) < fabs(best_away)) {
88 best_away = half_notes_away;
93 for (unsigned i = 0; i < sizeof(notes)/sizeof(note); ++i)
94 if (i == best_away_ind)
99 printf(" (%s %+.2f, %5.2fHz) [%5.2fdB] [", notes[best_away_ind].notename, best_away, freq, amp);
102 for (int i = -10; i <= 10; ++i) {
103 if (best_away >= (i-0.5) * 0.05 && best_away < (i+0.5) * 0.05) {
116 for (int i = -10; i <= 10; ++i) {
117 if (best_away >= (i-0.5) * 0.01 && best_away < (i+0.5) * 0.01) {