Assert when trying to record MIDI

I am getting an assert in tracktion_EditTimeRange.cpp Line 16 when I try to record from a MIDI input using the block of code below. It is complaining that the EndTime (negative) is less that the StartTime (0.0). For some reason performStop() is being called while the recording is in pre-playback time.

Is this the correct way to record MIDI? I used the RecordingDemo code to write this, though that was written for wave input. Oddly enough, when recording Wave Input I don’t get this assert.

edit = std::make_unique<te::Edit>(engine, te::createEmptyEdit(), te::Edit::forEditing, nullptr, 0);
auto track = te::getAudioTracks(*edit)[0];

edit->playInStopEnabled = true;

auto& dm = engine.getDeviceManager();

for (int i = 0; i < dm.getNumMidiInDevices(); i++)
    if (auto wip = dm.getMidiInDevice(i))


for (auto instance : edit->getAllInputDevices())
    if (instance->getInputDevice().getDeviceType() == te::InputDevice::physicalMidiDevice)
        instance->setTargetTrack(track, 0);


auto& transport = edit->getTransport();

You’re assigning all the physical MIDI input devices to the same slot on the first track, they will overwrite each other, are you sure this is what you want? Wouldn’t it be better to assign only the MIDI input you want to assign?

Apart from that it looks ok, when is the MIDI recording stopped? Can you trace when applyLastRecordingToEdit is called and what the time range there is?

I only have 1 Midi device so at the moment this is ok.

I never got to the point of stopping the recording, as soon as I call record() it asserts.

It seems stopRecording() is being called twice for some reason.
The first time the time range is fine 0,0 and applyLastRecordingToEdit is called properly.
The second time, the time range is 0, -2.46 (recEndTime, recEndPos both -2.46) and the assert occurs on Line 1306 of tracktion_TransportControl.cpp before applyLastRecordingToEdit() or even recordingFinished() can be called.

Is the above your actual code? If so, the Edit will be deleted as soon as the recording starts and the whole thing will fall apart.

I’m not sure why stopRecording is being called twice, that seems odd. Are you sure it’s being called after the recording has started?

The above is most of the actual code, edit is a member variable outside the block so it isn’t being deleted once the above code is run.

Sorry I meant to say performStop in my previous post and not stopRecording.

It seems that after calling transport.record(false); in the code above, that performRecord is being called two separate times from Line 227 in tracktion_TransportControl.cpp. In performRecord it is calling stop (false, false); and the second time performStop has the (0, -2.46) values which is causing the assert.

Can you perhaps upload a minimal example project or a PIP that I can test with?
It’s a bit difficult to visualise what state you’ve got the recording in to without being able to see and trace all the code.

I have attached a sample project made with Projucer.
A Midi Input device should be plugged in before running the project. (73.1 KB)

Sorry for the delay in tracking this down. It would only happen if you tried to record before you ever tried to play. Turns out CachedValues call their callbacks if you set them to their current value if the current value is the default. So the first time you tried to record it would set playing to false, and even though playing was already false, it would still call the callback and stop the recording that was in the middle of starting. A fix should appear on develop soon.