Strumming MIDI notes with addEvent - Time problem


Hi. This probably is a beginner question but i’m trying to add few notes to the MIDI buffer with slightly different time, like this:

for (MidiBuffer::Iterator i(midiMessages); i.getNextEvent(midi_in, time);)
   if received a note .....
 for (int j = 0; j < 2; j++)
   Processed_Midi.addEvent(MidiMessage::noteOn(midi_in.getChannel(), Notes[j], midi_in.getVelocity()), time+j*offset);

Doesn’t matter what time i set all the notes are playing at the same time…
If this is wrong how should i do this? A timer? Another method or buffer?


keep in mind that the process time slice is very short. It looks like all your added midi events will be processed within that time slice regardless of the offset you have added.
Solution: use a separate buffer for your strummed notes. From there add only those notes to the process buffer that have an offset < process buffer length. You will of course have to decrement the offsets of the events held in your buffer with every process call.


I’m not sure how exactly that could work…
What sample or time reference can i use to add in the next notes? How do you get the buffer length and keep track of beginning or end of a process cycle?
I hope i don’t have to use a timer for this.


Don’t worry, it shouldn’t be too tricky. The important thing is having your own separate queue that holds the events you need to delay across blocks. Use getBlockSize() to determine this value, and processBlock(...) will be called once for every new block, so just do your update in there to keep track of times and output any events in the queue whose time is up.

If you have multiple affected events, it’s much more efficient to have a global timer for comparison rather than subtracting from the time remaining for each event.


What i learned:
The time stamp is ignored when adding notes. So you should count the samples in the process block and add the new notes when the time is right.