Make an SDL/OpenGL-frontend; might be more intuitive to read, although I'm not sure...
authorsgunderson@bigfoot.com <>
Mon, 19 Jul 2010 21:51:09 +0000 (23:51 +0200)
committersgunderson@bigfoot.com <>
Mon, 19 Jul 2010 21:51:09 +0000 (23:51 +0200)
Makefile
glpitch.cpp [new file with mode: 0644]

index 9f3a83f..3ba2c2f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,14 @@
 CXXFLAGS=-g -Wall
-CPPFLAGS=
+CPPFLAGS=$(shell sdl-config --cflags)
 
-all: pitch
+all: pitch glpitch
 clean:
-       $(RM) pitch pitch.o pitchdetector.o linux_audio.o
+       $(RM) pitch glpitch pitch.o glpitch.o pitchdetector.o linux_audio.o
 
 pitch: pitch.o pitchdetector.o linux_audio.o
        $(CXX) -o pitch pitch.o pitchdetector.o linux_audio.o -lfftw3
 
+glpitch: glpitch.o pitchdetector.o linux_audio.o
+       $(CXX) -o glpitch glpitch.o pitchdetector.o linux_audio.o $(shell sdl-config --libs) -lfftw3 -lGL
+
 .PHONY: clean
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();
+       }
+}