feat: SynthSound moved to utils

This commit is contained in:
2023-06-18 14:32:15 +04:00
parent e871d49364
commit 9ec3a3dc20
5 changed files with 44 additions and 40 deletions

1
.vscode/tasks.json vendored
View File

@@ -9,6 +9,7 @@
"-fansi-escape-codes",
"-g",
"${file}",
"${fileDirname}/utils.c",
"${fileDirname}/ring_buffer.c",
"${fileDirname}/parser.c",
"-lm",

View File

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

39
main.c
View File

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

30
utils.c Normal file
View File

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

12
utils.h
View File

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