# Path: utils/util.py
# def mkdir_join(path_to_dir, *dir_name):
#     """Concatenate root path and 1 or more paths, and make a new direcory if
#     the direcory does not exist.
#     Args:
#         path_to_dir (string): path to a diretcory
#         dir_name (string): a direcory name
#     Returns:
#         path to the new directory
#     """
#     if path_to_dir is None:
#         return path_to_dir
# 
#     path_to_dir = mkdir(path_to_dir)
#     for i in range(len(dir_name)):
#         if i == len(dir_name) - 1 and '.' in dir_name[i]:
#             path_to_dir = os.path.join(path_to_dir, dir_name[i])
#         else:
#             path_to_dir = mkdir(os.path.join(path_to_dir, dir_name[i]))
#     return path_to_dir


from os.path import basename, join
from tqdm import tqdm
from utils.util import mkdir_join
import numpy as np
import wave

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function



def split_wav(wav_paths, save_path, speaker_dict):
    """Read WAV files & divide them with respect to each utterance.
    Args:
        wav_paths (list): path to WAV files
        save_path (string): path to save WAV files
        speaker_dict (dict): the dictionary of utterances of each speaker
            key => speaker
            value => the dictionary of utterance information of each speaker
                key => utterance index
                value => [start_frame, end_frame, transcript]
    """
    # Read each WAV file
    print('==> Reading WAV files...')
    print(speaker_dict.keys())
    for wav_path in tqdm(wav_paths):
        speaker = basename(wav_path).split('.')[0]

        # NOTE: For Switchboard
        speaker = speaker.replace('sw0', 'sw')
        speaker = speaker.replace('sw_', 'sw')
        speaker = speaker.replace('en_', 'en')

        utt_dict = speaker_dict[speaker]

# Path: utils/util.py
# def mkdir_join(path_to_dir, *dir_name):
#     """Concatenate root path and 1 or more paths, and make a new direcory if
#     the direcory does not exist.
#     Args:
#         path_to_dir (string): path to a diretcory
#         dir_name (string): a direcory name
#     Returns:
#         path to the new directory
#     """
#     if path_to_dir is None:
#         return path_to_dir
# 
#     path_to_dir = mkdir(path_to_dir)
#     for i in range(len(dir_name)):
#         if i == len(dir_name) - 1 and '.' in dir_name[i]:
#             path_to_dir = os.path.join(path_to_dir, dir_name[i])
#         else:
#             path_to_dir = mkdir(os.path.join(path_to_dir, dir_name[i]))
#     return path_to_dir


# Path: utils/inputs/wav_split.py
from os.path import basename, join
from tqdm import tqdm
from utils.util import mkdir_join
import numpy as np
import wave


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function



def split_wav(wav_paths, save_path, speaker_dict):
    """Read WAV files & divide them with respect to each utterance.
    Args:
        wav_paths (list): path to WAV files
        save_path (string): path to save WAV files
        speaker_dict (dict): the dictionary of utterances of each speaker
            key => speaker
            value => the dictionary of utterance information of each speaker
                key => utterance index
                value => [start_frame, end_frame, transcript]
    """
    # Read each WAV file
    print('==> Reading WAV files...')
    print(speaker_dict.keys())
    for wav_path in tqdm(wav_paths):
        speaker = basename(wav_path).split('.')[0]

        # NOTE: For Switchboard
        speaker = speaker.replace('sw0', 'sw')
        speaker = speaker.replace('sw_', 'sw')
        speaker = speaker.replace('en_', 'en')

        utt_dict = speaker_dict[speaker]

        wav_utt_save_path = mkdir_join(save_path, speaker)