1
0

template.c: refactor code and change initial type of w

This commit is contained in:
Intel A80486DX2-66 2023-11-18 20:05:55 +03:00
parent de7e641abb
commit adad4dcb24
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 57 additions and 36 deletions

View File

@ -3,5 +3,5 @@
## Bytebeat code
**Variables:**
- `w`: `long double`
- `w`: a `size_t` variable casted as `long double`
- `t`: `w` casted as `uintmax_t`

View File

@ -127,30 +127,37 @@ main(int argc, char** argv)
}
// * allocate heap for sample data
SAMPLE_TYPE* q = calloc(PRODUCT, sizeof(SAMPLE_TYPE));
SAMPLE_TYPE* buffer = calloc(PRODUCT, sizeof(SAMPLE_TYPE));
if (q == NULL)
if (buffer == NULL) {
perror("calloc");
return 1;
}
debug_print();
// * bytebeat generating loop
for (uintmax_t w = 0; w < PRODUCT; w++) {
for (size_t w = 0; w < PRODUCT; w++) {
// 1. generate audio data
long double res = bytebeat((long double)w);
long double bytebeat_res = bytebeat((long double)w);
// 2. if signed, then wrap up into unsigned
if (IS_SIGNED)
q[w] = (SAMPLE_TYPE)signed_to_unsigned(res) & BIT_DEPTH_LIMITER;
else
q[w] = (SAMPLE_TYPE)res & BIT_DEPTH_LIMITER;
bytebeat_res = signed_to_unsigned(bytebeat_res);
// 3. convert audio data to sample
SAMPLE_TYPE sample_res = (SAMPLE_TYPE)bytebeat_res &
BIT_DEPTH_LIMITER;
// 4. if bit depth is less than 8, stretch it
#if BIT_DEPTH < 8
q[(size_t)w] = (SAMPLE_TYPE)((long double)q[(size_t)w] *
((long double)BIT_DEPTH / 8.0));
sample_res = (SAMPLE_TYPE)
((long double)buffer[w] * ((long double)BIT_DEPTH / 8.L));
#endif
// 5. save sample into buffer
buffer[w] = sample_res;
// 6. log
if (
!silent_mode &&
@ -174,36 +181,50 @@ main(int argc, char** argv)
fflush(stdout);
// 1. open file
FILE* r = fopen("output.wav", "wb");
FILE* output_file = fopen("output.wav", "wb");
if (r == NULL || !r)
if (output_file == NULL || !output_file) {
perror("fopen");
return 1;
}
// 2. write headers and data
fwrite("RIFF", 1, 4, r);
uint32_t t = PRODUCT;
fwrite(&t, sizeof(t), 1, r);
fwrite("WAVE", 1, 4, r);
fwrite("fmt ", 1, 4, r);
uint32_t y = 16; // what? 16-bit depth? or [something else]?
fwrite(&y, sizeof(y), 1, r);
uint16_t u = 1; // 1 what? 1 = unsigned?
fwrite(&u, sizeof(u), 1, r);
uint16_t i = CHANNELS;
fwrite(&i, sizeof(i), 1, r);
uint32_t o = SAMPLE_RATE;
fwrite(&o, sizeof(o), 1, r);
uint32_t s = SAMPLE_RATE * i;
fwrite(&s, sizeof(s), 1, r);
uint16_t p = i;
fwrite(&p, sizeof(p), 1, r);
uint16_t a = BIT_DEPTH >= 8 ? BIT_DEPTH : 8;
// 2. prepare variables
const uint32_t buffer_size = PRODUCT,
file_length =
4 * 4 /* 4 strings of 4 characters */ +
4 * 4 /* 4 uint32_t values */ +
5 * 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;
const uint16_t fmt_type = 1, // format type is PCM
channels = CHANNELS,
byte_rate = (SAMPLE_RATE * BIT_DEPTH * CHANNELS) / 8,
block_align = (BIT_DEPTH * CHANNELS) / 8,
bit_depth = BIT_DEPTH > 8 ? BIT_DEPTH : 8;
fwrite(&a, sizeof(a), 1, r);
fwrite("data", 1, 4, r);
fwrite(&t, sizeof(t), 1, r);
fwrite(q, sizeof(uint8_t), PRODUCT, r);
fclose(r);
// 3. write headers
// <L = Little-endian or B = Big-endian> : <name> : <bytes of field>
fwrite("RIFF", 1, 4, output_file); // B : ChunkID : 4
fwrite(&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(&fmt_data_length, 4, 1, output_file); // L : Subchunk1Size : 4
fwrite(&fmt_type, 2, 1, output_file); // L : AudioFormat : 2
fwrite(&channels, 2, 1, output_file); // L : NumChannels : 2
fwrite(&sample_rate, 4, 1, output_file); // L : SampleRate : 4
fwrite(&byte_rate, 4, 1, output_file); // L : ByteRate : 4
fwrite(&block_align, 2, 1, output_file); // L : BlockAlign : 2
fwrite(&bit_depth, 2, 1, output_file); // L : BitsPerSample : 2
fwrite("data", 1, 4, output_file); // B : Subchunk2ID : 4
fwrite(&buffer_size, 4, 1, output_file); // B : Subchunk2Size : 4
// 4. write sample data
fwrite(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file);
// 5. close file
fclose(output_file);
debug_print();
// * end of program