In [1]:
from fileoperations.fileoperations import getFileNamesInDir
import os
import json
import numpy
from scipy.io import loadmat
from alignedpitchfilter.alignedpitchfilter import correctOctaveErrors

import pdb

In [2]:
datafolder = '../../data/'
pitchfiles, pitchfolders, pitchnames = getFileNamesInDir(datafolder, keyword='predominantMelody.mat')
notefiles = [os.path.join(pf, 'alignedNotes.json') for pf in pitchfolders]

savepitchfiles = [os.path.join(pf, 'correctedPredominantMelody.json') for pf in pitchfolders]
savenotefiles = [os.path.join(pf, 'correctedAlignedNotes.json') for pf in pitchfolders]

In [3]:
for pf, nf, sp, sn in zip(pitchfiles, notefiles, savepitchfiles, savenotefiles):
    pitch = loadmat(pf)
    alignednotes = json.load(open(nf, 'r'))['notes']
    
    pitch_corrected = pitch
    pitch_corrected['pitch'], synthpitch, notes_corrected = correctOctaveErrors(pitch['pitch'], alignednotes)
    
    for key in pitch_corrected.keys():
        try:
            pitch_corrected[key] = pitch_corrected[key].tolist() 
        except:
            pass
    
    json.dump(pitch_corrected, open(sp, 'w'), indent=4)
    json.dump({'notes': notes_corrected}, open(sn, 'w'), indent=4)

In [ ]: