# Path: general/utilsFunctions.py
# def hz2cents(pitchInHz, tonic=261.626):
#     cents = 1200*np.log2(1.0*pitchInHz/tonic)
#     return cents


import essentia.standard as ess
import matplotlib.pyplot as plt
import numpy as np
import json
import vamp
from essentia.streaming import *
from general.parameters import *
from general.utilsFunctions import hz2cents
from os import path
        moving = ess.MovingAverage(size=9)
        pitchInCents = moving(pitchInCents)
        pitchInCents = pitchInCents[4:-4]

    # mean normalization
    # pitchInCents = pitchInCents-np.mean(pitchInCents)

    return pitchInCents,pitchConfidence

def discardPitch(pitch,pitchConfidence,low_threshold_pitch,high_threshold_pitch,threshold_confidence):
    '''
    keep the pitch if confidence > threshold and pitch > low_threshold and pitch < high_threshold
    '''
    pitchNew = []
    pitchConfidenceNew = []
    for ii in range(len(pitch)):
        if pitchConfidence[ii] > threshold_confidence \
                and pitch[ii] >low_threshold_pitch \
                and pitch[ii] < high_threshold_pitch:
            pitchNew.append(pitch[ii])
            pitchConfidenceNew.append(pitchConfidence[ii])
    pitchNew = np.array(pitchNew)
    pitchConfidenceNew = np.array(pitchConfidenceNew)
    return pitchNew,pitchConfidenceNew

def pitchProcessingPyin(filename_wav,sr,movingAve=False):
    loader = ess.MonoLoader(filename=filename_wav, downmix = 'left', sampleRate = sr)
    audio = loader()
    data = vamp.collect(audio, sr, "pyin:pyin", output='smoothedpitchtrack')
    pitch = data['vector'][1]

# Path: general/utilsFunctions.py
# def hz2cents(pitchInHz, tonic=261.626):
#     cents = 1200*np.log2(1.0*pitchInHz/tonic)
#     return cents


# Path: melodicSimilarity/melodicSimi.py
import essentia.standard as ess
import matplotlib.pyplot as plt
import numpy as np
import json
import vamp
from essentia.streaming import *
from general.parameters import *
from general.utilsFunctions import hz2cents
from os import path

        moving = ess.MovingAverage(size=9)
        pitchInCents = moving(pitchInCents)
        pitchInCents = pitchInCents[4:-4]

    # mean normalization
    # pitchInCents = pitchInCents-np.mean(pitchInCents)

    return pitchInCents,pitchConfidence

def discardPitch(pitch,pitchConfidence,low_threshold_pitch,high_threshold_pitch,threshold_confidence):
    '''
    keep the pitch if confidence > threshold and pitch > low_threshold and pitch < high_threshold
    '''
    pitchNew = []
    pitchConfidenceNew = []
    for ii in range(len(pitch)):
        if pitchConfidence[ii] > threshold_confidence \
                and pitch[ii] >low_threshold_pitch \
                and pitch[ii] < high_threshold_pitch:
            pitchNew.append(pitch[ii])
            pitchConfidenceNew.append(pitchConfidence[ii])
    pitchNew = np.array(pitchNew)
    pitchConfidenceNew = np.array(pitchConfidenceNew)
    return pitchNew,pitchConfidenceNew

def pitchProcessingPyin(filename_wav,sr,movingAve=False):
    loader = ess.MonoLoader(filename=filename_wav, downmix = 'left', sampleRate = sr)
    audio = loader()
    data = vamp.collect(audio, sr, "pyin:pyin", output='smoothedpitchtrack')
    pitch = data['vector'][1]

    pitchInCents = hz2cents(pitch)