4

I need help in the following: I am designing a new music notation. I want to read a midi file and get a list that contains every note/start-stop-time/track. Desired result:

[[60, 0, 0.25, 1], [62, 0.25, 0.50, 1]]# the format is [note, start-time, stop-time, miditrack]

*update 1 - getting [note, note_on(time), note_off(time), channel]

The following code creates a dictionary in which the delta times are converted to linear time(but I am not sure if this is the right approach):

from mido import MidiFile

mid = MidiFile('testsunvox.mid')
midimsgs = []

# Put all note on/off in midinote as dictionary
for i in mid:
    if i.type == 'note_on' or i.type == 'note_off':
        midimsgs.append(i.dict())

# change time values from delta to relative time # don't know for sure if this is the right way...
mem1=0
for i in midimsgs:
    time = i['time'] + mem1
    i['time'] = time
    mem1 = i['time']

# put note, starttime, stoptime, as nested list in a list. # format is [note, start, stop, channel]
for i in midimsgs:
    print (i)

I cannot find the right question for this right now but the goal is to get:

[note, note_on(time), note_off(time), channel]

for each note. But the problem is that there are two messages(note-on/off) and I want to make it one. I will post my solution if I found it. (or maybe someone knows a very simple trick of the mido libriary to do this...)

  • 1
    For each note-on message, you have to find the corresponding note-off message, i.e., the next note-off message with the same note and channel. Two nested loops. – CL. Jul 27 '20 at 08:15
  • Thank you! I have posted the solution to my first goal! – Philip Bergwerf Jul 27 '20 at 18:45

2 Answers2

0

I see now that my aproach is wrong. I need to keep seperated note-on and note-off messages. The following code:

from mido import MidiFile

mid = MidiFile('testlilypond.mid')
mididict = []
output = []

# Put all note on/off in midinote as dictionary.
for i in mid:
    if i.type == 'note_on' or i.type == 'note_off' or i.type == 'time_signature':
        mididict.append(i.dict())
# change time values from delta to relative time.
mem1=0
for i in mididict:
    time = i['time'] + mem1
    i['time'] = time
    mem1 = i['time']
# make every note_on with 0 velocity note_off
    if i['type'] == 'note_on' and i['velocity'] == 0:
        i['type'] = 'note_off'
# put note, starttime, stoptime, as nested list in a list. # format is [type, note, time, channel]
    mem2=[]
    if i['type'] == 'note_on' or i['type'] == 'note_off':
        mem2.append(i['type'])
        mem2.append(i['note'])
        mem2.append(i['time'])
        mem2.append(i['channel'])
        output.append(mem2)
# put timesignatures
    if i['type'] == 'time_signature':
        mem2.append(i['type'])
        mem2.append(i['numerator'])
        mem2.append(i['denominator'])
        mem2.append(i['time'])
        output.append(mem2)
# viewing the midimessages.
for i in output:
    print(i)
print(mid.ticks_per_beat)

gives this output:([type, note, time, channel])

['time_signature', 4, 4, 0]
['note_on', 69, 0, 0]
['note_off', 69, 0.500053, 0]
['note_on', 71, 0.500053, 0]
['note_off', 71, 0.7500795, 0]
['note_on', 69, 0.7500795, 0]
['note_off', 69, 1.000106, 0]
['note_on', 71, 1.000106, 0]
['note_off', 71, 1.500159, 0]
['note_on', 69, 1.500159, 0]
['note_off', 69, 2.000212, 0]
['time_signature', 3, 4, 2.000212]
['note_on', 66, 2.000212, 0]
['note_off', 66, 2.5002649999999997, 0]
['note_on', 64, 2.5002649999999997, 0]
['note_off', 64, 3.0003179999999996, 0]
['note_on', 62, 3.0003179999999996, 0]
['note_off', 62, 3.5003709999999995, 0]
384
[Finished in 0.0s]

Which is was my goal: getting all needed information in a list from a midifile. This is a way to get info from messages into a python list.(I also made the (delta)time linear. You need to add the previous 'time' to the current one using a for loop)

0

You can just directly get parameter values... [https://mido.readthedocs.io/en/latest/message_types.html#parameter-types][1]

For Example, I think it would be a lot easier to just do something like this:

time= 0
for msg in mid:
    if msg.type == "note_on":
    time+=msg.time
    print(time,msg.note,msg.velocity)