import os,json,pickle,csv from shutil import copyfile def event_times( eventTimeFn ): eventL = [] with open(eventTimeFn,"r") as f: rdr = csv.reader(f) for row in rdr: if row[0] == 'start': beginMs = int(row[1]) elif row[0] == 'key_down': key_downMs = int(row[1]) - beginMs elif row[0] == 'key_up': key_upMs = row[1] eventL.append( [ key_downMs, key_downMs+1000 ] ) return eventL def pulse_lengths( pulseLenFn ): with open(pulseLenFn,'rb') as f: d = pickle.load(f) msL = d['msL'] # note: first posn in table is a multiplier return [ msL[i]*msL[0] for i in range(1,len(msL))] def convert( inDir, outDir ): if not os.path.isdir(outDir): os.mkdir(outDir) for dirStr in os.listdir(inDir): idir = os.path.join( inDir, dirStr ) if os.path.isdir(idir): eventTimeFn = os.path.join( idir, "labels_0.csv" ) eventTimeL = event_times(eventTimeFn) pulseTimeFn = os.path.join( idir, "table_0.pickle") pulseUsL = pulse_lengths( pulseTimeFn ) pitch = idir.split("/")[-1] d = { "pulseUsL":pulseUsL, "pitchL":[ pitch ], "noteDurMs":1000, "pauseDurMs":0, "holdDutyPct":50, "eventTimeL":eventTimeL, "beginMs":0 } odir = os.path.join( outDir, pitch ) if not os.path.isdir(odir): os.mkdir(odir) with open(os.path.join( odir, "seq.json" ),"w") as f: f.write(json.dumps( d )) copyfile( os.path.join(idir,"audio_0.wav"), os.path.join(odir,"audio.wav")) if __name__ == "__main__": inDir = "/home/kevin/temp/picadae_ac_2/full_map" outDir = "/home/kevin/temp/p_ac_3_cvt" convert( inDir, outDir )