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

Re: [pygame] detectng midi events in pygame?



"""
Thanks for your reply Lenard,

I looked at the examples and created this working program (see program below). When I close the program, this error message appears in the terminal:

PortMidi call failed...
  PortMidi: `Bad pointer'
type ENTER...

What can I change in the program to avoid the "PortMidi-call-failed-Bad-pointer" error? Any code snippets, links to articles, or suggestions would be greatly appreciated.

"""

#--------------start miditest.py--------------
import pygame
import pygame.midi
from pygame.locals import *

pygame.init()

pygame.fastevent.init()
event_get = pygame.fastevent.get
event_post = pygame.fastevent.post

pygame.midi.init()
input_id = pygame.midi.get_default_input_id()
i = pygame.midi.Input( input_id )

pygame.display.set_caption("midi test")
screen = pygame.display.set_mode((400, 300), RESIZABLE, 32)

print "starting"

going = True

while going:

        events = event_get()
        for e in events:
                if e.type in [QUIT]:
                        going = False
                if e.type in [KEYDOWN]:
                        going = False

        if i.poll():
                midi_events = i.read(10)
                #print "full midi_events " + str(midi_events)
                print "my midi note is " + str(midi_events[0][0][1])
                # convert them into pygame events.
                midi_evs = pygame.midi.midis2events(midi_events, i.device_id)

                for m_e in midi_evs:
                        event_post( m_e )

print "exit button clicked."
i.close()
pygame.midi.quit()
pygame.quit()
exit()
#--------------  end miditest.py--------------



On Wed, Dec 15, 2010 at 7:13 PM, Lenard Lindstrom <len-l@xxxxxxxxx> wrote:
Hi Brian,

The pygame midi.py example has an input event display mode.

python -m pygame.examples.midi -i

It is in the examples subdirectory in the pygame package directory. Unfortunately, the PortMidi with pygame.midi wraps does not have a midi event callback option, so midi events must be polled.

Lenard Lindstrom


On 15/12/10 03:19 PM, Brian Gryder wrote:
"""
How can I detect midi events in pygame? I am able to detect the count of MIDI devices and input and output device names (see program below)  I'd like to display the midi note names like "C3 ON" or "D3 OFF". I am using the JACK Audio Connection Kit. Any code snippets, links to articles, or suggestions would be greatly appreciated.
"""

import pygame
import pygame.midi
from pygame.locals import *

pygame.init()
pygame.display.set_caption("midi test")
screen = pygame.display.set_mode((400, 300), RESIZABLE, 32)

pygame.midi.init()

print "There are " + str(pygame.midi.get_count()) + " MIDI devices"

print "The default input device number is "  + str(pygame.midi.get_default_input_id())

print "The default input device info is " + str(pygame.midi.get_device_info(pygame.midi.get_default_input_id()))

print "The default output device info is " + str(pygame.midi.get_device_info(pygame.midi.get_default_output_id()))

print "The current time on the PortMidi timer is " + str(pygame.midi.time()) + " ms"

event_text = []

while True:

       for event in pygame.event.get():
               if event.type == QUIT:
                       exit()

       event = pygame.event.wait()
       print str(event)


"""
output:
user@lappy:~/Documents$ python -u miditest.py
There are 5 MIDI devices
The default input device number is 1
The default input device info is ('ALSA', 'Midi Through Port-0', 1, 0, 0)
The default output device info is ('ALSA', 'Midi Through Port-0', 0, 1, 0)
The current time on the PortMidi timer is 1 ms
"""