From 7e190a572951be3bf3b66d0ac1694dd49d74e0bb Mon Sep 17 00:00:00 2001 From: Tyrolyean Date: Mon, 18 Jul 2022 19:56:18 +0200 Subject: [PATCH] Add data structures and functions Signed-off-by: Tyrolyean --- shadermeh.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/shadermeh.c b/shadermeh.c index 68c2e04..6dd8681 100644 --- a/shadermeh.c +++ b/shadermeh.c @@ -85,6 +85,27 @@ static int write_retry(int fd, const void *buffer, size_t size) return 0; } +#define SND_BUFFER_SIZE 512 + +static int read_audio_buffers(int fd, float buffer[SND_BUFFER_SIZE], + unsigned int sampling_rate){ + + return 0; +} + +static void normalize_audio_buffers(float buffer_in[SND_BUFFER_SIZE], + float buffer_out[SND_BUFFER_SIZE]){ + + if(buffer_in == NULL || buffer_out == NULL){ + return; + } + + for(size_t i = 0; i < SND_BUFFER_SIZE; i++){ + buffer_out[i] = (buffer_in[i]+1.0)/2.0; + } + return; +} + static const struct option long_opts[] = { { "width", required_argument, NULL, 'w' }, { "height", required_argument, NULL, 'h' }, @@ -110,7 +131,6 @@ static const char *usage_str = " --shader, -s \n" "\n"; -#define SND_BUFFER_SIZE 512 int main(int argc, char **argv) { @@ -125,7 +145,7 @@ int main(int argc, char **argv) float iTime, iTimeDelta; bool to_stdout = false; window *wnd; - int i; + int i, ret; unsigned int sampling_rate = 0; /* Leaving this at 0 disables audio * input */ @@ -220,6 +240,15 @@ int main(int argc, char **argv) fputs("Sampling rate specified and STDIN not a tty! " "You habe been warnded!\n", stderr); } + if(sampling_rate != 0){ + ret = fcntl(STDIN_FILENO, F_SETFL, + fcntl(STDIN_FILENO,F_GETFL) + |(O_NONBLOCK)); + if(ret == -1){ + perror("Failed to set STDIN_FILENO to non-blocking " + "mode! This will likely mean failure"); + } + } /********** create window and make context current **********/ wnd = window_create(width, height, "shader meh..."); @@ -332,6 +361,13 @@ int main(int argc, char **argv) break; } } + if(sampling_rate != 0){ + if(read_audio_buffers(STDIN_FILENO, in_samples, + sampling_rate) < 0){ + break; + } + normalize_audio_buffers(in_samples, norm_samples); + } /* update timers */ clock_gettime(CLOCK_MONOTONIC_RAW, &frame_end);