diff --git a/Makefile b/Makefile index d22af13..705ac2c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS = -ansi -pedantic -Wall -Wextra -O2 -Ofast -D_DEFAULT_SOURCE -LDFLAGS = -lX11 -lGL -lGLEW -lm -lrt +LDFLAGS = -lX11 -lGL -lGLEW -lfftw3 -lm -lrt shadermeh: shadermeh.o window.o shader.o diff --git a/shadermeh.c b/shadermeh.c index cbeaf2c..f29af1d 100644 --- a/shadermeh.c +++ b/shadermeh.c @@ -22,6 +22,9 @@ static GLfloat vertex_buffer[] = { static GLubyte audio_buffer[AUDIO_SAMPLES * AUDIO_CHANNELS]; static float audio_sample_data[AUDIO_SAMPLES]; +static fftw_complex fftw_in[AUDIO_SAMPLES]; +static fftw_complex fftw_out[AUDIO_SAMPLES]; +static fftw_plan plan; static int try_fetch_audio(void) { @@ -48,9 +51,17 @@ static int try_fetch_audio(void) count += ret; } - for (i = 0; i < (count / sizeof(float)); ++i) { + for (i = 0; i < AUDIO_SAMPLES; ++i) + fftw_in[i][0] = audio_sample_data[i]; + + fftw_execute(plan); + + for (i = 0; i < AUDIO_SAMPLES; ++i) { + float x = fftw_out[i][0], y = fftw_out[i][1]; + float a = sqrt(x * x + y * y); + audio_buffer[i + AUDIO_SAMPLES] = audio_sample_data[i] * 127.0f + 127.0f; - audio_buffer[i] = 0.0f; + audio_buffer[i] = 127.0f + a * 127.0f; } return 0; @@ -329,6 +340,11 @@ int main(int argc, char **argv) glSamplerParameteri(sampler_sound, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindSampler(0, sampler_sound); + if (have_audio) { + plan = fftw_plan_dft_1d(AUDIO_SAMPLES, fftw_in, fftw_out, + FFTW_FORWARD, FFTW_ESTIMATE); + } + /******************** framebuffer object ********************/ if (to_stdout) { glGenFramebuffers(1, &fbo); @@ -349,10 +365,6 @@ int main(int argc, char **argv) } /******************** drawing loop ********************/ - for (i = 0; i < AUDIO_SAMPLES; ++i) { - audio_sample_data[i] = (float)i / (float)AUDIO_SAMPLES; - } - clock_gettime(CLOCK_MONOTONIC_RAW, &start); while (to_stdout || window_handle_events()) { @@ -426,6 +438,10 @@ int main(int argc, char **argv) glUseProgram(0); glDeleteProgram(prog); + + if (have_audio) { + fftw_destroy_plan(plan); + } fail_vao: glBindBuffer(GL_ARRAY_BUFFER, 0); glDeleteBuffers(1, &vbo); diff --git a/shadermeh.h b/shadermeh.h index 88b0c8a..1782810 100644 --- a/shadermeh.h +++ b/shadermeh.h @@ -27,6 +27,9 @@ #include #include +#include +#include + #define AUDIO_SAMPLES (512) #define AUDIO_CHANNELS (2)