skyrim.py 2.7 KB
import argparse
from utils import SubCommandRunner
from core.data import read_lst
import os



def lst2wav(lst: str, root: str, outfile: str):
    lst_ = read_lst(lst)

    with open(outfile, "w") as f:
        for id_ in lst_:
            # Split id
            splited = id_.replace("\n", "").split(",")

            # Get language
            language = None
            if splited[0] == "en-us":
                language = "english"
            elif splited[0] == "fr-fr":
                language = "french"

            # Create wav filename
            splited3 = splited[3].split(".")
            wav = ("-".join(splited3) + ".wav").replace(" ", "")

            # Create full path
            full_path = os.path.join(root, language, "wave-audio", wav)

            f.write(f"{id_} {full_path}\n")


def utt2spk(lst: str, outfile: str):
    lst_ = read_lst(lst)

    with open(outfile, "w") as f:
        for id_ in lst_:
            splited = id_.split(",")
            spk_id = splited[0] + "-" + splited[1]
            f.write(id_ + " " + spk_id + "\n")


def utt2char(lst: str, outfile: str):
    lst_ = read_lst(lst)

    with open(outfile, "w") as f:
        for id_ in lst_:
            splited = id_.split(",")
            char_id = splited[1]
            f.write(id_ + " " + char_id + "\n")


if __name__ == '__main__':
    # Main parser
    parser = argparse.ArgumentParser(description="Skyrim data-management commands")
    subparsers = parser.add_subparsers(title="action")

    parser_lst2wav = subparsers.add_parser("lst2wav", help="Generate wav file from lst")
    parser_lst2wav.add_argument("--lst", type=str, help="list file .lst")
    parser_lst2wav.add_argument("--root", type=str, help="root directory with audio files")
    parser_lst2wav.add_argument("--outfile", type=str, help="output wav.scp-like file")
    parser_lst2wav.set_defaults(which="lst2wav")

    parser_utt2spk = subparsers.add_parser("utt2spk", help="generate utt2spk file")
    parser_utt2spk.add_argument("--lst", required=True, type=str, help="list file .lst")
    parser_utt2spk.add_argument("--outfile", required=True, type=str, help="utt2spk output file")
    parser_utt2spk.set_defaults(which="utt2spk")

    parser_utt2char = subparsers.add_parser("utt2char", help="..")
    parser_utt2char.add_argument("--lst", required=True, type=str, help="list file .lst")
    parser_utt2char.add_argument("--outfile", required=True, type=str, help="utt2char output file")
    parser_utt2char.set_defaults(which="utt2char")

    # Parse
    args = parser.parse_args()

    # Run commands
    runner = SubCommandRunner({
        "lst2wav": lst2wav,
        "utt2spk": utt2spk,
        "utt2char": utt2char
    })

    runner.run(args.which, args.__dict__, remove="which")