How to rewrite this code using new JUCE 6 MidiBufferIterator?

Moved my projects to JUCE 6 today. Am now getting warnings on “Iterator” is deprecated.

Can someone kindly help explain how to rewrite this type of Midi Iterator with the new way? How to get the sample number?

I’m having some difficulty grokking this…

// iterate through the incoming MIDI and grab only events for a certain channel

MidiMessage msg;
int sampleNumber;

for (MidiBuffer::Iterator it (midiMessages); it.getNextEvent (msg, sampleNumber);)
{
	if (msg.isForChannel(midiChannel))
		copyBuffer.addEvent (msg, sampleNumber);
}

Untested, but I think this should do it:

for (const auto meta : midiMessages) {
  const auto msg = meta.getMessage();

  if (msg.isForChannel (midiChannel))
    copyBuffer.addEvent (msg, meta.samplePosition);
}
1 Like

Thank you!