+void
+MidiSource::mark_midi_streaming_write_completed (const Lock& lock,
+ Evoral::Sequence<Evoral::MusicalTime>::StuckNoteOption option,
+ Evoral::MusicalTime end)
+{
+ if (_model) {
+ _model->end_write (option, end);
+
+ /* Make captured controls discrete to play back user input exactly. */
+ for (MidiModel::Controls::iterator i = _model->controls().begin(); i != _model->controls().end(); ++i) {
+ if (i->second->list()) {
+ i->second->list()->set_interpolation(Evoral::ControlList::Discrete);
+ _interpolation_style.insert(std::make_pair(i->second->parameter(), Evoral::ControlList::Discrete));
+ }
+ }
+ }
+
+ _writing = false;
+}
+
+void
+MidiSource::mark_streaming_write_completed (const Lock& lock)
+{
+ mark_midi_streaming_write_completed (lock, Evoral::Sequence<Evoral::MusicalTime>::DeleteStuckNotes);
+}
+
+int
+MidiSource::write_to (const Lock& lock, boost::shared_ptr<MidiSource> newsrc, Evoral::MusicalTime begin, Evoral::MusicalTime end)
+{
+ Lock newsrc_lock (newsrc->mutex ());
+
+ newsrc->set_timeline_position (_timeline_position);
+ newsrc->copy_interpolation_from (this);
+ newsrc->copy_automation_state_from (this);
+
+ if (_model) {
+ if (begin == Evoral::MinMusicalTime && end == Evoral::MaxMusicalTime) {
+ _model->write_to (newsrc, newsrc_lock);
+ } else {
+ _model->write_section_to (newsrc, newsrc_lock, begin, end);
+ }
+ } else {
+ error << string_compose (_("programming error: %1"), X_("no model for MidiSource during ::clone()"));
+ return -1;
+ }
+
+ newsrc->flush_midi(newsrc_lock);
+
+ /* force a reload of the model if the range is partial */
+
+ if (begin != Evoral::MinMusicalTime || end != Evoral::MaxMusicalTime) {
+ newsrc->load_model (newsrc_lock, true);
+ } else {
+ newsrc->set_model (newsrc_lock, _model);
+ }
+
+ /* this file is not removable (but since it is MIDI, it is mutable) */
+
+ boost::dynamic_pointer_cast<FileSource> (newsrc)->prevent_deletion ();
+
+ return 0;
+}
+
+void
+MidiSource::session_saved()
+{
+ Lock lm (_lock);
+
+ /* this writes a copy of the data to disk.
+ XXX do we need to do this every time?
+ */
+
+ if (_model && _model->edited()) {
+ /* The model is edited, write its contents into the current source
+ file (overwiting previous contents). */
+
+ /* Temporarily drop our reference to the model so that as the model
+ pushes its current state to us, we don't try to update it. */
+ boost::shared_ptr<MidiModel> mm = _model;
+ _model.reset ();
+
+ /* Flush model contents to disk. */
+ mm->sync_to_source (lm);
+
+ /* Reacquire model. */
+ _model = mm;
+
+ } else {
+ flush_midi(lm);
+ }
+}
+
+void
+MidiSource::set_note_mode(const Lock& lock, NoteMode mode)
+{
+ if (_model) {
+ _model->set_note_mode(mode);
+ }
+}
+
+void
+MidiSource::drop_model (const Lock& lock)
+{
+ _model.reset();
+ invalidate(lock);
+ ModelChanged (); /* EMIT SIGNAL */
+}
+
+void
+MidiSource::set_model (const Lock& lock, boost::shared_ptr<MidiModel> m)
+{
+ _model = m;
+ invalidate(lock);
+ ModelChanged (); /* EMIT SIGNAL */
+}
+
+Evoral::ControlList::InterpolationStyle
+MidiSource::interpolation_of (Evoral::Parameter p) const
+{
+ InterpolationStyleMap::const_iterator i = _interpolation_style.find (p);
+ if (i == _interpolation_style.end()) {
+ return EventTypeMap::instance().interpolation_of (p);
+ }
+
+ return i->second;
+}
+
+AutoState
+MidiSource::automation_state_of (Evoral::Parameter p) const
+{
+ AutomationStateMap::const_iterator i = _automation_state.find (p);
+ if (i == _automation_state.end()) {
+ /* default to `play', otherwise if MIDI is recorded /
+ imported with controllers etc. they are by default
+ not played back, which is a little surprising.
+ */
+ return Play;
+ }
+
+ return i->second;
+}
+
+/** Set interpolation style to be used for a given parameter. This change will be
+ * propagated to anyone who needs to know.
+ */
+void
+MidiSource::set_interpolation_of (Evoral::Parameter p, Evoral::ControlList::InterpolationStyle s)
+{
+ if (interpolation_of (p) == s) {
+ return;
+ }
+
+ if (EventTypeMap::instance().interpolation_of (p) == s) {
+ /* interpolation type is being set to the default, so we don't need a note in our map */
+ _interpolation_style.erase (p);
+ } else {
+ _interpolation_style[p] = s;
+ }
+
+ InterpolationChanged (p, s); /* EMIT SIGNAL */
+}
+
+void
+MidiSource::set_automation_state_of (Evoral::Parameter p, AutoState s)
+{
+ if (automation_state_of (p) == s) {
+ return;
+ }
+
+ if (s == Play) {
+ /* automation state is being set to the default, so we don't need a note in our map */
+ _automation_state.erase (p);
+ } else {
+ _automation_state[p] = s;
+ }
+
+ AutomationStateChanged (p, s); /* EMIT SIGNAL */
+}
+
+void
+MidiSource::copy_interpolation_from (boost::shared_ptr<MidiSource> s)
+{
+ copy_interpolation_from (s.get ());
+}
+
+void
+MidiSource::copy_automation_state_from (boost::shared_ptr<MidiSource> s)
+{
+ copy_automation_state_from (s.get ());
+}
+
+void
+MidiSource::copy_interpolation_from (MidiSource* s)
+{
+ _interpolation_style = s->_interpolation_style;
+
+ /* XXX: should probably emit signals here */
+}
+
+void
+MidiSource::copy_automation_state_from (MidiSource* s)
+{
+ _automation_state = s->_automation_state;
+
+ /* XXX: should probably emit signals here */
+}