Issue with splitting clips in midi recording demo [solved]

I am trying to implement the ability to split a midi clip. I started by modifying the midi recording demo a bit to simply split the selected clip at the playhead whenever the new track button is pressed:

newTrackButton.onClick = [this]
            edit->ensureNumberOfAudioTracks (getAudioTracks (*edit).size() + 1);
            auto sel = selectionManager.getSelectedObject (0);
            if (auto clip = dynamic_cast<te::MidiClip*> (sel))
                if (auto clipTrack = clip->getClipTrack())
                    clipTrack->splitClip(*clip, edit->getTransport().getCurrentPosition());


However I have encountered some weird UI behavior. The clip gets split, and the 2 parts of the clip are drawn in the correct places, however the midi notes are not drawn correctly. It seems like the notes of the first part of the clip get duplicatd in the second part. This is only a UI issue, the correct notes are played back by the engine, its just they dont look correct on screen. I cannot seem to figure out why this is happening. The clip component updates correctly as the clip is shown split in the correct place, and is 2 distinct clips. But the notes are not correct.

Here are some pictures of what I am describing:

Before splitting:

After splitting (notice how the notes in the first segment are duplicated in the second):

@RolandMR any ideas on why this is happening? Am I misunderstanding what splitClip does? Do I need to manually copy the midi sequence into the new, split clip? My initial thought is that the clip track only knows start and end times and doesnt know about any midi stuff since its a generic clip track. I can see that in splitClip the state of the old clip is just copied into the new clip and the start and end times are updated, which would be why the start and end times are correct in the UI. How can I correct the midi notes?

The UI drawing code in the demo is pretty simple. I’m pretty sure I don’t take looping or offset of the midi clip into account. When you split a midi clip, it duplicates the clip, shorts the first one, moves the start of the second one, and then increases the offset by the same amount.


Thank you! the offset was what I was missing. I modified the drawing code to subtract the offset in beats from the start and end beats and the notes are being displayed correctly now.

In the midi clip component paint method:

for (auto n : seq.getNotes())
    double startBeat = mc->getStartBeat() + n->getStartBeat() - mc->getOffsetInBeats();
    double endBeat = mc->getStartBeat() + n->getEndBeat() - mc->getOffsetInBeats();

    auto& tempoSequence = clip->edit.tempoSequence;

    auto startTime = tempoSequence.beatsToTime(startBeat);
    auto endTime = tempoSequence.beatsToTime(endBeat);