# get file name tmp = [] for _,_,f in os.walk(JSON_DIR): tmp = f files = [] for t in tmp: if'hard'in t: files.append(t)
#print(files)
# run werrors = [] jerrors = [] for name in files: print('starting -> '+name) # get notes in file try: withopen(JSON_DIR+name,'r') as f: datas = json.loads(f.read()) except: jerrors.append(name) print('get json error -> '+name) print('------------------') continue speed = datas['speed'] #print(speed) #print(list(datas))
notes = datas['notes'] #print(list(notes[0]))
sounds = [] for n in notes: try: try: t = n['_time'] except: t = 0 for s in n['sounds']: sounds.append({'d':s['d'],'p':s['p'],'v':s['v'],'t':t}) except KeyError: #print('!') None #print(len(sounds)) #print(list(notes[0]['sounds'][0])) try: start = sounds[0]['t'] for s in sounds: s['t'] -= start except: None
# get bpm times = [] for s in sounds: #if s['p']<50: times.append(s['t']) #times = [s['t'] for s in sounds] if(len(times)<2): bpm = 60 else: dt = [] for i inrange(len(times))[1:]: r = times[i]-times[i-1] if r != 0: dt.append(r) gm = pd.Series(data=dt) bpm = 60/gm.mode()[0] while(bpm>200): bpm /= 2 while(bpm<60): bpm *= 2 bpm = round(bpm) #bpm = int(bpm) print('bpm -> '+str(bpm))
# write midi MyMIDI = MIDIFile(1) track = 0 time = 0 bias = float(bpm/60) # Add track name and tempo. MyMIDI.addTrackName(track,time,name+'_deemo') MyMIDI.addTempo(track,time,bpm)
for s in sounds: MyMIDI.addNote(0,0,s['p'],s['t']*bias,s['d']*bias,s['v']) # And write it to disk. try: binfile = open(RESULT_DIR + name.split('.')[0]+'.mid', 'wb') MyMIDI.writeFile(binfile) binfile.close() except: werrors.append(name) print('write file error -> '+name) #continue