import math
import glob
import os
import sys

#os.system("apt-get -y update")
#os.system("apt-get -y dist-upgrade")
#os.system("apt-get -y upgrade")
#os.system("apt-get install -y python-tk")
#os.system("pip install youtube-dl")
#os.system("youtube-dl -U")
#os.system("apt-get install -y ffmpeg")
os.system("rm -rf videos")
os.system("rm -rf Saves")
os.mkdir("Saves")
os.mkdir("videos")

#if not os.path.isdir("aud"):
#    os.mkdir("aud")    
    
    
    

import json
import tensorflow as tf
import numpy as np
#from sklearn.metrics import precision_recall_fscore_support
#import matplotlib
import librosa
import os
import json
import subprocess
import voice
import time
import shelve

SAMPLE_RATE= 5000

d = shelve.open("data")

def variable_summaries(var):
  with tf.name_scope('summaries'):
    mean = tf.reduce_mean(var)
    tf.summary.scalar('mean', mean)
    with tf.name_scope('stddev'):
      stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
    tf.summary.scalar('stddev', stddev)
    tf.summary.scalar('max', tf.reduce_max(var))
    tf.summary.scalar('min', tf.reduce_min(var))
    tf.summary.histogram('histogram', var)


def run(cmd, shell=True):
    return subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell).communicate()[0]

#keys_train = []
#keys_test=[]
#vids = run("youtube-dl --get-id https://www.youtube.com/channel/UCYO_jab_esuFRV4b17AJtAw -i")
#vids = vids.split()
#vids_=vids[0:2]
#print vids_
#for i in vids_:
#    keys_train = keys_train + voice.main(i)
#keys_test.append(voice.main(vids[6]))



#print keys_train
#print keys_test
#d["train"] = keys_train
#d["test"] = keys_test

keys_train = d["train"]
keys_test = d["test"]

for i in range(len(keys_train)):
     keys_train[i][0]=keys_train[i][0]+".flac"
   
for i in range(len(keys_test)):
     keys_test[i][1]=keys_test[i][1]+".flac"
d.close()




def run(cmd, shell=True):
    return subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=shell).communicate()[0]



def extract_feature(file_name):
    X, sample_rate = librosa.load(file_name)
    stft = np.abs(librosa.stft(X))
    mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
    #chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
    mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
    contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
    #tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)
    #return mfccs,chroma,mel,contrast,tonnetz
    return mfccs,mel,contrast






def join_sound(file_in, file_out,size=300, mode=0):
#    print mode
    if mode == 0:
        
        inp,_ = librosa.load(file_in, sr=SAMPLE_RATE)
        dur=librosa.get_duration(inp,SAMPLE_RATE)
        x,_ = librosa.load(file_out,sr=SAMPLE_RATE)
        #print x
        new_dur= librosa.get_duration(x,sr=SAMPLE_RATE)

        out, _ = librosa.load(file_out, sr=int(SAMPLE_RATE*dur/new_dur))
        print out.shape
        print inp.shape
        if(len(inp)>len(out)):
            inp=inp[0:len(out)]
        else:
            out=out[0:len(inp)]
        #print len(out)
        #print len(inp)



    else:
        inp = file_in
        out = file_out

    
    for i in range(len(inp)):
        inp[i]= (inp[i]+1)/2

    for i in range(len(out)):
        out[i]= (out[i]+1)/2
    
    out = out[0:int(size*math.floor(len(out)/size))]
    inp = inp[0:int(size*math.floor(len(inp)/size))]
    inp=np.split(inp,len(inp)/size)
    out=np.split(out,len(out)/size)

    newInp = []
    newOut = []    
    index = 0
    for i in out:
        print i 
        for j in range(len(i)):
            print "j "+ str(j)
            newOut.append(i.tolist())
            newOut[-1].append(1/(j+1))
            newInp.append(inp[index][j])
        index+=1
            
    
#    for i in range(len(inp)):
#        inp[i]=np.append(inp[i],1/(i+1))
#        inp[i]=np.append(inp[i],1/(len(inp)+1))

    for i in range(len(out)):
        out[i]=np.append(out[i],1/(i+1))
        out[i]=np.append(out[i],1/(len(out)+1))
    

    
    return np.array(newOut[:-1]),np.vstack(newInp[:-1])
    
#inp,out = join_sound("aud/" + keys_train[10][0],"aud/" + keys_train[10][1])
#print inp
#print out

#print inp.shape
#print out.shape

#raise

def parse_audio_files(files,dir,ind=None):
    if ind is None:
        ind = len(files)
    inputs, outputs = [], []
    
    #print files
    #print len(files[0:ind])
    for fn in files[0:ind]:
        print "in"
        if len(fn) == 2: