[pygame] midi events

2 views
Skip to first unread message

Folkert van Heusden

unread,
Jul 26, 2021, 5:02:04 AM7/26/21
to pygame...@seul.org
Hi,


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

I tried that in a tiny test program, but that never gets triggered.
pygame.event.get() does work, so something is received.

My test-code is:

#! /usr/bin/python3

import pygame
import pygame.midi
import time

pygame.init()
pygame.midi.init()
midi_in = pygame.midi.Input(pygame.midi.get_default_input_id())

pygame.fastevent.init()

while True:
    for event in pygame.fastevent.get():
        if event.type == pygame.QUIT:
            sys.exit(0)

        if event.type in [pygame.midi.MIDIIN]:
            print(event)

    time.sleep(0.001)

pygame.quit()


I would like to receive it as an event, as that way I can do get() with a timeout instead of get() & sleep(); the timeout variant would reduce the latency that is introduced by the sleep.


regards

Daniel Foerster

unread,
Jul 26, 2021, 9:54:35 AM7/26/21
to pygame...@seul.org
In that example, the midi events are converted to Pygame events and posted to the Pygame event queue here: https://github.com/pygame/pygame/blob/main/examples/midi.py#L74

Folkert van Heusden

unread,
Jul 26, 2021, 10:10:29 AM7/26/21
to pygame...@seul.org
Hi Daniel,

Ah indeed, that would work as well!

Unfortunately not entirely: pygame 2 allows get() to have a timeout value, so then it might take a while before the midi events could be retrieved. And I don't think I should create a busy-loop.

Now I could do the midi polling in a separate thread and then push them into the main event loop. According to https://stackoverflow.com/questions/15538287/can-i-add-pygame-events-from-a-second-thread the SDL event-loop underneath it is supposed to be thread-safe.

Let's see if that works.
Thanks sofar!
Reply all
Reply to author
Forward
0 new messages