[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] midi events



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!

On Mon, Jul 26, 2021 at 3:54 PM Daniel Foerster <pydsigner@xxxxxxxxx> wrote:
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

On Mon, Jul 26, 2021, 04:02 Folkert van Heusden <mail@xxxxxxxxxxxxxx> wrote:
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