template.c: generate bytebeat after opening file
This commit is contained in:
parent
a48567f4fa
commit
564a9c11d8
@ -129,6 +129,55 @@ main(void)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
#endif
|
#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
|
||||||
|
// <L = Little-endian or B = Big-endian> : <name> : <bytes of field>
|
||||||
|
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
|
// * allocate heap for sample data
|
||||||
SAMPLE_TYPE* buffer = calloc(PRODUCT, sizeof(SAMPLE_TYPE));
|
SAMPLE_TYPE* buffer = calloc(PRODUCT, sizeof(SAMPLE_TYPE));
|
||||||
|
|
||||||
@ -193,55 +242,7 @@ main(void)
|
|||||||
printf("%s", ANSI_CLEAR);
|
printf("%s", ANSI_CLEAR);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// * wave file output
|
// * save the sample data into the file
|
||||||
// 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
|
|
||||||
// <L = Little-endian or B = Big-endian> : <name> : <bytes of field>
|
|
||||||
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
|
|
||||||
fwrite_le(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file);
|
fwrite_le(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file);
|
||||||
|
|
||||||
// 5. close file
|
// 5. close file
|
||||||
|
Loading…
Reference in New Issue
Block a user