From 110b065459bb236ffa1113db28f5288da0b8c02b Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Sun, 19 May 2024 12:02:14 +0300 Subject: [PATCH] template.c: stringize integers where applicable --- src/template.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/template.c b/src/template.c index 7aedc0b..040c18d 100644 --- a/src/template.c +++ b/src/template.c @@ -44,6 +44,8 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING; #define SEQUENTIAL_MODE `sequential_mode` #define BLOCK_SIZE_BYTES `block_size` +#define MINIMUM_BLOCK_SIZE ((sizeof(SAMPLE_TYPE) + sizeof(uint8_t) - 1) / \ + sizeof(uint8_t)) #define FP_RETURN_TYPE `fp_return_type` #define PRECALCULATED_RATIO `precalculated_ratio` @@ -53,6 +55,9 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING; #define unsigned_to_signed(x) (x - PCM_COEFFICIENT) #define signed_to_unsigned(x) (x + PCM_COEFFICIENT) +#define STRINGIZE(x) #x +#define INT2STR(x) STRINGIZE(x) + // macros #define ALLOCATE_MEMORY(nmemb) \ SAMPLE_TYPE* buffer = malloc( \ @@ -112,15 +117,24 @@ main(void) 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); + "Sample rate: " INT2STR(SAMPLE_RATE) " Hz\n" + "Channels: " INT2STR(CHANNELS) +#if CHANNELS <= 2 + " (" +# if CHANNELS == 2 + "stereo" +# else + "mono" +# endif + ")" +#endif + "\n" + "Bit depth: " +#if !IS_SIGNED + "un" +#endif + "signed " INT2STR(BIT_DEPTH) "-bit\n" + "Duration: "); if (seconds > 0) if (seconds >= 3600) @@ -204,10 +218,8 @@ main(void) size_t BLOCK_SIZE = BLOCK_SIZE_BYTES / (sizeof(SAMPLE_TYPE) / sizeof(uint8_t)); if (BLOCK_SIZE < 1) { - printf("The block size %" PRIuMAX " is too small, should be at least " - "%" PRIuMAX " bytes\n", (uintmax_t) BLOCK_SIZE_BYTES, - (uintmax_t) ((sizeof(SAMPLE_TYPE) + sizeof(uint8_t) - 1) / - sizeof(uint8_t))); + printf("The block size " INT2STR(BLOCK_SIZE_BYTES) " is too small, " + "should be at least %" PRIuMAX " bytes\n", MINIMUM_BLOCK_SIZE); return EXIT_FAILURE; }