template.c: add support for big-endian machines
This commit is contained in:
parent
8c758e1224
commit
df91110d80
@ -12,13 +12,17 @@ PATHS = {
|
||||
"build_dir": "build/",
|
||||
"template": "template.c",
|
||||
"substitute": "substituted.c",
|
||||
"output": "render_bytebeat"
|
||||
"output": "render_bytebeat",
|
||||
"fwrite_le_header": "fwrite_le.h",
|
||||
"fwrite_le": "fwrite_le.c"
|
||||
}
|
||||
|
||||
# Solve paths
|
||||
PATHS["template"] = path_join(PATHS["src_dir"], PATHS["template"])
|
||||
PATHS["substitute"] = path_join(PATHS["build_dir"], PATHS["substitute"])
|
||||
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 = {
|
||||
@ -150,10 +154,12 @@ if __name__ == "__main__":
|
||||
"length": samples,
|
||||
"silent_mode": "true" if 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"])))
|
||||
|
||||
# Compile by invoking the shell script
|
||||
print("Compiling")
|
||||
|
||||
# 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])
|
||||
|
@ -7,6 +7,8 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "`path_to_fwrite_le`"
|
||||
|
||||
// constants
|
||||
#if defined(_WIN32)
|
||||
# define __ANSI_CLEAR_STRING "\r"
|
||||
@ -180,7 +182,7 @@ main(void)
|
||||
}
|
||||
|
||||
// 2. prepare variables
|
||||
const uint32_t buffer_size = PRODUCT,
|
||||
uint32_t buffer_size = PRODUCT,
|
||||
file_length =
|
||||
4 * 4 /* 4 strings of 4 characters */ +
|
||||
5 * 4 /* 4 uint32_t values */ +
|
||||
@ -190,29 +192,29 @@ main(void)
|
||||
in the file format structure */,
|
||||
sample_rate = SAMPLE_RATE,
|
||||
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,
|
||||
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(&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); // L : Subchunk2Size : 4
|
||||
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(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file);
|
||||
fwrite_le(buffer, sizeof(SAMPLE_TYPE), buffer_size, output_file);
|
||||
|
||||
// 5. close file
|
||||
fclose(output_file);
|
||||
|
Loading…
Reference in New Issue
Block a user