I’m working on a sequencer using Tracktion Engine, and I have the code below which creates a MIDI output and loops over 1 bar allowing the user to toggle notes on and off using an external controller. It all works, but when I modify the sequence, either adding notes, removing notes, clearing the sequence, etc. the playback starts from the beginning, sometimes after a small delay. How can I get it to continue playing and not restart when the MidiSequence is modified? I tried creating a separate sequence and clearing the current one then copying the sequence using addFrom() but that didn’t work.
void createMidiOut()
{
track = tracktion_engine::getAudioTracks(*edit)[0];
if (track)
{
track->getOutput().setOutputByName("IAC Driver Bus 2");
const te::EditTimeRange editTimeRange(0, edit->tempoSequence.barsBeatsToTime({ 1, 0.0 }));
track->insertMIDIClip("MIDI Clip", editTimeRange, nullptr);
if (auto clip = dynamic_cast<te::MidiClip*> (track->getClips()[0]))
{
midiClip = *clip;
}
}
}
void loop()
{
auto& transport = edit->getTransport();
transport.setLoopRange({ 0.0, edit->getLength() });
transport.looping = true;
transport.play(false);
}
void toggleNote(int col, int row)
{
double beats = edit->tempoSequence.timeToBeats(col * .125);
Note note = {
75 + output->getCols() - row,
beats,
.25,
127
};
auto& transport = edit->getTransport();
double pos = transport.getCurrentPosition();
auto& seq = midiClip->getSequence();
if (notes.find(note) == notes.end())
{
auto midiNote = seq.addNote(note.pitch, note.beats, note.length, note.velocity, 0, nullptr);
notes[note] = midiNote;
}
else
{
seq.removeNote(*notes[note], nullptr);
notes.erase(note);
}
transport.setCurrentPosition(pos);
}