Hack up audio fft with libfftw3

Signed-off-by: David Oberhollenzer <goliath@infraroot.at>
This commit is contained in:
David Oberhollenzer 2022-07-19 22:37:09 +02:00
parent f3bf686a52
commit b519a77ad9
3 changed files with 26 additions and 7 deletions

View File

@ -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

View File

@ -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);

View File

@ -27,6 +27,9 @@
#include <poll.h>
#include <time.h>
#include <fftw3.h>
#include <math.h>
#define AUDIO_SAMPLES (512)
#define AUDIO_CHANNELS (2)