Real-time MIDI queue: bounded processing without lost Note-Off or sustain events

Hi everyone,

We’re developing a professional polyphonic workstation/synthesizer using C++ and JUCE, targeting macOS and Linux (Raspberry Pi 5).

Our audio engine runs at 48 kHz / 128 samples, giving us approximately 2.67 ms per audio block.

We identified two MIDI-related performance bottlenecks:

  1. Our controller/pitch-bend processing repeatedly scanned a 256-voice pool across 17 parts. We’ve optimized this, reducing processing time by around 90% in some stress tests without changing PCM output.
  2. Our MIDI consumer (drainMidiQueue()) currently processes incoming FIFO events until the queue is empty. Under continuous controller traffic, this can exceed the audio callback deadline.

We’re considering a bounded MIDI consumer with a preallocated FIFO, admission control and reserved capacity for Note-Off and sustain-release events.

Our main questions:

  • How do professional synthesizers or JUCE-based audio engines bound MIDI processing without introducing unacceptable latency?
  • What is the recommended strategy for preserving Note-Off and sustain-release events during controller floods?
  • Should MIDI parsing, admission and event preparation happen outside the audio thread, leaving only bounded event application inside the callback?
  • Are there established open-source implementations or benchmarks for this problem?

We want to preserve MIDI ordering, note ownership, musical timing and audio-thread real-time safety, without requiring a manual reset after overload.

Our performance measurements are currently from offline tests; physical hardware validation is pending.

Any architectural suggestions or references would be greatly appreciated.

Thanks!

Moin,

The fact that you consider pitch bend a performance problem implies that you are doing sth fundamentally wrong. Pitch bend is easy to parse and is usually one of many factors affecting pitch, so this should not be problematic at all.

In hardware synthesizers, for every frame I process MIDI/UI, Effects, Voices in this order. I maintain a list of voices in with decreasing priority and process them in this order until all are done or time runs out. This ensures the proper voices are killed.

But again, Pitch bend, Note-off, Sustain should not pose a performance problem and indicate your architecture might be flawed, sorry for stating this so bluntly.

Stefan

1 Like

What exactly does drainMidiQueue() return? Is it individual MidiMessage events? If this is the case then most likely that’s your problem. You must be calling MidiBuffer::addEvent(), and when you get more events than are pre-allocated, you’re forcing it to allocate on the audio thread.

A better solution is for your FIFO to contain a MidiBuffer with all of that block’s events, which you can simply swap in on the audio thread (swapWith()). Of course this introduces other complications, because you have to have some new mechanism which is aggregating the MidiBuffer on a timer before it pushes to the FIFO. A common problem would be for the audio thread to encounter two MidiBuffer`s. If it swaps both of them in, then the first one gets dropped; if it copies them across, then you’re likely allocating.

By the way, I am not a professional, but I’ve been doing this for a while and it seems likely to me that memory allocation is the source of your problem.

1 Like

Something is odd here. MIDI parsing itself is typically a very tiny overhead compared to something like a filter or oscillator.
Perhaps some CPU profiling is in order, mayby the pitch-bend events are causing an expensive recalculation of the filter cutoff coefs or something like that.

1 Like

Agreeing with the others that parsing itself shouldn’t cost much. But to answer the bounding question directly, the usual pattern is to coalesce instead of dropping:

  • Outside the audio thread (MIDI input callback), parse into a fixed-size POD event struct and push to a preallocated SPSC FIFO (juce::AbstractFifo over a std::array works).
  • In the callback, drain everything. Draining a few hundred PODs is cheap. The expensive part is whatever each event triggers. So apply note on/off and sustain immediately, in order. For continuous controllers (CC, pitch bend, channel pressure), only write the latest value per channel/controller into a small table and mark it dirty.
  • After draining, apply each dirty controller once per block, or per sub-block if you need sample-accurate timing. A flood of 1000 CC74 messages then costs one filter recompute, not 1000.
  • Never drop note-off or sustain-release. With coalescing you don’t have to, because the queue only has to be big enough for discrete events. A note-off for a voice that was already stolen just becomes a no-op lookup.

The 256 voices x 17 parts scan suggests per-event work is O(voices). Keep a per-channel (or per-part) list of active voices so a pitch bend only touches the voices that own it, and let each voice read the bend value lazily at its next render instead of being pushed an update.

1 Like