MikrotikMusic/conv-midi-mikro.py
2021-05-19 10:37:31 +02:00

123 lines
2.6 KiB
Python

import mido
import time
import winsound
import math
def playNote(freq, dur):
winsound.Beep(freq, dur)
def noteToFreq(note):
return (2**((note-69)/12))*440
channelIsole = [0]
file = mido.MidiFile("laputa.mid")
tempoMult = 1/1
freqMult = 1.3
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):
# print(tabNote[i])
# while i < 5000:
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()