From bc6766fc3fc951d514964eb1e4ec457aa52e2919 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Thu, 18 Nov 2021 10:03:34 -0700 Subject: [PATCH] midi region view: fix crashes when adding notes A region may have no notes, or none in the correct time range. Finding a note to get channel or velocity info from may fail --- gtk2_ardour/midi_region_view.cc | 34 ++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/gtk2_ardour/midi_region_view.cc b/gtk2_ardour/midi_region_view.cc index b142c9b2ac..90e8bf40d2 100644 --- a/gtk2_ardour/midi_region_view.cc +++ b/gtk2_ardour/midi_region_view.cc @@ -4261,9 +4261,17 @@ MidiRegionView::get_channel_for_add (MidiModel::TimeType time) const } /* second, use the nearest note in the region-view (consistent with get_velocity_for_add behavior) */ + if (!_model->notes().empty()) { MidiModel::Notes::const_iterator m = _model->note_lower_bound(time); - return (*m)->channel(); + if (m == _model->notes().begin()) { + // Before the start, use the channel of the first note + return (*m)->channel(); + } else if (m == _model->notes().end()) { + // Past the end, use the channel of the last note + --m; + return (*m)->channel(); + } } /* lastly: query the track's channel filter */ @@ -4284,17 +4292,25 @@ MidiRegionView::get_velocity_for_add (MidiModel::TimeType time) const return editor.draw_velocity(); } - if (_model->notes().empty()) { + if (_model->notes().size() < 2) { return 0x40; // No notes, use default } - MidiModel::Notes::const_iterator m = _model->note_lower_bound(time); - if (m == _model->notes().begin()) { - // Before the start, use the velocity of the first note - return (*m)->velocity(); - } else if (m == _model->notes().end()) { - // Past the end, use the velocity of the last note - --m; + MidiModel::Notes::const_iterator m = _model->notes().end(); + + if (!_model->notes().empty()) { + m = _model->note_lower_bound(time); + if (m == _model->notes().begin()) { + // Before the start, use the velocity of the first note + return (*m)->velocity(); + } else if (m == _model->notes().end()) { + // Past the end, use the velocity of the last note + --m; + return (*m)->velocity(); + } + } + + if (_model->notes().size() == 1) { return (*m)->velocity(); }