1
0

template.c: add sequential mode and set as default

This commit is contained in:
Intel A80486DX2-66 2024-01-10 04:07:15 +03:00
parent 67ab00c084
commit d1906f0dc1
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 154 additions and 70 deletions

View File

@ -124,6 +124,11 @@ if __name__ == "__main__":
"default = +0 samples") "default = +0 samples")
parser.add_argument("-a", "--no-return", default=False, action="store_true", parser.add_argument("-a", "--no-return", default=False, action="store_true",
help="do not insert return statement before the code") help="do not insert return statement before the code")
parser.add_argument("-u", "--mode", default="sequential", type=str,
help="mode of writing: `sequential` or `instant` (the latter is not "
"recommended, since the whole result would be stored in RAM)")
parser.add_argument("-n", "--block-size", default=65536, type=int,
help="sequential mode only: block size of each sequence, bytes")
parser.add_argument("-q", "--silent", default=False, action="store_true", parser.add_argument("-q", "--silent", default=False, action="store_true",
help="do not output anything during generation") help="do not output anything during generation")
parser.add_argument("-v", "--verbose", default=False, action="store_true", parser.add_argument("-v", "--verbose", default=False, action="store_true",
@ -186,6 +191,9 @@ if __name__ == "__main__":
print("CLI: Count of samples should be greater than zero.") print("CLI: Count of samples should be greater than zero.")
raise SystemExit raise SystemExit
if args.mode != "sequential" and args.mode != "instant":
print("Invalid mode '%s'" % args.mode)
rewrite_file(PATHS["substitute"], substitute_vars({ rewrite_file(PATHS["substitute"], substitute_vars({
"bytebeat_contents": bytebeat_contents, "bytebeat_contents": bytebeat_contents,
"output_file": C_str_repr(args.output), "output_file": C_str_repr(args.output),
@ -199,6 +207,8 @@ if __name__ == "__main__":
"fp_return_type": args.floating_point, "fp_return_type": args.floating_point,
"channels": args.channels, "channels": args.channels,
"length": samples, "length": samples,
"sequential_mode": args.mode == "sequential",
"block_size": args.block_size,
"silent_mode": args.silent, "silent_mode": args.silent,
"verbose_mode": args.verbose and not args.silent, "verbose_mode": args.verbose and not args.silent,
"fwrite_le": "../" + PATHS["fwrite_le_header"] "fwrite_le": "../" + PATHS["fwrite_le_header"]

View File

@ -41,6 +41,9 @@ const char* ANSI_CLEAR = __ANSI_CLEAR_STRING;
#define PRODUCT (LENGTH * CHANNELS) #define PRODUCT (LENGTH * CHANNELS)
#define FREQUENCY_OF_STATUS_REPORTING 5000 #define FREQUENCY_OF_STATUS_REPORTING 5000
#define SEQUENTIAL_MODE `sequential_mode`
#define BLOCK_SIZE_BYTES `block_size`
#define FP_RETURN_TYPE `fp_return_type` #define FP_RETURN_TYPE `fp_return_type`
#define PRECALCULATED_RATIO `precalculated_ratio` #define PRECALCULATED_RATIO `precalculated_ratio`
@ -71,8 +74,12 @@ bytebeat(long double w)
{ {
#if PRECALCULATED_RATIO #if PRECALCULATED_RATIO
`final_sample_rate_code` `final_sample_rate_code`
#else #elif ORIGINAL_SAMPLE_RATE != SAMPLE_RATE
# if SAMPLE_RATE > ORIGINAL_SAMPLE_RATE
w /= ((long double) SAMPLE_RATE) / ((long double) ORIGINAL_SAMPLE_RATE); w /= ((long double) SAMPLE_RATE) / ((long double) ORIGINAL_SAMPLE_RATE);
# else
w *= ((long double) ORIGINAL_SAMPLE_RATE / ((long double) SAMPLE_RATE));
# endif
#endif #endif
uintmax_t t = (uintmax_t) w; uintmax_t t = (uintmax_t) w;
`bytebeat_contents`; `bytebeat_contents`;
@ -122,7 +129,7 @@ main(void)
printf("%" PRIuMAX " samples", samples); printf("%" PRIuMAX " samples", samples);
printf( printf(
#if VERBOSE_MODE #if VERBOSE_MODE || SEQUENTIAL_MODE
"\n" "\n"
#endif #endif
"\n" "\n"
@ -132,6 +139,11 @@ main(void)
#endif #endif
// * write WAVE headers // * write WAVE headers
// 0. log
#if SEQUENTIAL_MODE
printf("Writing WAVE headers...\n");
#endif
// 1. open file // 1. open file
FILE* output_file = fopen(OUTPUT_FILE, "wb"); FILE* output_file = fopen(OUTPUT_FILE, "wb");
if (output_file == NULL) { if (output_file == NULL) {
@ -146,7 +158,7 @@ main(void)
5 * 4 /* 4 uint32_t values */ + 5 * 4 /* 4 uint32_t values */ +
4 * 2 /* 5 uint16_t values */ + 4 * 2 /* 5 uint16_t values */ +
PRODUCT /* sample data */, PRODUCT /* sample data */,
fmt_data_length = 16 /* fmt_data_length = 16 /* <--
* length of format data before this value * length of format data before this value
* in the file format structure * in the file format structure
*/, */,
@ -174,8 +186,34 @@ main(void)
fwrite_le(&buffer_size, 4, 1, output_file); // L : Subchunk2Size : 4 fwrite_le(&buffer_size, 4, 1, output_file); // L : Subchunk2Size : 4
// 4. write sample data // 4. write sample data
#if SEQUENTIAL_MODE
const 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)));
exit(EXIT_FAILURE);
}
const size_t MAX = (PRODUCT + (BLOCK_SIZE - 1)) / BLOCK_SIZE;
size_t w = 0;
for (size_t seq = 0; seq < MAX; seq++) {
#else
do {
#endif
size_t calc_block_size = BLOCK_SIZE;
if ((w + BLOCK_SIZE) >= PRODUCT)
calc_block_size = PRODUCT - w;
// * allocate heap for sample data // * allocate heap for sample data
SAMPLE_TYPE* buffer = calloc(PRODUCT, sizeof(SAMPLE_TYPE)); SAMPLE_TYPE* buffer = calloc(
#if SEQUENTIAL_MODE
calc_block_size,
#else
PRODUCT,
#endif
sizeof(SAMPLE_TYPE));
if (buffer == NULL) { if (buffer == NULL) {
perror("calloc"); perror("calloc");
@ -194,7 +232,12 @@ main(void)
#endif #endif
; ;
#if SEQUENTIAL_MODE
size_t idx = 0;
for (; idx < BLOCK_SIZE && w < PRODUCT; idx++, w++) {
#else
for (size_t w = 0; w < PRODUCT; w++) { for (size_t w = 0; w < PRODUCT; w++) {
#endif
// 1. generate audio data // 1. generate audio data
#if FP_RETURN_TYPE #if FP_RETURN_TYPE
long double bytebeat_res = floor(bytebeat((long double) floor(w))); long double bytebeat_res = floor(bytebeat((long double) floor(w)));
@ -224,7 +267,11 @@ main(void)
#endif #endif
// 5. save sample into buffer // 5. save sample into buffer
#if SEQUENTIAL_MODE
buffer[idx] = sample_res;
#else
buffer[w] = sample_res; buffer[w] = sample_res;
#endif
// 6. log // 6. log
#if VERBOSE_MODE #if VERBOSE_MODE
@ -244,23 +291,50 @@ main(void)
printf("%s", ANSI_CLEAR); printf("%s", ANSI_CLEAR);
// 5. log // 5. log
#if SEQUENTIAL_MODE
printf("Writing out file " OUTPUT_FILE " (part %" PRIuMAX "/%"
PRIuMAX ", %.2Lf%%)...\n", (uintmax_t) seq + 1,
(uintmax_t) MAX,
((long double) seq * 100.L) / ((long double) MAX));
#else
printf("\nWriting out file " OUTPUT_FILE "...\n"); printf("\nWriting out file " OUTPUT_FILE "...\n");
#endif
fflush(stdout); fflush(stdout);
#endif #endif
// * save the sample data into the file // * save the sample data into the file
fwrite_le(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file); fwrite_le(buffer, sizeof(SAMPLE_TYPE),
#if SEQUENTIAL_MODE
calc_block_size,
#else
PRODUCT,
#endif
output_file);
#if SEQUENTIAL_MODE
fflush(output_file);
#endif
#if SEQUENTIAL_MODE
// * free allocated heap
free(buffer);
#endif
}
#if !SEQUENTIAL_MODE
while (0);
#endif
// 6. close file // 6. close file
fclose(output_file); fclose(output_file);
#if !SEQUENTIAL_MODE
// * free allocated heap
free(buffer);
#endif
// * end of program // * end of program
#if !SILENT_MODE #if !SILENT_MODE
printf("Done!\n"); printf("Done!\n");
#endif #endif
// * free allocated heap
free(buffer);
return 0; return 0;
} }