Timing issues with MIDIOut

I’ve got some code here: https://sccode.org/1-5e4-yiP_9ggFeA

The intent is for each MIDI event coming from my EWI to be broadcast to every active channel on other devices in my studio. I’ve found that this code works OK when I use a keyboard to play notes, but when I use my EWI (which sends data continuously while I’m blowing into it), some note on and note off messages are received by the other devices super late! Any idea what I could be doing wrong?

One possible issue is that you’re creating a new MIDIOut object for every event times device times channel – at a quick glance, I’d guess 30 or so. So if you have 100 messages per second, you’re creating and destroying 3000 distinct MIDIOuts every second – but for only 30 or so targets. That’s inefficient.

I’d suggest to create a flat array containing one MIDIOut per target device/channel, and then simplify the MIDIdefs to iterate over this flat array. Reusing the output objects will reduce load on the interpreter.

I don’t know if this will completely solve your problem – there have been other issues reported with MIDI output timing (and this seems to be system-dependent – I’ve gotten good results on my Linux system but not everyone has) – but it’s not a bad idea to start by eliminating as much inefficiency as possible.