template.c: support stdout output
This commit is contained in:
parent
35fe39b0e0
commit
0d65942d81
@ -27,6 +27,7 @@ typedef long double bb_fp_return_t;
|
||||
#endif
|
||||
|
||||
#define OUTPUT_FILE `output_file`
|
||||
#define STDOUT_OUTPUT (OUTPUT_FILE == "-")
|
||||
|
||||
#define SAMPLE_RATE `sample_rate`
|
||||
#define ORIGINAL_SAMPLE_RATE `original_sample_rate`
|
||||
@ -86,6 +87,16 @@ typedef long double bb_fp_return_t;
|
||||
#define STRINGIZE(x) #x
|
||||
#define INT2STR(x) STRINGIZE(x)
|
||||
|
||||
#if STDOUT_OUTPUT
|
||||
# define smart_printf(...) fprintf(stderr, __VA_ARGS__)
|
||||
# define smart_puts(s) fprintf(stderr, s "\n")
|
||||
# define smart_fflush fflush(stderr)
|
||||
#else
|
||||
# define smart_printf printf
|
||||
# define smart_puts puts
|
||||
# define smart_fflush fflush(stdout)
|
||||
#endif
|
||||
|
||||
// macros
|
||||
#define ALLOCATE_MEMORY(nmemb) \
|
||||
SAMPLE_TYPE* buffer = malloc( \
|
||||
@ -137,21 +148,22 @@ print_time(uintmax_t length) {
|
||||
|
||||
if (seconds > 0)
|
||||
if (seconds >= 3600)
|
||||
printf(
|
||||
smart_printf(
|
||||
"%" PRIuMAX ":%02" PRIuMAX ":%02" PRIuMAX,
|
||||
seconds / 3600,
|
||||
(seconds / 60) % 60,
|
||||
seconds % 60);
|
||||
else if (seconds >= 60)
|
||||
printf("%" PRIuMAX ":%02" PRIuMAX, seconds / 60, seconds % 60);
|
||||
smart_printf("%" PRIuMAX ":%02" PRIuMAX, seconds / 60,
|
||||
seconds % 60);
|
||||
else
|
||||
printf("%" PRIuMAX " seconds", seconds);
|
||||
smart_printf("%" PRIuMAX " seconds", seconds);
|
||||
|
||||
if (seconds > 0 && samples > 0)
|
||||
printf(" + ");
|
||||
smart_printf(" + ");
|
||||
|
||||
if (samples > 0)
|
||||
printf("%" PRIuMAX " samples", samples);
|
||||
smart_printf("%" PRIuMAX " samples", samples);
|
||||
}
|
||||
|
||||
int
|
||||
@ -159,13 +171,10 @@ main(void)
|
||||
{
|
||||
// * log -> welcome
|
||||
#if !SILENT_MODE
|
||||
puts(":: C bytebeat generator: runtime unit");
|
||||
fflush(stdout);
|
||||
smart_puts(":: C bytebeat generator: runtime unit");
|
||||
smart_fflush();
|
||||
|
||||
const uintmax_t seconds = LENGTH / SAMPLE_RATE,
|
||||
samples = LENGTH % SAMPLE_RATE;
|
||||
|
||||
printf(
|
||||
smart_printf(
|
||||
"\n"
|
||||
"Sample rate: " INT2STR(SAMPLE_RATE) " Hz\n"
|
||||
"Channels: " INT2STR(CHANNELS)
|
||||
@ -193,28 +202,29 @@ main(void)
|
||||
print_time(RUNNING_LENGTH);
|
||||
|
||||
#if REPEAT_TIMES > 0
|
||||
printf(", %llu times -> ", REPEAT_TIMES + 1);
|
||||
smart_printf(", %llu times -> ", REPEAT_TIMES + 1);
|
||||
print_time(RUNNING_LENGTH * (REPEAT_TIMES + 1));
|
||||
#endif
|
||||
|
||||
#if INITIAL_TIME != 0
|
||||
printf("\nStart time: ");
|
||||
smart_printf("\nStart time: ");
|
||||
print_time(INITIAL_TIME);
|
||||
#endif
|
||||
|
||||
#if VERBOSE_MODE || SEQUENTIAL_MODE
|
||||
puts("\n");
|
||||
smart_puts("\n");
|
||||
#endif
|
||||
|
||||
fflush(stdout);
|
||||
smart_fflush();
|
||||
#endif
|
||||
|
||||
// * write WAVE headers
|
||||
// 0. log
|
||||
#if !SILENT_MODE && VERBOSE_MODE
|
||||
puts("Writing WAVE headers...");
|
||||
#if VERBOSE_MODE
|
||||
smart_puts("Writing WAVE headers...");
|
||||
#endif
|
||||
|
||||
#if !STDOUT_OUTPUT
|
||||
// 1. open file
|
||||
FILE* output_file = fopen(OUTPUT_FILE,
|
||||
"wb"
|
||||
@ -223,10 +233,11 @@ main(void)
|
||||
# endif
|
||||
);
|
||||
if (output_file == NULL) {
|
||||
fflush(stdout);
|
||||
smart_fflush();
|
||||
perror("fopen");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#endif
|
||||
|
||||
// 2. prepare variables
|
||||
const uint32_t header_size =
|
||||
@ -253,6 +264,7 @@ main(void)
|
||||
block_align = (ACTUAL_BIT_DEPTH * CHANNELS) / 8,
|
||||
bit_depth = ACTUAL_BIT_DEPTH;
|
||||
|
||||
#if !STDOUT_OUTPUT
|
||||
// 3. write headers
|
||||
// <L = Little-endian or B = Big-endian> : <name> : <bytes of field>
|
||||
fwrite ("RIFF", 1, 4, output_file); // B : ChunkID : 4
|
||||
@ -268,6 +280,7 @@ main(void)
|
||||
fwrite_le(&bit_depth, 2, 1, output_file); // L : BitsPerSample : 2
|
||||
fwrite ("data", 1, 4, output_file); // B : Subchunk2ID : 4
|
||||
fwrite_le(&buffer_size, 4, 1, output_file); // L : Subchunk2Size : 4
|
||||
#endif
|
||||
|
||||
// 4. write sample data
|
||||
#if SEQUENTIAL_MODE
|
||||
@ -360,10 +373,10 @@ main(void)
|
||||
] = sample_res;
|
||||
|
||||
// 6. log
|
||||
#if VERBOSE_MODE
|
||||
#if VERBOSE_MODE && !STDOUT_OUTPUT
|
||||
if (time % FREQUENCY_OF_STATUS_REPORTING == 0 ||
|
||||
time >= LOOP_END_MINUS_1 /* or if writing last sample */) {
|
||||
printf(
|
||||
smart_printf(
|
||||
"%sremaining samples = %18" PRIuMAX " (%6.2Lf%% done)"
|
||||
#if SEQUENTIAL_MODE
|
||||
" (part %" PRIuMAX "/%" PRIuMAX ")"
|
||||
@ -376,26 +389,26 @@ main(void)
|
||||
, (uintmax_t) seq + 1, (uintmax_t) MAX
|
||||
#endif
|
||||
);
|
||||
fflush(stdout);
|
||||
smart_fflush();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !SILENT_MODE
|
||||
printf(_ANSI_CLEAR_STRING);
|
||||
smart_printf(_ANSI_CLEAR_STRING);
|
||||
|
||||
// 5. log
|
||||
#if !(SEQUENTIAL_MODE && VERBOSE_MODE)
|
||||
# if SEQUENTIAL_MODE
|
||||
if (seq == 0)
|
||||
# endif
|
||||
puts(
|
||||
smart_puts(
|
||||
# if !SEQUENTIAL_MODE
|
||||
"\n"
|
||||
# endif
|
||||
"Writing out file " OUTPUT_FILE "...");
|
||||
#endif
|
||||
fflush(stdout);
|
||||
smart_fflush();
|
||||
#endif
|
||||
|
||||
// * save the sample data into the file
|
||||
@ -405,9 +418,20 @@ main(void)
|
||||
#else
|
||||
GEN_LENGTH,
|
||||
#endif
|
||||
output_file);
|
||||
#if STDOUT_OUTPUT
|
||||
stdout
|
||||
#else
|
||||
output_file
|
||||
#endif
|
||||
);
|
||||
#if SEQUENTIAL_MODE
|
||||
fflush(output_file);
|
||||
fflush(
|
||||
# if STDOUT_OUTPUT
|
||||
stdout
|
||||
# else
|
||||
output_file
|
||||
# endif
|
||||
);
|
||||
#endif
|
||||
|
||||
#if SEQUENTIAL_MODE
|
||||
@ -415,6 +439,7 @@ main(void)
|
||||
#endif
|
||||
|
||||
#if REPEAT_TIMES > 0
|
||||
# if !(STDOUT_OUTPUT && !SEQUENTIAL_MODE)
|
||||
// * repeat as much as needed
|
||||
|
||||
# if !SILENT_MODE
|
||||
@ -451,13 +476,18 @@ main(void)
|
||||
fwrite_le(buffer, sizeof(SAMPLE_TYPE), GEN_LENGTH, output_file);
|
||||
# endif
|
||||
}
|
||||
# else
|
||||
fprintf(stderr, "Repeating is not supported in STDOUT, sequential mode\n");
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// * free allocated heap
|
||||
free(buffer);
|
||||
|
||||
#if !STDOUT_OUTPUT
|
||||
// 6. close file
|
||||
fclose(output_file);
|
||||
#endif
|
||||
|
||||
// * end of program
|
||||
#if !SILENT_MODE
|
||||
|
Loading…
Reference in New Issue
Block a user