bg3 / create_dataset.py
Gatozu35's picture
Upload 7 files
7e07a0b verified
import json
import re
from os import getenv
from pathlib import Path
from tqdm import tqdm
from soundfile import SoundFile
ROOT = getenv("BG3_VOICE")
assert ROOT is not None, "Please set the BG3_VOICE environment variable to the directory Voice.pak was unpacked in."
def main():
Path("./wav/").mkdir(exist_ok=True)
root_path = Path(ROOT)
sound_path = root_path/"Mods/Gustav/Localization/English/Soundbanks"
with open("english.loca.json", "r") as loca_f:
uid_to_txt = json.load(loca_f)
with open("bg3.ndjson", "w") as bg3_f:
for file in tqdm(list(sound_path.glob(f"*.wav"))):
file_path=file.resolve()
try:
contentuid = re.search(r"h.*(?=\.wem)", file_path.stem).group()
except AttributeError:
continue
try:
text = uid_to_txt[contentuid]
except KeyError:
continue
text = re.sub(r"\(\[.*\]\)", "", text)
Path("wav/"+file_path.name).hardlink_to(file_path)
snd_file = SoundFile(str(file_path), "r")
sample = dict(
name=file_path.name,
text=text,
spk_id=file_path.name.replace(
f"_{contentuid}.wem.wav",""
),
is_effect=bool(re.match(r"v[0-9]", text)),
samplerate=snd_file.samplerate,
duration=snd_file.frames/snd_file.samplerate,
n_channels=snd_file.channels,
)
json.dump(sample, bg3_f)
bg3_f.write("\n")
if __name__ == "__main__":
main()