-
-
Save anonymous/a2ec814c5d52164dd61dd7dba64004a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import glob | |
import struct | |
import tempfile | |
import subprocess | |
from kdttool import KDT | |
if len(sys.argv) != 2: | |
sys.exit("no dir") | |
dir_in = sys.argv[1] | |
if os.path.isdir(dir_in) is not True: | |
sys.exit("not a dir") | |
os.chdir( os.path.dirname( __file__ ) ) | |
libdirObj = tempfile.TemporaryDirectory() | |
libdir = libdirObj.name | |
filter = os.path.join(dir_in, "*.psf2lib") | |
libpath = glob.glob(filter)[0] | |
r = subprocess.call([ | |
r"unpkpsf2.exe", | |
libpath, | |
libdir | |
]) | |
if r != 0: | |
sys.exit("unpkpsf2 returned error: %d" % r) | |
filter = os.path.join(dir_in, "*.minipsf2") | |
for psf2path in glob.glob(filter): | |
rootdirObj = tempfile.TemporaryDirectory() | |
rootdir = rootdirObj.name | |
r = subprocess.call([ | |
r"unpkpsf2.exe", | |
psf2path, | |
rootdir | |
]) | |
if r != 0: | |
sys.exit("unpkpsf2 returned error: %d" % r) | |
with open(psf2path, "rb") as psf2: | |
psf2.seek(0x04) | |
size = struct.unpack("<I", psf2.read(4))[0] | |
psf2.seek(0x10 + size) | |
tags = psf2.read()[5:].decode("utf-8").rstrip("\n").split("\n") | |
for filename in os.listdir(rootdir): | |
filepath = os.path.join(rootdir, filename) | |
if filename[-4:].lower() != '.ini': | |
if filename[-3:].upper() == '.TD': | |
kdt = KDT(filepath, log=False, convert=False) | |
for trknum in range(kdt.tracks): | |
kdt.set_track(trknum) | |
while kdt.offset < kdt.trk_off_end: | |
if kdt.running: | |
if kdt.buf[kdt.offset] & 0x80: | |
cmd = kdt.buf[kdt.offset] & 0x7F | |
param = kdt.buf[kdt.offset+1] & 0x7F | |
if cmd == 0x63 and param == 0x14: | |
loop_start = kdt.time | |
elif cmd == 0x63 and param == 0x1E: | |
loop_end = kdt.time | |
kdt.read_seq() | |
newpath = os.path.join(libdir, filename) | |
if os.path.isfile(newpath): | |
os.remove(filepath) | |
else: | |
os.rename(filepath, newpath) | |
subprocess.call([ | |
r"mkpsf2.exe", | |
psf2path, | |
rootdir | |
]) | |
rootdirObj.cleanup() | |
del rootdirObj | |
length = (loop_end + (loop_end - loop_start)) * 60 / kdt.tickdiv / kdt.bpm | |
length = "length=%02d:%07.4f" % divmod(length, 60) | |
tags.append(length) | |
tags.append("fade=10") | |
tags = bytes("[TAG]%s\n" % "\n".join(tags), encoding="utf-8") | |
with open(psf2path, "ab") as psf2: | |
psf2.write(tags) | |
subprocess.call([ | |
r"mkpsf2.exe", | |
libpath, | |
libdir | |
]) | |
libdirObj.cleanup() | |
del libdirObj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment