# Place this script into directory with tsv files and clips directory

import os, glob, argparse
from pathlib import Path
from shutil import copyfile


def main(datasets_root: Path):
    file_path = os.path.join(datasets_root, "validated.tsv")
    file = open(file_path, 'r', encoding='utf-8')
    num_lines = sum(1 for _ in open(file_path, 'r'))

    i = 0
    for line in file.readlines()[1:]:
        speaker = line.split('\t')[0]
        audio = line.split('\t')[1]
        speaker_audio_folder = os.path.join(datasets_root, "LibriTTS", "train-clean-100", speaker, "0")
        Path(speaker_audio_folder).mkdir(parents=True, exist_ok=True)
        sentence_file = open(os.path.join(speaker_audio_folder, audio[:len(audio) - 3]) + "txt", 'w',
                             encoding='utf-8')
        sentence_file.write(line.split('\t')[2])
        sentence_file.close()
        copyfile(os.path.join(datasets_root, "clips", audio), os.path.join(speaker_audio_folder, audio))
        i = i + 1
        print(i, "/", num_lines)


if __name__ == '__main__':
    num_lines = sum(1 for _ in open("/Users/petr/dev/apps/third-party/Real-Time-Voice-Cloning/datasets_root/SV2TTS/synthesizer/train.txt", 'r'))

    print(num_lines)
    # parser = argparse.ArgumentParser(
    #     description="Automatically creates a dataset for use from Mozilla Common Voice dataset.",
    #     formatter_class=argparse.ArgumentDefaultsHelpFormatter
    # )
    # parser.add_argument("datasets_root", type=Path, help="Directory for all your datasets.")
    # args = parser.parse_args()
    # main(**vars(args))

# Place this script into directory with tsv files and clips directory

import os, glob, argparse
from pathlib import Path
from shutil import copyfile


def main(datasets_root: Path):
    file_path = os.path.join(datasets_root, "validated.tsv")
    file = open(file_path, 'r', encoding='utf-8')
    num_lines = sum(1 for _ in open(file_path, 'r'))

    i = 0
    for line in file.readlines()[1:]:
        speaker = line.split('\t')[0]
        audio = line.split('\t')[1]
        speaker_audio_folder = os.path.join(datasets_root, "LibriTTS", "train-clean-100", speaker, "0")
        Path(speaker_audio_folder).mkdir(parents=True, exist_ok=True)
        sentence_file = open(os.path.join(speaker_audio_folder, audio[:len(audio) - 3]) + "txt", 'w',
                             encoding='utf-8')
        sentence_file.write(line.split('\t')[2])
        sentence_file.close()
        copyfile(os.path.join(datasets_root, "clips", audio), os.path.join(speaker_audio_folder, audio))
        i = i + 1
        print(i, "/", num_lines)


if __name__ == '__main__':
    num_lines = sum(1 for _ in open("/Users/petr/dev/apps/third-party/Real-Time-Voice-Cloning/datasets_root/SV2TTS/synthesizer/train.txt", 'r'))

    print(num_lines)
    # parser = argparse.ArgumentParser(
    #     description="Automatically creates a dataset for use from Mozilla Common Voice dataset.",
    #     formatter_class=argparse.ArgumentDefaultsHelpFormatter
    # )
    # parser.add_argument("datasets_root", type=Path, help="Directory for all your datasets.")
    # args = parser.parse_args()
    # main(**vars(args))

# Place this script into directory with tsv files and clips directory # parser = argparse.ArgumentParser( #     description="Automatically creates a dataset for use from Mozilla Common Voice dataset.", #     formatter_class=argparse.ArgumentDefaultsHelpFormatter # ) # parser.add_argument("datasets_root", type=Path, help="Directory for all your datasets.") # args = parser.parse_args() # main(**vars(args))