+ 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 change;
+ }
+
+ int
+ MidiModel::DiffCommand::set_state(const XMLNode& diff_command, int /*version*/)
+ {
+ if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
+ return 1;
+ }
+
+ /* additions */
+
+ _added_notes.clear();
+ XMLNode* added_notes = diff_command.child(ADDED_NOTES_ELEMENT);
+ if (added_notes) {
+ XMLNodeList notes = added_notes->children();
+ transform(notes.begin(), notes.end(), back_inserter(_added_notes),
+ boost::bind (&DiffCommand::unmarshal_note, this, _1));
+ }
+
+
+ /* removals */
+
+ _removed_notes.clear();
+ XMLNode* removed_notes = diff_command.child(REMOVED_NOTES_ELEMENT);
+ if (removed_notes) {
+ XMLNodeList notes = removed_notes->children();
+ transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
+ boost::bind (&DiffCommand::unmarshal_note, this, _1));
+ }
+
+
+ /* changes */
+
+ _changes.clear();
+
+ XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
+
+ if (changed_notes) {
+ XMLNodeList notes = changed_notes->children();
+ transform (notes.begin(), notes.end(), back_inserter(_changes),
+ boost::bind (&DiffCommand::unmarshal_change, this, _1));
+
+ }
+
+ /* side effect removals caused by changes */
+
+ side_effect_removals.clear();
+
+ XMLNode* side_effect_notes = diff_command.child(SIDE_EFFECT_REMOVALS_ELEMENT);
+
+ if (side_effect_notes) {
+ XMLNodeList notes = side_effect_notes->children();
+ for (XMLNodeList::iterator n = notes.begin(); n != notes.end(); ++n) {
+ side_effect_removals.insert (unmarshal_note (*n));
+ }
+ }
+
+ return 0;
+ }
+
+ XMLNode&
+ MidiModel::DiffCommand::get_state ()
+ {
+ XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
+ diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
+
+ 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* added_notes = diff_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 (&DiffCommand::marshal_note, this, _1)));
+
+ XMLNode* removed_notes = diff_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 (&DiffCommand::marshal_note, this, _1)));
+
+ /* if this command had side-effects, store that state too
+ */
+
+ if (!side_effect_removals.empty()) {
+ XMLNode* side_effect_notes = diff_command->add_child(SIDE_EFFECT_REMOVALS_ELEMENT);
+ for_each(side_effect_removals.begin(), side_effect_removals.end(),
+ boost::bind (
+ boost::bind (&XMLNode::add_child_nocopy, side_effect_notes, _1),
+ boost::bind (&DiffCommand::marshal_note, this, _1)));
+ }
+
+ return *diff_command;
+ }
+
+ /** Write all of 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
+ * user can switch a recorded track (with note durations from some instrument)
+ * 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)
+ {
+ ReadLock lock(read_lock());
+
+ 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);
+ }
+
+ set_percussive(old_percussive);
+ source->mark_streaming_write_completed();
+
+ set_edited(false);
+
+ return true;
+ }
+
+ /** Write part or all of 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
+ * user can switch a recorded track (with note durations from some instrument)
+ * to percussive, save, reload, then switch it back to sustained without
+ * destroying the original note durations.
+ */
+ bool
+ MidiModel::write_section_to (boost::shared_ptr<MidiSource> source, Evoral::MusicalTime begin_time, Evoral::MusicalTime end_time)
+ {
+ ReadLock lock(read_lock());
+ MidiStateTracker mst;
+ Evoral::MusicalTime extra_note_on_time = end_time;
+
+ 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) {
+ const Evoral::Event<Evoral::MusicalTime>& ev (*i);
+
+ if (ev.time() >= begin_time && ev.time() < end_time) {
+
+ const Evoral::MIDIEvent<Evoral::MusicalTime>* mev =
+ static_cast<const Evoral::MIDIEvent<Evoral::MusicalTime>* > (&ev);
+
+ if (!mev) {
+ continue;
+ }
+
+
+ if (mev->is_note_off()) {
+
+ if (!mst.active (mev->note(), mev->channel())) {
+
+ /* add a note-on at the start of the range we're writing
+ to the file. velocity is just an arbitary reasonable value.
+ */
+
+ Evoral::MIDIEvent<Evoral::MusicalTime> on (mev->event_type(), extra_note_on_time, 3, 0, true);
+ on.set_type (mev->type());
+ on.set_note (mev->note());
+ on.set_channel (mev->channel());
+ on.set_velocity (mev->velocity());
+
+ cerr << "Add note on for odd note off, note = " << (int) on.note() << endl;
+ source->append_event_unlocked_beats (on);
+ mst.add (on.note(), on.channel());
+ mst.dump (cerr);
+ extra_note_on_time += 1.0/128.0;
+ }
+
+ cerr << "MIDI Note off (note = " << (int) mev->note() << endl;
+ source->append_event_unlocked_beats (*i);
+ mst.remove (mev->note(), mev->channel());
+ mst.dump (cerr);
+
+ } else if (mev->is_note_on()) {
+ cerr << "MIDI Note on (note = " << (int) mev->note() << endl;
+ mst.add (mev->note(), mev->channel());
+ source->append_event_unlocked_beats(*i);
+ mst.dump (cerr);
+ } else {
+ cerr << "MIDI other event type\n";
+ source->append_event_unlocked_beats(*i);
+ }
+ }
+ }
+
+ mst.resolve_notes (*source, end_time);
+
+ set_percussive(old_percussive);
+ source->mark_streaming_write_completed();
+
+ set_edited(false);
+
+ return true;
+ }
+
+ XMLNode&
+ MidiModel::get_state()
+ {
+ XMLNode *node = new XMLNode("MidiModel");
+ return *node;
+ }
+
+ Evoral::Sequence<MidiModel::TimeType>::NotePtr
+ MidiModel::find_note (NotePtr other)
+ {
+ Notes::iterator l = notes().lower_bound(other);
+
+ if (l != notes().end()) {
+ for (; (*l)->time() == other->time(); ++l) {
+ /* NB: compare note contents, not note pointers.
+ If "other" was a ptr to a note already in
+ the model, we wouldn't be looking for it,
+ would we now?
+ */
+ if (**l == *other) {
+ return *l;
+ }
+ }
+ }
+
+ return NotePtr();
+ }
+
+ /** 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));
+ }
+
+ int
+ MidiModel::resolve_overlaps_unlocked (const NotePtr note, void* arg)
+ {
+ using namespace Evoral;
+
+ if (_writing || insert_merge_policy() == InsertMergeRelax) {
+ return 0;
+ }
+
+ DiffCommand* cmd = static_cast<DiffCommand*>(arg);
+
+ TimeType sa = note->time();
+ TimeType ea = note->end_time();
+
+ const Pitches& p (pitches (note->channel()));
+ NotePtr search_note(new Note<TimeType>(0, 0, 0, note->note()));
+ set<NotePtr> to_be_deleted;
+ bool set_note_length = false;
+ bool set_note_time = false;
+ TimeType note_time = note->time();
+ TimeType note_length = note->length();
+
+ for (Pitches::const_iterator i = p.lower_bound (search_note);
+ i != p.end() && (*i)->note() == note->note(); ++i) {
+
+ TimeType sb = (*i)->time();
+ TimeType eb = (*i)->end_time();
+ OverlapType overlap = OverlapNone;
+
+ if ((sb > sa) && (eb <= ea)) {
+ overlap = OverlapInternal;
+ } else if ((eb >= sa) && (eb <= ea)) {
+ overlap = OverlapStart;
+ } else if ((sb > sa) && (sb <= ea)) {
+ overlap = OverlapEnd;
+ } else if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
+ overlap = OverlapExternal;
+ } else {
+ /* no overlap */
+ continue;
+ }
+
+ if (insert_merge_policy() == InsertMergeReject) {
+ return -1;
+ }
+
+ switch (overlap) {
+ case OverlapStart:
+ cerr << "OverlapStart\n";
+ /* existing note covers start of new note */
+ switch (insert_merge_policy()) {
+ case InsertMergeReplace:
+ to_be_deleted.insert (*i);
+ break;
+ case InsertMergeTruncateExisting:
+ if (cmd) {
+ cmd->change (*i, DiffCommand::Length, (note->time() - (*i)->time()));
+ }
+ (*i)->set_length (note->time() - (*i)->time());
+ break;
+ case InsertMergeTruncateAddition:
+ set_note_time = true;
+ set_note_length = true;
+ note_time = (*i)->time() + (*i)->length();
+ note_length = min (note_length, (*i)->length() - ((*i)->end_time() - note->time()));
+ break;
+ case InsertMergeExtend:
+ if (cmd) {
+ cmd->change ((*i), DiffCommand::Length, note->end_time() - (*i)->time());
+ }
+ (*i)->set_length (note->end_time() - (*i)->time());
+ return -1; /* do not add the new note */
+ break;
+ default:
+ /*NOTREACHED*/
+ /* stupid gcc */
+ break;
+ }
+ break;
+
+ case OverlapEnd:
+ cerr << "OverlapEnd\n";
+ /* existing note covers end of new note */
+ switch (insert_merge_policy()) {
+ case InsertMergeReplace:
+ to_be_deleted.insert (*i);
+ break;
+
+ case InsertMergeTruncateExisting:
+ /* resetting the start time of the existing note
+ is a problem because of time ordering.
+ */
+ break;
+
+ case InsertMergeTruncateAddition:
+ set_note_length = true;
+ note_length = min (note_length, ((*i)->time() - note->time()));
+ break;
+
+ case InsertMergeExtend:
+ /* we can't reset the time of the existing note because
+ that will corrupt time ordering. So remove the
+ existing note and change the position/length
+ of the new note (which has not been added yet)
+ */
+ to_be_deleted.insert (*i);
+ set_note_length = true;
+ note_length = min (note_length, (*i)->end_time() - note->time());
+ break;
+ default:
+ /*NOTREACHED*/
+ /* stupid gcc */
+ break;
+ }
+ break;
+
+ case OverlapExternal:
+ cerr << "OverlapExt\n";
+ /* existing note overlaps all the new note */
+ switch (insert_merge_policy()) {
+ case InsertMergeReplace:
+ to_be_deleted.insert (*i);
+ break;
+ case InsertMergeTruncateExisting:
+ case InsertMergeTruncateAddition:
+ case InsertMergeExtend:
+ /* cannot add in this case */
+ return -1;
+ default:
+ /*NOTREACHED*/
+ /* stupid gcc */
+ break;
+ }
+ break;
+
+ case OverlapInternal:
+ cerr << "OverlapInt\n";
+ /* new note fully overlaps an existing note */
+ switch (insert_merge_policy()) {
+ case InsertMergeReplace:
+ case InsertMergeTruncateExisting:
+ case InsertMergeTruncateAddition:
+ case InsertMergeExtend:
+ /* delete the existing note, the new one will cover it */
+ to_be_deleted.insert (*i);
+ break;
+ default:
+ /*NOTREACHED*/
+ /* stupid gcc */
+ break;
+ }
+ break;
+
+ default:
+ /*NOTREACHED*/
+ /* stupid gcc */
+ break;
+ }
+ }
+
+ for (set<NotePtr>::iterator i = to_be_deleted.begin(); i != to_be_deleted.end(); ++i) {
+ remove_note_unlocked (*i);
+
+ if (cmd) {
+ cmd->side_effect_remove (*i);
+ }
+ }
+
+ if (set_note_time) {
+ if (cmd) {
+ cmd->change (note, DiffCommand::StartTime, note_time);
+ }
+ note->set_time (note_time);
+ }
+
+ if (set_note_length) {
+ if (cmd) {
+ cmd->change (note, DiffCommand::Length, note_length);
+ }
+ note->set_length (note_length);
+ }
+
+ return 0;
+ }
+
+ InsertMergePolicy
+ MidiModel::insert_merge_policy () const
+ {
+ /* XXX ultimately this should be a per-track or even per-model policy */
+
+ return _midi_source->session().config.get_insert_merge_policy();
+}
+
+void
+MidiModel::set_midi_source (MidiSource* s)