import mido import time import winsound import math #plays a Note through windows def playNote(freq, dur): winsound.Beep(freq, dur) #convert a note to a frequency def noteToFreq(note): return (2**((note-69)/12))*440 #select which midi channel you want to convert. Can be multiple, but it often doesn't work. Not like anything else does. channelIsole = [0] # set the midi file here file = mido.MidiFile("MIDIs/laputa.mid") # use this to modify the tempo. tempoMult = 1/1 # use this to modify the key. I've found out my routerboards is less tone deaf with a higher key. Mileage will vary. freqMult = 1.3 # start of the shitty code tabNote = [msg for msg in file] # print(f"tab lenght is {len(tabNote)}") note1 = tabNote[0] note2 = tabNote[0] i = 0 isFound = False biMode = False out = open("out.txt",'w') while i < len(tabNote): while((not isFound) and i < len(tabNote)): # print("b1", i, tabNote[i]) if tabNote[i].type == "note_on": if(tabNote[i].channel in channelIsole): note1 = tabNote[i] isFound = True i += 1 isFound = False # print("found note 1", i-1, note1) while((not isFound) and i < len(tabNote)): # print("b2", i, tabNote[i]) if tabNote[i].type == "note_off": if(tabNote[i].channel in channelIsole ): note2 = tabNote[i] isFound = True biMode = True elif tabNote[i].type == "note_on": if( tabNote[i].channel in channelIsole): biMode = False isFound = True i += 1 isFound = False if(biMode): # print("######") # print("lol",note1) # print("mdr",note2) # print("######") freq = int(noteToFreq(note1.note)) duration = int((note1.time + note2.time) *1000) # in ms if(duration == 0): duration = 150 # print(freq, duration) #if(msg.time>0.1): # playNote(freq, int(duration*tempoMult)) out.write(f":beep frequency={int(freq*freqMult)} length={int(duration*tempoMult)}ms;\n") out.write(f":delay {int(note2.time/8*1000 + duration*tempoMult)}ms;\n") # time.sleep((note2.time/8)*1000) elif note1.type == "note_on": # break # print(note1) freq = int(noteToFreq(note1.note)) duration = int((note1.time) *1000) # in ms if(duration == 0): duration = 15 # print(freq, duration) # if(msg.time>0.1): # out.write(f":beep frequency={freq} length={int(duration*tempoMult)}ms;\n") # out.write(f":delay {int(note1.time/8*1000 + duration*tempoMult)}ms;\n") # playNote(freq, duration) out.write(f":beep frequency={int(freq*freqMult)} length={duration}ms;\n") out.write(f":delay {duration}ms;\n") #time.sleep(note1.time/8) out.close()