Merge remote-tracking branch 'upstream/master'
[ardour.git] / libs / ardour / midi_model.cc
index d6f21f1f4cfa6e955c3a149949b71c46bd07f856..259a04bc0ffa261191e663e7649ed34cf442c157 100644 (file)
@@ -38,7 +38,6 @@
 #include "ardour/midi_source.h"
 #include "ardour/midi_state_tracker.h"
 #include "ardour/session.h"
-#include "ardour/smf_source.h"
 #include "ardour/types.h"
 
 #include "i18n.h"
@@ -196,11 +195,11 @@ MidiModel::NoteDiffCommand::change (const NotePtr note, Property prop,
 
        case StartTime:
                fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
                break;
        case Length:
                fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
                break;
        }
 
@@ -290,6 +289,15 @@ MidiModel::NoteDiffCommand::operator() ()
 
                for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
                        Property prop = i->property;
+
+                       if (!i->note) {
+                               /* note found during deserialization, so try
+                                  again now that the model state is different.
+                               */
+                               i->note = _model->find_note (i->note_id);
+                               assert (i->note);
+                       }
+
                        switch (prop) {
                        case NoteNumber:
                                if (temporary_removals.find (i->note) == temporary_removals.end()) {
@@ -342,7 +350,7 @@ MidiModel::NoteDiffCommand::operator() ()
                                _removed_notes.push_back (*i);
                        }
                }
-
+               
                if (!side_effect_removals.empty()) {
                        cerr << "SER: \n";
                        for (set<NotePtr>::iterator i = side_effect_removals.begin(); i != side_effect_removals.end(); ++i) {
@@ -374,15 +382,25 @@ MidiModel::NoteDiffCommand::undo ()
                /* notes we modify in a way that requires remove-then-add to maintain ordering */
                set<NotePtr> temporary_removals;
 
+
+               /* lazily discover any affected notes that were not discovered when
+                * loading the history because of deletions, etc.
+                */
+
+               for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+                       if (!i->note) {
+                               i->note = _model->find_note (i->note_id);
+                               assert (i->note);
+                       }
+               }
+                               
                for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
                        Property prop = i->property;
-                       switch (prop) {
 
+                       switch (prop) {
                        case NoteNumber:
-                               if (
-                                       temporary_removals.find (i->note) == temporary_removals.end() &&
-                                       find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()
-                                       ) {
+                               if (temporary_removals.find (i->note) == temporary_removals.end() &&
+                                   find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()) {
 
                                        /* We only need to mark this note for re-add if (a) we haven't
                                           already marked it and (b) it isn't on the _removed_notes
@@ -397,10 +415,8 @@ MidiModel::NoteDiffCommand::undo ()
                                break;
 
                        case StartTime:
-                               if (
-                                       temporary_removals.find (i->note) == temporary_removals.end() &&
-                                       find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()
-                                       ) {
+                               if (temporary_removals.find (i->note) == temporary_removals.end() &&
+                                   find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()) {
 
                                        /* See above ... */
 
@@ -411,10 +427,8 @@ MidiModel::NoteDiffCommand::undo ()
                                break;
 
                        case Channel:
-                               if (
-                                       temporary_removals.find (i->note) == temporary_removals.end() &&
-                                       find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()
-                                       ) {
+                               if (temporary_removals.find (i->note) == temporary_removals.end() &&
+                                   find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()) {
 
                                        /* See above ... */
 
@@ -454,7 +468,7 @@ MidiModel::NoteDiffCommand::undo ()
                        _model->add_note_unlocked (*i);
                }
        }
-
+       
        _model->ContentsChanged(); /* EMIT SIGNAL */
 }
 
@@ -508,8 +522,8 @@ MidiModel::NoteDiffCommand::unmarshal_note (XMLNode *xml_note)
        unsigned int note;
        XMLProperty* prop;
        unsigned int channel;
-       unsigned int time;
-       unsigned int length;
+       MidiModel::TimeType time;
+       MidiModel::TimeType length;
        unsigned int velocity;
        gint id;
 
@@ -613,7 +627,7 @@ MidiModel::NoteDiffCommand::unmarshal_change (XMLNode *xml_change)
                change.property = (Property) string_2_enum (prop->value(), change.property);
        } else {
                fatal << "!!!" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if ((prop = xml_change->property ("id")) == 0) {
@@ -634,7 +648,7 @@ MidiModel::NoteDiffCommand::unmarshal_change (XMLNode *xml_change)
                }
        } else {
                fatal << "!!!" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if ((prop = xml_change->property ("new")) != 0) {
@@ -648,19 +662,17 @@ MidiModel::NoteDiffCommand::unmarshal_change (XMLNode *xml_change)
                }
        } else {
                fatal << "!!!" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        /* we must point at the instance of the note that is actually in the model.
-          so go look for it ...
+          so go look for it ... it may not be there (it could have been
+          deleted in a later operation, so store the note id so that we can
+          look it up again later).
        */
 
        change.note = _model->find_note (note_id);
-
-       if (!change.note) {
-               warning << "MIDI note #" << note_id << " not found in model - programmers should investigate this" << endmsg;
-               return change;
-       }
+       change.note_id = note_id;
 
        return change;
 }
@@ -787,6 +799,19 @@ MidiModel::SysExDiffCommand::operator() ()
        {
                MidiModel::WriteLock lock (_model->edit_lock ());
 
+               for (list<SysExPtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
+                       _model->remove_sysex_unlocked (*i);
+               }
+
+               /* find any sysex events that were missing when unmarshalling */
+
+               for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+                       if (!i->sysex) {
+                               i->sysex = _model->find_sysex (i->sysex_id);
+                               assert (i->sysex);
+                       }
+               }
+
                for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
                        switch (i->property) {
                        case Time:
@@ -804,6 +829,19 @@ MidiModel::SysExDiffCommand::undo ()
        {
                MidiModel::WriteLock lock (_model->edit_lock ());
 
+               for (list<SysExPtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
+                       _model->add_sysex_unlocked (*i);
+               }
+
+               /* find any sysex events that were missing when unmarshalling */
+
+               for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+                       if (!i->sysex) {
+                               i->sysex = _model->find_sysex (i->sysex_id);
+                               assert (i->sysex);
+                       }
+               }
+
                for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
                        switch (i->property) {
                        case Time:
@@ -817,6 +855,12 @@ MidiModel::SysExDiffCommand::undo ()
        _model->ContentsChanged(); /* EMIT SIGNAL */
 }
 
+void
+MidiModel::SysExDiffCommand::remove (SysExPtr sysex)
+{
+       _removed.push_back(sysex);
+}
+
 XMLNode&
 MidiModel::SysExDiffCommand::marshal_change (const Change& change)
 {
@@ -855,7 +899,7 @@ MidiModel::SysExDiffCommand::unmarshal_change (XMLNode *xml_change)
                change.property = (Property) string_2_enum (prop->value(), change.property);
        } else {
                fatal << "!!!" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if ((prop = xml_change->property ("id")) == 0) {
@@ -870,7 +914,7 @@ MidiModel::SysExDiffCommand::unmarshal_change (XMLNode *xml_change)
                old_str >> change.old_time;
        } else {
                fatal << "!!!" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if ((prop = xml_change->property ("new")) != 0) {
@@ -878,7 +922,7 @@ MidiModel::SysExDiffCommand::unmarshal_change (XMLNode *xml_change)
                new_str >> change.new_time;
        } else {
                fatal << "!!!" << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        /* we must point at the instance of the sysex that is actually in the model.
@@ -886,11 +930,7 @@ MidiModel::SysExDiffCommand::unmarshal_change (XMLNode *xml_change)
        */
 
        change.sysex = _model->find_sysex (sysex_id);
-
-       if (!change.sysex) {
-               warning << "Sys-ex #" << sysex_id << " not found in model - programmers should investigate this" << endmsg;
-               return change;
-       }
+       change.sysex_id = sysex_id;
 
        return change;
 }
@@ -978,6 +1018,7 @@ MidiModel::PatchChangeDiffCommand::change_channel (PatchChangePtr patch, uint8_t
        c.patch = patch;
        c.old_channel = patch->channel ();
        c.new_channel = channel;
+       c.patch_id = patch->id();
 
        _changes.push_back (c);
 }
@@ -990,6 +1031,7 @@ MidiModel::PatchChangeDiffCommand::change_program (PatchChangePtr patch, uint8_t
        c.patch = patch;
        c.old_program = patch->program ();
        c.new_program = program;
+       c.patch_id = patch->id();
 
        _changes.push_back (c);
 }
@@ -1020,6 +1062,15 @@ MidiModel::PatchChangeDiffCommand::operator() ()
                        _model->remove_patch_change_unlocked (*i);
                }
 
+               /* find any patch change events that were missing when unmarshalling */
+
+               for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+                       if (!i->patch) {
+                               i->patch = _model->find_patch_change (i->patch_id);
+                               assert (i->patch);
+                       }
+               }
+
                set<PatchChangePtr> temporary_removals;
 
                for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
@@ -1068,6 +1119,15 @@ MidiModel::PatchChangeDiffCommand::undo ()
                        _model->add_patch_change_unlocked (*i);
                }
 
+               /* find any patch change events that were missing when unmarshalling */
+
+               for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
+                       if (!i->patch) {
+                               i->patch = _model->find_patch_change (i->patch_id);
+                               assert (i->patch);
+                       }
+               }
+
                set<PatchChangePtr> temporary_removals;
 
                for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
@@ -1192,12 +1252,12 @@ MidiModel::PatchChangePtr
 MidiModel::PatchChangeDiffCommand::unmarshal_patch_change (XMLNode* n)
 {
        XMLProperty* prop;
-       Evoral::event_id_t id;
+       Evoral::event_id_t id = 0;
        Evoral::MusicalTime time = 0;
-       uint8_t channel = 0;
-       uint8_t program = 0;
+       int channel = 0;
+       int program = 0;
        int bank = 0;
-
+       
        if ((prop = n->property ("id")) != 0) {
                istringstream s (prop->value());
                s >> id;
@@ -1224,6 +1284,7 @@ MidiModel::PatchChangeDiffCommand::unmarshal_patch_change (XMLNode* n)
        }
 
        PatchChangePtr p (new Evoral::PatchChange<TimeType> (time, channel, program, bank));
+       assert(id);
        p->set_id (id);
        return p;
 }
@@ -1233,6 +1294,7 @@ MidiModel::PatchChangeDiffCommand::unmarshal_change (XMLNode* n)
 {
        XMLProperty* prop;
        Change c;
+       int an_int;
 
        prop = n->property ("property");
        assert (prop);
@@ -1242,6 +1304,10 @@ MidiModel::PatchChangeDiffCommand::unmarshal_change (XMLNode* n)
        assert (prop);
        Evoral::event_id_t const id = atoi (prop->value().c_str());
 
+       /* we need to load via an int intermediate for all properties that are 
+          actually uint8_t (char/byte).
+       */
+
        prop = n->property ("old");
        assert (prop);
        {
@@ -1249,11 +1315,14 @@ MidiModel::PatchChangeDiffCommand::unmarshal_change (XMLNode* n)
                if (c.property == Time) {
                        s >> c.old_time;
                } else if (c.property == Channel) {
-                       s >> c.old_channel;
+                       s >> an_int;
+                       c.old_channel = an_int;
                } else if (c.property == Program) {
-                       s >> c.old_program;
+                       s >> an_int;
+                       c.old_program = an_int;
                } else if (c.property == Bank) {
-                       s >> c.old_bank;
+                       s >> an_int;
+                       c.old_bank = an_int;
                }
        }
 
@@ -1261,19 +1330,23 @@ MidiModel::PatchChangeDiffCommand::unmarshal_change (XMLNode* n)
        assert (prop);
        {
                istringstream s (prop->value ());
+
                if (c.property == Time) {
                        s >> c.new_time;
                } else if (c.property == Channel) {
-                       s >> c.new_channel;
+                       s >> an_int;
+                       c.new_channel = an_int;
                } else if (c.property == Program) {
-                       s >> c.new_program;
+                       s >> an_int;
+                       c.new_program = an_int;
                } else if (c.property == Bank) {
-                       s >> c.new_bank;
+                       s >> an_int;
+                       c.new_bank = an_int;
                }
        }
 
        c.patch = _model->find_patch_change (id);
-       assert (c.patch);
+       c.patch_id = id;
 
        return c;
 }
@@ -1556,7 +1629,7 @@ MidiModel::edit_lock()
        boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
        assert (ms);
 
-       Glib::Mutex::Lock* source_lock = new Glib::Mutex::Lock (ms->mutex());
+       Glib::Threads::Mutex::Lock* source_lock = new Glib::Threads::Mutex::Lock (ms->mutex());
        ms->invalidate(); // Release cached iterator's read lock on model
        return WriteLock(new WriteLockImpl(source_lock, _lock, _control_lock));
 }
@@ -1571,7 +1644,7 @@ MidiModel::write_lock()
        assert (ms);
 
        assert (!ms->mutex().trylock ());
-       return WriteLock(new WriteLockImpl(NULL, _lock, _control_lock));
+       return WriteLock(new WriteLockImpl(0, _lock, _control_lock));
 }
 
 int
@@ -1655,7 +1728,7 @@ MidiModel::resolve_overlaps_unlocked (const NotePtr note, void* arg)
                                return -1; /* do not add the new note */
                                break;
                        default:
-                               /*NOTREACHED*/
+                               abort(); /*NOTREACHED*/
                                /* stupid gcc */
                                break;
                        }
@@ -1691,7 +1764,7 @@ MidiModel::resolve_overlaps_unlocked (const NotePtr note, void* arg)
                                note_length = min (note_length, (*i)->end_time() - note->time());
                                break;
                        default:
-                               /*NOTREACHED*/
+                               abort(); /*NOTREACHED*/
                                /* stupid gcc */
                                break;
                        }
@@ -1710,7 +1783,7 @@ MidiModel::resolve_overlaps_unlocked (const NotePtr note, void* arg)
                                /* cannot add in this case */
                                return -1;
                        default:
-                               /*NOTREACHED*/
+                               abort(); /*NOTREACHED*/
                                /* stupid gcc */
                                break;
                        }
@@ -1728,14 +1801,14 @@ MidiModel::resolve_overlaps_unlocked (const NotePtr note, void* arg)
                                to_be_deleted.insert (*i);
                                break;
                        default:
-                               /*NOTREACHED*/
+                               abort(); /*NOTREACHED*/
                                /* stupid gcc */
                                break;
                        }
                        break;
 
                default:
-                       /*NOTREACHED*/
+                       abort(); /*NOTREACHED*/
                        /* stupid gcc */
                        break;
                }
@@ -1808,7 +1881,7 @@ MidiModel::set_midi_source (boost::shared_ptr<MidiSource> s)
 void
 MidiModel::source_interpolation_changed (Evoral::Parameter p, Evoral::ControlList::InterpolationStyle s)
 {
-       Glib::Mutex::Lock lm (_control_lock);
+       Glib::Threads::Mutex::Lock lm (_control_lock);
        control(p)->list()->set_interpolation (s);
 }
 
@@ -1827,7 +1900,7 @@ MidiModel::control_list_interpolation_changed (Evoral::Parameter p, Evoral::Cont
 void
 MidiModel::source_automation_state_changed (Evoral::Parameter p, AutoState s)
 {
-       Glib::Mutex::Lock lm (_control_lock);
+       Glib::Threads::Mutex::Lock lm (_control_lock);
        boost::shared_ptr<AutomationList> al = boost::dynamic_pointer_cast<AutomationList> (control(p)->list ());
        al->set_automation_state (s);
 }
@@ -1868,7 +1941,7 @@ MidiModel::midi_source ()
        return _midi_source.lock ();
 }
 
-/** Moves notes, controllers and sys-ex to insert silence at the start of the model.
+/** Moves notes, patch changes, controllers and sys-ex to insert silence at the start of the model.
  *  Adds commands to the session's current undo stack to reflect the movements.
  */
 void
@@ -1889,6 +1962,18 @@ MidiModel::insert_silence_at_start (TimeType t)
                apply_command_as_subcommand (s->session(), c);
        }
 
+       /* Patch changes */
+
+       if (!patch_changes().empty ()) {
+               PatchChangeDiffCommand* c = new_patch_change_diff_command ("insert silence");
+
+               for (PatchChanges::const_iterator i = patch_changes().begin(); i != patch_changes().end(); ++i) {
+                       c->change_time (*i, (*i)->time() + t);
+               }
+
+               apply_command_as_subcommand (s->session(), c);
+       }
+
        /* Controllers */
 
        for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {