thestk / rtmidi

A set of C++ classes that provide a common API for realtime MIDI input/output across Linux (ALSA & JACK), Macintosh OS X (CoreMIDI) and Windows (Multimedia)
Other
954 stars 265 forks source link

Playing midi events from different sources #263

Open pianonyy opened 2 years ago

pianonyy commented 2 years ago

Hello!

I’m trying to realize real time python module which can play input midi events, and play existing midi files simultaneously. So in this way we can play something in real-time for existing midi patterns. But, I face the problem of outport it can’t handle several midi event sequences, or I ‘m doing something wrong.

Please, could you help me to understand, how to organise multiple midi outputs using rtmidi simultaneously?

keinstein commented 2 years ago

Hi, there are several ways to go. One – the one that always works – is to mix the MIDI events in your application. So you have multiple inputs that send their MIDI events to the same output port. This works quite well if you carefully plan your MIDI streams. Actually there can be 16 MIDI nearly separated MIDI streams per output port. These are called channels. So when your MIDI files play in different channels everything should work as expected. It is also possible to time multiplex the MIDI streams. But this includes lots of programming efforts. You can have a look at the routing subtree (https://github.com/keinstein/mutabor/tree/master/src/kernel/routing) of Mutabor, how I implemented the tracking of controllers, programs and so on.

You could also open the same MIDI port in different output port objects. This does not work on WinMM. In that case the underlying MIDI framework handles the mixing, which in most cases means just mixing of MIDI events. So you have to solve the issues with overlapping channels in your software or your data as described above.

Keeping track of the state of a MIDI instrument (e.g. the current controller value) is not part of the MIDI standard (at least for MIDI 1.0). So you never know in which state the Synth is. And sending all controllers, RPNs and NRPNs before every note on or note off will practically break the performance of your MIDI connection.

pianonyy commented 2 years ago

@keinstein thank you very much! I've done your first variant and it works as required!

Now I am trying to reduce latency from a midi input using the Asio driver, more precisely to increase buffer size. But I can't find Asio settings in python environment. Maybe, you can help me to reduce latency using another method you know? Thank you a lot for your tips and experience!!!