Midi Events discarded by the AudioUnit wrapper


#1

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) ?


#2

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


#3

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 ?


#4

here is what I would do:

[code]Render() {

{
ScopedLock lock(midi_mutex);
midiEvents = midiEvents0;
}

}

HandleMidiEvent(…) {
ScopedLock lock(midi_mutex);
midiEvents0.addEvent(…);
}[/code]

(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:


#5

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.


#6

Yes, it works perfectly. Issue solved :slight_smile:

Thanks a lot !