Problem with MultiPort use in python mido MIDI package

Question:

I am working in a program that autosaves MIDI messages as they are played, and everything is working really well when using a single input port.
I want to extend to use 2 or 3 simultaneous ports (the app is aimed at VST piano players, and some use the piano as one MIDI device, and the sustain pedal as a second device in another USB slot). I was able to get it to work using two separate input ports, and then using 2 iter_pending() loops. The basic code is something like this:

ports = mido.get_input_names()
port_A = mido.open_input(ports[0])
port_B = mido.open_input(ports[1])

for msg_A in port_A.iter_pending():
    ...
for msg_B in port_B.iter_pending():
    ...

The problem with this is that the loops are sequential. When I test by sending the same midi message through the 2 ports, the event when enters through port_B is processed with a few milliseconds of delay.

The MIDO package includes a different type of port exactly for this: mido.ports.MultiPort()
My problem is that I cannot make it to work.

If I try:

multi = mido.ports.MultiPort([port_A, port_B])
for msg in multi:
    print (msg)

as suggested in the MIDO documentation, no msg is printed…

I have done all sorts of syntax combinations for the ports in multi, but nothing seems to work.

What am I doing wrong here?

Asked By: autosave midi

||

Answers:

Have you tried:

open_ports = [portA, portB]

while True:
    for port in open_ports:
        do_whatever(port.poll())

or to make it asynchronous:

portA = mido.open_input(ports[0], callback=do_whatever)
portB = mido.open_input(ports[1], callback=do_whatever)

while True:
    pass

In both cases, do_whatever is a function that takes a single message as an argument and does with that message whatever it is you want to do.

Answered By: franklinscudder
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.