Array or Linked List or any other suggestions?

Hello again,

in my current plugin I am creating MIDI notes depending on incoming notes. So say in Bar 1.1 a Quarter note comes in, I create a “set” of notes responding to this input with their start and end samples. Every process block I loop over my set of notes to play and create the noteOn / noteOff for these notes in the midibuffer it falls into the current block.

What would you use for this? ATM I have a little class note (simplified for ease of reading) :

class note()
{
        int noteNumber;
        uint8 noteVelocity;
        long startSample;
        long endSample;
}

ATM I am using a LinkedList and it works fine. But is that the way its meant to be done? Maybe there is a better way, more elegant way?

cheers,

Jens

Linked lists are perfect if you need to insert nodes in the middle of the list. But for just playing back vectors are much faster, easier and safer to use.

Sample count can be used as the time format for MIDI notes but a bit more musical option is to use beats (or PPQ). You can get the song position (as beats) from the host thru AudioPlayHead class and then decide in the processBlock which notes are to be sent in the current time frame.

Linked lists probably require heap allocation too which can be a bad idea in the audio thread.

And I’d go as far to say that the answer to “should i use a linked list” is a solid no like 95% of the time.

3 Likes

100% agreed. Since your class/struct is trivially copyable, vector is the way to go.
If it isn’t copyable, a vector of unique_ptrs is the thing.

If you are concerned about moving data around, it is a trivial memory move, that has very little overhead. And on the plus side, reading or traversing the vector is always with the cache. Following the linked list can create more overhead than you saved in the first place.

If you are after a sorted list, use map. A map is always sorted after the key. Inserting and lookup is done in O(log(n)). In a linked list everything is worse than O(n). And in a vector, access is O(1).

1 Like

You could also consider using MidiMessageSequence.