Midi Events discarded by the AudioUnit wrapper

Hi Jules,

The juce AU wrapper assumes that the HandleMidiEvent callback will never be called during Render(). It seems to be true on most hosts, but I have been notified of one host that does indeed call HandleMidiEvent during Render. Since the midiEvents.clear() is done at the end of Render(), all events that have been received during render are discarded.

Btw another host that does not take any precaution at not calling HandleMidiEvent during Render() is the Apple AudioUnitHosting example project.

I’m not sure of the best fix for that. Probably using two MidiBuffer and swapping them at the beginning of Render() (with a mutex to protect them) ?

Sheesh. Do you think it’d be lazy to just wrap both callbacks in a mutex?

I don’t know if all hosts will enjoy having their midi callback being locked for the duration of the rendercallback, maybe that could lead to missing other events or jittering , don’t you think ?

here is what I would do:

[code]Render() {

ScopedLock lock(midi_mutex);
midiEvents = midiEvents0;


HandleMidiEvent(…) {
ScopedLock lock(midi_mutex);

(that is assuming that operator= does not call malloc, I did not check)

But a a swap function for doing midiEvents.swap(midiEvents0) would be much more efficient of course :slight_smile:

Ok, I’ve checked in some stuff now if you want to try it - the operator= does malloc, so I’ve had to add a swap() method.

Yes, it works perfectly. Issue solved :slight_smile:

Thanks a lot !