feat: SynthSound moved to utils
This commit is contained in:
1
.vscode/tasks.json
vendored
1
.vscode/tasks.json
vendored
@@ -9,6 +9,7 @@
|
||||
"-fansi-escape-codes",
|
||||
"-g",
|
||||
"${file}",
|
||||
"${fileDirname}/utils.c",
|
||||
"${fileDirname}/ring_buffer.c",
|
||||
"${fileDirname}/parser.c",
|
||||
"-lm",
|
||||
|
||||
2
build.sh
2
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
|
||||
|
||||
39
main.c
39
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
|
||||
//------------------------------------------------------------------------------------
|
||||
|
||||
30
utils.c
Normal file
30
utils.c
Normal 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
12
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
|
||||
Reference in New Issue
Block a user