2 Copyright (C) 2007 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #define __STDC_LIMIT_MACROS 1
27 #include "pbd/error.h"
28 #include "pbd/enumwriter.h"
29 #include "midi++/events.h"
31 #include "ardour/midi_model.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/types.h"
34 #include "ardour/session.h"
37 using namespace ARDOUR;
40 MidiModel::MidiModel(MidiSource* s, size_t size)
41 : AutomatableSequence<TimeType>(s->session(), size)
46 /** Start a new Delta command.
48 * This has no side-effects on the model or Session, the returned command
49 * can be held on to for as long as the caller wishes, or discarded without
50 * formality, until apply_command is called and ownership is taken.
52 MidiModel::DeltaCommand*
53 MidiModel::new_delta_command(const string name)
55 DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
59 /** Start a new Diff command.
61 * This has no side-effects on the model or Session, the returned command
62 * can be held on to for as long as the caller wishes, or discarded without
63 * formality, until apply_command is called and ownership is taken.
65 MidiModel::DiffCommand*
66 MidiModel::new_diff_command(const string name)
68 DiffCommand* cmd = new DiffCommand(_midi_source->model(), name);
74 * Ownership of cmd is taken, it must not be deleted by the caller.
75 * The command will constitute one item on the undo stack.
78 MidiModel::apply_command(Session& session, Command* cmd)
80 session.begin_reversible_command(cmd->name());
82 session.commit_reversible_command(cmd);
86 /** Apply a command as part of a larger reversible transaction
88 * Ownership of cmd is taken, it must not be deleted by the caller.
89 * The command will constitute one item on the undo stack.
92 MidiModel::apply_command_as_subcommand(Session& session, Command* cmd)
95 session.add_command(cmd);
102 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
110 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
118 MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
120 _removed_notes.remove(note);
121 _added_notes.push_back(note);
125 MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
127 _added_notes.remove(note);
128 _removed_notes.push_back(note);
132 MidiModel::DeltaCommand::operator()()
134 // This could be made much faster by using a priority_queue for added and
135 // removed notes (or sort here), and doing a single iteration over _model
137 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
138 _model->_midi_source->invalidate(); // release model read lock
139 _model->write_lock();
141 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
142 _model->add_note_unlocked(*i);
145 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
146 _model->remove_note_unlocked(*i);
149 _model->write_unlock();
150 _model->ContentsChanged(); /* EMIT SIGNAL */
154 MidiModel::DeltaCommand::undo()
156 // This could be made much faster by using a priority_queue for added and
157 // removed notes (or sort here), and doing a single iteration over _model
159 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
160 _model->_midi_source->invalidate(); // release model read lock
161 _model->write_lock();
163 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
164 _model->remove_note_unlocked(*i);
167 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
168 _model->add_note_unlocked(*i);
171 _model->write_unlock();
172 _model->ContentsChanged(); /* EMIT SIGNAL */
176 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
178 XMLNode* xml_note = new XMLNode("note");
179 ostringstream note_str(ios::ate);
180 note_str << int(note->note());
181 xml_note->add_property("note", note_str.str());
183 ostringstream channel_str(ios::ate);
184 channel_str << int(note->channel());
185 xml_note->add_property("channel", channel_str.str());
187 ostringstream time_str(ios::ate);
188 time_str << int(note->time());
189 xml_note->add_property("time", time_str.str());
191 ostringstream length_str(ios::ate);
192 length_str <<(unsigned int) note->length();
193 xml_note->add_property("length", length_str.str());
195 ostringstream velocity_str(ios::ate);
196 velocity_str << (unsigned int) note->velocity();
197 xml_note->add_property("velocity", velocity_str.str());
202 boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
203 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
207 unsigned int channel;
210 unsigned int velocity;
212 if ((prop = xml_note->property("note")) != 0) {
213 istringstream note_str(prop->value());
216 warning << "note information missing note value" << endmsg;
220 if ((prop = xml_note->property("channel")) != 0) {
221 istringstream channel_str(prop->value());
222 channel_str >> channel;
224 warning << "note information missing channel" << endmsg;
228 if ((prop = xml_note->property("time")) != 0) {
229 istringstream time_str(prop->value());
232 warning << "note information missing time" << endmsg;
236 if ((prop = xml_note->property("length")) != 0) {
237 istringstream length_str(prop->value());
238 length_str >> length;
240 warning << "note information missing length" << endmsg;
244 if ((prop = xml_note->property("velocity")) != 0) {
245 istringstream velocity_str(prop->value());
246 velocity_str >> velocity;
248 warning << "note information missing velocity" << endmsg;
252 boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
253 channel, time, length, note, velocity));
257 #define ADDED_NOTES_ELEMENT "AddedNotes"
258 #define REMOVED_NOTES_ELEMENT "RemovedNotes"
259 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
262 MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
264 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
268 _added_notes.clear();
269 XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
271 XMLNodeList notes = added_notes->children();
272 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
273 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
276 _removed_notes.clear();
277 XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
279 XMLNodeList notes = removed_notes->children();
280 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
281 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
288 MidiModel::DeltaCommand::get_state()
290 XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
291 delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
293 XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
294 for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
295 sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
296 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
298 XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
299 for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
300 sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
301 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
303 return *delta_command;
306 /************** DIFF COMMAND ********************/
308 #define DIFF_NOTES_ELEMENT "ChangedNotes"
309 #define DIFF_COMMAND_ELEMENT "DiffCommand"
311 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
319 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
327 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
330 NotePropertyChange change;
333 change.property = prop;
334 change.new_value = new_value;
338 change.old_value = note->note();
341 change.old_value = note->velocity();
344 fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
348 fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
352 change.old_value = note->channel();
356 _changes.push_back (change);
360 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
363 NotePropertyChange change;
366 change.property = prop;
367 change.new_time = new_time;
373 fatal << "MidiModel::DiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
376 change.old_time = note->time();
379 change.old_time = note->length();
383 _changes.push_back (change);
387 MidiModel::DiffCommand::operator()()
389 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
390 _model->_midi_source->invalidate(); // release model read lock
391 _model->write_lock();
393 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
394 Property prop = i->property;
397 i->note->set_note (i->new_value);
400 i->note->set_velocity (i->new_value);
403 i->note->set_time (i->new_time);
406 i->note->set_length (i->new_time);
409 i->note->set_channel (i->new_value);
414 _model->write_unlock();
415 _model->ContentsChanged(); /* EMIT SIGNAL */
419 MidiModel::DiffCommand::undo()
421 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
422 _model->_midi_source->invalidate(); // release model read lock
423 _model->write_lock();
425 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
426 Property prop = i->property;
429 i->note->set_note (i->old_value);
432 i->note->set_velocity (i->old_value);
435 i->note->set_time (i->old_time);
438 i->note->set_length (i->old_time);
441 i->note->set_channel (i->old_value);
446 _model->write_unlock();
447 _model->ContentsChanged(); /* EMIT SIGNAL */
451 MidiModel::DiffCommand::marshal_change(const NotePropertyChange& change)
453 XMLNode* xml_change = new XMLNode("change");
455 /* first, the change itself */
457 xml_change->add_property ("property", enum_2_string (change.property));
460 ostringstream old_value_str (ios::ate);
461 old_value_str << (unsigned int) change.old_value;
462 xml_change->add_property ("old", old_value_str.str());
466 ostringstream new_value_str (ios::ate);
467 new_value_str << (unsigned int) change.old_value;
468 xml_change->add_property ("new", new_value_str.str());
471 /* now the rest of the note */
473 if (change.property != NoteNumber) {
474 ostringstream note_str(ios::ate);
475 note_str << int(change.note->note());
476 xml_change->add_property("note", note_str.str());
479 if (change.property != Channel) {
480 ostringstream channel_str(ios::ate);
481 channel_str << int(change.note->channel());
482 xml_change->add_property("channel", channel_str.str());
485 if (change.property != StartTime) {
486 ostringstream time_str(ios::ate);
487 time_str << int(change.note->time());
488 xml_change->add_property("time", time_str.str());
491 if (change.property != Length) {
492 ostringstream length_str(ios::ate);
493 length_str <<(unsigned int) change.note->length();
494 xml_change->add_property("length", length_str.str());
497 if (change.property != Velocity) {
498 ostringstream velocity_str(ios::ate);
499 velocity_str << (unsigned int) change.note->velocity();
500 xml_change->add_property("velocity", velocity_str.str());
506 MidiModel::DiffCommand::NotePropertyChange
507 MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
510 NotePropertyChange change;
512 unsigned int channel;
515 unsigned int velocity;
517 if ((prop = xml_change->property("property")) != 0) {
518 change.property = (Property) string_2_enum (prop->value(), change.property);
520 fatal << "!!!" << endmsg;
524 if ((prop = xml_change->property ("old")) != 0) {
525 istringstream old_str (prop->value());
526 old_str >> change.old_value;
528 fatal << "!!!" << endmsg;
532 if ((prop = xml_change->property ("new")) != 0) {
533 istringstream new_str (prop->value());
534 new_str >> change.new_value;
536 fatal << "!!!" << endmsg;
540 if (change.property != NoteNumber) {
541 if ((prop = xml_change->property("note")) != 0) {
542 istringstream note_str(prop->value());
545 warning << "note information missing note value" << endmsg;
549 note = change.new_value;
552 if (change.property != Channel) {
553 if ((prop = xml_change->property("channel")) != 0) {
554 istringstream channel_str(prop->value());
555 channel_str >> channel;
557 warning << "note information missing channel" << endmsg;
561 channel = change.new_value;
564 if (change.property != StartTime) {
565 if ((prop = xml_change->property("time")) != 0) {
566 istringstream time_str(prop->value());
569 warning << "note information missing time" << endmsg;
573 time = change.new_value;
576 if (change.property != Length) {
577 if ((prop = xml_change->property("length")) != 0) {
578 istringstream length_str(prop->value());
579 length_str >> length;
581 warning << "note information missing length" << endmsg;
585 length = change.new_value;
588 if (change.property != Velocity) {
589 if ((prop = xml_change->property("velocity")) != 0) {
590 istringstream velocity_str(prop->value());
591 velocity_str >> velocity;
593 warning << "note information missing velocity" << endmsg;
597 velocity = change.new_value;
600 /* we must point at the instance of the note that is actually in the model.
601 so go look for it ...
604 boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
606 change.note = _model->find_note (new_note);
609 warning << "MIDI note not found in model - programmers should investigate this" << endmsg;
610 /* use the actual new note */
611 change.note = new_note;
618 MidiModel::DiffCommand::set_state(const XMLNode& diff_command)
620 if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
626 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
629 XMLNodeList notes = changed_notes->children();
631 transform (notes.begin(), notes.end(), back_inserter(_changes),
632 sigc::mem_fun(*this, &DiffCommand::unmarshal_change));
639 MidiModel::DiffCommand::get_state ()
641 XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
642 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
644 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
645 for_each(_changes.begin(), _changes.end(), sigc::compose(
646 sigc::mem_fun(*changes, &XMLNode::add_child_nocopy),
647 sigc::mem_fun(*this, &DiffCommand::marshal_change)));
649 return *diff_command;
652 /** Write the model to a MidiSource (i.e. save the model).
653 * This is different from manually using read to write to a source in that
654 * note off events are written regardless of the track mode. This is so the
655 * user can switch a recorded track (with note durations from some instrument)
656 * to percussive, save, reload, then switch it back to sustained without
657 * destroying the original note durations.
660 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
664 const bool old_percussive = percussive();
665 set_percussive(false);
667 source->drop_model();
669 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
670 source->append_event_unlocked_beats(*i);
673 set_percussive(old_percussive);
682 MidiModel::get_state()
684 XMLNode *node = new XMLNode("MidiModel");
688 boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
689 MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
691 Notes::iterator i = find (notes().begin(), notes().end(), other);
693 if (i == notes().end()) {
694 return boost::shared_ptr<Evoral::Note<TimeType> > ();