1
0

template.c: improve silent mode

src/template.c:
1. Improve silent mode (the program doesn't output anything to STDOUT in
   silent mode now)
2. Use conditional compilation for silent mode
This commit is contained in:
Intel A80486DX2-66 2023-11-25 20:50:25 +03:00
parent 3acc0be646
commit 976b7468cc
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -68,36 +68,38 @@ int
main(void) main(void)
{ {
// * log -> welcome // * log -> welcome
#if !SILENT_MODE
printf(":: C bytebeat generator runtime unit\n"); printf(":: C bytebeat generator runtime unit\n");
fflush(stdout); fflush(stdout);
#endif
if (!SILENT_MODE) { #if !SILENT_MODE
printf(
"\n"
"Sample rate: %d Hz\n"
"Channels: %d%s\n"
"Bit depth: %ssigned %d-bit\n"
"Duration: ",
SAMPLE_RATE,
CHANNELS,
CHANNELS == 1 ? " (mono)" : (CHANNELS > 2 ? "" : " (stereo)"),
IS_SIGNED ? "" : "un",
BIT_DEPTH);
if (SECONDS >= 3600)
printf( printf(
"\n" "%d:%02d:%02d",
"Sample rate: %d Hz\n" SECONDS / 3600,
"Channels: %d%s\n" (SECONDS / 60) % 60,
"Bit depth: %ssigned %d-bit\n" SECONDS % 60);
"Duration: ", else if (SECONDS >= 60)
SAMPLE_RATE, printf("%d:%02d", SECONDS / 60, SECONDS % 60);
CHANNELS, else
CHANNELS == 1 ? " (mono)" : (CHANNELS > 2 ? "" : " (stereo)"), printf("%d seconds", SECONDS);
IS_SIGNED ? "" : "un",
BIT_DEPTH);
if (SECONDS >= 3600) printf("\n\n");
printf( fflush(stdout);
"%d:%02d:%02d", #endif
SECONDS / 3600,
(SECONDS / 60) % 60,
SECONDS % 60);
else if (SECONDS >= 60)
printf("%d:%02d", SECONDS / 60, SECONDS % 60);
else
printf("%d seconds", SECONDS);
printf("\n\n");
fflush(stdout);
}
// * allocate heap for sample data // * allocate heap for sample data
SAMPLE_TYPE* buffer = calloc(PRODUCT, sizeof(SAMPLE_TYPE)); SAMPLE_TYPE* buffer = calloc(PRODUCT, sizeof(SAMPLE_TYPE));
@ -130,10 +132,9 @@ main(void)
buffer[w] = sample_res; buffer[w] = sample_res;
// 6. log // 6. log
if ( #if !SILENT_MODE
!SILENT_MODE && if (w % FREQUENCY_OF_STATUS_REPORTING == 0 ||
(w % FREQUENCY_OF_STATUS_REPORTING == 0 || w >= PRODUCT - 1 /* writing last sample */) {
w >= PRODUCT - 1 /* writing last sample */)) {
printf( printf(
"%sremaining samples = %18" PRIuMAX " (%.2Lf%% done)", "%sremaining samples = %18" PRIuMAX " (%.2Lf%% done)",
ANSI_CLEAR, ANSI_CLEAR,
@ -141,14 +142,17 @@ main(void)
(long double)w * 100 / (long double)PRODUCT); (long double)w * 100 / (long double)PRODUCT);
fflush(stdout); fflush(stdout);
} }
#endif
} }
printf("%s", ANSI_CLEAR); printf("%s", ANSI_CLEAR);
// * wave file output // * wave file output
// 0. log // 0. log
#if !SILENT_MODE
printf("\nWriting out file output.wav...\n"); printf("\nWriting out file output.wav...\n");
fflush(stdout); fflush(stdout);
#endif
// 1. open file // 1. open file
FILE* output_file = fopen("output.wav", "wb"); FILE* output_file = fopen("output.wav", "wb");
@ -197,7 +201,9 @@ main(void)
fclose(output_file); fclose(output_file);
// * end of program // * end of program
#if !SILENT_MODE
printf("Done!\n"); printf("Done!\n");
#endif
// * free allocated heap // * free allocated heap
free(buffer); free(buffer);