Loop MidiBuffer

Disclaimer I’m a JUCE / C++ novice.

I’d like to iterate continuously over a buffer of MIDI messages.

I was considering implementing a TimerCallback() to fire off a MidiBuffer::Iterator, but I’m concerned about blocking (should I be?).

So, what might be the best way to handle this? What thread should this be happening on?

Ultimately I’d like to have multiple buffers looping in the background.

You can loop on your midi buffer in the processBlock in each block called by the audio thread, no need to have external threads running for it. Not just that, processBlock already passes the midi buffer for you to be able to access in it.

So, would I inherit from the AudioProcessor class in that case? And what initialization does that entail?

You can use AudioProcessor but it mostly depends on what are you trying to do (a plugin, a standalone app, etc.) as there are many different ways to get an audio/midi output. Take a look at tutorials to see those different ways.

About the looping midi buffers on the background, what are you trying to achieve? If you say what are you doing people may tell you easier/more efficient ways to do it

Thank you. I wasn’t able to find a tutorial that employed the AudioProcessor class directly.

As far as what I’m trying to achieve, it’s rather simple. I have a GUI with sliders that send MIDI CC data. When a certain mode is engaged, the user’s GUI input is recorded from the time they grab a slider until they release it. Once they release a slider, the recorded gesture begins looping immediately, updating the GUI and sending MIDI CC.

I don’t think you need to have background threads running extra midi buffers, you just need to have an atomic flag that your GUI set’s true/false and your audio thread reads. When you set it true (click on a slider) you either have a valuetree + ParameterAttachments that syncs your GUI and audio values, thus changes in the slider will set the valuetree values that you can read in your audio thread to save them in an extra buffer you will loop to add those values in the main midi Buffer. Or you either send those values into a lockfree queue that you read and copy from the audio thread to a local buffer. Then when you release the mouse click, you set the flag to false.

If you wait till June for Juce 6, ParameterAttachment won’t need to use valuetree

Edit: you don’t even need an atomic flag, just having one bool parameter in your system (valuetree, ParameterAttachment) is enough to check that