1
0

template.c: reformat and add more comments

This commit is contained in:
Intel A80486DX2-66 2023-11-18 19:57:57 +03:00
parent 5e4fee2a87
commit 8bc7c712b7
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 27 additions and 25 deletions

View File

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

View File

@ -24,20 +24,20 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING;
#define SECONDS `seconds` #define SECONDS `seconds`
#if BIT_DEPTH <= 8 #if BIT_DEPTH <= 8
#define SAMPLE_TYPE uint8_t # define SAMPLE_TYPE uint8_t
#elif BIT_DEPTH >= 16 #elif BIT_DEPTH >= 16
#if IS_SIGNED # if IS_SIGNED
#define SAMPLE_TYPE int16_t # define SAMPLE_TYPE int16_t
#else # else
#define SAMPLE_TYPE uint16_t # define SAMPLE_TYPE uint16_t
#endif # endif
#endif #endif
#define PRODUCT (SAMPLE_RATE * SECONDS * CHANNELS) #define PRODUCT (SAMPLE_RATE * SECONDS * CHANNELS)
#define FREQUENCY_OF_STATUS_REPORTING 5000 #define FREQUENCY_OF_STATUS_REPORTING 5000
#define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1) #define BIT_DEPTH_LIMITER ((1 << BIT_DEPTH) - 1)
#define PCM_COEFFICIENT ((1 << (BIT_DEPTH - 1)) - 1) #define PCM_COEFFICIENT ((1 << (BIT_DEPTH - 1)) - 1)
#define unsigned_to_signed(x) (x - PCM_COEFFICIENT) #define unsigned_to_signed(x) (x - PCM_COEFFICIENT)
#define signed_to_unsigned(x) (x + PCM_COEFFICIENT) #define signed_to_unsigned(x) (x + PCM_COEFFICIENT)
@ -97,20 +97,20 @@ bytebeat(long double w)
{ {
`final_sample_rate_code` `final_sample_rate_code`
uintmax_t t = (uintmax_t)w; uintmax_t t = (uintmax_t)w;
`bytebeat_contents` `bytebeat_contents`
; ;
} }
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
// * parse CLI arguments
silent_mode = argc > 1 && argv[1] != NULL && silent_mode = argc > 1 && argv[1] != NULL &&
(!strcmp(argv[1], "-s") || !strcmp(argv[1], "--silent")); (!strcmp(argv[1], "-s") || !strcmp(argv[1], "--silent"));
debug_mode = argc > 1 && argv[1] != NULL && debug_mode = argc > 1 && argv[1] != NULL &&
(!strcmp(argv[1], "-d") || !strcmp(argv[1], "--debug")); (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--debug"));
// * log -> welcome
printf(":: C bytebeat generator runtime unit\n"); printf(":: C bytebeat generator runtime unit\n");
fflush(stdout); fflush(stdout);
@ -142,6 +142,7 @@ main(int argc, char** argv)
fflush(stdout); fflush(stdout);
} }
// * allocate heap for sample data
SAMPLE_TYPE* q = calloc(PRODUCT, sizeof(SAMPLE_TYPE)); SAMPLE_TYPE* q = calloc(PRODUCT, sizeof(SAMPLE_TYPE));
if (q == NULL) if (q == NULL)
@ -149,22 +150,28 @@ main(int argc, char** argv)
debug_print(); debug_print();
// * bytebeat generating loop
for (uintmax_t w = 0; w < PRODUCT; w++) { for (uintmax_t w = 0; w < PRODUCT; w++) {
// 1. generate audio data
long double res = bytebeat((long double)w); long double res = bytebeat((long double)w);
// 2. if signed, then wrap up into unsigned
if (IS_SIGNED) if (IS_SIGNED)
q[w] = (SAMPLE_TYPE)signed_to_unsigned(res) & BIT_DEPTH_LIMITER; q[w] = (SAMPLE_TYPE)signed_to_unsigned(res) & BIT_DEPTH_LIMITER;
else else
q[w] = (SAMPLE_TYPE)res & BIT_DEPTH_LIMITER; q[w] = (SAMPLE_TYPE)res & BIT_DEPTH_LIMITER;
// 4. if bit depth is less than 8, stretch it
#if BIT_DEPTH < 8 #if BIT_DEPTH < 8
q[(size_t)w] = (SAMPLE_TYPE)((long double)q[(size_t)w] * q[(size_t)w] = (SAMPLE_TYPE)((long double)q[(size_t)w] *
((long double)BIT_DEPTH / 8.0)); ((long double)BIT_DEPTH / 8.0));
#endif #endif
// 6. log
if ( if (
!silent_mode && !silent_mode &&
(w % FREQUENCY_OF_STATUS_REPORTING == 0 || w >= PRODUCT - 1)) { (w % FREQUENCY_OF_STATUS_REPORTING == 0 ||
w >= PRODUCT - 1 /* writing last sample */)) {
printf( printf(
"%sremaining samples = %18" PRIuMAX " (%.2Lf%% done)", "%sremaining samples = %18" PRIuMAX " (%.2Lf%% done)",
ANSI_CLEAR, ANSI_CLEAR,
@ -177,13 +184,18 @@ main(int argc, char** argv)
printf("%s", ANSI_CLEAR); printf("%s", ANSI_CLEAR);
debug_print(); debug_print();
// * wave file output
// 0. log
printf("\nWriting out file output.wav...\n"); printf("\nWriting out file output.wav...\n");
fflush(stdout); fflush(stdout);
// 1. open file
FILE* r = fopen("output.wav", "wb"); FILE* r = fopen("output.wav", "wb");
if (r == NULL || !r) if (r == NULL || !r)
return 1; return 1;
// 2. write headers and data
fwrite("RIFF", 1, 4, r); fwrite("RIFF", 1, 4, r);
uint32_t t = PRODUCT; uint32_t t = PRODUCT;
fwrite(&t, sizeof(t), 1, r); fwrite(&t, sizeof(t), 1, r);
@ -210,9 +222,11 @@ main(int argc, char** argv)
fclose(r); fclose(r);
debug_print(); debug_print();
free(q); // * end of program
printf("Done!\n"); printf("Done!\n");
// * free allocated heap
free(buffer);
return 0; return 0;
} }