from math import sqrt
from pickle import FALSE, TRUE
import librosa
import numpy as np
import matplotlib.pyplot as plt
import json

# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

def getNoiseAtRange(buffer, indexStart, indexEnd):
    returnValue=0
    for n in range(indexStart, indexEnd):
        returnValue+=abs(buffer[n]-buffer[n+1])

    returnValue=returnValue/(indexEnd-indexStart)
    return returnValue


def getThresholdFilter(fftBuffer, threshold, indexStart, _indexEnd):
    indexEnd = _indexEnd
    if len(fftBuffer) < indexEnd:
        indexEnd = len(fftBuffer)

    indexEnd = indexEnd-2
    if indexEnd < 0:
        indexEnd = 0

    returnValue=[]
    resultIndex = 0
    startIndex=indexStart
    endIndex=indexStart
    bSearchStart = 1

    while bSearchStart:
        if startIndex >= indexEnd:
            bSearchStart = 0
            break

        if fftBuffer[startIndex] > threshold:
            endIndex=startIndex
            bSearchEnd = 1
            while bSearchEnd:
                endIndex = endIndex + 1
                if endIndex >= indexEnd:
                    bSearchStart = 0
                    bSearchEnd = 0
                    break

                if (fftBuffer[endIndex] < threshold) and (fftBuffer[endIndex+1] < threshold):
                    bSearchEnd = 0
                    elementSum=0
                    for rIndex in range(startIndex,endIndex):
                        #elementSum = elementSum + fftBuffer[rIndex] - threshold
                        elementSum = elementSum + fftBuffer[rIndex]
                    
                    returnValue.append({})
                    #returnValue[resultIndex] = {}
                    returnValue[resultIndex]["startIndex"] = startIndex
                    returnValue[resultIndex]["endIndex"] = endIndex
                    returnValue[resultIndex]["Index"] = (startIndex+endIndex)/2
                    returnValue[resultIndex]["Range"] = endIndex-startIndex
                    v = endIndex-startIndex
                    if v == 0:
                        v=0.5

                    returnValue[resultIndex]["Value"] = elementSum / (v)
                    resultIndex = resultIndex + 1
                    startIndex = endIndex

                #else:
                    #endIndex = endIndex + 1

            #while bSearchEnd

        #if fftBuffer[x] > threshold
        else:
            startIndex = startIndex + 1

    #while bSearchStart
    return returnValue




def getPhoneme(audioFileBuffer, startOffset, Range):
    returnValue={}
    returnValue["fftBuffer"]=[]
    workBuffer = []
    indexEndFile = len(audioFileBuffer)
    for n in range(Range):
        if (startOffset+n) < indexEndFile:
            workBuffer.append(audioFileBuffer[startOffset+n])

    res = np.fft.fft(workBuffer)

    result = []
    for n in range(100):
        vA = res[n].real * res[n].real
        vB = res[n].imag * res[n].imag
        value=sqrt(vA + vB)
        result.append(value)

    result[0]=0
    maximum = max(result)
    scaleBy = 100/maximum

    for n in range(100):
        value = abs(result[n])*scaleBy
        result[n] = value
        returnValue["fftBuffer"].append(result[n])
    
    returnValue["fftRangeOriginal"]=maximum
    returnValue["NoiseRangeLower"] = getNoiseAtRange(result, 00, 50)
    returnValue["NoiseRangeUpper"] = getNoiseAtRange(result, 50, 98)
    threshold=returnValue["NoiseRangeLower"]+5
    #if threshold < 0:
    #    threshold = 0

    returnValue["thresholdFilter"] = getThresholdFilter(result, (threshold), 0, 50)
    return returnValue







# 1. Get the file path to an included audio example
#y, sr = librosa.load('hallowelt.wav')

##removed g,q,k,v
input = ['a','b','c','d','e','f','l','m','n','o','p','r','s','t','u','x']
path="normann_eng\\"


#input = ['a','b','c','d','e','f','l','m','n','o','p','r','s','t','u','x','g','q','k','v']
#path="claudene_eng\\"

#path=""
#input = ['a']
out=[]
f = open("result.js",'w')
for letter in input:
    #name="a.wav"
    name=letter+".wav"
    y, sr = librosa.load(path+name)

    value = getPhoneme(y, 0, 2000)
    #del value["fftBuffer"]

    result={}
    result["letter"]=letter
    result["values"]=value
    out.append(result)

f.write("data=")
f.write(json.dumps(out))
f.close

print('----------------------------\n\n')
print(out)
print('----------------------------\n\n')


exit()

# https://matplotlib.org/stable/plot_types/index
fig,ax = plt.subplots()
ax.stem(result)
plt.title(name)
plt.show()
exit()