How to grab the note attribute from Message in mido?

1.2k Views Asked by At

I'm trying to print out the note attribute in each message output by mido in a .mid file. Right now, my code looks like this:

for msg in mid.tracks[1]:
    if not msg.is_meta:
        print(msg.note)

But, upon running the code, I get the error:

AttributeError: 'Message' object has no attribute 'note'

I'm confused, as the documentation shows the same "msg.note" syntax working just fine. Any help would be much appreciated.

2

There are 2 best solutions below

0
On

There are many different types of MIDI messages, and not all of them have a note number.

If you want to print out all notes, you have to check the message type first. If you want to print out all messages, you cannot rely on the note field being there.

0
On

Try

if not msg.is_meta:
   if msg.type == 'note_on':
      print(msg.note)

This should produce just the numerical notes as output.