From 6c358a842baaf6d211a5c8c1717d815e7813ed96 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sun, 11 Jul 2021 18:45:38 +0800 Subject: First commit Now I have a kind of piano like instrument. A violin-like instrument is being constructed. --- util.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 util.c (limited to 'util.c') diff --git a/util.c b/util.c new file mode 100644 index 0000000..c5921e5 --- /dev/null +++ b/util.c @@ -0,0 +1,80 @@ +#include "util.h" +#include +#include +#include + +UNUSED +long +read_entire_file (const char *file_name, char **str) +{ + long file_size = 0; + + FILE *file = fopen(file_name, "r"); + + if (!file) { + fprintf(stderr, "Cannot open file \"%s\": ", file_name); + perror(NULL); + return 0; + } + + fseek(file, 0, SEEK_END); + file_size = ftell(file); + fseek(file, 0, SEEK_SET); + + *str = (char*)realloc(*str, file_size+1); + *((*str)+file_size) = 0; + + fread(*str, sizeof(char), file_size, file); + + fclose(file); + return file_size; +} + +UC_ATTR +LENT +max(const LENT a, const LENT b) +{ + return (a < b) ? b : a; +} + +U_ATTR +LENT +min(LENT a, LENT b) +{ + return (a < b) ? a : b; +} + +U_ATTR +WaveFrag +mix_waves(WaveFrag *frags, LENT len) +{ + WaveFrag wf = (WaveFrag) { NULL, 0 }; + + LENT max_len = 0; + + Pulse p = 0.0f; + + for (LENT i = 0; i < len; i++) + max_len = max(max_len, (frags+i)->n); + + wf.n = max_len; + wf.w = MYALLOC(Pulse, max_len); + + for (LENT i = 0; i < max_len; i++) { + p = 0.0f; + + for (LENT j = 0; j < len; j++) + if (i < (frags+j)->n) p += *((frags+j)->w+i); + + *(wf.w+i) = p / (float) len; + } + + return wf; +} + +HC_ATTR +Hertz +stoh(const Semitones st) +{ + return STDP * pow(STD_BASE, st); +} -- cgit v1.2.3-18-g5258