SpotlightKid / python-rtmidi

Python bindings for the cross-platform MIDI I/O library RtMidi
https://spotlightkid.github.io/python-rtmidi/
Other
349 stars 64 forks source link

Increase the sysex input buffer size on Windows #200

Open MaddyGuthridge opened 2 months ago

MaddyGuthridge commented 2 months ago

According to the RtMidi Tutorial, RtMidi supports sysex messages with a maximum size of 1024 bytes. All larger messages are discarded.

The Windows Multimedia library MIDI calls used in RtMidi do not make use of streaming functionality. Incoming system exclusive messages read by RtMidiIn are limited to a length as defined by the preprocessor definition RT_SYSEX_BUFFER_SIZE (set in RtMidi.cpp). The default value is 1024. There is no such limit for outgoing sysex messages via RtMidiOut.

A project I am working (https://github.com/MiguelGuthridge/Flapi) on uses sysex messages to communicate with an isolated Python environment, and this limitation on the size of messages makes receiving responses from the environment very unreliable.

Would it be possible to increase this buffer size in python-rtmidi to allow for larger messages to be exchanged? Making it 64KB or so would be a good compromise between reliability and low memory footprint.

SpotlightKid commented 2 months ago

The value used by python-rtmidi is actually 8196 bytes (should probably be 8192).

MaddyGuthridge commented 2 months ago

Interesting - in my tests, any messages over 1024 bytes are not received. FL Studio is able to receive these messages (using their alpha build where their buffer size is increased), so I am certain that this is an issue in python-rtmidi (the mido backend I am using). image

Here is the code for the project I am using to test this: https://github.com/MiguelGuthridge/fl-sysex-overflow

SpotlightKid commented 2 months ago

I'm sorry, but I don't currently have the time to debug this myself, nor do I have a Windows or FL Studio installation to test it with. On Windows reception of SysEx messages up to 8192 bytes length should be supported.

At the moment I can only offer this advice:

If none of these options work for you, then I'm afraid python-rtmidi might not be the right tool for the job on Windows. The MultiMedia API RtMidi uses is fairly limited compared to ALSA on Linux or CoreMIDI on macOS. There is libremidi, which uses more modern MIDI APIs on Windows, but I don't know whether anyone has created a Python wrapper for it yet.