Skip to content

Instantly share code, notes, and snippets.

@vivaria
Last active November 12, 2024 03:27
Show Gist options
  • Save vivaria/663ac4ffaf37345f0abd55e1cc8dfe32 to your computer and use it in GitHub Desktop.
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
"""
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)
@vivaria
Copy link
Author

vivaria commented Nov 12, 2024

Usage instructions:

Step 1: Installing Python:

  • Install Python onto your system. Any version should be OK. (e.g. Windows: https://www.python.org/ftp/python/3.13.0/python-3.13.0-amd64.exe). During the installation, make sure you check the box "add Python to the PATH".
  • When you are done installing Python, open up a terminal window. This program could be called "Terminal", "Command Prompt", etc. depending on your operating system. Then, type the command "python --version" to see if you installed Python correctly. If you see a version, then you did it right.

Step 2: Downloading the Script

  • On this page, click "Download zip" in the top right corner, then unzip the zip.
  • Then, open the script in Notepad. You will want to change the location of the "CUSTOMSONG_DIR" to match the installation location for Taiko: the Drum Master. For example, if TaikoTDM is installed to your home folder, you would maybe change the line to say:
CUSTOMSONG_DIR = os.path.join("C:\\", "Users", "YourUsername", "TaikoTDM", "customSongs")
  • Save the changes.

Step 3: Running the Script

  • Open the folder containing the the script in your Terminal. For example, if you downloaded and extracted the script to your Downloads folder, you would open the Terminal window, then type "cd ~/Downloads" to change the directory to that folder.
  • Then, run the command python fix_converted_tja_fumenOffsetPos.py. It will run the script to fix your custom songs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment