2

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?

1 Answers1

1

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.

  • This suggestion by franklinscudder did the trick. The first suggestion (use of port.poll() ) worked well. Now the same MIDI message sent through 2 devices arrives at exactly the same time. Not as syntax elegant as using the MultiPort wrapper, but the MIDO documentation is not very helpful (or I am not very good at understanding it). I have not tried the second suggestion (using callbacks would require a significant change to my original code). The first one is simpler for my case. – autosave midi Jul 31 '21 at 01:47