diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 26f74c1..eba6acd 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -9,6 +9,7 @@ "-fansi-escape-codes", "-g", "${file}", + "${fileDirname}/utils.c", "${fileDirname}/ring_buffer.c", "${fileDirname}/parser.c", "-lm", diff --git a/build.sh b/build.sh index 5358a2e..0c4a7a5 100644 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/bin/bash CC="${CXX:-cc}" -$CC -Wall -std=c11 ./main.c ./ring_buffer.c ./parser.c -lm -lraylib -o ./bin/main +$CC -Wall -std=c11 ./main.c ./utils.c ./ring_buffer.c ./parser.c -lm -lraylib -o ./bin/main diff --git a/main.c b/main.c index c9c7335..4632b3c 100644 --- a/main.c +++ b/main.c @@ -24,45 +24,6 @@ #define WINDOW_HEIGHT 480 - - - -//------------------------------------------------------------------------------------ -// General SynthSound -//------------------------------------------------------------------------------------ - -typedef struct SynthSound { - float* samples; - size_t sample_count; -} SynthSound; - -// frees the original sounds -SynthSound concat_sounds(SynthSound* sounds, size_t count) { - size_t total_count = 0; - for (size_t i = 0; i < count; i++) { - total_count += sounds[i].sample_count; - } - // array to hold the result - float* total = malloc(total_count * sizeof(float)); - - size_t current_count = 0; - for (size_t i = 0; i < count; i++) { - memcpy(total + current_count, - sounds[i].samples, - sounds[i].sample_count * sizeof(float)); - current_count += sounds[i].sample_count; - - free(sounds[i].samples); - } - - SynthSound result = { - .samples = total, - .sample_count = total_count - }; - - return result; -} - //------------------------------------------------------------------------------------ // Oscillator //------------------------------------------------------------------------------------ diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..694def5 --- /dev/null +++ b/utils.c @@ -0,0 +1,30 @@ +#include "utils.h" +#include "stdlib.h" +#include "string.h" + +// frees the original sounds +SynthSound concat_sounds(SynthSound* sounds, size_t count) { + size_t total_count = 0; + for (size_t i = 0; i < count; i++) { + total_count += sounds[i].sample_count; + } + // array to hold the result + float* total = malloc(total_count * sizeof(float)); + + size_t current_count = 0; + for (size_t i = 0; i < count; i++) { + memcpy(total + current_count, + sounds[i].samples, + sounds[i].sample_count * sizeof(float)); + current_count += sounds[i].sample_count; + + free(sounds[i].samples); + } + + SynthSound result = { + .samples = total, + .sample_count = total_count + }; + + return result; +} \ No newline at end of file diff --git a/utils.h b/utils.h index 6941528..287b8bc 100644 --- a/utils.h +++ b/utils.h @@ -7,4 +7,16 @@ printf(format, ## args); \ } while(0) +//------------------------------------------------------------------------------------ +// General SynthSound +//------------------------------------------------------------------------------------ + +typedef struct SynthSound { + float* samples; + size_t sample_count; +} SynthSound; + +// frees the original sounds +SynthSound concat_sounds(SynthSound* sounds, size_t count); + #endif \ No newline at end of file