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/midi_state_tracker.h"
34 #include "ardour/smf_source.h"
35 #include "ardour/types.h"
36 #include "ardour/session.h"
39 using namespace ARDOUR;
42 MidiModel::MidiModel(MidiSource* s)
43 : AutomatableSequence<TimeType>(s->session())
48 /** Start a new Delta command.
50 * This has no side-effects on the model or Session, the returned command
51 * can be held on to for as long as the caller wishes, or discarded without
52 * formality, until apply_command is called and ownership is taken.
54 MidiModel::DeltaCommand*
55 MidiModel::new_delta_command(const string name)
57 DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
61 /** Start a new Diff command.
63 * This has no side-effects on the model or Session, the returned command
64 * can be held on to for as long as the caller wishes, or discarded without
65 * formality, until apply_command is called and ownership is taken.
67 MidiModel::DiffCommand*
68 MidiModel::new_diff_command(const string name)
70 DiffCommand* cmd = new DiffCommand(_midi_source->model(), name);
76 * Ownership of cmd is taken, it must not be deleted by the caller.
77 * The command will constitute one item on the undo stack.
80 MidiModel::apply_command(Session& session, Command* cmd)
82 session.begin_reversible_command(cmd->name());
84 session.commit_reversible_command(cmd);
88 /** Apply a command as part of a larger reversible transaction
90 * Ownership of cmd is taken, it must not be deleted by the caller.
91 * The command will constitute one item on the undo stack.
94 MidiModel::apply_command_as_subcommand(Session& session, Command* cmd)
97 session.add_command(cmd);
104 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
112 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
116 set_state(node, Stateful::loading_state_version);
120 MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
122 _removed_notes.remove(note);
123 _added_notes.push_back(note);
127 MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
129 _added_notes.remove(note);
130 _removed_notes.push_back(note);
134 MidiModel::DeltaCommand::operator()()
136 // This could be made much faster by using a priority_queue for added and
137 // removed notes (or sort here), and doing a single iteration over _model
139 MidiModel::WriteLock lock(_model->edit_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);
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 MidiModel::WriteLock lock(_model->edit_lock());;
161 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
162 _model->remove_note_unlocked(*i);
165 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
166 _model->add_note_unlocked(*i);
170 _model->ContentsChanged(); /* EMIT SIGNAL */
174 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
176 XMLNode* xml_note = new XMLNode("note");
177 ostringstream note_str(ios::ate);
178 note_str << int(note->note());
179 xml_note->add_property("note", note_str.str());
181 ostringstream channel_str(ios::ate);
182 channel_str << int(note->channel());
183 xml_note->add_property("channel", channel_str.str());
185 ostringstream time_str(ios::ate);
186 time_str << int(note->time());
187 xml_note->add_property("time", time_str.str());
189 ostringstream length_str(ios::ate);
190 length_str <<(unsigned int) note->length();
191 xml_note->add_property("length", length_str.str());
193 ostringstream velocity_str(ios::ate);
194 velocity_str << (unsigned int) note->velocity();
195 xml_note->add_property("velocity", velocity_str.str());
200 boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
201 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
205 unsigned int channel;
208 unsigned int velocity;
210 if ((prop = xml_note->property("note")) != 0) {
211 istringstream note_str(prop->value());
214 warning << "note information missing note value" << endmsg;
218 if ((prop = xml_note->property("channel")) != 0) {
219 istringstream channel_str(prop->value());
220 channel_str >> channel;
222 warning << "note information missing channel" << endmsg;
226 if ((prop = xml_note->property("time")) != 0) {
227 istringstream time_str(prop->value());
230 warning << "note information missing time" << endmsg;
234 if ((prop = xml_note->property("length")) != 0) {
235 istringstream length_str(prop->value());
236 length_str >> length;
238 warning << "note information missing length" << endmsg;
242 if ((prop = xml_note->property("velocity")) != 0) {
243 istringstream velocity_str(prop->value());
244 velocity_str >> velocity;
246 warning << "note information missing velocity" << endmsg;
250 boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
251 channel, time, length, note, velocity));
255 #define ADDED_NOTES_ELEMENT "AddedNotes"
256 #define REMOVED_NOTES_ELEMENT "RemovedNotes"
257 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
260 MidiModel::DeltaCommand::set_state (const XMLNode& delta_command, int /*version*/)
262 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
266 _added_notes.clear();
267 XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
269 XMLNodeList notes = added_notes->children();
270 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
271 boost::bind (&DeltaCommand::unmarshal_note, this, _1));
274 _removed_notes.clear();
275 XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
277 XMLNodeList notes = removed_notes->children();
278 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
279 boost::bind (&DeltaCommand::unmarshal_note, this, _1));
286 MidiModel::DeltaCommand::get_state()
288 XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
289 delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
291 XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
292 for_each(_added_notes.begin(), _added_notes.end(),
294 boost::bind (&XMLNode::add_child_nocopy, added_notes, _1),
295 boost::bind (&DeltaCommand::marshal_note, this, _1)));
297 XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
298 for_each(_removed_notes.begin(), _removed_notes.end(),
300 boost::bind (&XMLNode::add_child_nocopy, removed_notes, _1),
301 boost::bind (&DeltaCommand::marshal_note, this, _1)));
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)
323 set_state(node, Stateful::loading_state_version);
327 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
334 if (new_value == note->note()) {
337 change.old_value = note->note();
340 if (new_value == note->velocity()) {
343 change.old_value = note->velocity();
346 if (new_value == note->channel()) {
349 change.old_value = note->channel();
354 fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
358 fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
364 change.property = prop;
365 change.new_value = new_value;
367 _changes.push_back (change);
371 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
380 fatal << "MidiModel::DiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
384 if (Evoral::musical_time_equal (note->time(), new_time)) {
387 change.old_time = note->time();
390 if (Evoral::musical_time_equal (note->length(), new_time)) {
393 change.old_time = note->length();
398 change.property = prop;
399 change.new_time = new_time;
401 _changes.push_back (change);
405 MidiModel::DiffCommand::operator()()
408 MidiModel::WriteLock lock(_model->edit_lock());
410 set<boost::shared_ptr<Evoral::Note<TimeType> > > removed_notes;
412 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
413 Property prop = i->property;
416 if (removed_notes.find (i->note) == removed_notes.end()) {
417 _model->remove_note_unlocked (i->note);
418 removed_notes.insert (i->note);
420 i->note->set_note (i->new_value);
423 i->note->set_velocity (i->new_value);
426 if (removed_notes.find (i->note) == removed_notes.end()) {
427 _model->remove_note_unlocked (i->note);
428 removed_notes.insert (i->note);
431 i->note->set_time (i->new_time);
434 i->note->set_length (i->new_time);
437 if (removed_notes.find (i->note) == removed_notes.end()) {
438 _model->remove_note_unlocked (i->note);
439 removed_notes.insert (i->note);
441 i->note->set_channel (i->new_value);
446 for (set<boost::shared_ptr<Evoral::Note<TimeType> > >::iterator i = removed_notes.begin(); i != removed_notes.end(); ++i) {
447 _model->add_note_unlocked (*i);
451 _model->ContentsChanged(); /* EMIT SIGNAL */
455 MidiModel::DiffCommand::undo()
458 MidiModel::WriteLock lock(_model->edit_lock());
460 set<boost::shared_ptr<Evoral::Note<TimeType> > > removed_notes;
462 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
463 Property prop = i->property;
466 if (removed_notes.find (i->note) == removed_notes.end()) {
467 _model->remove_note_unlocked (i->note);
468 removed_notes.insert (i->note);
470 i->note->set_note (i->old_value);
473 i->note->set_velocity (i->old_value);
476 if (removed_notes.find (i->note) == removed_notes.end()) {
477 _model->remove_note_unlocked (i->note);
478 removed_notes.insert (i->note);
480 i->note->set_time (i->old_time);
483 i->note->set_length (i->old_time);
486 if (removed_notes.find (i->note) == removed_notes.end()) {
487 _model->remove_note_unlocked (i->note);
488 removed_notes.insert (i->note);
490 i->note->set_channel (i->old_value);
495 for (set<boost::shared_ptr<Evoral::Note<TimeType> > >::iterator i = removed_notes.begin(); i != removed_notes.end(); ++i) {
496 _model->add_note_unlocked (*i);
500 _model->ContentsChanged(); /* EMIT SIGNAL */
504 MidiModel::DiffCommand::marshal_change(const NoteChange& change)
506 XMLNode* xml_change = new XMLNode("change");
508 /* first, the change itself */
510 xml_change->add_property ("property", enum_2_string (change.property));
513 ostringstream old_value_str (ios::ate);
514 if (change.property == StartTime || change.property == Length) {
515 old_value_str << change.old_time;
517 old_value_str << (unsigned int) change.old_value;
519 xml_change->add_property ("old", old_value_str.str());
523 ostringstream new_value_str (ios::ate);
524 if (change.property == StartTime || change.property == Length) {
525 new_value_str << change.new_time;
527 new_value_str << (unsigned int) change.new_value;
529 xml_change->add_property ("new", new_value_str.str());
532 /* now the rest of the note */
534 const SMFSource* smf = dynamic_cast<const SMFSource*> (_model->midi_source());
536 if (change.property != NoteNumber) {
537 ostringstream note_str;
538 note_str << int(change.note->note());
539 xml_change->add_property("note", note_str.str());
542 if (change.property != Channel) {
543 ostringstream channel_str;
544 channel_str << int(change.note->channel());
545 xml_change->add_property("channel", channel_str.str());
548 if (change.property != StartTime) {
549 ostringstream time_str;
551 time_str << smf->round_to_file_precision (change.note->time());
553 time_str << change.note->time();
555 xml_change->add_property("time", time_str.str());
558 if (change.property != Length) {
559 ostringstream length_str;
561 length_str << smf->round_to_file_precision (change.note->length());
563 length_str << change.note->length();
565 xml_change->add_property ("length", length_str.str());
568 if (change.property != Velocity) {
569 ostringstream velocity_str;
570 velocity_str << int (change.note->velocity());
571 xml_change->add_property("velocity", velocity_str.str());
577 MidiModel::DiffCommand::NoteChange
578 MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
583 unsigned int channel;
584 unsigned int velocity;
585 Evoral::MusicalTime time;
586 Evoral::MusicalTime length;
588 if ((prop = xml_change->property("property")) != 0) {
589 change.property = (Property) string_2_enum (prop->value(), change.property);
591 fatal << "!!!" << endmsg;
595 if ((prop = xml_change->property ("old")) != 0) {
596 istringstream old_str (prop->value());
597 if (change.property == StartTime || change.property == Length) {
598 old_str >> change.old_time;
600 int integer_value_so_that_istream_does_the_right_thing;
601 old_str >> integer_value_so_that_istream_does_the_right_thing;
602 change.old_value = integer_value_so_that_istream_does_the_right_thing;
605 fatal << "!!!" << endmsg;
609 if ((prop = xml_change->property ("new")) != 0) {
610 istringstream new_str (prop->value());
611 if (change.property == StartTime || change.property == Length) {
612 new_str >> change.new_time;
614 int integer_value_so_that_istream_does_the_right_thing;
615 new_str >> integer_value_so_that_istream_does_the_right_thing;
616 change.new_value = integer_value_so_that_istream_does_the_right_thing;
619 fatal << "!!!" << endmsg;
623 if (change.property != NoteNumber) {
624 if ((prop = xml_change->property("note")) != 0) {
625 istringstream note_str(prop->value());
628 warning << "note information missing note value" << endmsg;
632 note = change.new_value;
635 if (change.property != Channel) {
636 if ((prop = xml_change->property("channel")) != 0) {
637 istringstream channel_str(prop->value());
638 channel_str >> channel;
640 warning << "note information missing channel" << endmsg;
644 channel = change.new_value;
647 if (change.property != StartTime) {
648 if ((prop = xml_change->property("time")) != 0) {
649 istringstream time_str(prop->value());
652 warning << "note information missing time" << endmsg;
656 time = change.new_time;
659 if (change.property != Length) {
660 if ((prop = xml_change->property("length")) != 0) {
661 istringstream length_str(prop->value());
662 length_str >> length;
664 warning << "note information missing length" << endmsg;
668 length = change.new_time;
671 if (change.property != Velocity) {
672 if ((prop = xml_change->property("velocity")) != 0) {
673 istringstream velocity_str(prop->value());
674 velocity_str >> velocity;
676 warning << "note information missing velocity" << endmsg;
680 velocity = change.new_value;
683 /* we must point at the instance of the note that is actually in the model.
684 so go look for it ...
687 boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
689 change.note = _model->find_note (new_note);
692 warning << "MIDI note " << *new_note << " not found in model - programmers should investigate this" << endmsg;
693 /* use the actual new note */
694 change.note = new_note;
701 MidiModel::DiffCommand::set_state(const XMLNode& diff_command, int /*version*/)
703 if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
709 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
712 XMLNodeList notes = changed_notes->children();
713 transform (notes.begin(), notes.end(), back_inserter(_changes),
714 boost::bind (&DiffCommand::unmarshal_change, this, _1));
722 MidiModel::DiffCommand::get_state ()
724 XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
725 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
727 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
728 for_each(_changes.begin(), _changes.end(),
730 boost::bind (&XMLNode::add_child_nocopy, changes, _1),
731 boost::bind (&DiffCommand::marshal_change, this, _1)));
733 return *diff_command;
736 /** Write all of the model to a MidiSource (i.e. save the model).
737 * This is different from manually using read to write to a source in that
738 * note off events are written regardless of the track mode. This is so the
739 * user can switch a recorded track (with note durations from some instrument)
740 * to percussive, save, reload, then switch it back to sustained without
741 * destroying the original note durations.
744 MidiModel::write_to (boost::shared_ptr<MidiSource> source)
746 ReadLock lock(read_lock());
748 const bool old_percussive = percussive();
749 set_percussive(false);
751 source->drop_model();
752 source->mark_streaming_midi_write_started(note_mode(), _midi_source->timeline_position());
754 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
755 source->append_event_unlocked_beats(*i);
758 set_percussive(old_percussive);
759 source->mark_streaming_write_completed();
766 /** Write part or all of the model to a MidiSource (i.e. save the model).
767 * This is different from manually using read to write to a source in that
768 * note off events are written regardless of the track mode. This is so the
769 * user can switch a recorded track (with note durations from some instrument)
770 * to percussive, save, reload, then switch it back to sustained without
771 * destroying the original note durations.
774 MidiModel::write_section_to (boost::shared_ptr<MidiSource> source, Evoral::MusicalTime begin_time, Evoral::MusicalTime end_time)
776 ReadLock lock(read_lock());
777 MidiStateTracker mst;
778 Evoral::MusicalTime extra_note_on_time = end_time;
780 const bool old_percussive = percussive();
781 set_percussive(false);
783 source->drop_model();
784 source->mark_streaming_midi_write_started(note_mode(), _midi_source->timeline_position());
786 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
787 const Evoral::Event<Evoral::MusicalTime>& ev (*i);
789 if (ev.time() >= begin_time && ev.time() < end_time) {
791 const Evoral::MIDIEvent<Evoral::MusicalTime>* mev =
792 static_cast<const Evoral::MIDIEvent<Evoral::MusicalTime>* > (&ev);
799 if (mev->is_note_off()) {
801 if (!mst.active (mev->note(), mev->channel())) {
803 /* add a note-on at the start of the range we're writing
804 to the file. velocity is just an arbitary reasonable value.
807 Evoral::MIDIEvent<Evoral::MusicalTime> on (mev->event_type(), extra_note_on_time, 3, 0, true);
808 on.set_type (mev->type());
809 on.set_note (mev->note());
810 on.set_channel (mev->channel());
811 on.set_velocity (mev->velocity());
813 cerr << "Add note on for odd note off, note = " << (int) on.note() << endl;
814 source->append_event_unlocked_beats (on);
815 mst.add (on.note(), on.channel());
817 extra_note_on_time += 1.0/128.0;
820 cerr << "MIDI Note off (note = " << (int) mev->note() << endl;
821 source->append_event_unlocked_beats (*i);
822 mst.remove (mev->note(), mev->channel());
825 } else if (mev->is_note_on()) {
826 cerr << "MIDI Note on (note = " << (int) mev->note() << endl;
827 mst.add (mev->note(), mev->channel());
828 source->append_event_unlocked_beats(*i);
831 cerr << "MIDI other event type\n";
832 source->append_event_unlocked_beats(*i);
837 mst.resolve_notes (*source, end_time);
839 set_percussive(old_percussive);
840 source->mark_streaming_write_completed();
848 MidiModel::get_state()
850 XMLNode *node = new XMLNode("MidiModel");
854 boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
855 MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
857 Notes::iterator l = notes().lower_bound(other);
859 if (l != notes().end()) {
860 for (; (*l)->time() == other->time(); ++l) {
861 /* NB: compare note contents, not note pointers.
862 If "other" was a ptr to a note already in
863 the model, we wouldn't be looking for it,
872 return boost::shared_ptr<Evoral::Note<TimeType> >();
875 /** Lock and invalidate the source.
876 * This should be used by commands and editing things
879 MidiModel::edit_lock()
881 Glib::Mutex::Lock* source_lock = new Glib::Mutex::Lock(_midi_source->mutex());
882 _midi_source->invalidate(); // Release cached iterator's read lock on model
883 return WriteLock(new WriteLockImpl(source_lock, _lock, _control_lock));
886 /** Lock just the model, the source lock must already be held.
887 * This should only be called from libardour/evoral places
890 MidiModel::write_lock()
892 assert(!_midi_source->mutex().trylock());
893 return WriteLock(new WriteLockImpl(NULL, _lock, _control_lock));