Removing notes from a MidiList

What is the best way to remove a MidiNote from a MidiList in Tracktion? It’s not as easy as it looks:

        auto v = createNoteValueTree(55, beat, 0.5, 127, 0);

        auto note = te::MidiNote(v);

        auto noteState = note.state;

        auto& sequence = clip.getSequence();

        sequence.addNote(note, nullptr);

        sequence.removeNote(note, nullptr);   // doesn't remove the note

        if (const auto noteState2 = sequence.getNoteFor(note.state))    // also doesn't remove the note
            sequence.removeNote(*noteState2, nullptr);

Short of raking through the state ValueTree manually, I can’t see how to do it. Am I missing something? Should I just mute the note instead?

On a related note, it seems that notes are still there on the list after they have been played. Is it possible / advisable to remove past notes? After

addNote creates a copy of the state so you’d need to remove the note state returned by addNote. E.g.

auto addedNote = sequence.addNote (note, nullptr);
sequence.removeNote (*addedNote);

Why would notes be removed after they’ve been played? That would be very odd in a DAW if every time the play head moved over a note they disappeared…

But it sounds like you’ve not quite understood the model/playback separation here. The MidiList is the model of the MIDI sequence, that is independent of it being played back.
When the model needs to be played back, a juce::MidiMessageSequence is created using MidiList::exportToPlaybackMidiSequence which is then used to create a MidiNode which adds the notes from the sequence to the play back graph at the correct play head time.

Ah, I didn’t realize that addNote() returned anything. Very simple indeed. Thanks for the clarification.

I have a follow-up question. I’ve managed to remove notes from the MidiList model as you advised, but I’m finding that this orphans notes which haven’t received a note-off message. So if I do addNote(duraiton = 5 seconds), wait for 2 seconds and then removeNote(), the note stays on without ever turning off.

My use case is a little different than standard DAW, but it seems like this would be a common scenario that any DAW would have to deal with: the user clicks on a note to turn it off after it has been played, but before the note-off has been sent. What is the recommended way or dealing with it in Tracktion?

I think the best way to deal with that at the moment is to actually trigger a note off when you remove the note. See AudioTrack::injectLiveMidiMessage

I agree it would be nice if it did this automatically though. Unfortunately it’s not as simple as it seems as it requires keeping track of all the currently playing MIDI notes from all possible sources and then triggering note offs only if the note states change and there hasn’t been a corresponding note-off MIDI message in the current buffer.