Reputation: 1036
Is there a way to send and receive MIDI messages in Java without constructing MidiMessage objects?
I've been working on a kind of proof-of-concept high-performance real-time music workstation prototype, and one of the design criteria is that ordinary playing of instruments and signal processing generally do not allocate any objects: objects are only allocated when the user changes settings. (I want to reduce as much as possible the need for garbage collection pauses.)
Obviously, allocating a MidiMessage object for every single fine-grained MIDI event is the opposite of the philosophy I wanted to follow. The javax.sound.sampled package lets me deal with sampled sound without allocations, but so far I've been unable to find any way to use MIDI in Java without allocating tons of objects.
Is there a way for me just to read bytes from the MIDI device into a buffer, instead of receiving MidiMessage objects? MIDI messages are pretty easy to decode, so I'm happy to go without the "help" offered by the MidiMessage class (which, since you still need to decode the bytes in the message, isn't much help anyway).
Upvotes: 2
Views: 207
Reputation: 3223
If I understand your issue correctly, can't you reuse a single MidiMessage
object in your code and reassign its data via setMessage()
? This way the reference is never lost and will not be garbage collected.
Upvotes: 2