File size: 1,633 Bytes
7e07a0b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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()
|