def wrapperSalientContourFeatures(root_dir, audioExt='.mp3'):
    """
  """
    filenames = BP.GetFileNamesInDir(root_dir, '.mp3')
    mbid_to_ragaId_map = get_raga_id(
        '/home/kaustuv/Documents/ISMIR-2016/MelodyEvolution/Dataset/mbid_ragaid_Hindustani_300_selected.tsv'
    )
    raga_info = get_vadi_samvadi(
        '/home/kaustuv/Documents/ISMIR-2016/MelodyEvolution/Dataset/vadiSamvadiMapping.tsv'
    )
    '''
  for filename in filenames[:]:
    print "Processing file %s" %filename
    
    fname, ext = os.path.splitext(filename)
    
    endTime = readValidVistarRegion(fname)
    dist_mtx, track_max = getSvaraDistInBPsCum(filename, endTime, bphraseExt = '.bphrases', transExt = '.transcription', windowSize = 10, hopSize = 1)
    track_max_pklfile = fname + '_svaraContour.pkl'
    pickle.dump(track_max, open(track_max_pklfile,'w'))
    
    mbid = get_mbid_from_mp3(filename)    
    raga_id = mbid_to_ragaId_map[mbid]
    
    feature_pklfile = fname + '_contourNormFeatures.pkl'
    extractFeaturesNoteEvolutionContour(track_max_pklfile, feature_pklfile, raga_info[raga_id])
  '''
    cnt = 0
    for filename in filenames[:]:
        print "Processing file %s" % filename

        fname, ext = os.path.splitext(filename)
        feature_pklfile = fname + '_contourNormFeatures.pkl'

        output = pickle.load(open(feature_pklfile, 'r'))
        mbid = get_mbid_from_mp3(filename)
        raga_id = mbid_to_ragaId_map[mbid]

        feature_order = [
            output['F1'], output['F2'], output['F3'], output['F4'],
            output['F5'], output['F6'], output['F7'], output['F8'],
            output['F9'], output['F10'], output['F11'], output['F12'],
            output['F13'], output['F14'], output['F15'], output['F16'],
            output['F17'], output['F18'], output['F19'], output['F20'], raga_id
        ]

        if cnt == 0:
            feature_overall = np.array([feature_order])
        else:
            feature_overall = np.vstack((feature_overall, feature_order))
        cnt += 1

    print feature_overall
    feature_filename = os.path.join(root_dir, 'feature_for_weka.csv')
    fid = open(feature_filename, 'w')
    for row in feature_overall:
        row = [str(r) for r in row]
        fid.write("%s," * len(row) % tuple(row))
        fid.write('\n')
    fid.close()
def batchProcessGaussianFilterPitchContour(root_dir,
                                           searchExt='.wav',
                                           pitchExt='.tpe',
                                           tonicExt='.tonic',
                                           outExt='.tpeMedFilt',
                                           filtDur=.05,
                                           gaussSigma=0.03):
    """
  Simple wrapper for batch processing of gaussian filtering
  """
    filenames = BP.GetFileNamesInDir(root_dir, searchExt)
    for filename in filenames:
        fname, ext = os.path.splitext(filename)
        timePitch = np.loadtxt(fname + pitchExt)
        hopSize = timePitch[1, 0] - timePitch[0, 0]
        tonic = float(np.loadtxt(fname + tonicExt))

        pitchOut = gaussianFilterPitchContour(timePitch[:, 1],
                                              tonic=tonic,
                                              hopSize=hopSize,
                                              filtLen=filtDur,
                                              sigma=gaussSigma)

    timePitch[:, 1] = pitchOut

    np.savetxt(fname + outExt, timePitch, delimiter="\t")
def obtainPitchTonicInDB(root_dir):

    audiofiles = BP.GetFileNamesInDir(root_dir, 'mp3')
    cnt = 0
    for audiofile in audiofiles:
        fname, ext = os.path.splitext(audiofile)

        pithcFile = fname + '.pitch'
        tonicFile = fname + '.tonic'

        if not os.path.exists(pithcFile):

            try:
                mbid = easyid3.ID3(
                    audiofile)['UFID:http://musicbrainz.org'].data
                pitchUrl = 'http://dunya.compmusic.upf.edu/document/by-id/' + mbid + '/pitch?v=0.4&subtype=pitch'
                tonicUrl = 'http://dunya.compmusic.upf.edu/document/by-id/' + mbid + '/ctonic?v=0.2&subtype=tonic'
                pitchData = np.array(eval(requests.get(pitchUrl).content))
                tonicData = float(requests.get(tonicUrl).content)
                np.savetxt(pithcFile, pitchData, delimiter="\t", fmt='%0.5f')
                np.savetxt(tonicFile,
                           np.array([tonicData]),
                           delimiter="\t",
                           fmt='%0.5f')
            except:
                cnt = cnt + 1
                print audiofile

    print cnt
def generateDummyTaniSegments(root_dir):

    files = BP.GetFileNamesInDir(root_dir, '.mp3')

    for f in files:
        fname, ext = os.path.splitext(f)
        fid = open(fname + '.tablaSec', "w")
        fid.close()
示例#5
0
def batchProcessDumpSubSeqFixedDuration(root_dir,
                                        paramFile,
                                        fileExtFile,
                                        searchExt='.mp3'):

    filenames = BP.GetFileNamesInDir(root_dir, searchExt)

    for ii, filename in enumerate(filenames):
        fname, ext = os.path.splitext(filename)
        print "Processing file %d of %d" % (ii + 1, len(filenames))
        cmd1 = './dumpSubSeqFixedDuration_O3 ' + '"' + fname + '"' + ' ' + paramFile + ' ' + fileExtFile + ' 1'
        os.system(cmd1)
示例#6
0
def createPatternMatchTable(root_dir,
                            logFile,
                            motifDiscExt,
                            motifSearchExt,
                            motifSearchMappExt,
                            myDatabase=''):

    t1 = time.time()
    if not isinstance(motifSearchExt, list):
        print "motifSearchExt: Expected a list"
        return
    #important stuff
    #motifDiscExt = '.disPatt_2s_config2'
    #motifSearchExt = ['.srhPatts1SqEuclidean', '.srhPatts1CityBlock', '.srhPatts1ShiftCityBlock', '.srhPatts1ShiftLinExp']
    #motifSearchExt = ['.srchPatt_2s_config2SqEuclidean']
    mainDistanceIndex = 0  # this is basically index of extions stored in motifSearchExt which is the main distance files
    #motifSearchMappExt = '.srchMap_2s_config2'

    #lets get all the files in the root_dir
    fileNames = BP.GetFileNamesInDir(root_dir, motifDiscExt)

    #commands for doing different tasks with psql

    cmd1 = "INSERT INTO pattern (file_id, start_time, end_time, isseed) VALUES (%ld, %f, %f, %d) RETURNING id"  #storing seed motifs
    cmd6 = "INSERT INTO match (source_id, target_id, distance, version) VALUES (%ld, %ld, %f, %d)"

    #cmd3 = "INSERT INTO pattern (id, pair_id) VALUES (%ld, %ld)"#storing seed motifs
    #cmd4 = "SELECT currval('pattern_id_seq')"
    cmd5 = "UPDATE pattern SET pair_id = %ld WHERE id = %ld"

    cmd2 = "SELECT id FROM file WHERE mbid = '%s'"  #searching file id
    cmd7 = "SELECT id FROM file WHERE filename = $$%s$$"  #searching file id

    cmd8 = "create index on pattern(file_id)"
    cmd9 = "create index on pattern(pair_id)"
    cmd10 = "create index on pattern(isseed)"
    cmd11 = "create index on match(source_id)"
    cmd12 = "create index on match(target_id)"
    cmd13 = "create index on match(version)"

    #rety the connection
    con = None
    try:
        con = psy.connect(database=myDatabase, user=myUser)
        cur = con.cursor()
        print "Successfully connected to %s database" % (myDatabase)

    except psy.DatabaseError, e:
        print 'Error %s' % e
        if con:
            con.close()
        sys.exit(1)
def batchProcessPatternDataDump(root_dir, exePath):
    """
    this is a batch function for dumping data for the extracted pattern for each file. it uses a C code which does exactly the same preprocessing as was done at the time of pattern extraction.
    """

    filenames = BP.GetFileNamesInDir(root_dir, '.allPatternsInfoConf2')

    for ii, filename in enumerate(filenames):
        print "Processing %d out of %d file\n" % (ii + 1, len(filenames))
        print "%s\n" % (filename)
        fname, ext = os.path.splitext(filename)
        cmd = exePath + '/' + 'DumpTimeSeries_O3 ' + "\"" + fname + "\"" + " '.pitch' '.tonic' '.allPatternsInfoConf2' '.patternDataConf1' 2.0 5 5 1"
        os.system(cmd)
示例#8
0
 def generateLinearDataset(self, root_dir, output_dir, pitchExt, tonicExt, downsampleFactor, min_nyas_dur=-1):
     
     pitch=np.array([])
     timeInfo=np.array([])
     fileInfo={}
     
     filenames = BP.GetFileNamesInDir(root_dir,pitchExt)
     
     for filename in filenames:
         fname, ext = os.path.splitext(filename)
         #reading pitch and tonic data
         pitchData,timeStamps,pHop = BPO.readPitchFile(fname+pitchExt)
         tonic = np.loadtxt(open(fname+tonicExt,"r"))
         pCents = BPO.PitchHz2Cents(pitchData, tonic)
         
         
         #some preprocessing
         
         #removing flat regions
         if (min_nyas_dur>0):
             msObj = MS.nyasSegmentation()
             msObj.ComputeNyasCandidates(pitchData, tonic.tolist(), pHop)
             msObj.FilterNyasCandidates(min_nyas_duration=min_nyas_dur)
         
             for swar in msObj.nyasInfo.keys():
                 for seg in msObj.nyasInfo[swar]:
                     pCents[seg[0]:seg[1]]=-5000
         
         
         #downsampling
         factor=downsampleFactor
         pCents, pHop, timeStamps = BPO.downsamplesPitchData(pCents,pHop,timeStamps, factor)
         
         
         
         #removing silence regions
         ind_silence = np.where(pCents<-4000)[0] ###Please correct this silence condition once log eps is used
         pCents = np.delete(pCents,ind_silence)
         timeStamps = np.delete(timeStamps,ind_silence)
         
         #accumulating
         pitch = np.append(pitch, pCents)
         timeInfo = np.append(timeInfo, timeStamps)
         fileInfo[filename]= [timeInfo.size-timeStamps.size, timeInfo.size]
         
     np.savetxt(output_dir+'/'+'AggPitch.txt', pitch, fmt='%.2f')
     np.savetxt(output_dir+'/'+'AggTime.txt', timeInfo, fmt='%.2f')
     stream = file(output_dir+'/'+'fileInfo.yaml','w')
     yaml.dump(fileInfo, stream)
     
def extractSoloPercussionBATCHPROC(root_dir, segExt, modelFile, normFile, frameDur, hopDur, aggDur, medianDur=20, overwrite_files = 0):
    
    
    audiofiles = BP.GetFileNamesInDir(root_dir,'mp3')
    
    for ii, audiofile in enumerate(audiofiles):
        print "pricessing %d of %d files"%(ii+1, len(audiofiles))
        print "File being processed %s\n"%audiofile
        fname, ext = os.path.splitext(audiofile)
        segFile = fname + segExt
        if os.path.isfile(segFile) and not overwrite_files:
            	print "################# THIS FILE IS ALREADY PROCESSED#####################"
		continue
        extractSoloPercussion(audiofile, segFile, modelFile, normFile, frameDur, hopDur, aggDur, medianDur)
示例#10
0
def ComputeDatabaseStats(root_dir):
    # this function computes the stats of all the phrase find in a root directory

    #get all the file list in a given directory of a specific type
    file_names = BP.GetFileNamesInDir(root_dir, filter=".textgrid")
    Phrase_Stats = {}
    #print "files detected ="+str(len(file_names))

    for file_name in file_names:

        phrase_dict = TextGrid2Dict(file_name, "Kaustuv_Anotations_Simple")
        Phrase_Stats = AppendPhraseStats(phrase_dict, Phrase_Stats)

    return Phrase_Stats
示例#11
0
def getSeedPatternDistancesTXT(root_dir,
                               seedExt='.2s25Motif_CONF1',
                               distFile='seedDistances'):

    filenames = BP.GetFileNamesInDir(root_dir, seedExt)
    distArray = []
    for ii, filename in enumerate(filenames):
        #print "processing %d of %d files"%(ii+1, len(filenames))
        seedData = np.loadtxt(filename)
        indValid = np.where(seedData[:, 4] < 99999999999999999999)[0]
        distArray.extend(seedData[indValid, 4].tolist())

    #np.save(distFile, distArray)

    return distArray
def batchProcessPitchPostProcess(root_dir,
                                 searchExt='.wav',
                                 pitchExt='.tpe',
                                 tonicExt='.tonic',
                                 outExt='.tpeOctCorr',
                                 filtDurMed=0.05,
                                 filtDurGaus=0.05,
                                 winDurOctCorr=0.3,
                                 sigmaGauss=0.025,
                                 fillSilDur=0.25,
                                 interpAllSil=False,
                                 upSampleFactor=1,
                                 over_write=1):
    """
  Wrapper to batch process pitch post processing
  """
    filenames = BP.GetFileNamesInDir(root_dir, searchExt)
    for filename in filenames:
        print "Processing file %s" % filename
        fname, ext = os.path.splitext(filename)
        try:
            timePitch = np.loadtxt(fname + pitchExt)
        except:
            continue
        hopSize = timePitch[1, 0] - timePitch[0, 0]
        try:
            tonic = float(np.loadtxt(fname + tonicExt))
        except:
            tonic = 207.65
        if os.path.isfile(fname + outExt) and over_write == 0:
            continue
        pitchOut = postProcessPitchSequence(timePitch[:, 1],
                                            tonic=tonic,
                                            hopSize=hopSize,
                                            filtDurMed=filtDurMed,
                                            filtDurGaus=filtDurGaus,
                                            winDurOctCorr=winDurOctCorr,
                                            sigmaGauss=sigmaGauss,
                                            fillSilDur=fillSilDur,
                                            interpAllSil=interpAllSil,
                                            upSampleFactor=upSampleFactor)
        TStamps = float(hopSize) * float(upSampleFactor) * np.arange(
            pitchOut.size)
        timePitch = np.array([TStamps, pitchOut]).transpose()
        np.savetxt(fname + outExt, timePitch, delimiter="\t", fmt='%.7f')
def batchProc(root_dir,
              audioExt='.mp3',
              pitchExt='.pitchSilIntrpPP',
              tonicExt='.tonicFine'):

    filenames = BP.GetFileNamesInDir(root_dir, '.mp3')
    segObj = seg.melodySegmentation()

    #fig = plt.figure(figsize=(15,10), dpi=80)

    for filename in filenames[:]:
        print "Processing file %s" % filename

        #======================
        ## This is done for all
        #======================

        fname, ext = os.path.splitext(filename)
        pitch, time, Hop = BO.readPitchFile(fname + pitchExt)
        tonic = np.loadtxt(fname + tonicExt)
        pcents = BO.PitchHz2Cents(pitch, tonic)
        pdata = (time, pcents, Hop)

        ## Extract Breath Phrases
        #------------------------
        breathPhrases = findBreathPhrases(segObj, fname, pcents, Hop)

        ## Histogram processing to extract note locations
        #------------------------------------------------
        svaraSemitone, ignoreNotes = findValidSvaras(pitch, tonic)
        #print svaraSemitone, ignoreNotes
        print "Notes being ignored are: %s" % ignoreNotes

        ## Read valid region for evolution
        #---------------------------------
        #endTime = readValidVistarRegion(fname)

        ## Svara transcription
        #---------------------
        transcription = transcribePitch(fname, pdata, ignoreNotes)
        #print transcription

        print "-------\nDone !!\n-------"
        '''
def getNoteDurDistPerRaga(root_dir,
                          audioExt='.mp3',
                          pitchExt='.pitchSilIntrpPP',
                          tonicExt='.tonicFine'):
    """
  """
    filenames = BP.GetFileNamesInDir(root_dir, '.mp3')

    for filename in filenames[:]:
        print "Processing file %s" % filename

        fname, ext = os.path.splitext(filename)
        endTime = readValidVistarRegion(fname)
        print endTime

        getDurDistPerNotePerFile(filename,
                                 endTime,
                                 bphraseExt='.bphrases',
                                 transExt='.transcription')
    plt.show()
示例#15
0
def update_tonic_in_db(root_dir,
                       tonicExt='.tonicFine',
                       myDatabase='',
                       myUser=''):

    audioFiles = BP.GetFileNamesInDir(root_dir, '.mp3')
    cmd = u"update file set tonic = %f where mbid='%s'"
    try:
        con = psy.connect(database=myDatabase, user=myUser)
        cur = con.cursor()
        for audiofile in audioFiles:
            print audiofile
            mbid = fetchMBID(audiofile)
            fname, ext = os.path.splitext(audiofile)
            tonic = np.loadtxt(fname + tonicExt)

            cur.execute(cmd % (tonic, mbid))

        con.commit()
    except:
        pass
示例#16
0
def createFileTable(root_dir, myDatabase, filterExt='.mp3'):

    con = None
    audioFiles = BP.GetFileNamesInDir(root_dir, filterExt)
    dataDump = []
    query = "INSERT INTO file (filename, mbid) VALUES (%s, %s)"
    cmd1 = "create index on file(filename)"
    cmd2 = "create index on file(mbid)"
    cmd3 = "create index on file(id)"

    for audiofile in audioFiles:
        filename, ext = os.path.splitext(audiofile)
        audiofile = filename + '.mp3'
        try:
            mbid = fetchMBID(audiofile)
        except:
            print "MBID not embedded in file %s" % audiofile
        else:
            audiofile_WOPre = stripPrefix(audiofile)
            dataDump.append((audiofile_WOPre, mbid))

    print "Dump array successfully created"
    try:
        con = psy.connect(database=myDatabase, user=myUser)
        cur = con.cursor()
        cur.executemany(query, dataDump)
        cur.execute(cmd1)
        cur.execute(cmd2)
        cur.execute(cmd3)
        con.commit()
        print "Successfully updated file table in %s database" % (myDatabase)

    except psy.DatabaseError, e:
        print 'Error %s' % e
        if con:
            con.rollback()
            con.close()
        sys.exit(1)
示例#17
0
def batchProc(root_dir,
              audioExt='.mp3',
              pitchExt='.pitchSilIntrpPP',
              transExt='.transcription',
              outputExt='.seg'):

    filenames = BP.GetFileNamesInDir(root_dir, '.mp3')

    for filename in filenames[:]:
        print "Processing file %s" % filename

        #======================
        ## This is done for all
        #======================

        fname, ext = os.path.splitext(filename)
        pitch_file = fname + pitchExt
        transcription_file = fname + transExt
        output_file = fname + outputExt

        # Dump segentation file
        #----------------------
        generate_segmentation_file(pitch_file, transcription_file, output_file)
def combinePatternInfoAndDataFiles(root_dir, infoExt, dataExt, outInfoFile,
                                   outDataFile):

    filenames = BP.GetFileNamesInDir(root_dir, infoExt)

    for ii, filename in enumerate(filenames):
        print "Processing %d out of %d file\n" % (ii + 1, len(filenames))
        print "%s\n" % (filename)

        fid1 = open(outInfoFile, "a")
        fid2 = open(outDataFile, "ab")

        fname, ext = os.path.splitext(filename)

        Info = np.loadtxt(fname + infoExt)
        np.savetxt(fid1, Info, fmt=['%ld', '%f', '%f'], delimiter='\t')

        data = open(fname + dataExt, 'rb').read()

        fid2.write(data)

        fid1.close()
        fid2.close()
示例#19
0
def BatchProcessGetPhaseSpaceEmbPitchSignal(root_dir,
                                            pitch_ext,
                                            out_ext,
                                            tonic_ext,
                                            delay_tap,
                                            oct_fold,
                                            overwrite=1):

    filenames = BP.GetFileNamesInDir(root_dir, '.mp3')

    for filename in filenames:
        fname, ext = os.path.splitext(filename)
        tonic = np.loadtxt(fname + tonic_ext)
        print "Processing %s" % fname
        if os.path.isfile(fname + out_ext) and overwrite == 0:
            continue
        getPhaseSpaceEmbPitchSignal(fname + pitch_ext,
                                    delay_tap,
                                    tonic,
                                    min_pitch=60,
                                    resolution=10,
                                    range_cents=(0, 1200),
                                    out_ext=out_ext,
                                    oct_fold=oct_fold)
def batchProcessRemoveSpuriousPitchJumps(root_dir,
                                         searchExt='.wav',
                                         pitchExt='.tpe',
                                         tonicExt='.tonic',
                                         outExt='.tpeOctCorr',
                                         filtDur=.3):
    """
  Simple wrapper for batch processing of removing spurious pitch errors
  """
    filenames = BP.GetFileNamesInDir(root_dir, searchExt)
    for filename in filenames:
        fname, ext = os.path.splitext(filename)
        timePitch = np.loadtxt(fname + pitchExt)
        hopSize = timePitch[1, 0] - timePitch[0, 0]
        tonic = float(np.loadtxt(fname + tonicExt))

        pitchOut = removeSpuriousPitchJumps(timePitch[:, 1],
                                            tonic=tonic,
                                            hopSize=hopSize,
                                            filtLen=filtDur)

    timePitch[:, 1] = pitchOut

    np.savetxt(fname + outExt, timePitch, delimiter="\t")
示例#21
0
 def generateSubsequenceDataset(self, root_dir, output_dir, pitchExt, tonicExt, downsampleFactor, windowLength, combineData = 0, writeBinary = 1, meanNormalize = 0, flatnessThreshold=0.8, binsPOctave=120, fixPointData=1):
     
     
     if combineData:
         timeInfo=np.array([])
         fileInfo={}
     
     #obtaining all the files in the directory
     filenames = BP.GetFileNamesInDir(root_dir,pitchExt)
     
     # iterating over each file
     for kk, filename in enumerate(filenames):
         
         #separate file name from extension
         fname, ext = os.path.splitext(filename)
         
         audiofile = fname.split('/')[-1]
         
         #if data is not to be combined, then output goes into individual directories (dirname = filename)
         if not combineData:
             out_dir = output_dir + '/' + audiofile
             #creating directory if doesn't exist
             if not os.path.isdir(out_dir):
                 os.makedirs(out_dir)
         
         #reading pitch and tonic data
         pitchData,timeStamps,pHop = BPO.readPitchFile(fname+pitchExt)
         tonic = np.loadtxt(open(fname+tonicExt,"r"))            
         
         #Convert the pitch values into cents, Note that we add a offset of 1 octave to make everything positive, assuming that pitch wont go below one octave to tonic
         pCents=np.round(binsPOctave*np.log2((eps+pitchData)/tonic)).astype(np.int) + binsPOctave 
         
         
         #downsampling pitch data
         factor=downsampleFactor
         pCents, pHop, timeStamps = BPO.downsamplesPitchData(pCents,pHop,timeStamps, factor)
         
         
         #removing silence regions from the pitch sequence
         ind_silence = np.where(pCents<0)[0] ###Please correct this silence condition once log eps is used
         pCents = np.delete(pCents,ind_silence)
         timeStamps = np.delete(timeStamps,ind_silence)
         
         #computing all the indices which have non flat region of pitch (binsPOctave is an input to decide threshold, thats it!!)
         nonFlatIndexes = self.nonFlatIndexes(pCents, pHop,binsPOctave)
         
         #making an array which will be 1 for nonflat and 0 for flat regions
         flatNonflat = np.zeros(pCents.shape[0])
         flatNonflat[nonFlatIndexes]=1
         
         #given the windowLength or motigLength compute how many samples do they correspond to
         windowSamples = int(np.round(windowLength/pHop))
         
         #to create a matlab buffer like thing, we do the following
         row = np.array([np.arange(windowSamples)])
         col = np.array([np.arange(pCents.size-windowSamples)])
         col = np.transpose(col)
         col2 = copy.deepcopy(col)
         col2[:]=1
         ind = row*col2 + col
         
         
         #so after creating matrix where each row is a subsequence, look which subsequence to reject based on flatness threshold
         mtx = flatNonflat[ind]            
         mean_array = np.mean(mtx,axis=1)            
         ind_Invalid = np.where(mean_array<flatnessThreshold)[0]
         
         ind = np.delete(ind,[ind_Invalid],axis=0)
         
         #finally obtain pitch matrix and timestamp array
         mtx = pCents[ind]
         timeStamps = timeStamps[ind[:,0]]
         
         ###TODO impement mean normalizatoin
         
         ### Adding a crucial check!!
         if timeStamps.shape[0] != mtx.shape[0]:
             print filename
         
         if combineData:#keep on appending the data to combine it
             
             if kk==0:
                 pitch = copy.deepcopy(mtx)
                 timeInfo = copy.deepcopy(timeStamps)
             else:
                 pitch = np.append(pitch, mtx,axis=0)
                 timeInfo = np.append(timeInfo, timeStamps)
             fileInfo[filename]= [timeInfo.size-timeStamps.size, timeInfo.size]
         
         
         else:#just dump for each file
             
             if writeBinary:
                 if fixPointData:
                     mtx.astype(np.uint32).tofile(out_dir+'/'+ audiofile +'.pitchSubDBbin')
                 else:
                     mtx.astype(np.float).tofile(out_dir+'/'+ audiofile +'.pitchSubDBbin')
                     
                 timeStamps.astype(np.float).tofile(out_dir+'/'+ audiofile +'.timeSubDBbin')
             else:
                 np.savetxt(out_dir+'/'+ audiofile +'.pitchSubDBtxt', mtx , fmt='%d')
                 np.savetxt(out_dir+'/'+ audiofile +'.timeSubDBtxt', timeStamps, fmt='%.3f')
             
         if combineData:
             #another crucial check at each step
             if pitch.shape[0] != timeInfo.shape[0]:
                 print filename
         
     if combineData:
         
         if writeBinary:
             if fixPointData:
                     pitch.astype(np.uint32).tofile(output_dir+'/'+'AggPitch.bin')
             else:
                     pitch.astype(np.float).tofile(output_dir+'/'+'AggPitch.bin')
             timeInfo.astype(np.float).tofile(output_dir+'/'+'AggTime.bin')
         else:
             np.savetxt(output_dir+'/'+'AggPitch.txt', pitch , fmt='%d')
             np.savetxt(output_dir+'/'+'AggTime.txt', timeInfo, fmt='%.3f')
         
         stream = file(output_dir+'/'+'fileInfo.yaml','w')
         yaml.dump(fileInfo, stream)
         
         print "Total number of time series : " + str(pitch.shape[0])
         print "Length of single Sub sequence : " + str(pitch.shape[1])
def generateBinaryAggMFCCARFF(class1Folder, class2Folder, class1, class2, arffFile, frameDur, hopDur, aggDur):
    """
    This function generates an arff file of MFCC features for two classes class1 and class2 for which MFCCs are extracted from the audio files kept in appropriate folders (first two args)
    mappFile store audiofile names and number of features extracted from that file
    """
    fname,ext = os.path.splitext(arffFile)
    mappFile = fname + '.mappFileFeat'
    
    #features extracted and features to use
    features = featuresExtracted()
    features2Use = featuresUsed()
    
    #array of class labels
    classes = [class1, class2]
    
    #finding index of features to be used in classification
    ind_features = []
    for feat in features2Use:
        ind_features.append(features.index(feat))

    #selecting those features
    features = np.array(features)
    features = features[ind_features]
    features = features.tolist()
    
    #writing header for arff file
    fid = open(arffFile,'w')
    
    fid.write("@relation 'ToWeka_sectionSegmentation'\n")
    for feature in features:
        fid.write("@attribute %s numeric\n"%feature)
    fid.write("@attribute class  {")
    for clas in classes:
        fid.write("%s,\t"%clas)
    fid.write("}\n")
    fid.write("@data\n")
    fid.close()

    fidMapp = open(mappFile,'w')
    fidMapp.close()
    
    
    #loading the audio file into an array
    ML =es.MonoLoader()
    
    
    #start extracting features and write
    class1audiofiles = BP.GetFileNamesInDir(class1Folder,'wav')
    for audiofile in class1audiofiles:
        fid = open(arffFile,'a')
        fidMapp = open(mappFile,'a')
        print audiofile
        #computing dynamically fs, aggLen based on provided hop size
        fs=float(es.MetadataReader(filename=audiofile)()[9])
        framesize = int(np.round(fs*frameDur))
        if framesize%2 ==1:
            framesize=framesize+1
        hopsize = int(np.round(fs*hopDur))
        aggLen = int(np.round(aggDur*fs/hopsize))

        ML.configure(filename = audiofile)
        audio_in = ML()
        featuresAll = feature_extractor_standard(audio_in, framesize, hopsize, aggLen)
        featuresAll = featuresAll[:,ind_features]
        for ftr in featuresAll:
            fid.write("%f,"*len(features)%tuple(ftr))
            fid.write("%s\n"%classes[0])
        fidMapp.write("%s\t%d\n"%(audiofile, featuresAll.shape[0]))
        del featuresAll
        fid.close()
        fidMapp.close()
        
    
    class2audiofiles = BP.GetFileNamesInDir(class2Folder,'wav')
    for audiofile in class2audiofiles:
        fid = open(arffFile,'a')
        fidMapp = open(mappFile,'a')
        print audiofile
        #computing dynamically fs, aggLen based on provided hop size
        fs=float(es.MetadataReader(filename=audiofile)()[9])
        framesize = int(np.round(fs*frameDur))
        if framesize%2 ==1:
            framesize=framesize+1
        hopsize = int(np.round(fs*hopDur))
        aggLen = int(np.round(aggDur*fs/hopsize))
        
        ML.configure(filename = audiofile)
        audio_in = ML()
        featuresAll = feature_extractor_standard(audio_in, framesize, hopsize, aggLen)
        featuresAll = featuresAll[:,ind_features]
        for ftr in featuresAll:
            fid.write("%f,"*len(features)%tuple(ftr))
            fid.write("%s\n"%classes[1])
        fidMapp.write("%s\t%d\n"%(audiofile, featuresAll.shape[0]))
        del featuresAll
        fid.close()
        fidMapp.close()