# Path: general/textgridParser.py
# def syllableTextgridExtraction(textgrid_path, recording, tier0, tier1):
# 
#     '''
#     Extract syllable boundary and phoneme boundary from textgrid
#     :param textgrid_path:
#     :param recording:
#     :param tier0: parent tier
#     :param tier1: child tier which should be covered by parent tier
#     :return:
#     nestedPhonemeList, element[0] - syllable, element[1] - a list containing the phoneme of the syllable
#     '''
# 
#     textgrid_file   = os.path.join(textgrid_path,recording+'.TextGrid')
# 
#     syllableList    = textGrid2WordList(textgrid_file, whichTier=tier0)
#     phonemeList     = textGrid2WordList(textgrid_file, whichTier=tier1)
# 
#     # parse syllables of groundtruth
#     nestedPhonemeLists, numSyllables, numPhonemes   = wordListsParseByLines(syllableList, phonemeList)
# 
#     return nestedPhonemeLists, numSyllables, numPhonemes
# 
# Path: general/parameters.py



from os import path
from general.textgridParser import syllableTextgridExtraction
from general.parameters import list_N_frames,hopsize_phoneticSimilarity
from targetAudioProcessing import gmmPhoModelLoad,processFeature,obsMatrixPho
from general.filePath import *
from ParallelLRHMM import ParallelLRHMM
from makeNet import makeNet
from ParallelLRHSMM import ParallelLRHSMM
from makeHSMMNet import makeHSMMNet
from scoreDictFilter import runDictScoreRankNFilter
from scipy.io import wavfile
import sys
import json
import pickle
import numpy as np
import pyximport
    wav_line = wavData[start_frame:end_frame]
    wavfile.write('temp.wav', sampleRate, wav_line)

    # choose feature type as mfcc or mfccBands for GMM or DNN
    if am == 'gmm':
        mfcc_target = processFeature('temp.wav', feature_type='mfcc')
    elif am == 'cnn':
        mfcc_target = processFeature('temp.wav', feature_type='mfccBands2D')

    N_frame         = mfcc_target.shape[0]
    duration_target = (N_frame * hopsize_phoneticSimilarity) / float(sampleRate)

    # only examine on the first N ranking results of melodic similarity
    query_phrase_name   = filename + '_' + str(i)
    dict_score_100      = runDictScoreRankNFilter(dict_score,
                                                 path_melodic_similarity_results,
                                                 query_phrase_name,
                                                 N=100)
    # print 'dict_score_100 len:',len(dict_score_100)

    if method == 'obsMatrix':
        # not used
        obsM = obsMatrixPho(mfcc_target, gmmModel)  # pho gmm
        obsM = np.exp(obsM)
        obsM = obsM / np.sum(obsM, axis=0)
        print obsM
    elif method == 'candidateSynthesizeData':
        # not used
        # choose the template which has the nearest length of the target mfcc
        index_template = np.argmin(np.abs((np.array(list_N_frames) - N_frame)))

# Path: general/textgridParser.py
# def syllableTextgridExtraction(textgrid_path, recording, tier0, tier1):
# 
#     '''
#     Extract syllable boundary and phoneme boundary from textgrid
#     :param textgrid_path:
#     :param recording:
#     :param tier0: parent tier
#     :param tier1: child tier which should be covered by parent tier
#     :return:
#     nestedPhonemeList, element[0] - syllable, element[1] - a list containing the phoneme of the syllable
#     '''
# 
#     textgrid_file   = os.path.join(textgrid_path,recording+'.TextGrid')
# 
#     syllableList    = textGrid2WordList(textgrid_file, whichTier=tier0)
#     phonemeList     = textGrid2WordList(textgrid_file, whichTier=tier1)
# 
#     # parse syllables of groundtruth
#     nestedPhonemeLists, numSyllables, numPhonemes   = wordListsParseByLines(syllableList, phonemeList)
# 
#     return nestedPhonemeLists, numSyllables, numPhonemes
# 
# Path: general/parameters.py



# Path: phoneticSimilarity/generalProcess.py
from os import path
from general.textgridParser import syllableTextgridExtraction
from general.parameters import list_N_frames,hopsize_phoneticSimilarity
from targetAudioProcessing import gmmPhoModelLoad,processFeature,obsMatrixPho
from general.filePath import *
from ParallelLRHMM import ParallelLRHMM
from makeNet import makeNet
from ParallelLRHSMM import ParallelLRHSMM
from makeHSMMNet import makeHSMMNet
from scoreDictFilter import runDictScoreRankNFilter
from scipy.io import wavfile
import sys
import json
import pickle
import numpy as np
import pyximport

    wav_line = wavData[start_frame:end_frame]
    wavfile.write('temp.wav', sampleRate, wav_line)

    # choose feature type as mfcc or mfccBands for GMM or DNN
    if am == 'gmm':
        mfcc_target = processFeature('temp.wav', feature_type='mfcc')
    elif am == 'cnn':
        mfcc_target = processFeature('temp.wav', feature_type='mfccBands2D')

    N_frame         = mfcc_target.shape[0]
    duration_target = (N_frame * hopsize_phoneticSimilarity) / float(sampleRate)

    # only examine on the first N ranking results of melodic similarity
    query_phrase_name   = filename + '_' + str(i)
    dict_score_100      = runDictScoreRankNFilter(dict_score,
                                                 path_melodic_similarity_results,
                                                 query_phrase_name,
                                                 N=100)
    # print 'dict_score_100 len:',len(dict_score_100)

    if method == 'obsMatrix':
        # not used
        obsM = obsMatrixPho(mfcc_target, gmmModel)  # pho gmm
        obsM = np.exp(obsM)
        obsM = obsM / np.sum(obsM, axis=0)
        print obsM
    elif method == 'candidateSynthesizeData':
        # not used
        # choose the template which has the nearest length of the target mfcc
        index_template = np.argmin(np.abs((np.array(list_N_frames) - N_frame)))

        output = open('syllable_mfcc_templates/dic_mfcc_synthesized_' + str(list_N_frames[index_template]) + '.pkl', 'r')