diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dabdb96 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +end_of_line = crlf +insert_final_newline = true +indent_size = 4 +charset = utf-8 +indent_style = tab diff --git a/.gitignore b/.gitignore index 4b9f15c..b9197c7 100644 --- a/.gitignore +++ b/.gitignore @@ -53,14 +53,5 @@ Mkfile.old dkms.conf # ---> products -# Substituted code -formula_substituted.* - -# Executables -*.a -*.out -*.exe -render_bytebeat.* - -# Output +build/ output.wav diff --git a/EXAMPLE_USAGE.txt b/EXAMPLE_USAGE.txt index e52eec6..cecbc89 100644 --- a/EXAMPLE_USAGE.txt +++ b/EXAMPLE_USAGE.txt @@ -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 diff --git a/bytebeat_compiler.py b/bytebeat_compiler.py index e9bc499..a2098fc 100644 --- a/bytebeat_compiler.py +++ b/bytebeat_compiler.py @@ -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])) diff --git a/documentation.md b/documentation.md new file mode 100644 index 0000000..9e5af6c --- /dev/null +++ b/documentation.md @@ -0,0 +1,7 @@ +# C_bytebeat_render + +## Bytebeat code + +**Variables:** +- `w`: `long double` +- `t`: `w` casted as `uintmax_t` diff --git a/formula_template.c b/formula_template.c deleted file mode 100644 index 28c9e12..0000000 --- a/formula_template.c +++ /dev/null @@ -1,13 +0,0 @@ -#include - -#include "formula_substituted.h" - -SAMPLE_TYPE -bytebeat(long double w) -{ - uintmax_t t = (uintmax_t)w; - - `bytebeat_contents` - - ; -} \ No newline at end of file diff --git a/formula_template.h b/formula_template.h deleted file mode 100644 index c30c6f5..0000000 --- a/formula_template.h +++ /dev/null @@ -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 */ diff --git a/engine.c b/src/template.c similarity index 86% rename from engine.c rename to src/template.c index d85ecb6..6137f48 100644 --- a/engine.c +++ b/src/template.c @@ -7,8 +7,7 @@ #include #include -#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) {