In glpitch, stretch the x axis a bit so that we have more precision around the intere...
[pitch] / glpitch.cpp
1 #include <SDL.h>
2 #include <GL/gl.h>
3
4 #include "config.h"
5 #include "linux_audio.h"
6 #include "notes.h"
7 #include "pitchdetector.h"
8
9 #define MIN_X -40
10 #define MAX_X 5
11
12 double find_linear_pos(double freq)
13 {
14         return 12.0 * log2(freq / BASE_PITCH) - 3.0;
15 }
16
17 #if COMPRESS_PITCH_DISPLAY
18 // bend the scale a bit to get more precision around the interesting points
19 double magnifying_glass(double x)
20 {
21         double sigma = PITCH_COMPRESSION_SIGMA;
22         double div = 1.0 / sqrt(2.0 * sigma * sigma);
23         return
24                 erf((x - find_linear_pos(notes[0].freq)) * div) +
25                 erf((x - find_linear_pos(notes[1].freq)) * div) +
26                 erf((x - find_linear_pos(notes[2].freq)) * div) +
27                 erf((x - find_linear_pos(notes[3].freq)) * div) +
28                 erf((x - find_linear_pos(notes[4].freq)) * div) +
29                 erf((x - find_linear_pos(notes[5].freq)) * div);
30 }
31
32 // like nonlinear_func, but f(MIN_X) = MIN_X and f(MAX_X) = MAX_X
33 double normalized_magnifying_glass(double x)
34 {
35         double y = magnifying_glass(x);
36         double min_y = magnifying_glass(MIN_X);
37         double max_y = magnifying_glass(MAX_X);
38         return (y - min_y) * (MAX_X - MIN_X) / (max_y - min_y) + MIN_X;
39 }
40 #endif /* COMPRESS_PITCH_DISPLAY */
41
42 double find_display_pos(double freq)
43 {
44         double linear_pos = find_linear_pos(freq);
45 #if COMPRESS_PITCH_DISPLAY
46         return normalized_magnifying_glass(linear_pos);
47 #else
48         return linear_pos;
49 #endif
50 }
51
52 int main(void)
53 {
54         PitchDetector pd(SAMPLE_RATE, FFT_LENGTH, PAD_FACTOR, OVERLAP);
55         int fd = get_dsp_fd(SAMPLE_RATE, FFT_LENGTH, OVERLAP);
56
57         SDL_Init(SDL_INIT_VIDEO);
58         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
59         SDL_SetVideoMode(1024, 200, 32, SDL_OPENGL);
60
61         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
62         glClear(GL_COLOR_BUFFER_BIT);
63
64         for ( ;; ) {
65                 SDL_Event event;
66                 while (SDL_PollEvent(&event)) {
67                         switch (event.type) {
68                         case SDL_KEYUP:
69                                 if (event.key.keysym.sym == SDLK_ESCAPE) {
70                                         exit(0);
71                                 }
72                                 break;
73                         case SDL_QUIT:
74                                 exit(0);
75                         }
76                 }
77
78                 short buf[FFT_LENGTH / PAD_FACTOR / OVERLAP];
79                 read_chunk(fd, buf, FFT_LENGTH / PAD_FACTOR / OVERLAP);
80                 std::pair<double, double> peak = pd.detect_pitch(buf);
81
82                 glMatrixMode(GL_PROJECTION);
83                 glLoadIdentity();
84                 glOrtho(MIN_X, MAX_X, 1.0f, 0.0f, 0.0f, 1.0f);
85
86                 glMatrixMode(GL_MODELVIEW);
87                 glLoadIdentity();
88
89                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
90                 glEnable(GL_BLEND); 
91
92                 glBegin(GL_QUADS);      
93                 glColor4f(0.0, 0.0f, 0.0f, 0.05f);
94                 glVertex2f(MIN_X, 0.0f);
95                 glVertex2f(MIN_X, 1.0f);
96                 glVertex2f(MAX_X, 1.0f);
97                 glVertex2f(MAX_X, 0.0f);
98                 glEnd();
99
100                 if (peak.second - log10(FFT_LENGTH) >= 0.0) {
101                         double peak_pos = find_display_pos(peak.first);
102
103                         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
104                         glBegin(GL_QUADS);      
105                         glColor4f(0.0, 1.0f, 0.0f, 0.2f);
106                         glVertex2f(peak_pos - 0.1, 0.0f);
107                         glVertex2f(peak_pos - 0.1, 1.0f);
108                         glVertex2f(peak_pos + 0.1, 1.0f);
109                         glVertex2f(peak_pos + 0.1, 0.0f);
110                         glEnd();
111                 }
112
113                 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
114                 glBegin(GL_LINES);
115
116                 for (int i = 0; i < 6; ++i) {
117                         double pos = find_display_pos(notes[i].freq);
118                         glVertex2f(pos, 0.0f);
119                         glVertex2f(pos, 1.0f);
120                 }
121                 glEnd();
122
123                 SDL_GL_SwapBuffers();
124         }
125 }