1

So I'm using this function to detect events from my MIDI keyboard which then get printed out as sheet music:

for e in events:
    if 
    if e.type in [pygame.midi.MIDIIN]:

This returns 2 events, one when the key is pressed down and one when it is pressed up.This works great for individual notes because I just create a function to only make the if statement trigger every other time there is an event but it's difficult for chords because the events could be coming in a variety of order note 40 keydown, note 41 keydown, note 41 key up, note 40 key up. or note 40 down, note 41 up, note 40 down, note 41 up. Etc etc. Except it doesnt say 'key up' or 'key down' it just triggers the if statement. So my question is is there a function that will only get triggered on the key being pressed either down or up? something like pygame.midi.MIDI_KEYDOWN

  • What attributes does the midi event have? `print(dir(e))` – skrx Sep 12 '17 at 02:18
  • I just use e.data1 which tells me which note was played but there are 4 or 5 I think – Tyler Bloom Sep 12 '17 at 03:05
  • Do the midi events have `keydown` and `keyup` attributes similar to the `pygame.KEYDOWN` and `KEYUP` event types? – skrx Sep 12 '17 at 03:07
  • Not that I know of that is what my question is – Tyler Bloom Sep 12 '17 at 03:13
  • Oh wait I know what you mean hold on a sec – Tyler Bloom Sep 12 '17 at 03:14
  • In the `if e.type in [pygame.midi.MIDIIN]:` clause, add `print(dir(e))` and then post the result. I'm not familiar with the midi module and don't have a midi keyboard, so you have to check it out for me. You can already try to experiment with the attributes if you find something that looks suspicious. ;) And if they have something like a `keydown` attribute, you could use [`hasattr`](https://docs.python.org/3/library/functions.html#hasattr) to check if it's a keydown or keyup event. – skrx Sep 12 '17 at 03:15
  • Ok so I can actually use status because it is 128 when its key down and 144 when key up. Thanks I didn't even think of that. what is hasattr? – Tyler Bloom Sep 12 '17 at 03:26
  • Oh it's just a function ok thanks – Tyler Bloom Sep 12 '17 at 03:27
  • A function that checks if an object has a specific attribute. – skrx Sep 12 '17 at 03:27
  • You can post that as an answer, if somebody else has the same problem. – skrx Sep 12 '17 at 03:30

1 Answers1

1

The read() function simply returns the raw bytes of a MIDI message, without parsing them.

The midis2events() function does not really do anything with these bytes either:

    ((status,data1,data2,data3),timestamp) = midi

    e = pygame.event.Event(MIDIIN,
                           status=status,
                           data1=data1,
                           data2=data2,
                           data3=data3,
                           timestamp=timestamp,
                           vice_id = device_id)

So you have to parse the messages yourself: any status byte between 0x90 and 0x9F is note on, and between 0x80 and 0x8F, note off.

CL.
  • 173,858
  • 17
  • 217
  • 259