From 8bc7c712b7e3e281c42eee337194c4e1710f07df Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sat, 18 Nov 2023 19:57:57 +0300 Subject: [PATCH] template.c: reformat and add more comments --- .clang-format | 12 ------------ src/template.c | 40 +++++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 25 deletions(-) delete mode 100644 .clang-format diff --git a/.clang-format b/.clang-format deleted file mode 100644 index b2bc796..0000000 --- a/.clang-format +++ /dev/null @@ -1,12 +0,0 @@ -BasedOnStyle: Mozilla -AlignAfterOpenBracket: AlwaysBreak -AlignConsecutiveAssignments: false -AlignTrailingComments: false -AllowShortIfStatementsOnASingleLine: false -AllowShortLoopsOnASingleLine: false -BreakBeforeBinaryOperators: None -ColumnLimit: 80 -IndentWidth: 4 -SpaceBeforeRangeBasedForLoopColon: false -TabWidth: 4 -UseTab: Always \ No newline at end of file diff --git a/src/template.c b/src/template.c index 0d6eebf..2823bcc 100644 --- a/src/template.c +++ b/src/template.c @@ -24,20 +24,20 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING; #define SECONDS `seconds` #if BIT_DEPTH <= 8 -#define SAMPLE_TYPE uint8_t +# define SAMPLE_TYPE uint8_t #elif BIT_DEPTH >= 16 -#if IS_SIGNED -#define SAMPLE_TYPE int16_t -#else -#define SAMPLE_TYPE uint16_t -#endif +# if IS_SIGNED +# define SAMPLE_TYPE int16_t +# else +# define SAMPLE_TYPE uint16_t +# endif #endif #define PRODUCT (SAMPLE_RATE * SECONDS * CHANNELS) #define FREQUENCY_OF_STATUS_REPORTING 5000 -#define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1) -#define PCM_COEFFICIENT ((1 << (BIT_DEPTH - 1)) - 1) +#define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1) +#define PCM_COEFFICIENT ((1 << (BIT_DEPTH - 1)) - 1) #define unsigned_to_signed(x) (x - PCM_COEFFICIENT) #define signed_to_unsigned(x) (x + PCM_COEFFICIENT) @@ -97,20 +97,20 @@ bytebeat(long double w) { `final_sample_rate_code` uintmax_t t = (uintmax_t)w; - `bytebeat_contents` - ; } int main(int argc, char** argv) { + // * parse CLI arguments silent_mode = argc > 1 && argv[1] != NULL && (!strcmp(argv[1], "-s") || !strcmp(argv[1], "--silent")); debug_mode = argc > 1 && argv[1] != NULL && (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--debug")); + // * log -> welcome printf(":: C bytebeat generator runtime unit\n"); fflush(stdout); @@ -142,6 +142,7 @@ main(int argc, char** argv) fflush(stdout); } + // * allocate heap for sample data SAMPLE_TYPE* q = calloc(PRODUCT, sizeof(SAMPLE_TYPE)); if (q == NULL) @@ -149,22 +150,28 @@ main(int argc, char** argv) debug_print(); + // * bytebeat generating loop for (uintmax_t w = 0; w < PRODUCT; w++) { + // 1. generate audio data long double res = bytebeat((long double)w); + // 2. if signed, then wrap up into unsigned if (IS_SIGNED) q[w] = (SAMPLE_TYPE)signed_to_unsigned(res) & BIT_DEPTH_LIMITER; else q[w] = (SAMPLE_TYPE)res & BIT_DEPTH_LIMITER; + // 4. if bit depth is less than 8, stretch it #if BIT_DEPTH < 8 q[(size_t)w] = (SAMPLE_TYPE)((long double)q[(size_t)w] * ((long double)BIT_DEPTH / 8.0)); #endif + // 6. log if ( !silent_mode && - (w % FREQUENCY_OF_STATUS_REPORTING == 0 || w >= PRODUCT - 1)) { + (w % FREQUENCY_OF_STATUS_REPORTING == 0 || + w >= PRODUCT - 1 /* writing last sample */)) { printf( "%sremaining samples = %18" PRIuMAX " (%.2Lf%% done)", ANSI_CLEAR, @@ -177,13 +184,18 @@ main(int argc, char** argv) printf("%s", ANSI_CLEAR); debug_print(); + // * wave file output + // 0. log printf("\nWriting out file output.wav...\n"); fflush(stdout); + + // 1. open file FILE* r = fopen("output.wav", "wb"); if (r == NULL || !r) return 1; + // 2. write headers and data fwrite("RIFF", 1, 4, r); uint32_t t = PRODUCT; fwrite(&t, sizeof(t), 1, r); @@ -210,9 +222,11 @@ main(int argc, char** argv) fclose(r); debug_print(); - free(q); - + // * end of program printf("Done!\n"); + // * free allocated heap + free(buffer); + return 0; }