From 17065ed00d3ff65e6f28ae575b685247180a6c33 Mon Sep 17 00:00:00 2001 From: Louis Thurman Date: Tue, 7 Mar 2023 00:32:26 +0000 Subject: [PATCH] Update util script --- .gitignore | 1 + auto_render.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++ render_and_play.py | 30 ----------------- 3 files changed, 82 insertions(+), 30 deletions(-) create mode 100644 auto_render.py delete mode 100644 render_and_play.py diff --git a/.gitignore b/.gitignore index 6b7b37b..297cb21 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ Library-.* *.aux *.log *.orig +*.mid *~ diff --git a/auto_render.py b/auto_render.py new file mode 100644 index 0000000..d978475 --- /dev/null +++ b/auto_render.py @@ -0,0 +1,81 @@ +#!/usr/bin/python3 -u + +import logging +import pathlib +import subprocess +import sys +import time + + +win_root = pathlib.Path(r"C:\Users\louis\GitKraken\dottes") +lin_root = pathlib.Path("~/dottes") +file_update_times = {} + + +def run_continuous(): + logging.info("Run continous") + + pwd = pathlib.Path() + + while True: + for obj_path in pwd.rglob("*"): + obj_path_str = str(obj_path) + if not obj_path.is_file(): + continue + if not obj_path_str.lower().endswith(".abc"): + continue + + if obj_path_str not in file_update_times: + file_update_times[obj_path_str] = obj_path.lstat().st_mtime + else: + if obj_path.lstat().st_mtime > file_update_times[obj_path_str]: + file_update_times[obj_path_str] = obj_path.lstat().st_mtime + convert_single(lin_path=obj_path_str) + + + +def convert_single(win_path: str = None, lin_path: str = None): + if win_path: + input_path = win_path + win_abc_file_path = pathlib.Path(win_path) + lin_abc_file_path = lin_root / win_abc_file_path.relative_to(win_root) + elif lin_path: + input_path = lin_path + lin_abc_file_path = pathlib.Path(lin_path) + else: + raise Exception("Argument win_path or lin_path must be given") + + + logging.info(f"Converting {input_path}") + + out_file_name = lin_abc_file_path.name.rsplit(".", 1)[0] + ".mid" + output_path = lin_abc_file_path.parent / out_file_name + + if output_path.exists(): + output_path.unlink() + + r = subprocess.run( + [ + "abc2midi", + lin_abc_file_path.as_posix(), + "-o", output_path.as_posix() + ] + ) + + if r.returncode != 0: + logging.error(f"Error whilst convering {lin_abc_file_path}") + else: + logging.info(f"Converted {input_path} > {output_path}") + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + if len(sys.argv) > 1: + if sys.platform == "win32": + convert_single(win_path=sys.argv[1]) + elif sys.platform == "linux": + convert_single(lin_path=sys.argv[1]) + else: + raise Exception(f"Unknown platform '{sys.platform}', don't know how to interpret path") + else: + run_continuous() diff --git a/render_and_play.py b/render_and_play.py deleted file mode 100644 index bca12dc..0000000 --- a/render_and_play.py +++ /dev/null @@ -1,30 +0,0 @@ -import os -import pathlib -import subprocess -import sys - - -win_root = pathlib.Path(r"C:\Users\louis\GitKraken\dottes") -lin_root = pathlib.Path("~/dottes") - -try: - win_abc_file_path = pathlib.Path(sys.argv[1]) -except IndexError: - win_abc_file_path = pathlib.Path(input()) - -if not str(win_abc_file_path).lower().endswith(".abc"): - input("Doesn't look like an abc file.") - exit(1) - -lin_abc_file_path = lin_root / win_abc_file_path.relative_to(win_root) -out_file_name = lin_abc_file_path.name.rsplit(".", 1)[0] + ".mid" -lin_output_path = lin_abc_file_path.parent / out_file_name -win_output_path = win_abc_file_path.parent / out_file_name - -if win_output_path.exists(): - win_output_path.unlink() - -command = f"abc2midi {lin_abc_file_path.as_posix()} -o {lin_output_path.as_posix()}" -print("Command:", command) -r = subprocess.run([r"wsl"], input=command.encode()) -os.system(str(win_output_path))