+uint8_t
+MidiRegionView::get_velocity_for_add (MidiModel::TimeType time) const
+{
+ if (_model->notes().empty()) {
+ 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;
+ return (*m)->velocity();
+ }
+
+ // Interpolate velocity of surrounding notes
+ MidiModel::Notes::const_iterator n = m;
+ --n;
+
+ const double frac = ((time - (*n)->time()).to_double() /
+ ((*m)->time() - (*n)->time()).to_double());
+
+ return (*n)->velocity() + (frac * ((*m)->velocity() - (*n)->velocity()));
+}
+