Realtime MIDI input

Here is a simple script on using Py Port Midi 0.03 (download here)

It allows realtime midi input from any midi device your OS can detect, but has only been tested on windows XP

The simple test uses Midi CC #1 to control the X axis and CC #2 to control the Y axis of the default cube (or any object named Cube)

I think this function would best be used in the game engine, but currently it does not use that, im working on the GE, but would like to hear other peoples ideas for uses.

Edit: it seems you cant post attachments in the python section, could a Mod move this thread to an appropriate section where i could post an attachment, or allow me to post an attachment to this thread? thanks

Ok, ill just post the code here.
download PyPortMidi and install it.

In a new Scene, make sure there is an object named Cube, paste this code into a new script and run it.

import pypm
import array
import time
import Blender

NUM_MSGS = 100 # number of MIDI messages for input before closing

object1 = Blender.Object.Get('Cube')     

def TestInput(noteA, noteB):
    dev = 1 #device number, run test.py in pyportmidi directory to see list of midi devices
    MidiIn = pypm.Input(dev)
#    MidiIn.SetFilter(pypm.FILT_ACTIVE | pypm.FILT_CLOCK)
    for cntr in range(1,NUM_MSGS+1):
        while not MidiIn.Poll(): pass
        MidiData = MidiIn.Read(1) # read only 1 message at a time
        if (MidiData[0][0][1] ==1 ):
            noteA = MidiData[0][0][2]
        elif (MidiData[0][0][1] ==2 ): #Add elif's for each midi CC message
            noteB = MidiData[0][0][2]
        else:
             null = 0;
        print  MidiData[0][0][1], " " ,MidiData[0][0][2] # prints Midi CC number and 7-bit Value
        object1.setLocation (noteA/12.7, (noteB/12.7), 0)
        Blender.Redraw()
    del MidiIn
    
# main code begins here
pypm.Initialize() # always call this first, or OS may crash when you try to open a stream
TestInput(0,0)
pypm.Terminate()

The script is set up to exit when 50 midi messages are received, blender will appear to lock up in that time.

Does anyone know how to stop blender from locking up when the script is run?

You may want to put the script in a script link, then it will run every frame and not lock up Blender.

No that doesnt work, it freezes up blender still. but it re runs the script every frame.

It could work if you did not use a loop and simply stored the NUM_MSGS in the registry and fetched it on each frame.

great work Levon! have you gotten this script to work in real-time? Or do you know of any other Midi input BGE/python scripts that work in the BGE?

anyone heard any new development on realtime MIDI in the BGE?