1
0

template.c: add support for big-endian machines

This commit is contained in:
Intel A80486DX2-66 2023-12-30 15:31:11 +03:00
parent 8c758e1224
commit df91110d80
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 26 additions and 18 deletions

View File

@ -12,13 +12,17 @@ PATHS = {
"build_dir": "build/", "build_dir": "build/",
"template": "template.c", "template": "template.c",
"substitute": "substituted.c", "substitute": "substituted.c",
"output": "render_bytebeat" "output": "render_bytebeat",
"fwrite_le_header": "fwrite_le.h",
"fwrite_le": "fwrite_le.c"
} }
# Solve paths # Solve paths
PATHS["template"] = path_join(PATHS["src_dir"], PATHS["template"]) PATHS["template"] = path_join(PATHS["src_dir"], PATHS["template"])
PATHS["substitute"] = path_join(PATHS["build_dir"], PATHS["substitute"]) PATHS["substitute"] = path_join(PATHS["build_dir"], PATHS["substitute"])
PATHS["output"] = path_join(PATHS["build_dir"], PATHS["output"]) PATHS["output"] = path_join(PATHS["build_dir"], PATHS["output"])
PATHS["path_to_fwrite_le_header"] = PATHS["src_dir"] + PATHS["fwrite_le_header"]
PATHS["path_to_fwrite_le"] = PATHS["src_dir"] + PATHS["fwrite_le"]
# Default parameters # Default parameters
DEFAULT_PARAMETERS = { DEFAULT_PARAMETERS = {
@ -150,10 +154,12 @@ if __name__ == "__main__":
"length": samples, "length": samples,
"silent_mode": "true" if args.silent else "false", "silent_mode": "true" if args.silent else "false",
"verbose_mode": "true" if args.verbose and not args.silent else "false", "verbose_mode": "true" if args.verbose and not args.silent else "false",
"path_to_fwrite_le": "../" + PATHS["path_to_fwrite_le_header"]
}, read_file(PATHS["template"]))) }, read_file(PATHS["template"])))
# Compile by invoking the shell script # Compile by invoking the shell script
print("Compiling") print("Compiling")
# Let the system execute aliases by calling os.system # Let the system execute aliases by calling os.system
system(" ".join([CC, CFLAGS, INPUT_FILE, "-o", OUTPUT_FILE])) system(" ".join([CC, CFLAGS, INPUT_FILE, PATHS["path_to_fwrite_le"],
"-o", OUTPUT_FILE])

View File

@ -7,6 +7,8 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "`path_to_fwrite_le`"
// constants // constants
#if defined(_WIN32) #if defined(_WIN32)
# define __ANSI_CLEAR_STRING "\r" # define __ANSI_CLEAR_STRING "\r"
@ -180,7 +182,7 @@ main(void)
} }
// 2. prepare variables // 2. prepare variables
const uint32_t buffer_size = PRODUCT, uint32_t buffer_size = PRODUCT,
file_length = file_length =
4 * 4 /* 4 strings of 4 characters */ + 4 * 4 /* 4 strings of 4 characters */ +
5 * 4 /* 4 uint32_t values */ + 5 * 4 /* 4 uint32_t values */ +
@ -190,29 +192,29 @@ main(void)
in the file format structure */, in the file format structure */,
sample_rate = SAMPLE_RATE, sample_rate = SAMPLE_RATE,
byte_rate = (SAMPLE_RATE * BIT_DEPTH * CHANNELS) / 8; byte_rate = (SAMPLE_RATE * BIT_DEPTH * CHANNELS) / 8;
const uint16_t fmt_type = 1, // format type is PCM uint16_t fmt_type = 1, // format type is PCM
channels = CHANNELS, channels = CHANNELS,
block_align = (BIT_DEPTH * CHANNELS) / 8, block_align = (BIT_DEPTH * CHANNELS) / 8,
bit_depth = BIT_DEPTH > 8 ? BIT_DEPTH : 8; bit_depth = BIT_DEPTH > 8 ? BIT_DEPTH : 8;
// 3. write headers // 3. write headers
// <L = Little-endian or B = Big-endian> : <name> : <bytes of field> // <L = Little-endian or B = Big-endian> : <name> : <bytes of field>
fwrite("RIFF", 1, 4, output_file); // B : ChunkID : 4 fwrite ("RIFF", 1, 4, output_file); // B : ChunkID : 4
fwrite(&file_length, 4, 1, output_file); // L : ChunkSize : 4 fwrite_le(&file_length, 4, 1, output_file); // L : ChunkSize : 4
fwrite("WAVE", 1, 4, output_file); // B : Format : 4 fwrite ("WAVE", 1, 4, output_file); // B : Format : 4
fwrite("fmt ", 1, 4, output_file); // B : Subchunk1ID : 4 fwrite ("fmt ", 1, 4, output_file); // B : Subchunk1ID : 4
fwrite(&fmt_data_length, 4, 1, output_file); // L : Subchunk1Size : 4 fwrite_le(&fmt_data_length, 4, 1, output_file); // L : Subchunk1Size : 4
fwrite(&fmt_type, 2, 1, output_file); // L : AudioFormat : 2 fwrite_le(&fmt_type, 2, 1, output_file); // L : AudioFormat : 2
fwrite(&channels, 2, 1, output_file); // L : NumChannels : 2 fwrite_le(&channels, 2, 1, output_file); // L : NumChannels : 2
fwrite(&sample_rate, 4, 1, output_file); // L : SampleRate : 4 fwrite_le(&sample_rate, 4, 1, output_file); // L : SampleRate : 4
fwrite(&byte_rate, 4, 1, output_file); // L : ByteRate : 4 fwrite_le(&byte_rate, 4, 1, output_file); // L : ByteRate : 4
fwrite(&block_align, 2, 1, output_file); // L : BlockAlign : 2 fwrite_le(&block_align, 2, 1, output_file); // L : BlockAlign : 2
fwrite(&bit_depth, 2, 1, output_file); // L : BitsPerSample : 2 fwrite_le(&bit_depth, 2, 1, output_file); // L : BitsPerSample : 2
fwrite("data", 1, 4, output_file); // B : Subchunk2ID : 4 fwrite ("data", 1, 4, output_file); // B : Subchunk2ID : 4
fwrite(&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
fwrite(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file); fwrite_le(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file);
// 5. close file // 5. close file
fclose(output_file); fclose(output_file);