Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / midi_model.cc
index c22820c83c090bfeb572e8359b89d1f877b324e9..c1d53a25539d9247852e2de6d19f9bc79940e24c 100644 (file)
@@ -24,6 +24,7 @@
 #include <algorithm>
 #include <stdexcept>
 #include <stdint.h>
+#include <pbd/error.h>
 #include <pbd/enumwriter.h>
 #include <midi++/events.h>
 
@@ -34,7 +35,7 @@
 
 using namespace std;
 using namespace ARDOUR;
-
+using namespace PBD;
 
 MidiModel::MidiModel(MidiSource *s, size_t size)
        : AutomatableSequence(s->session(), size)
@@ -170,9 +171,9 @@ MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr<Evoral::Note> note
        time_str << int(note->time());
        xml_note->add_property("time", time_str.str());
 
-       ostringstream duration_str(ios::ate);
-       duration_str <<(unsigned int) note->duration();
-       xml_note->add_property("duration", duration_str.str());
+       ostringstream length_str(ios::ate);
+       length_str <<(unsigned int) note->length();
+       xml_note->add_property("length", length_str.str());
 
        ostringstream velocity_str(ios::ate);
        velocity_str << (unsigned int) note->velocity();
@@ -184,26 +185,53 @@ MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr<Evoral::Note> note
 boost::shared_ptr<Evoral::Note> MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
 {
        unsigned int note;
-       istringstream note_str(xml_note->property("note")->value());
-       note_str >> note;
-
+       XMLProperty* prop;
        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;
+       unsigned int length;
+       unsigned int velocity;
 
-       unsigned int duration;
-       istringstream duration_str(xml_note->property("duration")->value());
-       duration_str >> duration;
+       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;
+       }
 
-       unsigned int velocity;
-       istringstream velocity_str(xml_note->property("velocity")->value());
-       velocity_str >> velocity;
+       if ((prop = xml_note->property("channel")) != 0) {
+               istringstream channel_str(prop->value());
+               channel_str >> channel;
+       } else {
+               warning << "note information missing channel" << endmsg;
+               channel = 0;
+       }
 
-       boost::shared_ptr<Evoral::Note> note_ptr(new Evoral::Note(channel, time, duration, note, velocity));
+       if ((prop = xml_note->property("time")) != 0) {
+               istringstream time_str(prop->value());
+               time_str >> time;
+       } else {
+               warning << "note information missing time" << endmsg;
+               time = 0;
+       }
+
+       if ((prop = xml_note->property("length")) != 0) {
+               istringstream length_str(prop->value());
+               length_str >> length;
+       } else {
+               warning << "note information missing length" << endmsg;
+               note = 1;
+       }
+
+       if ((prop = xml_note->property("velocity")) != 0) {
+               istringstream velocity_str(prop->value());
+               velocity_str >> velocity;
+       } else {
+               warning << "note information missing velocity" << endmsg;
+               velocity = 127;
+       }
+
+       boost::shared_ptr<Evoral::Note> note_ptr(new Evoral::Note(channel, time, length, note, velocity));
        return note_ptr;
 }
 
@@ -235,7 +263,7 @@ int MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
 XMLNode& MidiModel::DeltaCommand::get_state()
 {
        XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
-       delta_command->add_property("midi_source", _model->midi_source()->id().to_s());
+       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(), sigc::compose(
@@ -250,13 +278,6 @@ XMLNode& MidiModel::DeltaCommand::get_state()
        return *delta_command;
 }
 
-struct EventTimeComparator {
-       typedef const Evoral::Event* value_type;
-       inline bool operator()(const Evoral::Event& a, const Evoral::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