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