1
0

major refactor

This commit is contained in:
Intel A80486DX2-66 2023-11-16 22:29:52 +03:00
parent ae04b22c7a
commit b68a426b92
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
8 changed files with 102 additions and 86 deletions

8
.editorconfig Normal file
View File

@ -0,0 +1,8 @@
root = true
[*]
end_of_line = crlf
insert_final_newline = true
indent_size = 4
charset = utf-8
indent_style = tab

11
.gitignore vendored
View File

@ -53,14 +53,5 @@ Mkfile.old
dkms.conf
# ---> products
# Substituted code
formula_substituted.*
# Executables
*.a
*.out
*.exe
render_bytebeat.*
# Output
build/
output.wav

View File

@ -1,5 +1,4 @@
$ cat | ./bytebeat_compiler.py - && ./render_bytebeat
t&(t>>7)-t&t>>8
$ echo 't&((t>>7)-t)&t>>8' | ./bytebeat_compiler.py - && ./render_bytebeat
Compiling
:: C bytebeat generator runtime unit

View File

@ -1,18 +1,33 @@
#!/usr/bin/python3
DEFAULT_PARAMETERS = {
"CC": "gcc",
"CC_FLAGS": "-Os -Wall -Werror -Wpedantic",
"INPUT_FILES": ["engine.c", "formula_substituted.c"],
"OUTPUT_FILE": "render_bytebeat"
}
from os.path import exists
from os import system, environ
from os.path import exists, join as path_join
from os import system, environ, makedirs
from argparse import ArgumentParser
from sys import stdin
import subprocess
# Paths
PATHS = {
"src_dir": "src/",
"build_dir": "build/",
"template": "template.c",
"substitute": "substituted.c",
"output": "render_bytebeat"
}
# 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"])
# Default parameters
DEFAULT_PARAMETERS = {
"CC": "gcc",
"CC_FLAGS": "-Os -Wall -Werror -Wpedantic",
"INPUT_FILE": PATHS["substitute"],
"OUTPUT_FILE": PATHS["output"]
}
def fetch(name: str):
return res if (res := environ.get(name)) else DEFAULT_PARAMETERS[name]
@ -36,16 +51,14 @@ def substitute_value(placeholder: str, replacement, text: str) -> str:
CC = fetch("CC")
CC_FLAGS = fetch("CC_FLAGS")
INPUT_FILES = fetch("INPUT_FILES")
INPUT_FILE = fetch("INPUT_FILE")
OUTPUT_FILE = fetch("OUTPUT_FILE")
if __name__ == "__main__":
print("Bytebeat compiler")
parser = ArgumentParser(description=\
"Substitutes supplied C (non-JavaScript!) bytebeat into the template, "
"then attempts to compile the instance of the template. Uses "
"environmental variables `CC`, `CC_FLAGS`, `INPUT_FILES`, "
"environmental variables `CC`, `CC_FLAGS`, `INPUT_FILE`, "
"`OUTPUT_FILE`.")
parser.add_argument("file", type=str,
help="bytebeat formula file")
@ -69,31 +82,29 @@ if __name__ == "__main__":
print("No valid contents")
raise SystemExit
# Substitute all placeholders in formula_template.c -> formula_substitute.c
# - Compilation
makedirs(PATHS["build_dir"], exist_ok=True)
if not args.no_return: # Insert return statement
bytebeat_contents = f"\treturn\n\n{bytebeat_contents}"
substitute_c = read_file("formula_template.c")
substitute_c = substitute_value("bytebeat_contents",
bytebeat_contents, substitute_c)
rewrite_file("formula_substituted.c", substitute_c)
# Substitute all placeholders in formula_template.h -> formula_substitute.h
substitute_h = read_file("formula_template.h")
substitute_h = substitute_value("sample_rate",
args.sample_rate, substitute_h)
substitute_h = substitute_value("bit_depth",
args.bit_depth, substitute_h)
substitute_h = substitute_value("is_signed",
"1" if args.signed else "0", substitute_h)
substitute_h = substitute_value("channels",
args.channels, substitute_h)
substitute_h = substitute_value("seconds",
args.seconds, substitute_h)
rewrite_file("formula_substituted.h", substitute_h)
substitute = read_file(PATHS["template"])
substitute = substitute_value("bytebeat_contents",
bytebeat_contents, substitute)
substitute = substitute_value("sample_rate",
args.sample_rate, substitute)
substitute = substitute_value("bit_depth",
args.bit_depth, substitute)
substitute = substitute_value("is_signed",
"1" if args.signed else "0", substitute)
substitute = substitute_value("channels",
args.channels, substitute)
substitute = substitute_value("seconds",
args.seconds, substitute)
rewrite_file(PATHS["substitute"], substitute)
# Compile by invoking the shell script
print("Compiling")
# Let system execute aliases by calling os.system
system(" ".join([CC, CC_FLAGS, *INPUT_FILES, "-o", OUTPUT_FILE]))
# Let the system execute aliases by calling os.system
system(" ".join([CC, CC_FLAGS, INPUT_FILE, "-o", OUTPUT_FILE]))

7
documentation.md Normal file
View File

@ -0,0 +1,7 @@
# C_bytebeat_render
## Bytebeat code
**Variables:**
- `w`: `long double`
- `t`: `w` casted as `uintmax_t`

View File

@ -1,13 +0,0 @@
#include <stdint.h>
#include "formula_substituted.h"
SAMPLE_TYPE
bytebeat(long double w)
{
uintmax_t t = (uintmax_t)w;
`bytebeat_contents`
;
}

View File

@ -1,23 +0,0 @@
#ifndef _FORMULA_TEMPLATE_H
#define _FORMULA_TEMPLATE_H
#define SAMPLE_RATE `sample_rate`
#define BIT_DEPTH `bit_depth`
#define IS_SIGNED `is_signed`
#define CHANNELS `channels`
#define SECONDS `seconds`
#if BIT_DEPTH <= 8
#define SAMPLE_TYPE uint8_t
#elif BIT_DEPTH >= 16
#if IS_SIGNED
#define SAMPLE_TYPE int16_t
#else
#define SAMPLE_TYPE uint16_t
#endif
#endif
SAMPLE_TYPE
bytebeat(long double w);
#endif /* _FORMULA_TEMPLATE_H */

View File

@ -7,8 +7,7 @@
#include <string.h>
#include <time.h>
#include "formula_substituted.h"
// constants
#if defined(_WIN32)
#define __ANSI_CLEAR_STRING "\r"
#elif defined(__unix__) || defined(__linux__)
@ -18,6 +17,22 @@
#endif
const char* ANSI_CLEAR = __ANSI_CLEAR_STRING;
#define SAMPLE_RATE `sample_rate`
#define BIT_DEPTH `bit_depth`
#define IS_SIGNED `is_signed`
#define CHANNELS `channels`
#define SECONDS `seconds`
#if BIT_DEPTH <= 8
#define SAMPLE_TYPE uint8_t
#elif BIT_DEPTH >= 16
#if IS_SIGNED
#define SAMPLE_TYPE int16_t
#else
#define SAMPLE_TYPE uint16_t
#endif
#endif
#define PRODUCT (SAMPLE_RATE * SECONDS * CHANNELS)
#define FREQUENCY_OF_STATUS_REPORTING 5000
@ -33,9 +48,20 @@ const char* dbgpnt_labels[3] = { "memory allocation",
#define dbgpnt_labels_size \
(unsigned int)(sizeof(dbgpnt_labels) / sizeof(dbgpnt_labels[0]))
bool silent_mode = 0;
bool debug_mode = 0;
// global variables
bool silent_mode = false, debug_mode = false;
// function prototypes
void
debug_print(void);
long double
random(void);
SAMPLE_TYPE
bytebeat(long double w);
// function implementations
void
debug_print(void)
{
@ -66,6 +92,16 @@ random(void)
return (long double)rand() / RAND_MAX;
}
SAMPLE_TYPE
bytebeat(long double w)
{
uintmax_t t = (uintmax_t)w;
`bytebeat_contents`
;
}
int
main(int argc, char** argv)
{