# 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
# 
# def mkdir(path_to_dir):
#     """Make a new directory if the directory does not exist.
#     Args:
#         path_to_dir (string): path to a directory
#     Returns:
#         path (string): path to the new directory
#     """
#     if path_to_dir is not None and (not os.path.isdir(path_to_dir)):
#         os.mkdir(path_to_dir)
#     return path_to_dir
# 
# Path: utils/inputs/htk.py
# def save_config(audio_file_type, feature_type, channels, config_save_path,
#                 sampling_rate=16000, window=0.025, slide=0.01,
#                 energy=True, delta=True, deltadelta=True):
#     """Save a configuration file for HTK.
#     Args:
#         audio_file_type (string): nist or wav
#         feature_type (string): the type of features, logmelfbank or mfcc
#         channels (int): the number of frequency channels
#         config_save_path (string): path to save the config file
#         sampling_rate (float, optional):
#         window (float, optional): window width to extract features
#         slide (float, optional): extract features per 'slide'
#         energy (bool, optional): if True, add the energy feature
#         delta (bool, optional): if True, delta features are also extracted
#         deltadelta (bool, optional): if True, double delta features are also extracted
# """
#     with open(join(config_save_path, feature_type + '.conf'), 'w') as f:
#         if audio_file_type not in ['nist', 'wav']:
#             raise ValueError('audio_file_type must be nist or wav.')
#         f.write('SOURCEFORMAT = %s\n' % audio_file_type.upper())
# 
#         # Sampling rate
#         if sampling_rate == 16000:
#             f.write('SOURCERATE = 625\n')
#         elif sampling_rate == 8000:
#             f.write('SOURCERATE = 1250\n')
# 
#         # Target features
#         if feature_type == 'fbank':
#             feature_type = 'FBANK'  # log mel-filter bank channel outputs
#         elif feature_type == 'mfcc':
#             feature_type = 'MFCC'  # mel-frequency cepstral coefficients
#         # elif feature_type == 'linearmelfbank':
#         # feature_type = 'MELSPEC'  # linear mel-filter bank channel outputs
#         else:
#             raise ValueError('feature_type must be fbank or mfcc.')
# 
#         if energy:
#             feature_type += '_E'
#         if delta:
#             feature_type += '_D'
#         if deltadelta:
#             feature_type += '_A'
#         f.write('TARGETKIND = %s\n' % feature_type)
# 
#         # f.write('DELTAWINDOW = 2')
#         # f.write('ACCWINDOW = 2')
# 
#         # Extract features per slide
#         f.write('TARGETRATE = %.1f\n' % (slide * 10000000))
# 
#         f.write('SAVECOMPRESSED = F\n')
#         f.write('SAVEWITHCRC = F\n')
# 
#         # Window size
#         f.write('WINDOWSIZE = %.1f\n' % (window * 10000000))
# 
#         f.write('USEHAMMING = T\n')
#         f.write('PREEMCOEF = 0.97\n')
#         f.write('NUMCHANS = %d\n' % channels)
#         f.write('ENORMALISE = F\n')
#         f.write('ZMEANSOURCE = T\n')


import sys
import argparse
from os.path import join, basename
from glob import glob
from utils.util import mkdir_join, mkdir
from utils.inputs.htk import save_config

    args = parser.parse_args()
    htk_save_path = mkdir(args.htk_save_path)

    # HTK settings
    save_config(audio_file_type='wav',
                feature_type=args.feature_type,
                channels=args.channels,
                config_save_path='./config',
                sampling_rate=8000,
                window=args.window,
                slide=args.slide,
                energy=bool(args.energy),
                delta=bool(args.delta),
                deltadelta=bool(args.deltadelta))
    # NOTE: 120-dim features are extracted by default

    # Switchboard
    with open('./config/wav2htk_swbd.scp', 'w') as f:
        for wav_path in glob(join(args.wav_save_path, 'swbd/*.wav')):
            # ex.) wav_path: wav/swbd/*.wav
            save_path = mkdir_join(
                htk_save_path, 'swbd', basename(wav_path).split('.')[0] + '.htk')
            f.write(wav_path + '  ' + save_path + '\n')
            # ex.) htk_path: wav/swbd/*.htk

    # eval2000 (swbd)
    with open('./config/wav2htk_eval2000_swbd.scp', 'w') as f:
        for wav_path in glob(join(args.wav_save_path, 'eval2000/swbd/*.wav')):
            # ex.) wav_path: wav/eval2000_swbd/*.wav

# 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
# 
# def mkdir(path_to_dir):
#     """Make a new directory if the directory does not exist.
#     Args:
#         path_to_dir (string): path to a directory
#     Returns:
#         path (string): path to the new directory
#     """
#     if path_to_dir is not None and (not os.path.isdir(path_to_dir)):
#         os.mkdir(path_to_dir)
#     return path_to_dir
# 
# Path: utils/inputs/htk.py
# def save_config(audio_file_type, feature_type, channels, config_save_path,
#                 sampling_rate=16000, window=0.025, slide=0.01,
#                 energy=True, delta=True, deltadelta=True):
#     """Save a configuration file for HTK.
#     Args:
#         audio_file_type (string): nist or wav
#         feature_type (string): the type of features, logmelfbank or mfcc
#         channels (int): the number of frequency channels
#         config_save_path (string): path to save the config file
#         sampling_rate (float, optional):
#         window (float, optional): window width to extract features
#         slide (float, optional): extract features per 'slide'
#         energy (bool, optional): if True, add the energy feature
#         delta (bool, optional): if True, delta features are also extracted
#         deltadelta (bool, optional): if True, double delta features are also extracted
# """
#     with open(join(config_save_path, feature_type + '.conf'), 'w') as f:
#         if audio_file_type not in ['nist', 'wav']:
#             raise ValueError('audio_file_type must be nist or wav.')
#         f.write('SOURCEFORMAT = %s\n' % audio_file_type.upper())
# 
#         # Sampling rate
#         if sampling_rate == 16000:
#             f.write('SOURCERATE = 625\n')
#         elif sampling_rate == 8000:
#             f.write('SOURCERATE = 1250\n')
# 
#         # Target features
#         if feature_type == 'fbank':
#             feature_type = 'FBANK'  # log mel-filter bank channel outputs
#         elif feature_type == 'mfcc':
#             feature_type = 'MFCC'  # mel-frequency cepstral coefficients
#         # elif feature_type == 'linearmelfbank':
#         # feature_type = 'MELSPEC'  # linear mel-filter bank channel outputs
#         else:
#             raise ValueError('feature_type must be fbank or mfcc.')
# 
#         if energy:
#             feature_type += '_E'
#         if delta:
#             feature_type += '_D'
#         if deltadelta:
#             feature_type += '_A'
#         f.write('TARGETKIND = %s\n' % feature_type)
# 
#         # f.write('DELTAWINDOW = 2')
#         # f.write('ACCWINDOW = 2')
# 
#         # Extract features per slide
#         f.write('TARGETRATE = %.1f\n' % (slide * 10000000))
# 
#         f.write('SAVECOMPRESSED = F\n')
#         f.write('SAVEWITHCRC = F\n')
# 
#         # Window size
#         f.write('WINDOWSIZE = %.1f\n' % (window * 10000000))
# 
#         f.write('USEHAMMING = T\n')
#         f.write('PREEMCOEF = 0.97\n')
#         f.write('NUMCHANS = %d\n' % channels)
#         f.write('ENORMALISE = F\n')
#         f.write('ZMEANSOURCE = T\n')


# Path: swbd/make_config.py
import sys
import argparse
from os.path import join, basename
from glob import glob
from utils.util import mkdir_join, mkdir
from utils.inputs.htk import save_config


    args = parser.parse_args()
    htk_save_path = mkdir(args.htk_save_path)

    # HTK settings
    save_config(audio_file_type='wav',
                feature_type=args.feature_type,
                channels=args.channels,
                config_save_path='./config',
                sampling_rate=8000,
                window=args.window,
                slide=args.slide,
                energy=bool(args.energy),
                delta=bool(args.delta),
                deltadelta=bool(args.deltadelta))
    # NOTE: 120-dim features are extracted by default

    # Switchboard
    with open('./config/wav2htk_swbd.scp', 'w') as f:
        for wav_path in glob(join(args.wav_save_path, 'swbd/*.wav')):
            # ex.) wav_path: wav/swbd/*.wav
            save_path = mkdir_join(
                htk_save_path, 'swbd', basename(wav_path).split('.')[0] + '.htk')
            f.write(wav_path + '  ' + save_path + '\n')
            # ex.) htk_path: wav/swbd/*.htk

    # eval2000 (swbd)
    with open('./config/wav2htk_eval2000_swbd.scp', 'w') as f:
        for wav_path in glob(join(args.wav_save_path, 'eval2000/swbd/*.wav')):
            # ex.) wav_path: wav/eval2000_swbd/*.wav

            save_path = mkdir_join(