How to change the timestamp of a MidiMessage from within a MidiBuffer::Iterator

I want to change the position of a midi message in a MidiBuffer in a Iterator like this:


MidiBuffer::Iterator iterator(b);
MidiMessage m(0xf4, 0.0);

while(iterator.getNextEvent(m, samplePos))
{
    m.setTimeStamp(newTimeStamp)
}

But if I iterate the Buffer again, samplePos does not contain the previously set timestamp:


MidiBuffer::Iterator iterator2(b); 
MidiMessage m(0xf4, 0.0); 

while(iterator2.getNextEvent(m, samplePos)) 
{ 
    // samplePos != newTimeStamp
}

In the API of Iterator::getNextEvent you can read:

this will be the message (the MidiMessage's timestamp is not set)

So how can I restore the previously set timestamp?

I tried to lurk in the source files of Iterator, but that's a bit too scary for me...

 

 

 

MidiBuffers are packed arrays whose order depends on the times of the events, so the only mutating operation they support is adding new events.

If it allowed you to change a timestamp, the entire buffer would need to be re-sorted each time, and that'd be very inefficient, so the way they work is that you should just build a new buffer with your new events and swap them over when you're done.

1 Like

Thanks, it works like you said.