Memory Allocation in MidiBuffer

I have implemented a metronome using a Synthesizer. During getNextAudioBlock() I stuff a MidiMessage with noteOn at appropriate locations and then call Synthesizer::renderNextBlock().

It seems that MidiBuffer allocates memory, which takes a critical section during the audio i/o callback. Is there some what we can improve MidiMessage so it can have a pre-allocated storage or something? Or allow construction of a MidiBuffer from an existing piece of memory (similar to how AudioSampleBuffer works)? This way one can pre-allocate a sufficiently large buffer and then just keep using it.

What about MidiBuffer::ensureSize()?

Ahhh yeah that looks really good!

I’m thinking now to just make the MidiBuffer a member of my class, and after I use it in the audioIODeviceCallback() just call clear(). The same MidiBuffer will be re-used over and over again. On the first few uses, it will stretch to the the largest size it needs to be and then stay there.

Hmm… neither MidiBuffer nor MemoryBlock have a reset() type function that frees the memory. Which I would want to use in releaseResources() of my audio object.

When I’ve had to do this sort of thing I’ve just created a blank object on the stack and used swapWith(). This is a quick operation and when the stack object goes out of scope it will delete the old memory that was allocated. Not the most elegant solution but it works.

NICE Hack!!! I will use it

I would just use a ScopedPointer and create and resize the MidiBuffer in the contructor. Then I would just assign 0 to the ScopedPointer
in ReleaseResources()…