template.c: refactor code and change initial type of w
This commit is contained in:
parent
de7e641abb
commit
adad4dcb24
@ -3,5 +3,5 @@
|
|||||||
## Bytebeat code
|
## Bytebeat code
|
||||||
|
|
||||||
**Variables:**
|
**Variables:**
|
||||||
- `w`: `long double`
|
- `w`: a `size_t` variable casted as `long double`
|
||||||
- `t`: `w` casted as `uintmax_t`
|
- `t`: `w` casted as `uintmax_t`
|
||||||
|
@ -127,30 +127,37 @@ main(int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// * allocate heap for sample data
|
// * 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;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
debug_print();
|
debug_print();
|
||||||
|
|
||||||
// * bytebeat generating loop
|
// * bytebeat generating loop
|
||||||
for (uintmax_t w = 0; w < PRODUCT; w++) {
|
for (size_t w = 0; w < PRODUCT; w++) {
|
||||||
// 1. generate audio data
|
// 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
|
// 2. if signed, then wrap up into unsigned
|
||||||
if (IS_SIGNED)
|
if (IS_SIGNED)
|
||||||
q[w] = (SAMPLE_TYPE)signed_to_unsigned(res) & BIT_DEPTH_LIMITER;
|
bytebeat_res = signed_to_unsigned(bytebeat_res);
|
||||||
else
|
|
||||||
q[w] = (SAMPLE_TYPE)res & BIT_DEPTH_LIMITER;
|
// 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
|
// 4. if bit depth is less than 8, stretch it
|
||||||
#if BIT_DEPTH < 8
|
#if BIT_DEPTH < 8
|
||||||
q[(size_t)w] = (SAMPLE_TYPE)((long double)q[(size_t)w] *
|
sample_res = (SAMPLE_TYPE)
|
||||||
((long double)BIT_DEPTH / 8.0));
|
((long double)buffer[w] * ((long double)BIT_DEPTH / 8.L));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// 5. save sample into buffer
|
||||||
|
buffer[w] = sample_res;
|
||||||
|
|
||||||
// 6. log
|
// 6. log
|
||||||
if (
|
if (
|
||||||
!silent_mode &&
|
!silent_mode &&
|
||||||
@ -174,36 +181,50 @@ main(int argc, char** argv)
|
|||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
||||||
// 1. open file
|
// 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;
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// 2. write headers and data
|
// 2. prepare variables
|
||||||
fwrite("RIFF", 1, 4, r);
|
const uint32_t buffer_size = PRODUCT,
|
||||||
uint32_t t = PRODUCT;
|
file_length =
|
||||||
fwrite(&t, sizeof(t), 1, r);
|
4 * 4 /* 4 strings of 4 characters */ +
|
||||||
fwrite("WAVE", 1, 4, r);
|
4 * 4 /* 4 uint32_t values */ +
|
||||||
fwrite("fmt ", 1, 4, r);
|
5 * 2 /* 5 uint16_t values */ +
|
||||||
uint32_t y = 16; // what? 16-bit depth? or [something else]?
|
PRODUCT /* sample data */,
|
||||||
fwrite(&y, sizeof(y), 1, r);
|
fmt_data_length = 16 /* length of format data before this value
|
||||||
uint16_t u = 1; // 1 what? 1 = unsigned?
|
in the file format structure */,
|
||||||
fwrite(&u, sizeof(u), 1, r);
|
sample_rate = SAMPLE_RATE;
|
||||||
uint16_t i = CHANNELS;
|
const uint16_t fmt_type = 1, // format type is PCM
|
||||||
fwrite(&i, sizeof(i), 1, r);
|
channels = CHANNELS,
|
||||||
uint32_t o = SAMPLE_RATE;
|
byte_rate = (SAMPLE_RATE * BIT_DEPTH * CHANNELS) / 8,
|
||||||
fwrite(&o, sizeof(o), 1, r);
|
block_align = (BIT_DEPTH * CHANNELS) / 8,
|
||||||
uint32_t s = SAMPLE_RATE * i;
|
bit_depth = BIT_DEPTH > 8 ? BIT_DEPTH : 8;
|
||||||
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;
|
|
||||||
|
|
||||||
fwrite(&a, sizeof(a), 1, r);
|
// 3. write headers
|
||||||
fwrite("data", 1, 4, r);
|
// <L = Little-endian or B = Big-endian> : <name> : <bytes of field>
|
||||||
fwrite(&t, sizeof(t), 1, r);
|
fwrite("RIFF", 1, 4, output_file); // B : ChunkID : 4
|
||||||
fwrite(q, sizeof(uint8_t), PRODUCT, r);
|
fwrite(&file_length, 4, 1, output_file); // L : ChunkSize : 4
|
||||||
fclose(r);
|
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();
|
debug_print();
|
||||||
|
|
||||||
// * end of program
|
// * end of program
|
||||||
|
Loading…
Reference in New Issue
Block a user