- if (_model) {
- // Find appropriate model iterator
- Evoral::Sequence<Evoral::MusicalTime>::const_iterator& i = _model_iter;
- if (_last_read_end == 0 || start != _last_read_end || !_model_iter_valid) {
- // Cached iterator is invalid, search for the first event past start
- i = _model->begin(converter.from(start), false, filtered);
- _model_iter_valid = true;
- }
+ if (!_model) {
+ return read_unlocked (lm, dst, source_start, start, cnt, loop_range, tracker, filter);
+ }
+
+ // Find appropriate model iterator
+ Evoral::Sequence<Evoral::Beats>::const_iterator& i = cursor.iter;
+ const bool linear_read = cursor.last_read_end != 0 && start == cursor.last_read_end;
+ if (!linear_read || !i.valid()) {
+ /* Cached iterator is invalid, search for the first event past start.
+ Note that multiple tracks can use a MidiSource simultaneously, so
+ all playback state must be in parameters (the cursor) and must not
+ be cached in the source of model itself.
+ See http://tracker.ardour.org/view.php?id=6541
+ */
+ cursor.connect(Invalidated);
+ cursor.iter = _model->begin(converter.from(start), false, filtered, &cursor.active_notes);
+ cursor.active_notes.clear();
+ }
+
+ cursor.last_read_end = start + cnt;
+
+ // Copy events in [start, start + cnt) into dst
+ for (; i != _model->end(); ++i) {
+
+ // Offset by source start to convert event time to session time
+
+ framepos_t time_frames = _session.tempo_map().frame_at_quarter_note (i->time().to_double() + start_qn);