MidiClock_SlaveTest: add basic framework
[ardour.git] / libs / ardour / midi_model.cc
index 02b076a5e5c24b457d186127c41aa2cbe874fbdd..baa3c2c7c016d4c54b408de7cf38e48c6d78e126 100644 (file)
@@ -1,22 +1,22 @@
 /*
- Copyright (C) 2007 Paul Davis 
- Written by Dave Robillard, 2007
+    Copyright (C) 2007 Paul Davis
+    Author: Dave Robillard
 
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
- */
+*/
 
 #define __STDC_LIMIT_MACROS 1
 
 #include <algorithm>
 #include <stdexcept>
 #include <stdint.h>
-#include <pbd/enumwriter.h>
-#include <midi++/events.h>
+#include "pbd/error.h"
+#include "pbd/enumwriter.h"
+#include "midi++/events.h"
 
-#include <ardour/midi_model.h>
-#include <ardour/midi_source.h>
-#include <ardour/types.h>
-#include <ardour/session.h>
+#include "ardour/midi_model.h"
+#include "ardour/midi_source.h"
+#include "ardour/smf_source.h"
+#include "ardour/types.h"
+#include "ardour/session.h"
 
 using namespace std;
 using namespace ARDOUR;
+using namespace PBD;
 
-void MidiModel::write_lock() {
-       _lock.writer_lock();
-       _automation_lock.lock();
+MidiModel::MidiModel(MidiSource* s, size_t size)
+       : AutomatableSequence<TimeType>(s->session(), size)
+       , _midi_source(s)
+{
 }
 
-void MidiModel::write_unlock() {
-       _lock.writer_unlock();
-       _automation_lock.unlock();
+/** Start a new Delta command.
+ *
+ * This has no side-effects on the model or Session, the returned command
+ * can be held on to for as long as the caller wishes, or discarded without
+ * formality, until apply_command is called and ownership is taken.
+ */
+MidiModel::DeltaCommand*
+MidiModel::new_delta_command(const string name)
+{
+       DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
+       return cmd;
 }
 
-void MidiModel::read_lock() const {
-       _lock.reader_lock();
-       /*_automation_lock.lock();*/
+/** Start a new Diff command.
+ *
+ * This has no side-effects on the model or Session, the returned command
+ * can be held on to for as long as the caller wishes, or discarded without
+ * formality, until apply_command is called and ownership is taken.
+ */
+MidiModel::DiffCommand*
+MidiModel::new_diff_command(const string name)
+{
+       DiffCommand* cmd = new DiffCommand(_midi_source->model(), name);
+       return cmd;
 }
 
-void MidiModel::read_unlock() const {
-       _lock.reader_unlock();
-       /*_automation_lock.unlock();*/
+/** Apply a command.
+ *
+ * Ownership of cmd is taken, it must not be deleted by the caller.
+ * The command will constitute one item on the undo stack.
+ */
+void
+MidiModel::apply_command(Session& session, Command* cmd)
+{
+       session.begin_reversible_command(cmd->name());
+       (*cmd)();
+       session.commit_reversible_command(cmd);
+       set_edited(true);
 }
 
-// Read iterator (const_iterator)
-
-MidiModel::const_iterator::const_iterator(const MidiModel& model, double t)
-       : _model(&model)
-       , _is_end( (t == DBL_MAX) || model.empty() )
-       , _locked( !_is_end )
+/** Apply a command as part of a larger reversible transaction
+ *
+ * Ownership of cmd is taken, it must not be deleted by the caller.
+ * The command will constitute one item on the undo stack.
+ */
+void
+MidiModel::apply_command_as_subcommand(Session& session, Command* cmd)
 {
-       //cerr << "Created MIDI iterator @ " << t << " (is end: " << _is_end << ")" << endl;
-
-       if (_is_end) {
-               return;
-       }
-
-       model.read_lock();
-
-       _note_iter = model.notes().end();
-       // find first note which begins after t
-       for (MidiModel::Notes::const_iterator i = model.notes().begin(); i != model.notes().end(); ++i) {
-               if ((*i)->time() >= t) {
-                       _note_iter = i;
-                       break;
-               }
-       }
-
-       MidiControlIterator earliest_control(boost::shared_ptr<AutomationList>(), DBL_MAX, 0.0);
-
-       _control_iters.reserve(model.controls().size());
-       
-       // find the earliest control event available
-       for (Automatable::Controls::const_iterator i = model.controls().begin();
-                       i != model.controls().end(); ++i) {
+       (*cmd)();
+       session.add_command(cmd);
+       set_edited(true);
+}
 
-               assert(
-                       i->first.type() == MidiCCAutomation ||
-                       i->first.type() == MidiPgmChangeAutomation ||
-                       i->first.type() == MidiPitchBenderAutomation ||
-                       i->first.type() == MidiChannelAftertouchAutomation);
 
-               double x, y;
-               bool ret = i->second->list()->rt_safe_earliest_event_unlocked(t, DBL_MAX, x, y);
-               if (!ret) {
-                       //cerr << "MIDI Iterator: CC " << i->first.id() << " (size " << i->second->list()->size()
-                       //      << ") has no events past " << t << endl;
-                       continue;
-               }
+// DeltaCommand
 
-               assert(x >= 0);
+MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
+       : Command(name)
+       , _model(m)
+       , _name(name)
+{
+       assert(_model);
+}
 
-               if (y < i->first.min() || y > i->first.max()) {
-                       cerr << "ERROR: Controller (" << i->first.to_string() << ") value '" << y
-                               << "' out of range [" << i->first.min() << "," << i->first.max()
-                               << "], event ignored" << endl;
-                       continue;
-               }
+MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
+       : _model(m)
+{
+       assert(_model);
+       set_state(node, Stateful::loading_state_version);
+}
 
-               const MidiControlIterator new_iter(i->second->list(), x, y);
+void
+MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
+{
+       _removed_notes.remove(note);
+       _added_notes.push_back(note);
+}
 
-               //cerr << "MIDI Iterator: CC " << i->first.id() << " added (" << x << ", " << y << ")" << endl;
-               _control_iters.push_back(new_iter);
+void
+MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
+{
+       _added_notes.remove(note);
+       _removed_notes.push_back(note);
+}
 
-               // if the x of the current control is less than earliest_control
-               // we have a new earliest_control
-               if (x < earliest_control.x) {
-                       earliest_control = new_iter;
-                       _control_iter = _control_iters.end();
-                       --_control_iter;
-                       // now _control_iter points to the last Element in _control_iters
-               }
-       }
+void
+MidiModel::DeltaCommand::operator()()
+{
+       // This could be made much faster by using a priority_queue for added and
+       // removed notes (or sort here), and doing a single iteration over _model
 
-       if (_note_iter != model.notes().end()) {
-               _event = boost::shared_ptr<MIDI::Event>(new MIDI::Event((*_note_iter)->on_event(), true));
-       }
+       MidiModel::WriteLock lock(_model->edit_lock());
 
-       double time = DBL_MAX;
-       // in case we have no notes in the region, we still want to get controller messages
-       if (_event.get()) {
-               time = _event->time();
-               // if the note is going to make it this turn, advance _note_iter
-               if (earliest_control.x > time) {
-                       _active_notes.push(*_note_iter);
-                       ++_note_iter;
-               }
-       }
-       
-       // <=, because we probably would want to send control events first 
-       if (earliest_control.automation_list.get() && earliest_control.x <= time) {
-               model.control_to_midi_event(_event, earliest_control);
-       } else {
-               _control_iter = _control_iters.end();
+       for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
+               _model->add_note_unlocked(*i);
        }
 
-       if ( (! _event.get()) || _event->size() == 0) {
-               //cerr << "Created MIDI iterator @ " << t << " is at end." << endl;
-               _is_end = true;
-
-               // eliminate possible race condition here (ugly)
-               static Glib::Mutex mutex;
-               Glib::Mutex::Lock lock(mutex);
-               if (_locked) {
-                       _model->read_unlock();
-                       _locked = false;
-               }
-       } else {
-               //printf("New MIDI Iterator = %X @ %lf\n", _event->type(), _event->time());
+       for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
+               _model->remove_note_unlocked(*i);
        }
 
-       assert(_is_end || (_event->buffer() && _event->buffer()[0] != '\0'));
+       lock.reset();
+       _model->ContentsChanged(); /* EMIT SIGNAL */
 }
 
-MidiModel::const_iterator::~const_iterator()
+void
+MidiModel::DeltaCommand::undo()
 {
-       if (_locked) {
-               _model->read_unlock();
-       }
-}
+       // This could be made much faster by using a priority_queue for added and
+       // removed notes (or sort here), and doing a single iteration over _model
 
-const MidiModel::const_iterator& MidiModel::const_iterator::operator++()
-{
-       if (_is_end) {
-               throw std::logic_error("Attempt to iterate past end of MidiModel");
-       }
-       
-       assert(_event->buffer() && _event->buffer()[0] != '\0');
+       MidiModel::WriteLock lock(_model->edit_lock());;
 
-       /*cerr << "const_iterator::operator++: _event type:" << hex << "0x" << int(_event->type()) 
-        << "   buffer: 0x" << int(_event->buffer()[0]) << " 0x" << int(_event->buffer()[1]) 
-        << " 0x" << int(_event->buffer()[2]) << endl;*/
+       for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
+               _model->remove_note_unlocked(*i);
+       }
 
-       if (! (_event->is_note() || _event->is_cc() || _event->is_pgm_change() || _event->is_pitch_bender() || _event->is_channel_aftertouch()) ) {
-               cerr << "FAILED event buffer: " << hex << int(_event->buffer()[0]) << int(_event->buffer()[1]) << int(_event->buffer()[2]) << endl;
+       for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
+               _model->add_note_unlocked(*i);
        }
-       assert((_event->is_note() || _event->is_cc() || _event->is_pgm_change() || _event->is_pitch_bender() || _event->is_channel_aftertouch()));
 
-       // Increment past current control event
-       if (!_event->is_note() && _control_iter != _control_iters.end() && _control_iter->automation_list.get()) {
-               double x = 0.0, y = 0.0;
-               const bool ret = _control_iter->automation_list->rt_safe_earliest_event_unlocked(
-                               _control_iter->x, DBL_MAX, x, y, false);
+       lock.reset();
+       _model->ContentsChanged(); /* EMIT SIGNAL */
+}
 
-               if (ret) {
-                       _control_iter->x = x;
-                       _control_iter->y = y;
-               } else {
-                       _control_iter->automation_list.reset();
-                       _control_iter->x = DBL_MAX;
-               }
-       }
+XMLNode&
+MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
+{
+       XMLNode* xml_note = new XMLNode("note");
+       ostringstream note_str(ios::ate);
+       note_str << int(note->note());
+       xml_note->add_property("note", note_str.str());
 
-       const std::vector<MidiControlIterator>::iterator old_control_iter = _control_iter;
-       _control_iter = _control_iters.begin();
+       ostringstream channel_str(ios::ate);
+       channel_str << int(note->channel());
+       xml_note->add_property("channel", channel_str.str());
 
-       // find the _control_iter with the earliest event time
-       for (std::vector<MidiControlIterator>::iterator i = _control_iters.begin();
-                       i != _control_iters.end(); ++i) {
-               if (i->x < _control_iter->x) {
-                       _control_iter = i;
-               }
-       }
+       ostringstream time_str(ios::ate);
+       time_str << int(note->time());
+       xml_note->add_property("time", time_str.str());
 
-       enum Type {NIL, NOTE_ON, NOTE_OFF, AUTOMATION};
+       ostringstream length_str(ios::ate);
+       length_str <<(unsigned int) note->length();
+       xml_note->add_property("length", length_str.str());
 
-       Type type = NIL;
-       double t = 0;
+       ostringstream velocity_str(ios::ate);
+       velocity_str << (unsigned int) note->velocity();
+       xml_note->add_property("velocity", velocity_str.str());
 
-       // Next earliest note on
-       if (_note_iter != _model->notes().end()) {
-               type = NOTE_ON;
-               t = (*_note_iter)->time();
-       }
+       return *xml_note;
+}
 
-       // Use the next earliest note off iff it's earlier than the note on
-       if (_model->note_mode() == Sustained && (! _active_notes.empty())) {
-               if (type == NIL || _active_notes.top()->end_time() <= (*_note_iter)->time()) {
-                       type = NOTE_OFF;
-                       t = _active_notes.top()->end_time();
-               }
-       }
+boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
+MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
+{
+       unsigned int note;
+       XMLProperty* prop;
+       unsigned int channel;
+       unsigned int time;
+       unsigned int length;
+       unsigned int velocity;
 
-       // Use the next earliest controller iff it's earlier than the note event
-       if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX /*&& _control_iter != old_control_iter */) {
-               if (type == NIL || _control_iter->x < t) {
-                       type = AUTOMATION;
-               }
+       if ((prop = xml_note->property("note")) != 0) {
+               istringstream note_str(prop->value());
+               note_str >> note;
+       } else {
+               warning << "note information missing note value" << endmsg;
+               note = 127;
        }
 
-       if (type == NOTE_ON) {
-               //cerr << "********** MIDI Iterator = note on" << endl;
-               *_event = (*_note_iter)->on_event();
-               _active_notes.push(*_note_iter);
-               ++_note_iter;
-       } else if (type == NOTE_OFF) {
-               //cerr << "********** MIDI Iterator = note off" << endl;
-               *_event = _active_notes.top()->off_event();
-               _active_notes.pop();
-       } else if (type == AUTOMATION) {
-               //cerr << "********** MIDI Iterator = Automation" << endl;
-               _model->control_to_midi_event(_event, *_control_iter);
+       if ((prop = xml_note->property("channel")) != 0) {
+               istringstream channel_str(prop->value());
+               channel_str >> channel;
        } else {
-               //cerr << "********** MIDI Iterator = End" << endl;
-               _is_end = true;
+               warning << "note information missing channel" << endmsg;
+               channel = 0;
        }
 
-       assert(_is_end || _event->size() > 0);
-
-       return *this;
-}
-
-bool MidiModel::const_iterator::operator==(const const_iterator& other) const
-{
-       if (_is_end || other._is_end) {
-               return (_is_end == other._is_end);
+       if ((prop = xml_note->property("time")) != 0) {
+               istringstream time_str(prop->value());
+               time_str >> time;
        } else {
-               return (_event == other._event);
+               warning << "note information missing time" << endmsg;
+               time = 0;
        }
-}
 
-MidiModel::const_iterator& MidiModel::const_iterator::operator=(const const_iterator& other)
-{
-       if (_locked && _model != other._model) {
-               _model->read_unlock();
+       if ((prop = xml_note->property("length")) != 0) {
+               istringstream length_str(prop->value());
+               length_str >> length;
+       } else {
+               warning << "note information missing length" << endmsg;
+               length = 1;
        }
 
-       _model         = other._model;
-       _active_notes  = other._active_notes;
-       _is_end        = other._is_end;
-       _locked        = other._locked;
-       _note_iter     = other._note_iter;
-       _control_iters = other._control_iters;
-       size_t index   = other._control_iter - other._control_iters.begin();
-       _control_iter  = _control_iters.begin() + index;
-       
-       if (!_is_end) {
-               _event =  boost::shared_ptr<MIDI::Event>(new MIDI::Event(*other._event, true));
+       if ((prop = xml_note->property("velocity")) != 0) {
+               istringstream velocity_str(prop->value());
+               velocity_str >> velocity;
+       } else {
+               warning << "note information missing velocity" << endmsg;
+               velocity = 127;
        }
 
-       return *this;
+       boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
+                       channel, time, length, note, velocity));
+       return note_ptr;
 }
 
-// MidiModel
-
-MidiModel::MidiModel(MidiSource *s, size_t size)
-       : Automatable(s->session(), "midi model")
-       , _notes(size)
-       , _note_mode(Sustained)
-       , _writing(false)
-       , _edited(false)
-       , _end_iter(*this, DBL_MAX)
-       , _next_read(UINT32_MAX)
-       , _read_iter(*this, DBL_MAX)
-       , _midi_source(s)
-{
-       assert(_end_iter._is_end);
-       assert( ! _end_iter._locked);
-}
+#define ADDED_NOTES_ELEMENT "AddedNotes"
+#define REMOVED_NOTES_ELEMENT "RemovedNotes"
+#define DELTA_COMMAND_ELEMENT "DeltaCommand"
 
-/** Read events in frame range \a start .. \a start+cnt into \a dst,
- * adding \a stamp_offset to each event's timestamp.
- * \return number of events written to \a dst
- */
-size_t MidiModel::read(MidiRingBuffer& dst, nframes_t start, nframes_t nframes,
-               nframes_t stamp_offset, nframes_t negative_stamp_offset) const
+int
+MidiModel::DeltaCommand::set_state (const XMLNode& delta_command, int /*version*/)
 {
-       //cerr << this << " MM::read @ " << start << " frames: " << nframes << " -> " << stamp_offset << endl;
-       //cerr << this << " MM # notes: " << n_notes() << endl;
-
-       size_t read_events = 0;
-
-       if (start != _next_read) {
-               _read_iter = const_iterator(*this, (double)start);
-               //cerr << "Repositioning iterator from " << _next_read << " to " << start << endl;
-       } else {
-               //cerr << "Using cached iterator at " << _next_read << endl;
+       if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
+               return 1;
        }
 
-       _next_read = start + nframes;
-
-       while (_read_iter != end() && _read_iter->time() < start + nframes) {
-               assert(_read_iter->size() > 0);
-               assert(_read_iter->buffer());
-               dst.write(_read_iter->time() + stamp_offset - negative_stamp_offset,
-                         _read_iter->size(), 
-                         _read_iter->buffer());
-               
-                /*cerr << this << " MidiModel::read event @ " << _read_iter->time()  
-                << " type: " << hex << int(_read_iter->type()) << dec 
-                << " note: " << int(_read_iter->note()) 
-                << " velocity: " << int(_read_iter->velocity()) 
-                << endl;*/
-               
-               ++_read_iter;
-               ++read_events;
+       _added_notes.clear();
+       XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
+       if (added_notes) {
+               XMLNodeList notes = added_notes->children();
+               transform(notes.begin(), notes.end(), back_inserter(_added_notes),
+                         boost::bind (&DeltaCommand::unmarshal_note, this, _1));
        }
 
-       return read_events;
-}
-
-/** Write the controller event pointed to by \a iter to \a ev.
- * The buffer of \a ev will be allocated or resized as necessary.
- * \return true on success
- */
-bool
-MidiModel::control_to_midi_event(boost::shared_ptr<MIDI::Event>& ev, const MidiControlIterator& iter) const
-{
-       assert(iter.automation_list.get());
-       if (!ev) {
-               ev = boost::shared_ptr<MIDI::Event>(new MIDI::Event(0, 3, NULL, true));
-       }
-       
-       switch (iter.automation_list->parameter().type()) {
-       case MidiCCAutomation:
-               assert(iter.automation_list.get());
-               assert(iter.automation_list->parameter().channel() < 16);
-               assert(iter.automation_list->parameter().id() <= INT8_MAX);
-               assert(iter.y <= INT8_MAX);
-               
-               ev->time() = iter.x;
-               ev->realloc(3);
-               ev->buffer()[0] = MIDI_CMD_CONTROL + iter.automation_list->parameter().channel();
-               ev->buffer()[1] = (Byte)iter.automation_list->parameter().id();
-               ev->buffer()[2] = (Byte)iter.y;
-               break;
-
-       case MidiPgmChangeAutomation:
-               assert(iter.automation_list.get());
-               assert(iter.automation_list->parameter().channel() < 16);
-               assert(iter.automation_list->parameter().id() == 0);
-               assert(iter.y <= INT8_MAX);
-               
-               ev->time() = iter.x;
-               ev->realloc(2);
-               ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.automation_list->parameter().channel();
-               ev->buffer()[1] = (Byte)iter.y;
-               break;
-
-       case MidiPitchBenderAutomation:
-               assert(iter.automation_list.get());
-               assert(iter.automation_list->parameter().channel() < 16);
-               assert(iter.automation_list->parameter().id() == 0);
-               assert(iter.y < (1<<14));
-               
-               ev->time() = iter.x;
-               ev->realloc(3);
-               ev->buffer()[0] = MIDI_CMD_BENDER + iter.automation_list->parameter().channel();
-               ev->buffer()[1] = ((Byte)iter.y) & 0x7F; // LSB
-               ev->buffer()[2] = (((Byte)iter.y) >> 7) & 0x7F; // MSB
-               break;
-
-       case MidiChannelAftertouchAutomation:
-               assert(iter.automation_list.get());
-               assert(iter.automation_list->parameter().channel() < 16);
-               assert(iter.automation_list->parameter().id() == 0);
-               assert(iter.y <= INT8_MAX);
-
-               ev->time() = iter.x;
-               ev->realloc(2);
-               ev->buffer()[0]
-                               = MIDI_CMD_CHANNEL_PRESSURE + iter.automation_list->parameter().channel();
-               ev->buffer()[1] = (Byte)iter.y;
-               break;
-
-       default:
-               return false;
+       _removed_notes.clear();
+       XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
+       if (removed_notes) {
+               XMLNodeList notes = removed_notes->children();
+               transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
+                         boost::bind (&DeltaCommand::unmarshal_note, this, _1));
        }
 
-       return true;
+       return 0;
 }
 
-
-/** Clear all events from the model.
- */
-void MidiModel::clear()
+XMLNode&
+MidiModel::DeltaCommand::get_state()
 {
-       _lock.writer_lock();
-       _notes.clear();
-       clear_automation();
-       _next_read = 0;
-       _read_iter = end();
-       _lock.writer_unlock();
-}
+       XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
+       delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
 
+       XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
+       for_each(_added_notes.begin(), _added_notes.end(), 
+                boost::bind(
+                        boost::bind (&XMLNode::add_child_nocopy, *added_notes, _1),
+                        boost::bind (&DeltaCommand::marshal_note, this, _1)));
 
-/** Begin a write of events to the model.
- *
- * If \a mode is Sustained, complete notes with duration are constructed as note
- * on/off events are received.  Otherwise (Percussive), only note on events are
- * stored; note off events are discarded entirely and all contained notes will
- * have duration 0.
- */
-void MidiModel::start_write()
-{
-       //cerr << "MM " << this << " START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
-       write_lock();
-       _writing = true;
-       for (int i = 0; i < 16; ++i)
-               _write_notes[i].clear();
-       
-       _dirty_automations.clear();
-       write_unlock();
-}
+       XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
+       for_each(_removed_notes.begin(), _removed_notes.end(), 
+                boost::bind (
+                        boost::bind (&XMLNode::add_child_nocopy, *removed_notes, _1),
+                        boost::bind (&DeltaCommand::marshal_note, this, _1)));
 
-/** Finish a write of events to the model.
- *
- * If \a delete_stuck is true and the current mode is Sustained, note on events
- * that were never resolved with a corresonding note off will be deleted.
- * Otherwise they will remain as notes with duration 0.
- */
-void MidiModel::end_write(bool delete_stuck)
-{
-       write_lock();
-       assert(_writing);
-
-       //cerr << "MM " << this << " END WRITE: " << _notes.size() << " NOTES\n";
-
-       if (_note_mode == Sustained && delete_stuck) {
-               for (Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
-                       if ((*n)->duration() == 0) {
-                               cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
-                               n = _notes.erase(n);
-                               // we have to break here because erase invalidates the iterator
-                               break;
-                       } else {
-                               ++n;
-                       }
-               }
-       }
+       return *delta_command;
+}
 
-       for (int i = 0; i < 16; ++i) {
-               if (!_write_notes[i].empty()) {
-                       cerr << "WARNING: MidiModel::end_write: Channel " << i << " has "
-                                       << _write_notes[i].size() << " stuck notes" << endl;
-               }
-               _write_notes[i].clear();
-       }
+/************** DIFF COMMAND ********************/
 
-       for (AutomationLists::const_iterator i = _dirty_automations.begin(); i != _dirty_automations.end(); ++i) {
-               (*i)->Dirty.emit();
-               (*i)->lookup_cache().left = -1;
-               (*i)->search_cache().left = -1;
-       }
-       
-       _writing = false;
-       write_unlock();
-}
+#define DIFF_NOTES_ELEMENT "ChangedNotes"
+#define DIFF_COMMAND_ELEMENT "DiffCommand"
 
-/** Append \a in_event to model.  NOT realtime safe.
- *
- * Timestamps of events in \a buf are expected to be relative to
- * the start of this model (t=0) and MUST be monotonically increasing
- * and MUST be >= the latest event currently in the model.
- */
-void MidiModel::append(const MIDI::Event& ev)
+MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
+       : Command(name)
+       , _model(m)
+       , _name(name)
 {
-       write_lock();
-       _edited = true;
-
-       assert(_notes.empty() || ev.time() >= _notes.back()->time());
-       assert(_writing);
-
-       if (ev.is_note_on()) {
-               append_note_on_unlocked(ev.channel(), ev.time(), ev.note(),
-                               ev.velocity());
-       } else if (ev.is_note_off()) {
-               append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
-       } else if (ev.is_cc()) {
-               append_automation_event_unlocked(MidiCCAutomation, ev.channel(),
-                               ev.time(), ev.cc_number(), ev.cc_value());
-       } else if (ev.is_pgm_change()) {
-               append_automation_event_unlocked(MidiPgmChangeAutomation, ev.channel(),
-                               ev.time(), ev.pgm_number(), 0);
-       } else if (ev.is_pitch_bender()) {
-               append_automation_event_unlocked(MidiPitchBenderAutomation,
-                               ev.channel(), ev.time(), ev.pitch_bender_lsb(),
-                               ev.pitch_bender_msb());
-       } else if (ev.is_channel_aftertouch()) {
-               append_automation_event_unlocked(MidiChannelAftertouchAutomation,
-                               ev.channel(), ev.time(), ev.channel_aftertouch(), 0);
-       } else {
-               printf("WARNING: MidiModel: Unknown event type %X\n", ev.type());
-       }
-
-       write_unlock();
+       assert(_model);
 }
 
-void MidiModel::append_note_on_unlocked(uint8_t chan, double time,
-               uint8_t note_num, uint8_t velocity)
+MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
+       : _model(m)
 {
-       /*cerr << "MidiModel " << this << " chan " << (int)chan <<
-        " note " << (int)note_num << " on @ " << time << endl;*/
-
-       assert(chan < 16);
-       assert(_writing);
-       _edited = true;
-
-       boost::shared_ptr<Note> new_note(new Note(chan, time, 0, note_num, velocity));
-       _notes.push_back(new_note);
-       if (_note_mode == Sustained) {
-               //cerr << "MM Sustained: Appending active note on " << (unsigned)(uint8_t)note_num << endl;
-               _write_notes[chan].push_back(_notes.size() - 1);
-       }/* else {
-        cerr << "MM Percussive: NOT appending active note on" << endl;
-        }*/
+       assert(_model);
+       set_state(node, Stateful::loading_state_version);
 }
 
-void MidiModel::append_note_off_unlocked(uint8_t chan, double time,
-               uint8_t note_num)
+void
+MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
+                              uint8_t new_value)
 {
-       /*cerr << "MidiModel " << this << " chan " << (int)chan <<
-        " note " << (int)note_num << " off @ " << time << endl;*/
-
-       assert(chan < 16);
-       assert(_writing);
-       _edited = true;
-
-       if (_note_mode == Percussive) {
-               cerr << "MidiModel Ignoring note off (percussive mode)" << endl;
-               return;
-       }
-
-       /* FIXME: make _write_notes fixed size (127 noted) for speed */
-
-       /* FIXME: note off velocity for that one guy out there who actually has
-        * keys that send it */
+       NotePropertyChange change;
 
-       bool resolved = false;
+       change.note = note;
+       change.property = prop;
+       change.new_value = new_value;
 
-       for (WriteNotes::iterator n = _write_notes[chan].begin(); n
-                       != _write_notes[chan].end(); ++n) {
-               Note& note = *_notes[*n].get();
-               if (note.note() == note_num) {
-                       assert(time >= note.time());
-                       note.set_duration(time - note.time());
-                       _write_notes[chan].erase(n);
-                       //cerr << "MM resolved note, duration: " << note.duration() << endl;
-                       resolved = true;
-                       break;
-               }
+       switch (prop) {
+       case NoteNumber:
+               change.old_value = note->note();
+               break;
+       case Velocity:
+               change.old_value = note->velocity();
+               break;
+       case StartTime:
+               fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
+               /*NOTREACHED*/
+               break;
+       case Length:
+               fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
+               /*NOTREACHED*/
+               break;
+       case Channel:
+               change.old_value = note->channel();
+               break;
        }
 
-       if (!resolved) {
-               cerr << "MidiModel " << this << " spurious note off chan " << (int)chan
-                               << ", note " << (int)note_num << " @ " << time << endl;
-       }
+       _changes.push_back (change);
 }
 
-void MidiModel::append_automation_event_unlocked(AutomationType type,
-               uint8_t chan, double time, uint8_t first_byte, uint8_t second_byte)
+void
+MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
+                              TimeType new_time)
 {
-       //cerr << "MidiModel " << this << " chan " << (int)chan <<
-       //              " CC " << (int)number << " = " << (int)value << " @ " << time << endl;
-
-       assert(chan < 16);
-       assert(_writing);
-       _edited = true;
-       double value;
+       NotePropertyChange change;
 
-       uint32_t id = 0;
+       change.note = note;
+       change.property = prop;
+       change.new_time = new_time;
 
-       switch (type) {
-       case MidiCCAutomation:
-               id = first_byte;
-               value = double(second_byte);
+       switch (prop) {
+       case NoteNumber:
+       case Channel:
+       case Velocity:
+               fatal << "MidiModel::DiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
                break;
-       case MidiChannelAftertouchAutomation:
-       case MidiPgmChangeAutomation:
-               id = 0;
-               value = double(first_byte);
+       case StartTime:
+               change.old_time = note->time();
                break;
-       case MidiPitchBenderAutomation:
-               id = 0;
-               value = double((0x7F & second_byte) << 7 | (0x7F & first_byte));
+       case Length:
+               change.old_time = note->length();
                break;
-       default:
-               assert(false);
        }
 
-       Parameter param(type, id, chan);
-       boost::shared_ptr<AutomationControl> control = Automatable::control(param, true);
-       control->list()->rt_add(time, value);
+       _changes.push_back (change);
 }
 
-void MidiModel::add_note_unlocked(const boost::shared_ptr<Note> note)
+void
+MidiModel::DiffCommand::operator()()
 {
-       //cerr << "MidiModel " << this << " add note " << (int)note.note() << " @ " << note.time() << endl;
-       _edited = true;
-       Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note,
-                       note_time_comparator);
-       _notes.insert(i, note);
-}
+       MidiModel::WriteLock lock(_model->edit_lock());
 
-void MidiModel::remove_note_unlocked(const boost::shared_ptr<const Note> note)
-{
-       _edited = true;
-       //cerr << "MidiModel " << this << " remove note " << (int)note.note() << " @ " << note.time() << endl;
-       for (Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
-               Note& _n = *(*n);
-               const Note& _note = *note;
-               // TODO: There is still the issue, that after restarting ardour
-               // persisted undo does not work, because of rounding errors in the
-               // event times after saving/restoring to/from MIDI files
-               cerr << "======================================= " << endl;
-               cerr << int(_n.note()) << "@" << int(_n.time()) << "[" << int(_n.channel()) << "] --" << int(_n.duration()) << "-- #" << int(_n.velocity()) << endl;
-               cerr << int(_note.note()) << "@" << int(_note.time()) << "[" << int(_note.channel()) << "] --" << int(_note.duration()) << "-- #" << int(_note.velocity()) << endl;
-               cerr << "Equal: " << bool(_n == _note) << endl;
-               cerr << endl << endl;
-               if (_n == _note) {
-                       _notes.erase(n);
-                       // we have to break here, because erase invalidates all iterators, ie. n itself
+       for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+               Property prop = i->property;
+               switch (prop) {
+               case NoteNumber:
+                       i->note->set_note (i->new_value);
+                       break;
+               case Velocity:
+                       i->note->set_velocity (i->new_value);
+                       break;
+               case StartTime:
+                       i->note->set_time (i->new_time);
+                       break;
+               case Length:
+                       i->note->set_length (i->new_time);
+                       break;
+               case Channel:
+                       i->note->set_channel (i->new_value);
                        break;
                }
        }
-}
-
-/** Slow!  for debugging only. */
-#ifndef NDEBUG
-bool MidiModel::is_sorted() const {
-       bool t = 0;
-       for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n)
-               if ((*n)->time() < t)
-                       return false;
-               else
-                       t = (*n)->time();
 
-       return true;
-}
-#endif
-
-/** Start a new command.
- *
- * This has no side-effects on the model or Session, the returned command
- * can be held on to for as long as the caller wishes, or discarded without
- * formality, until apply_command is called and ownership is taken.
- */
-MidiModel::DeltaCommand* MidiModel::new_delta_command(const string name)
-{
-       DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
-       return cmd;
+       lock.reset();
+       _model->ContentsChanged(); /* EMIT SIGNAL */
 }
 
-/** Apply a command.
- *
- * Ownership of cmd is taken, it must not be deleted by the caller.
- * The command will constitute one item on the undo stack.
- */
-void MidiModel::apply_command(Command* cmd)
+void
+MidiModel::DiffCommand::undo()
 {
-       _session.begin_reversible_command(cmd->name());
-       (*cmd)();
-       assert(is_sorted());
-       _session.commit_reversible_command(cmd);
-       _edited = true;
-}
-
-// MidiEditCommand
+       MidiModel::WriteLock lock(_model->edit_lock());
 
-MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
-               const std::string& name)
-       : Command(name)
-       , _model(m)
-       , _name(name)
-{
-}
+       for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+               Property prop = i->property;
+               switch (prop) {
+               case NoteNumber:
+                       i->note->set_note (i->old_value);
+                       break;
+               case Velocity:
+                       i->note->set_velocity (i->old_value);
+                       break;
+               case StartTime:
+                       i->note->set_time (i->old_time);
+                       break;
+               case Length:
+                       i->note->set_length (i->old_time);
+                       break;
+               case Channel:
+                       i->note->set_channel (i->old_value);
+                       break;
+               }
+       }
 
-MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
-               const XMLNode& node)
-       : _model(m)
-{
-       set_state(node);
+       lock.reset();
+       _model->ContentsChanged(); /* EMIT SIGNAL */
 }
 
-void MidiModel::DeltaCommand::add(const boost::shared_ptr<Note> note)
+XMLNode&
+MidiModel::DiffCommand::marshal_change(const NotePropertyChange& change)
 {
-       //cerr << "MEC: apply" << endl;
-       _removed_notes.remove(note);
-       _added_notes.push_back(note);
-}
+       XMLNode* xml_change = new XMLNode("change");
 
-void MidiModel::DeltaCommand::remove(const boost::shared_ptr<Note> note)
-{
-       //cerr << "MEC: remove" << endl;
-       _added_notes.remove(note);
-       _removed_notes.push_back(note);
-}
+       /* first, the change itself */
 
-void MidiModel::DeltaCommand::operator()()
-{
-       // This could be made much faster by using a priority_queue for added and
-       // removed notes (or sort here), and doing a single iteration over _model
+       xml_change->add_property ("property", enum_2_string (change.property));
 
-       // Need to reset iterator to drop the read lock it holds, or we'll deadlock
-       const bool reset_iter = (_model->_read_iter.locked());
-       double iter_time = -1.0;
+       {
+               ostringstream old_value_str (ios::ate);
+               if (change.property == StartTime || change.property == Length) {
+                       old_value_str << change.old_time;
+               } else {
+                       old_value_str << (unsigned int) change.old_value;
+               }
+               xml_change->add_property ("old", old_value_str.str());
+       }
 
-       if (reset_iter) {
-               if (_model->_read_iter.get_event_pointer().get()) {
-                       iter_time = _model->_read_iter->time();
+       {
+               ostringstream new_value_str (ios::ate);
+               if (change.property == StartTime || change.property == Length) {
+                       new_value_str << change.new_time;
                } else {
-                       cerr << "MidiModel::DeltaCommand::operator(): WARNING: _read_iter points to no event" << endl;
+                       new_value_str << (unsigned int) change.new_value;
                }
-               _model->_read_iter = _model->end(); // drop read lock
+               xml_change->add_property ("new", new_value_str.str());
        }
 
-       assert( ! _model->_read_iter.locked());
+       /* now the rest of the note */
 
-       _model->write_lock();
+       const SMFSource* smf = dynamic_cast<const SMFSource*> (_model->midi_source());
 
-       for (std::list< boost::shared_ptr<Note> >::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
-               _model->add_note_unlocked(*i);
+       if (change.property != NoteNumber) {
+               ostringstream note_str;
+               note_str << int(change.note->note());
+               xml_change->add_property("note", note_str.str());
+       }
 
-       for (std::list< boost::shared_ptr<Note> >::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
-               _model->remove_note_unlocked(*i);
+       if (change.property != Channel) {
+               ostringstream channel_str;
+               channel_str << int(change.note->channel());
+               xml_change->add_property("channel", channel_str.str());
+       }
+
+       if (change.property != StartTime) {
+               ostringstream time_str;
+               if (smf) {
+                       time_str << smf->round_to_file_precision (change.note->time());
+               } else {
+                       time_str << change.note->time();
+               }
+               xml_change->add_property("time", time_str.str());
+       }
 
-       _model->write_unlock();
+       if (change.property != Length) {
+               ostringstream length_str;
+               if (smf) {
+                       length_str << smf->round_to_file_precision (change.note->length());
+               } else {
+                       length_str << change.note->length();
+               }
+               xml_change->add_property ("length", length_str.str());
+       }
 
-       if (reset_iter && iter_time != -1.0) {
-               _model->_read_iter = const_iterator(*_model.get(), iter_time);
+       if (change.property != Velocity) {
+               ostringstream velocity_str;
+               velocity_str << int (change.note->velocity());
+               xml_change->add_property("velocity", velocity_str.str());
        }
 
-       _model->ContentsChanged(); /* EMIT SIGNAL */
+       return *xml_change;
 }
 
-void MidiModel::DeltaCommand::undo()
+MidiModel::DiffCommand::NotePropertyChange
+MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
 {
-       // This could be made much faster by using a priority_queue for added and
-       // removed notes (or sort here), and doing a single iteration over _model
+       XMLProperty* prop;
+       NotePropertyChange change;
+       unsigned int note;
+       unsigned int channel;
+       unsigned int velocity;
+       Evoral::MusicalTime time;
+       Evoral::MusicalTime length;
 
-       // Need to reset iterator to drop the read lock it holds, or we'll deadlock
-       const bool reset_iter = (_model->_read_iter.locked());
-       double iter_time = -1.0;
+       if ((prop = xml_change->property("property")) != 0) {
+               change.property = (Property) string_2_enum (prop->value(), change.property);
+       } else {
+               fatal << "!!!" << endmsg;
+               /*NOTREACHED*/
+       }
 
-       if (reset_iter) {
-               if (_model->_read_iter.get_event_pointer().get()) {
-                       iter_time = _model->_read_iter->time();
+       if ((prop = xml_change->property ("old")) != 0) {
+               istringstream old_str (prop->value());
+               if (change.property == StartTime || change.property == Length) {
+                       old_str >> change.old_time;
                } else {
-                       cerr << "MidiModel::DeltaCommand::undo(): WARNING: _read_iter points to no event" << endl;
+                       int integer_value_so_that_istream_does_the_right_thing;
+                       old_str >> integer_value_so_that_istream_does_the_right_thing;
+                       change.old_value = integer_value_so_that_istream_does_the_right_thing;
                }
-               _model->_read_iter = _model->end(); // drop read lock
+       } else {
+               fatal << "!!!" << endmsg;
+               /*NOTREACHED*/
        }
 
-       assert( ! _model->_read_iter.locked());
-
-       _model->write_lock();
-
-       for (std::list< boost::shared_ptr<Note> >::iterator i = _added_notes.begin(); i
-                       != _added_notes.end(); ++i)
-               _model->remove_note_unlocked(*i);
+       if ((prop = xml_change->property ("new")) != 0) {
+               istringstream new_str (prop->value());
+               if (change.property == StartTime || change.property == Length) {
+                       new_str >> change.new_time;
+               } else {
+                       int integer_value_so_that_istream_does_the_right_thing;
+                       new_str >> integer_value_so_that_istream_does_the_right_thing;
+                       change.new_value = integer_value_so_that_istream_does_the_right_thing;
+               }
+       } else {
+               fatal << "!!!" << endmsg;
+               /*NOTREACHED*/
+       }
 
-       for (std::list< boost::shared_ptr<Note> >::iterator i =
-                       _removed_notes.begin(); i != _removed_notes.end(); ++i)
-               _model->add_note_unlocked(*i);
+       if (change.property != NoteNumber) {
+               if ((prop = xml_change->property("note")) != 0) {
+                       istringstream note_str(prop->value());
+                       note_str >> note;
+               } else {
+                       warning << "note information missing note value" << endmsg;
+                       note = 127;
+               }
+       } else {
+               note = change.new_value;
+       }
 
-       _model->write_unlock();
+       if (change.property != Channel) {
+               if ((prop = xml_change->property("channel")) != 0) {
+                       istringstream channel_str(prop->value());
+                       channel_str >> channel;
+               } else {
+                       warning << "note information missing channel" << endmsg;
+                       channel = 0;
+               }
+       } else {
+               channel = change.new_value;
+       }
 
-       if (reset_iter && iter_time != -1.0) {
-               _model->_read_iter = const_iterator(*_model.get(), iter_time);
+       if (change.property != StartTime) {
+               if ((prop = xml_change->property("time")) != 0) {
+                       istringstream time_str(prop->value());
+                       time_str >> time;
+               } else {
+                       warning << "note information missing time" << endmsg;
+                       time = 0;
+               }
+       } else {
+               time = change.new_time;
        }
 
-       _model->ContentsChanged(); /* EMIT SIGNAL */
-}
+       if (change.property != Length) {
+               if ((prop = xml_change->property("length")) != 0) {
+                       istringstream length_str(prop->value());
+                       length_str >> length;
+               } else {
+                       warning << "note information missing length" << endmsg;
+                       length = 1;
+               }
+       } else {
+               length = change.new_time;
+       }
 
-XMLNode & MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr<Note> note)
-{
-       XMLNode *xml_note = new XMLNode("note");
-       ostringstream note_str(ios::ate);
-       note_str << int(note->note());
-       xml_note->add_property("note", note_str.str());
+       if (change.property != Velocity) {
+               if ((prop = xml_change->property("velocity")) != 0) {
+                       istringstream velocity_str(prop->value());
+                       velocity_str >> velocity;
+               } else {
+                       warning << "note information missing velocity" << endmsg;
+                       velocity = 127;
+               }
+       } else {
+               velocity = change.new_value;
+       }
 
-       ostringstream channel_str(ios::ate);
-       channel_str << int(note->channel());
-       xml_note->add_property("channel", channel_str.str());
+       /* we must point at the instance of the note that is actually in the model.
+          so go look for it ...
+       */
 
-       ostringstream time_str(ios::ate);
-       time_str << int(note->time());
-       xml_note->add_property("time", time_str.str());
+       boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
 
-       ostringstream duration_str(ios::ate);
-       duration_str <<(unsigned int) note->duration();
-       xml_note->add_property("duration", duration_str.str());
+       change.note = _model->find_note (new_note);
 
-       ostringstream velocity_str(ios::ate);
-       velocity_str << (unsigned int) note->velocity();
-       xml_note->add_property("velocity", velocity_str.str());
+       if (!change.note) {
+               warning << "MIDI note " << *new_note << " not found in model - programmers should investigate this" << endmsg;
+               /* use the actual new note */
+               change.note = new_note;
+       }
 
-       return *xml_note;
+       return change;
 }
 
-boost::shared_ptr<Note> MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
+int
+MidiModel::DiffCommand::set_state(const XMLNode& diff_command, int /*version*/)
 {
-       unsigned int note;
-       istringstream note_str(xml_note->property("note")->value());
-       note_str >> note;
-
-       unsigned int channel;
-       istringstream channel_str(xml_note->property("channel")->value());
-       channel_str >> channel;
-
-       unsigned int time;
-       istringstream time_str(xml_note->property("time")->value());
-       time_str >> time;
+       if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
+               return 1;
+       }
 
-       unsigned int duration;
-       istringstream duration_str(xml_note->property("duration")->value());
-       duration_str >> duration;
+       _changes.clear();
 
-       unsigned int velocity;
-       istringstream velocity_str(xml_note->property("velocity")->value());
-       velocity_str >> velocity;
-
-       boost::shared_ptr<Note> note_ptr(new Note(channel, time, duration, note, velocity));
-       return note_ptr;
-}
+       XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
 
-#define ADDED_NOTES_ELEMENT "added_notes"
-#define REMOVED_NOTES_ELEMENT "removed_notes"
-#define DELTA_COMMAND_ELEMENT "DeltaCommand"
+       if (changed_notes) {
+               XMLNodeList notes = changed_notes->children();
+               transform (notes.begin(), notes.end(), back_inserter(_changes),
+                          boost::bind (&DiffCommand::unmarshal_change, this, _1));
 
-int MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
-{
-       if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
-               return 1;
        }
 
-       _added_notes.clear();
-       XMLNode *added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
-       XMLNodeList notes = added_notes->children();
-       transform(notes.begin(), notes.end(), back_inserter(_added_notes),
-                       sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
-
-       _removed_notes.clear();
-       XMLNode *removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
-       notes = removed_notes->children();
-       transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
-                       sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
-
        return 0;
 }
 
-XMLNode& MidiModel::DeltaCommand::get_state()
+XMLNode&
+MidiModel::DiffCommand::get_state ()
 {
-       XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
-       delta_command->add_property("midi_source", _model->midi_source()->id().to_s());
+       XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
+       diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
 
-       XMLNode *added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
-       for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
-                       sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
-                       sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
+       XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
+       for_each(_changes.begin(), _changes.end(), 
+                boost::bind (
+                        boost::bind (&XMLNode::add_child_nocopy, *changes, _1),
+                        boost::bind (&DiffCommand::marshal_change, this, _1)));
 
-       XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
-       for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
-                       sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
-                       sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
-
-       return *delta_command;
+       return *diff_command;
 }
 
-struct EventTimeComparator {
-       typedef const MIDI::Event* value_type;
-       inline bool operator()(const MIDI::Event& a, const MIDI::Event& b) const {
-               return a.time() >= b.time();
-       }
-};
-
 /** Write the model to a MidiSource (i.e. save the model).
  * This is different from manually using read to write to a source in that
  * note off events are written regardless of the track mode.  This is so the
@@ -924,28 +682,69 @@ struct EventTimeComparator {
  * to percussive, save, reload, then switch it back to sustained without
  * destroying the original note durations.
  */
-bool MidiModel::write_to(boost::shared_ptr<MidiSource> source)
+bool
+MidiModel::write_to(boost::shared_ptr<MidiSource> source)
 {
-       read_lock();
+       ReadLock lock(read_lock());
 
-       const NoteMode old_note_mode = _note_mode;
-       _note_mode = Sustained;
-       
-       for (const_iterator i = begin(); i != end(); ++i) {
-               source->append_event_unlocked(Frames, *i);
+       const bool old_percussive = percussive();
+       set_percussive(false);
+
+       source->drop_model();
+       source->mark_streaming_midi_write_started(note_mode(), _midi_source->timeline_position());
+
+       for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
+               source->append_event_unlocked_beats(*i);
        }
-               
-       _note_mode = old_note_mode;
-       
-       read_unlock();
-       _edited = false;
+
+       set_percussive(old_percussive);
+       source->mark_streaming_write_completed();
+
+       set_edited(false);
 
        return true;
 }
 
-XMLNode& MidiModel::get_state()
+XMLNode&
+MidiModel::get_state()
 {
        XMLNode *node = new XMLNode("MidiModel");
        return *node;
 }
 
+boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
+MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
+{
+       Notes::iterator l = notes().lower_bound(other);
+
+       if (l != notes().end()) {
+               for (; (*l)->time() == other->time(); ++l) {
+                       if (*l == other) {
+                               return *l;
+                       }
+               }
+       }
+
+       return boost::shared_ptr<Evoral::Note<TimeType> >();
+}
+
+/** Lock and invalidate the source.
+ * This should be used by commands and editing things
+ */
+MidiModel::WriteLock
+MidiModel::edit_lock()
+{
+       Glib::Mutex::Lock* source_lock = new Glib::Mutex::Lock(_midi_source->mutex());
+       _midi_source->invalidate(); // Release cached iterator's read lock on model
+       return WriteLock(new WriteLockImpl(source_lock, _lock, _control_lock));
+}
+
+/** Lock just the model, the source lock must already be held.
+ * This should only be called from libardour/evoral places
+ */
+MidiModel::WriteLock
+MidiModel::write_lock()
+{
+       assert(!_midi_source->mutex().trylock());
+       return WriteLock(new WriteLockImpl(NULL, _lock, _control_lock));
+}