From 564a9c11d825f4d3073899998e17f02b04212824 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Tue, 9 Jan 2024 19:52:15 +0300 Subject: [PATCH] template.c: generate bytebeat after opening file --- src/template.c | 99 +++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/src/template.c b/src/template.c index 3b0c679..72906fb 100644 --- a/src/template.c +++ b/src/template.c @@ -129,6 +129,55 @@ main(void) fflush(stdout); #endif + // * write WAVE headers + // 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"); + if (output_file == NULL || !output_file) { + perror("fopen"); + return 1; + } + + // 2. prepare variables + uint32_t buffer_size = PRODUCT, + file_length = + 4 * 4 /* 4 strings of 4 characters */ + + 5 * 4 /* 4 uint32_t values */ + + 4 * 2 /* 5 uint16_t values */ + + PRODUCT /* sample data */, + fmt_data_length = 16 /* + * length of format data before this value + * in the file format structure + */, + sample_rate = SAMPLE_RATE, + byte_rate = (SAMPLE_RATE * BIT_DEPTH * CHANNELS) / 8; + uint16_t fmt_type = 1, // format type is PCM + channels = CHANNELS, + block_align = (BIT_DEPTH * CHANNELS) / 8, + bit_depth = BIT_DEPTH > 8 ? BIT_DEPTH : 8; + + // 3. write headers + // : : + fwrite ("RIFF", 1, 4, output_file); // B : ChunkID : 4 + fwrite_le(&file_length, 4, 1, output_file); // L : ChunkSize : 4 + fwrite ("WAVE", 1, 4, output_file); // B : Format : 4 + fwrite ("fmt ", 1, 4, output_file); // B : Subchunk1ID : 4 + fwrite_le(&fmt_data_length, 4, 1, output_file); // L : Subchunk1Size : 4 + fwrite_le(&fmt_type, 2, 1, output_file); // L : AudioFormat : 2 + fwrite_le(&channels, 2, 1, output_file); // L : NumChannels : 2 + fwrite_le(&sample_rate, 4, 1, output_file); // L : SampleRate : 4 + fwrite_le(&byte_rate, 4, 1, output_file); // L : ByteRate : 4 + fwrite_le(&block_align, 2, 1, output_file); // L : BlockAlign : 2 + 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 + + // 4. write sample data // * allocate heap for sample data SAMPLE_TYPE* buffer = calloc(PRODUCT, sizeof(SAMPLE_TYPE)); @@ -193,55 +242,7 @@ main(void) printf("%s", ANSI_CLEAR); #endif - // * 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"); - if (output_file == NULL || !output_file) { - perror("fopen"); - return 1; - } - - // 2. prepare variables - uint32_t buffer_size = PRODUCT, - file_length = - 4 * 4 /* 4 strings of 4 characters */ + - 5 * 4 /* 4 uint32_t values */ + - 4 * 2 /* 5 uint16_t values */ + - PRODUCT /* sample data */, - fmt_data_length = 16 /* - * length of format data before this value - * in the file format structure - */, - sample_rate = SAMPLE_RATE, - byte_rate = (SAMPLE_RATE * BIT_DEPTH * CHANNELS) / 8; - uint16_t fmt_type = 1, // format type is PCM - channels = CHANNELS, - block_align = (BIT_DEPTH * CHANNELS) / 8, - bit_depth = BIT_DEPTH > 8 ? BIT_DEPTH : 8; - - // 3. write headers - // : : - fwrite ("RIFF", 1, 4, output_file); // B : ChunkID : 4 - fwrite_le(&file_length, 4, 1, output_file); // L : ChunkSize : 4 - fwrite ("WAVE", 1, 4, output_file); // B : Format : 4 - fwrite ("fmt ", 1, 4, output_file); // B : Subchunk1ID : 4 - fwrite_le(&fmt_data_length, 4, 1, output_file); // L : Subchunk1Size : 4 - fwrite_le(&fmt_type, 2, 1, output_file); // L : AudioFormat : 2 - fwrite_le(&channels, 2, 1, output_file); // L : NumChannels : 2 - fwrite_le(&sample_rate, 4, 1, output_file); // L : SampleRate : 4 - fwrite_le(&byte_rate, 4, 1, output_file); // L : ByteRate : 4 - fwrite_le(&block_align, 2, 1, output_file); // L : BlockAlign : 2 - 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 - - // 4. write sample data + // * save the sample data into the file fwrite_le(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file); // 5. close file