Last active
November 12, 2024 03:27
-
-
Save vivaria/663ac4ffaf37345f0abd55e1cc8dfe32 to your computer and use it in GitHub Desktop.
Small Python script to correct wrong offset values for converted TJAs from TakoTako <=3.2.0
This file contains hidden or 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
""" | |
Fix offset values due to bug in older versions of TakoTako (<=3.2.0) | |
See also: https://github.com/Fluto/TakoTako/issues/16 | |
Bug description: | |
- TJA songs have an "OFFSET:" value. | |
- When converting TJAs to fumen, "OFFSET" is automatically included in | |
the data of the fumen files themselves. | |
- However, TakoTako also added the "OFFSET" value to "fumenOffsetPos" | |
field inside the "data.json" file for the converted TJA. | |
- This means that "OFFSET" is accidentally counted twice!! This causes desync. | |
So, to fix this problem, this script automatically edits all of the "data.json" | |
files and resets them back to their original values. This removes the desync. | |
""" | |
import os | |
import json | |
# Make sure to edit to point to the directory with custom songs | |
CUSTOMSONG_DIR = os.path.join("C:\\", "TaikoTDM", "CustomSongs") | |
# Step 1. Read in the existing song data | |
for root, dirs, files in os.walk(CUSTOMSONG_DIR, topdown=True): | |
if "data.json" in files: | |
json_path = os.path.join(root, "data.json") | |
json_dict = json.load(open(json_path, encoding="utf8")) | |
old_offset = json_dict['fumenOffsetPos'] | |
# 1: Offset is below 0 -- Fix by setting to 0. (Offset was modified by TakoTako) | |
if old_offset < 0: | |
json_dict['fumenOffsetPos'] = 0 | |
print(f"Old: {old_offset}, New: 0, Song: {root}") | |
# 2: Offset is below 2000 -- Fix by setting to 2000 | |
# Note: TakoTako adds 2s of silence for songs with small offsets. That's why there | |
# are songs with ~2000ms of offset -- to match the edited audio from TakoTako. | |
elif 1000 <= old_offset < 2000: | |
json_dict['fumenOffsetPos'] = 2000 | |
print(f"Old: {old_offset}, New: 2000, Song: {root}") | |
# 3. Offset is between 0 and 1000 -- Do nothing. (Offset was not modified by TakoTako.) | |
else: | |
print(f"Skipping song due to correct fumenOffsetPos '{old_offset}' ({root}).") | |
with open(json_path, "w", encoding="utf8") as outfile: | |
# Get the JSON data to be written, as a string | |
str_to_write = json.dumps(json_dict, indent="\t", ensure_ascii=False) | |
# Account for the fact that some keys are "key:value" while others are | |
# "key: value" | |
str_to_write = str_to_write.replace(': ', ':') | |
for key in ["songName", "songSubtitle", "songDetail", | |
"text", "font", "jpText", "jpFont", | |
"enText", "enFont", "krText", "krFont"]: | |
str_to_write = str_to_write.replace(f'"{key}":', f'"{key}": ') | |
# Account for the nonstandard tab indentation used by the existing | |
# files | |
str_to_write = str_to_write.replace("\t", "\t\t") | |
str_to_write = str_to_write.replace("\t\t\t\t", "\t\t\t") | |
str_to_write = "\t" + str_to_write[:-1] + "\t}" | |
# Write the modified JSON string to the file | |
outfile.write(str_to_write) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage instructions:
Step 1: Installing Python:
Step 2: Downloading the Script
Step 3: Running the Script
python fix_converted_tja_fumenOffsetPos.py
. It will run the script to fix your custom songs.