]> git.sesse.net Git - pitch/blobdiff - glpitch.cpp
Make an SDL/OpenGL-frontend; might be more intuitive to read, although I'm not sure...
[pitch] / glpitch.cpp
diff --git a/glpitch.cpp b/glpitch.cpp
new file mode 100644 (file)
index 0000000..90c47b8
--- /dev/null
@@ -0,0 +1,76 @@
+#include <SDL.h>
+#include <GL/gl.h>
+
+#include "config.h"
+#include "linux_audio.h"
+#include "notes.h"
+#include "pitchdetector.h"
+
+#define MIN_X -40
+#define MAX_X 5
+
+double find_pos(double freq)
+{
+       return 12.0 * log2(freq / BASE_PITCH) - 3.0;
+}
+
+int main(void)
+{
+       PitchDetector pd(SAMPLE_RATE, FFT_LENGTH, PAD_FACTOR, OVERLAP);
+       int fd = get_dsp_fd(SAMPLE_RATE, FFT_LENGTH, OVERLAP);
+
+       SDL_Init(SDL_INIT_VIDEO);
+       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+       SDL_SetVideoMode(1024, 200, 32, SDL_OPENGL);
+
+       glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+       glClear(GL_COLOR_BUFFER_BIT);
+
+       for ( ;; ) {
+               short buf[FFT_LENGTH / PAD_FACTOR / OVERLAP];
+               read_chunk(fd, buf, FFT_LENGTH / PAD_FACTOR / OVERLAP);
+               std::pair<double, double> peak = pd.detect_pitch(buf);
+
+               glMatrixMode(GL_PROJECTION);
+               glLoadIdentity();
+               glOrtho(MIN_X, MAX_X, 1.0f, 0.0f, 0.0f, 1.0f);
+
+               glMatrixMode(GL_MODELVIEW);
+               glLoadIdentity();
+
+               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+               glEnable(GL_BLEND); 
+
+               glBegin(GL_QUADS);      
+               glColor4f(0.0, 0.0f, 0.0f, 0.05f);
+               glVertex2f(MIN_X, 0.0f);
+               glVertex2f(MIN_X, 1.0f);
+               glVertex2f(MAX_X, 1.0f);
+               glVertex2f(MAX_X, 0.0f);
+               glEnd();
+
+               glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+
+               double peak_pos = find_pos(peak.first);
+
+               glBegin(GL_QUADS);      
+               glColor4f(0.0, 1.0f, 0.0f, 0.2f);
+               glVertex2f(peak_pos - 0.1, 0.0f);
+               glVertex2f(peak_pos - 0.1, 1.0f);
+               glVertex2f(peak_pos + 0.1, 1.0f);
+               glVertex2f(peak_pos + 0.1, 0.0f);
+               glEnd();
+
+               glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+               glBegin(GL_LINES);
+
+               for (int i = 0; i < 6; ++i) {
+                       double pos = find_pos(notes[i].freq);
+                       glVertex2f(pos, 0.0f);
+                       glVertex2f(pos, 1.0f);
+               }
+               glEnd();
+
+               SDL_GL_SwapBuffers();
+       }
+}