Search code examples
pythonmidimido

Mido midi - finds twice as many Note_On events if I play on my keyboard, as opposed to drawing notes in the piano roll...?


I have written a program to parse a midi file and find all the Note_On events.

I go into my DAW piano roll and draw in D4, D5, F4

My program correctly finds the Note_One events and identifies them correctly.

Then I go to my midi keyboard and play exactly the same notes, and save the midi file.

My program finds, D4,D4, D5,D5, F4,F4

i.e. it doubles the notes! Why am I getting a different result? Both midi files look the same in the DAW: they both have three notes.

Here are the two midi files:

https://drive.google.com/drive/folders/1CUgTJNH-jD5rPJEmT0aSwRp4PhaatxYq?usp=sharing

Heres my code which reads in the Midi data from file:

for i, track in enumerate(Ians_midi.tracks):
    for msg in track:
        if msg.type == 'note_on':   ... 

Solution

  • The MIDI specification says:

    MIDI provides two roughly equivalent means of turning off a note (voice). A note may be turned off either by sending a Note-Off message for the same note number and channel, or by sending a Note-On message for that note and channel with a velocity value of zero. The advantage to using "Note-On at zero velocity" is that it can avoid sending additional status bytes when Running Status is employed.

    Due to this efficiency, sending Note-On messages with velocity values of zero is the most commonly used method. However, some keyboard instruments implement release velocity where a Note-Off code (8nH) accompanied by a "velocity off" byte is used. A receiver must be capable of recognizing either method of turning off a note, and should treat them identically.

    So when you get a note_on message, you also have to check its velocity.