/*
- Copyright (C) 2006 Paul Davis
- Written by Dave Robillard, 2006
+ 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
*/
+#define __STDC_LIMIT_MACROS 1
+
#include <iostream>
-#include <ardour/midi_model.h>
-#include <ardour/types.h>
+#include <algorithm>
+#include <stdexcept>
+#include <stdint.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"
using namespace std;
using namespace ARDOUR;
+using namespace PBD;
-
-MidiModel::MidiModel(size_t size)
-: _events(size)
+MidiModel::MidiModel(MidiSource* s, size_t size)
+ : AutomatableSequence<TimeType>(s->session(), size)
+ , _midi_source(s)
{
}
-MidiModel::~MidiModel()
+/** 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)
{
- for (size_t i=0; i < _events.size(); ++i)
- delete _events[i].buffer;
+ DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
+ return cmd;
}
-
-/** Append contents of \a buf 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.
+/** Apply a command.
*
- * Events in buf are deep copied.
+ * 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::append(const MidiBuffer& buf)
+MidiModel::apply_command(Session& session, Command* cmd)
{
- for (size_t i=0; i < buf.size(); ++i) {
- const MidiEvent& buf_event = buf[i];
- assert(_events.empty() || buf_event.time >= _events.back().time);
-
- _events.push_back(buf_event);
- MidiEvent& my_event = _events.back();
- assert(my_event.time == buf_event.time);
- assert(my_event.size == buf_event.size);
-
- my_event.buffer = new Byte[my_event.size];
- memcpy(my_event.buffer, buf_event.buffer, my_event.size);
+ session.begin_reversible_command(cmd->name());
+ (*cmd)();
+ session.commit_reversible_command(cmd);
+ set_edited(true);
+}
+
+
+// DeltaCommand
+
+MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
+ : Command(name)
+ , _model(m)
+ , _name(name)
+{
+ assert(_model);
+}
+
+MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
+ : _model(m)
+{
+ assert(_model);
+ set_state(node);
+}
+
+void
+MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
+{
+ _removed_notes.remove(note);
+ _added_notes.push_back(note);
+}
+
+void
+MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
+{
+ _added_notes.remove(note);
+ _removed_notes.push_back(note);
+}
+
+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
+
+ Glib::Mutex::Lock lm (_model->_midi_source->mutex());
+ _model->_midi_source->invalidate(); // release model read lock
+ _model->write_lock();
+
+ for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
+ _model->add_note_unlocked(*i);
+
+ for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
+ _model->remove_note_unlocked(*i);
+
+ _model->write_unlock();
+ _model->ContentsChanged(); /* EMIT SIGNAL */
+}
+
+void
+MidiModel::DeltaCommand::undo()
+{
+ // 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
+
+ Glib::Mutex::Lock lm (_model->_midi_source->mutex());
+ _model->_midi_source->invalidate(); // release model read lock
+ _model->write_lock();
+
+ for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
+ _model->remove_note_unlocked(*i);
+
+ for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
+ _model->add_note_unlocked(*i);
+
+ _model->write_unlock();
+ _model->ContentsChanged(); /* EMIT SIGNAL */
+}
+
+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());
+
+ ostringstream channel_str(ios::ate);
+ channel_str << int(note->channel());
+ xml_note->add_property("channel", channel_str.str());
+
+ ostringstream time_str(ios::ate);
+ time_str << int(note->time());
+ xml_note->add_property("time", time_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();
+ xml_note->add_property("velocity", velocity_str.str());
+
+ return *xml_note;
+}
+
+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;
+
+ 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 ((prop = xml_note->property("channel")) != 0) {
+ istringstream channel_str(prop->value());
+ channel_str >> channel;
+ } else {
+ warning << "note information missing channel" << endmsg;
+ channel = 0;
+ }
+
+ 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<TimeType> > note_ptr(new Evoral::Note<TimeType>(
+ channel, time, length, note, velocity));
+ return note_ptr;
}
+#define ADDED_NOTES_ELEMENT "added_notes"
+#define REMOVED_NOTES_ELEMENT "removed_notes"
+#define DELTA_COMMAND_ELEMENT "DeltaCommand"
-/** 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.
+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* 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(), sigc::compose(
+ sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
+ sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
+
+ 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;
+}
+
+/** 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
+ * 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.
*/
-void
-MidiModel::append(double time, size_t size, Byte* in_buffer)
+bool
+MidiModel::write_to(boost::shared_ptr<MidiSource> source)
{
- assert(_events.empty() || time >= _events.back().time);
+ read_lock();
+
+ const bool old_percussive = percussive();
+ set_percussive(false);
- cerr << "Model event: time = " << time << endl;
+ source->drop_model();
+
+ for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
+ source->append_event_unlocked_beats(*i);
+ }
+
+ set_percussive(old_percussive);
+
+ read_unlock();
+ set_edited(false);
- Byte* my_buffer = new Byte[size];
- memcpy(my_buffer, in_buffer, size);
- _events.push_back(MidiEvent(time, size, my_buffer));
+ return true;
+}
+
+XMLNode&
+MidiModel::get_state()
+{
+ XMLNode *node = new XMLNode("MidiModel");
+ return *node;
}