2 Copyright (C) 2007 Paul Davis
3 Author: David Robillard
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.
27 #include "pbd/compose.h"
28 #include "pbd/enumwriter.h"
29 #include "pbd/error.h"
31 #include "evoral/Control.hpp"
33 #include "midi++/events.h"
35 #include "ardour/automation_control.h"
36 #include "ardour/midi_automation_list_binder.h"
37 #include "ardour/midi_model.h"
38 #include "ardour/midi_source.h"
39 #include "ardour/midi_state_tracker.h"
40 #include "ardour/session.h"
41 #include "ardour/types.h"
46 using namespace ARDOUR;
49 MidiModel::MidiModel (boost::shared_ptr<MidiSource> s)
50 : AutomatableSequence<TimeType>(s->session())
55 /** Start a new NoteDiff command.
57 * This has no side-effects on the model or Session, the returned command
58 * can be held on to for as long as the caller wishes, or discarded without
59 * formality, until apply_command is called and ownership is taken.
61 MidiModel::NoteDiffCommand*
62 MidiModel::new_note_diff_command (const string name)
64 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
67 return new NoteDiffCommand (ms->model(), name);
70 /** Start a new SysExDiff command */
71 MidiModel::SysExDiffCommand*
72 MidiModel::new_sysex_diff_command (const string name)
74 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
77 return new SysExDiffCommand (ms->model(), name);
80 /** Start a new PatchChangeDiff command */
81 MidiModel::PatchChangeDiffCommand*
82 MidiModel::new_patch_change_diff_command (const string name)
84 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
87 return new PatchChangeDiffCommand (ms->model(), name);
93 * Ownership of cmd is taken, it must not be deleted by the caller.
94 * The command will constitute one item on the undo stack.
97 MidiModel::apply_command(Session& session, Command* cmd)
99 session.begin_reversible_command(cmd->name());
101 session.commit_reversible_command(cmd);
105 /** Apply a command as part of a larger reversible transaction
107 * Ownership of cmd is taken, it must not be deleted by the caller.
108 * The command will constitute one item on the undo stack.
111 MidiModel::apply_command_as_subcommand(Session& session, Command* cmd)
114 session.add_command(cmd);
118 /************** DIFF COMMAND ********************/
120 #define NOTE_DIFF_COMMAND_ELEMENT "NoteDiffCommand"
121 #define DIFF_NOTES_ELEMENT "ChangedNotes"
122 #define ADDED_NOTES_ELEMENT "AddedNotes"
123 #define REMOVED_NOTES_ELEMENT "RemovedNotes"
124 #define SIDE_EFFECT_REMOVALS_ELEMENT "SideEffectRemovals"
125 #define SYSEX_DIFF_COMMAND_ELEMENT "SysExDiffCommand"
126 #define DIFF_SYSEXES_ELEMENT "ChangedSysExes"
127 #define PATCH_CHANGE_DIFF_COMMAND_ELEMENT "PatchChangeDiffCommand"
128 #define ADDED_PATCH_CHANGES_ELEMENT "AddedPatchChanges"
129 #define REMOVED_PATCH_CHANGES_ELEMENT "RemovedPatchChanges"
130 #define DIFF_PATCH_CHANGES_ELEMENT "ChangedPatchChanges"
132 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
140 MidiModel::NoteDiffCommand::NoteDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node)
141 : DiffCommand (m, "")
144 set_state (node, Stateful::loading_state_version);
148 MidiModel::NoteDiffCommand::add (const NotePtr note)
150 _removed_notes.remove(note);
151 _added_notes.push_back(note);
155 MidiModel::NoteDiffCommand::remove (const NotePtr note)
157 _added_notes.remove(note);
158 _removed_notes.push_back(note);
162 MidiModel::NoteDiffCommand::side_effect_remove (const NotePtr note)
164 side_effect_removals.insert (note);
168 MidiModel::NoteDiffCommand::change (const NotePtr note, Property prop,
177 if (new_value == note->note()) {
180 change.old_value = note->note();
183 if (new_value == note->velocity()) {
186 change.old_value = note->velocity();
189 if (new_value == note->channel()) {
192 change.old_value = note->channel();
197 fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
201 fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
207 change.property = prop;
208 change.new_value = new_value;
210 _changes.push_back (change);
214 MidiModel::NoteDiffCommand::change (const NotePtr note, Property prop,
225 fatal << "MidiModel::NoteDiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
229 if (Evoral::musical_time_equal (note->time(), new_time)) {
232 change.old_time = note->time();
235 if (Evoral::musical_time_equal (note->length(), new_time)) {
238 change.old_time = note->length();
243 change.property = prop;
244 change.new_time = new_time;
246 _changes.push_back (change);
249 MidiModel::NoteDiffCommand &
250 MidiModel::NoteDiffCommand::operator+= (const NoteDiffCommand& other)
252 if (this == &other) {
256 if (_model != other._model) {
260 _added_notes.insert (_added_notes.end(), other._added_notes.begin(), other._added_notes.end());
261 _removed_notes.insert (_removed_notes.end(), other._removed_notes.begin(), other._removed_notes.end());
262 side_effect_removals.insert (other.side_effect_removals.begin(), other.side_effect_removals.end());
263 _changes.insert (_changes.end(), other._changes.begin(), other._changes.end());
269 MidiModel::NoteDiffCommand::operator() ()
272 MidiModel::WriteLock lock(_model->edit_lock());
274 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
275 if (!_model->add_note_unlocked(*i)) {
276 /* failed to add it, so don't leave it in the removed list, to
277 avoid apparent errors on undo.
279 _removed_notes.remove (*i);
283 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
284 _model->remove_note_unlocked(*i);
287 /* notes we modify in a way that requires remove-then-add to maintain ordering */
288 set<NotePtr> temporary_removals;
290 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
291 Property prop = i->property;
294 /* note found during deserialization, so try
295 again now that the model state is different.
297 i->note = _model->find_note (i->note_id);
303 if (temporary_removals.find (i->note) == temporary_removals.end()) {
304 _model->remove_note_unlocked (i->note);
305 temporary_removals.insert (i->note);
307 i->note->set_note (i->new_value);
311 if (temporary_removals.find (i->note) == temporary_removals.end()) {
312 _model->remove_note_unlocked (i->note);
313 temporary_removals.insert (i->note);
315 i->note->set_time (i->new_time);
319 if (temporary_removals.find (i->note) == temporary_removals.end()) {
320 _model->remove_note_unlocked (i->note);
321 temporary_removals.insert (i->note);
323 i->note->set_channel (i->new_value);
326 /* no remove-then-add required for these properties, since we do not index them
330 i->note->set_velocity (i->new_value);
334 i->note->set_length (i->new_time);
340 for (set<NotePtr>::iterator i = temporary_removals.begin(); i != temporary_removals.end(); ++i) {
341 NoteDiffCommand side_effects (model(), "side effects");
342 if (_model->add_note_unlocked (*i, &side_effects)) {
343 /* The note was re-added ok */
344 *this += side_effects;
346 /* The note that we removed earlier could not be re-added. This change record
347 must say that the note was removed. We'll keep the changes we made, though,
348 as if the note is re-added by the undo the changes must also be undone.
350 _removed_notes.push_back (*i);
354 if (!side_effect_removals.empty()) {
356 for (set<NotePtr>::iterator i = side_effect_removals.begin(); i != side_effect_removals.end(); ++i) {
357 cerr << "\t" << *i << ' ' << **i << endl;
362 _model->ContentsChanged(); /* EMIT SIGNAL */
366 MidiModel::NoteDiffCommand::undo ()
369 MidiModel::WriteLock lock(_model->edit_lock());
371 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
372 _model->remove_note_unlocked(*i);
375 /* Apply changes first; this is important in the case of a note change which
376 resulted in the note being removed by the overlap checker. If the overlap
377 checker removes a note, it will be in _removed_notes. We are going to re-add
378 it below, but first we must undo the changes we made so that the overlap
379 checker doesn't refuse the re-add.
382 /* notes we modify in a way that requires remove-then-add to maintain ordering */
383 set<NotePtr> temporary_removals;
386 /* lazily discover any affected notes that were not discovered when
387 * loading the history because of deletions, etc.
390 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
392 i->note = _model->find_note (i->note_id);
397 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
398 Property prop = i->property;
402 if (temporary_removals.find (i->note) == temporary_removals.end() &&
403 find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()) {
405 /* We only need to mark this note for re-add if (a) we haven't
406 already marked it and (b) it isn't on the _removed_notes
407 list (which means that it has already been removed and it
408 will be re-added anyway)
411 _model->remove_note_unlocked (i->note);
412 temporary_removals.insert (i->note);
414 i->note->set_note (i->old_value);
418 if (temporary_removals.find (i->note) == temporary_removals.end() &&
419 find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()) {
423 _model->remove_note_unlocked (i->note);
424 temporary_removals.insert (i->note);
426 i->note->set_time (i->old_time);
430 if (temporary_removals.find (i->note) == temporary_removals.end() &&
431 find (_removed_notes.begin(), _removed_notes.end(), i->note) == _removed_notes.end()) {
435 _model->remove_note_unlocked (i->note);
436 temporary_removals.insert (i->note);
438 i->note->set_channel (i->old_value);
441 /* no remove-then-add required for these properties, since we do not index them
445 i->note->set_velocity (i->old_value);
449 i->note->set_length (i->old_time);
454 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
455 _model->add_note_unlocked(*i);
458 for (set<NotePtr>::iterator i = temporary_removals.begin(); i != temporary_removals.end(); ++i) {
459 _model->add_note_unlocked (*i);
462 /* finally add back notes that were removed by the "do". we don't care
463 about side effects here since the model should be back to its original
464 state once this is done.
467 for (set<NotePtr>::iterator i = side_effect_removals.begin(); i != side_effect_removals.end(); ++i) {
468 _model->add_note_unlocked (*i);
472 _model->ContentsChanged(); /* EMIT SIGNAL */
476 MidiModel::NoteDiffCommand::marshal_note(const NotePtr note)
478 XMLNode* xml_note = new XMLNode("note");
481 ostringstream id_str(ios::ate);
482 id_str << int(note->id());
483 xml_note->add_property("id", id_str.str());
487 ostringstream note_str(ios::ate);
488 note_str << int(note->note());
489 xml_note->add_property("note", note_str.str());
493 ostringstream channel_str(ios::ate);
494 channel_str << int(note->channel());
495 xml_note->add_property("channel", channel_str.str());
499 ostringstream time_str(ios::ate);
500 time_str << note->time();
501 xml_note->add_property("time", time_str.str());
505 ostringstream length_str(ios::ate);
506 length_str << note->length();
507 xml_note->add_property("length", length_str.str());
511 ostringstream velocity_str(ios::ate);
512 velocity_str << (unsigned int) note->velocity();
513 xml_note->add_property("velocity", velocity_str.str());
519 Evoral::Sequence<MidiModel::TimeType>::NotePtr
520 MidiModel::NoteDiffCommand::unmarshal_note (XMLNode *xml_note)
524 unsigned int channel;
525 MidiModel::TimeType time;
526 MidiModel::TimeType length;
527 unsigned int velocity;
530 if ((prop = xml_note->property("id")) != 0) {
531 istringstream id_str(prop->value());
534 error << "note information missing ID value" << endmsg;
538 if ((prop = xml_note->property("note")) != 0) {
539 istringstream note_str(prop->value());
542 warning << "note information missing note value" << endmsg;
546 if ((prop = xml_note->property("channel")) != 0) {
547 istringstream channel_str(prop->value());
548 channel_str >> channel;
550 warning << "note information missing channel" << endmsg;
554 if ((prop = xml_note->property("time")) != 0) {
555 istringstream time_str(prop->value());
558 warning << "note information missing time" << endmsg;
562 if ((prop = xml_note->property("length")) != 0) {
563 istringstream length_str(prop->value());
564 length_str >> length;
566 warning << "note information missing length" << endmsg;
570 if ((prop = xml_note->property("velocity")) != 0) {
571 istringstream velocity_str(prop->value());
572 velocity_str >> velocity;
574 warning << "note information missing velocity" << endmsg;
578 NotePtr note_ptr(new Evoral::Note<TimeType>(channel, time, length, note, velocity));
579 note_ptr->set_id (id);
585 MidiModel::NoteDiffCommand::marshal_change (const NoteChange& change)
587 XMLNode* xml_change = new XMLNode("Change");
589 /* first, the change itself */
591 xml_change->add_property ("property", enum_2_string (change.property));
594 ostringstream old_value_str (ios::ate);
595 if (change.property == StartTime || change.property == Length) {
596 old_value_str << change.old_time;
598 old_value_str << (unsigned int) change.old_value;
600 xml_change->add_property ("old", old_value_str.str());
604 ostringstream new_value_str (ios::ate);
605 if (change.property == StartTime || change.property == Length) {
606 new_value_str << change.new_time;
608 new_value_str << (unsigned int) change.new_value;
610 xml_change->add_property ("new", new_value_str.str());
613 ostringstream id_str;
614 id_str << change.note->id();
615 xml_change->add_property ("id", id_str.str());
620 MidiModel::NoteDiffCommand::NoteChange
621 MidiModel::NoteDiffCommand::unmarshal_change (XMLNode *xml_change)
626 if ((prop = xml_change->property("property")) != 0) {
627 change.property = (Property) string_2_enum (prop->value(), change.property);
629 fatal << "!!!" << endmsg;
633 if ((prop = xml_change->property ("id")) == 0) {
634 error << _("No NoteID found for note property change - ignored") << endmsg;
638 gint note_id = atoi (prop->value().c_str());
640 if ((prop = xml_change->property ("old")) != 0) {
641 istringstream old_str (prop->value());
642 if (change.property == StartTime || change.property == Length) {
643 old_str >> change.old_time;
645 int integer_value_so_that_istream_does_the_right_thing;
646 old_str >> integer_value_so_that_istream_does_the_right_thing;
647 change.old_value = integer_value_so_that_istream_does_the_right_thing;
650 fatal << "!!!" << endmsg;
654 if ((prop = xml_change->property ("new")) != 0) {
655 istringstream new_str (prop->value());
656 if (change.property == StartTime || change.property == Length) {
657 new_str >> change.new_time;
659 int integer_value_so_that_istream_does_the_right_thing;
660 new_str >> integer_value_so_that_istream_does_the_right_thing;
661 change.new_value = integer_value_so_that_istream_does_the_right_thing;
664 fatal << "!!!" << endmsg;
668 /* we must point at the instance of the note that is actually in the model.
669 so go look for it ... it may not be there (it could have been
670 deleted in a later operation, so store the note id so that we can
671 look it up again later).
674 change.note = _model->find_note (note_id);
675 change.note_id = note_id;
681 MidiModel::NoteDiffCommand::set_state (const XMLNode& diff_command, int /*version*/)
683 if (diff_command.name() != string (NOTE_DIFF_COMMAND_ELEMENT)) {
689 _added_notes.clear();
690 XMLNode* added_notes = diff_command.child(ADDED_NOTES_ELEMENT);
692 XMLNodeList notes = added_notes->children();
693 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
694 boost::bind (&NoteDiffCommand::unmarshal_note, this, _1));
700 _removed_notes.clear();
701 XMLNode* removed_notes = diff_command.child(REMOVED_NOTES_ELEMENT);
703 XMLNodeList notes = removed_notes->children();
704 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
705 boost::bind (&NoteDiffCommand::unmarshal_note, this, _1));
713 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
716 XMLNodeList notes = changed_notes->children();
717 transform (notes.begin(), notes.end(), back_inserter(_changes),
718 boost::bind (&NoteDiffCommand::unmarshal_change, this, _1));
722 /* side effect removals caused by changes */
724 side_effect_removals.clear();
726 XMLNode* side_effect_notes = diff_command.child(SIDE_EFFECT_REMOVALS_ELEMENT);
728 if (side_effect_notes) {
729 XMLNodeList notes = side_effect_notes->children();
730 for (XMLNodeList::iterator n = notes.begin(); n != notes.end(); ++n) {
731 side_effect_removals.insert (unmarshal_note (*n));
739 MidiModel::NoteDiffCommand::get_state ()
741 XMLNode* diff_command = new XMLNode (NOTE_DIFF_COMMAND_ELEMENT);
742 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
744 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
745 for_each(_changes.begin(), _changes.end(),
747 boost::bind (&XMLNode::add_child_nocopy, changes, _1),
748 boost::bind (&NoteDiffCommand::marshal_change, this, _1)));
750 XMLNode* added_notes = diff_command->add_child(ADDED_NOTES_ELEMENT);
751 for_each(_added_notes.begin(), _added_notes.end(),
753 boost::bind (&XMLNode::add_child_nocopy, added_notes, _1),
754 boost::bind (&NoteDiffCommand::marshal_note, this, _1)));
756 XMLNode* removed_notes = diff_command->add_child(REMOVED_NOTES_ELEMENT);
757 for_each(_removed_notes.begin(), _removed_notes.end(),
759 boost::bind (&XMLNode::add_child_nocopy, removed_notes, _1),
760 boost::bind (&NoteDiffCommand::marshal_note, this, _1)));
762 /* if this command had side-effects, store that state too
765 if (!side_effect_removals.empty()) {
766 XMLNode* side_effect_notes = diff_command->add_child(SIDE_EFFECT_REMOVALS_ELEMENT);
767 for_each(side_effect_removals.begin(), side_effect_removals.end(),
769 boost::bind (&XMLNode::add_child_nocopy, side_effect_notes, _1),
770 boost::bind (&NoteDiffCommand::marshal_note, this, _1)));
773 return *diff_command;
776 MidiModel::SysExDiffCommand::SysExDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode& node)
777 : DiffCommand (m, "")
780 set_state (node, Stateful::loading_state_version);
784 MidiModel::SysExDiffCommand::change (boost::shared_ptr<Evoral::Event<TimeType> > s, TimeType new_time)
789 change.property = Time;
790 change.old_time = s->time ();
791 change.new_time = new_time;
793 _changes.push_back (change);
797 MidiModel::SysExDiffCommand::operator() ()
800 MidiModel::WriteLock lock (_model->edit_lock ());
802 for (list<SysExPtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
803 _model->remove_sysex_unlocked (*i);
806 /* find any sysex events that were missing when unmarshalling */
808 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
810 i->sysex = _model->find_sysex (i->sysex_id);
815 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
816 switch (i->property) {
818 i->sysex->set_time (i->new_time);
823 _model->ContentsChanged (); /* EMIT SIGNAL */
827 MidiModel::SysExDiffCommand::undo ()
830 MidiModel::WriteLock lock (_model->edit_lock ());
832 for (list<SysExPtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
833 _model->add_sysex_unlocked (*i);
836 /* find any sysex events that were missing when unmarshalling */
838 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
840 i->sysex = _model->find_sysex (i->sysex_id);
845 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
846 switch (i->property) {
848 i->sysex->set_time (i->old_time);
855 _model->ContentsChanged(); /* EMIT SIGNAL */
859 MidiModel::SysExDiffCommand::remove (SysExPtr sysex)
861 _removed.push_back(sysex);
865 MidiModel::SysExDiffCommand::marshal_change (const Change& change)
867 XMLNode* xml_change = new XMLNode ("Change");
869 /* first, the change itself */
871 xml_change->add_property ("property", enum_2_string (change.property));
874 ostringstream old_value_str (ios::ate);
875 old_value_str << change.old_time;
876 xml_change->add_property ("old", old_value_str.str());
880 ostringstream new_value_str (ios::ate);
881 new_value_str << change.new_time;
882 xml_change->add_property ("new", new_value_str.str());
885 ostringstream id_str;
886 id_str << change.sysex->id();
887 xml_change->add_property ("id", id_str.str());
892 MidiModel::SysExDiffCommand::Change
893 MidiModel::SysExDiffCommand::unmarshal_change (XMLNode *xml_change)
898 if ((prop = xml_change->property ("property")) != 0) {
899 change.property = (Property) string_2_enum (prop->value(), change.property);
901 fatal << "!!!" << endmsg;
905 if ((prop = xml_change->property ("id")) == 0) {
906 error << _("No SysExID found for sys-ex property change - ignored") << endmsg;
910 gint sysex_id = atoi (prop->value().c_str());
912 if ((prop = xml_change->property ("old")) != 0) {
913 istringstream old_str (prop->value());
914 old_str >> change.old_time;
916 fatal << "!!!" << endmsg;
920 if ((prop = xml_change->property ("new")) != 0) {
921 istringstream new_str (prop->value());
922 new_str >> change.new_time;
924 fatal << "!!!" << endmsg;
928 /* we must point at the instance of the sysex that is actually in the model.
929 so go look for it ...
932 change.sysex = _model->find_sysex (sysex_id);
933 change.sysex_id = sysex_id;
939 MidiModel::SysExDiffCommand::set_state (const XMLNode& diff_command, int /*version*/)
941 if (diff_command.name() != string (SYSEX_DIFF_COMMAND_ELEMENT)) {
949 XMLNode* changed_sysexes = diff_command.child (DIFF_SYSEXES_ELEMENT);
951 if (changed_sysexes) {
952 XMLNodeList sysexes = changed_sysexes->children();
953 transform (sysexes.begin(), sysexes.end(), back_inserter (_changes),
954 boost::bind (&SysExDiffCommand::unmarshal_change, this, _1));
962 MidiModel::SysExDiffCommand::get_state ()
964 XMLNode* diff_command = new XMLNode (SYSEX_DIFF_COMMAND_ELEMENT);
965 diff_command->add_property ("midi-source", _model->midi_source()->id().to_s());
967 XMLNode* changes = diff_command->add_child(DIFF_SYSEXES_ELEMENT);
968 for_each (_changes.begin(), _changes.end(),
970 boost::bind (&XMLNode::add_child_nocopy, changes, _1),
971 boost::bind (&SysExDiffCommand::marshal_change, this, _1)));
973 return *diff_command;
976 MidiModel::PatchChangeDiffCommand::PatchChangeDiffCommand (boost::shared_ptr<MidiModel> m, const string& name)
977 : DiffCommand (m, name)
982 MidiModel::PatchChangeDiffCommand::PatchChangeDiffCommand (boost::shared_ptr<MidiModel> m, const XMLNode & node)
983 : DiffCommand (m, "")
986 set_state (node, Stateful::loading_state_version);
990 MidiModel::PatchChangeDiffCommand::add (PatchChangePtr p)
992 _added.push_back (p);
996 MidiModel::PatchChangeDiffCommand::remove (PatchChangePtr p)
998 _removed.push_back (p);
1002 MidiModel::PatchChangeDiffCommand::change_time (PatchChangePtr patch, TimeType t)
1007 c.old_time = patch->time ();
1010 _changes.push_back (c);
1014 MidiModel::PatchChangeDiffCommand::change_channel (PatchChangePtr patch, uint8_t channel)
1017 c.property = Channel;
1019 c.old_channel = patch->channel ();
1020 c.new_channel = channel;
1022 _changes.push_back (c);
1026 MidiModel::PatchChangeDiffCommand::change_program (PatchChangePtr patch, uint8_t program)
1029 c.property = Program;
1031 c.old_program = patch->program ();
1032 c.new_program = program;
1034 _changes.push_back (c);
1038 MidiModel::PatchChangeDiffCommand::change_bank (PatchChangePtr patch, int bank)
1043 c.old_bank = patch->bank ();
1046 _changes.push_back (c);
1050 MidiModel::PatchChangeDiffCommand::operator() ()
1053 MidiModel::WriteLock lock (_model->edit_lock ());
1055 for (list<PatchChangePtr>::iterator i = _added.begin(); i != _added.end(); ++i) {
1056 _model->add_patch_change_unlocked (*i);
1059 for (list<PatchChangePtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
1060 _model->remove_patch_change_unlocked (*i);
1063 /* find any patch change events that were missing when unmarshalling */
1065 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
1067 i->patch = _model->find_patch_change (i->patch_id);
1072 set<PatchChangePtr> temporary_removals;
1074 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
1075 switch (i->property) {
1077 if (temporary_removals.find (i->patch) == temporary_removals.end()) {
1078 _model->remove_patch_change_unlocked (i->patch);
1079 temporary_removals.insert (i->patch);
1081 i->patch->set_time (i->new_time);
1085 i->patch->set_channel (i->new_channel);
1089 i->patch->set_program (i->new_program);
1093 i->patch->set_bank (i->new_bank);
1098 for (set<PatchChangePtr>::iterator i = temporary_removals.begin(); i != temporary_removals.end(); ++i) {
1099 _model->add_patch_change_unlocked (*i);
1103 _model->ContentsChanged (); /* EMIT SIGNAL */
1107 MidiModel::PatchChangeDiffCommand::undo ()
1110 MidiModel::WriteLock lock (_model->edit_lock());
1112 for (list<PatchChangePtr>::iterator i = _added.begin(); i != _added.end(); ++i) {
1113 _model->remove_patch_change_unlocked (*i);
1116 for (list<PatchChangePtr>::iterator i = _removed.begin(); i != _removed.end(); ++i) {
1117 _model->add_patch_change_unlocked (*i);
1120 /* find any patch change events that were missing when unmarshalling */
1122 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
1124 i->patch = _model->find_patch_change (i->patch_id);
1129 set<PatchChangePtr> temporary_removals;
1131 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
1132 switch (i->property) {
1134 if (temporary_removals.find (i->patch) == temporary_removals.end()) {
1135 _model->remove_patch_change_unlocked (i->patch);
1136 temporary_removals.insert (i->patch);
1138 i->patch->set_time (i->old_time);
1142 i->patch->set_channel (i->old_channel);
1146 i->patch->set_program (i->old_program);
1150 i->patch->set_bank (i->old_bank);
1155 for (set<PatchChangePtr>::iterator i = temporary_removals.begin(); i != temporary_removals.end(); ++i) {
1156 _model->add_patch_change_unlocked (*i);
1161 _model->ContentsChanged (); /* EMIT SIGNAL */
1165 MidiModel::PatchChangeDiffCommand::marshal_patch_change (constPatchChangePtr p)
1167 XMLNode* n = new XMLNode ("patch-change");
1170 ostringstream s (ios::ate);
1171 s << int (p->id ());
1172 n->add_property ("id", s.str());
1176 ostringstream s (ios::ate);
1178 n->add_property ("time", s.str ());
1182 ostringstream s (ios::ate);
1183 s << int (p->channel ());
1184 n->add_property ("channel", s.str ());
1188 ostringstream s (ios::ate);
1189 s << int (p->program ());
1190 n->add_property ("program", s.str ());
1194 ostringstream s (ios::ate);
1195 s << int (p->bank ());
1196 n->add_property ("bank", s.str ());
1203 MidiModel::PatchChangeDiffCommand::marshal_change (const Change& c)
1205 XMLNode* n = new XMLNode (X_("Change"));
1207 n->add_property (X_("property"), enum_2_string (c.property));
1210 ostringstream s (ios::ate);
1211 if (c.property == Time) {
1213 } else if (c.property == Channel) {
1215 } else if (c.property == Program) {
1216 s << int (c.old_program);
1217 } else if (c.property == Bank) {
1221 n->add_property (X_("old"), s.str ());
1225 ostringstream s (ios::ate);
1227 if (c.property == Time) {
1229 } else if (c.property == Channel) {
1231 } else if (c.property == Program) {
1232 s << int (c.new_program);
1233 } else if (c.property == Bank) {
1237 n->add_property (X_("new"), s.str ());
1242 s << c.patch->id ();
1243 n->add_property ("id", s.str ());
1249 MidiModel::PatchChangePtr
1250 MidiModel::PatchChangeDiffCommand::unmarshal_patch_change (XMLNode* n)
1253 Evoral::event_id_t id;
1254 Evoral::MusicalTime time = 0;
1259 if ((prop = n->property ("id")) != 0) {
1260 istringstream s (prop->value());
1264 if ((prop = n->property ("time")) != 0) {
1265 istringstream s (prop->value ());
1269 if ((prop = n->property ("channel")) != 0) {
1270 istringstream s (prop->value ());
1274 if ((prop = n->property ("program")) != 0) {
1275 istringstream s (prop->value ());
1279 if ((prop = n->property ("bank")) != 0) {
1280 istringstream s (prop->value ());
1284 PatchChangePtr p (new Evoral::PatchChange<TimeType> (time, channel, program, bank));
1289 MidiModel::PatchChangeDiffCommand::Change
1290 MidiModel::PatchChangeDiffCommand::unmarshal_change (XMLNode* n)
1296 prop = n->property ("property");
1298 c.property = (Property) string_2_enum (prop->value(), c.property);
1300 prop = n->property ("id");
1302 Evoral::event_id_t const id = atoi (prop->value().c_str());
1304 /* we need to load via an int intermediate for all properties that are
1305 actually uint8_t (char/byte).
1308 prop = n->property ("old");
1311 istringstream s (prop->value ());
1312 if (c.property == Time) {
1314 } else if (c.property == Channel) {
1316 c.old_channel = an_int;
1317 } else if (c.property == Program) {
1319 c.old_program = an_int;
1320 } else if (c.property == Bank) {
1322 c.old_bank = an_int;
1326 prop = n->property ("new");
1329 istringstream s (prop->value ());
1331 if (c.property == Time) {
1333 } else if (c.property == Channel) {
1335 c.new_channel = an_int;
1336 } else if (c.property == Program) {
1338 c.new_program = an_int;
1339 } else if (c.property == Bank) {
1341 c.new_bank = an_int;
1345 c.patch = _model->find_patch_change (id);
1352 MidiModel::PatchChangeDiffCommand::set_state (const XMLNode& diff_command, int /*version*/)
1354 if (diff_command.name() != PATCH_CHANGE_DIFF_COMMAND_ELEMENT) {
1359 XMLNode* added = diff_command.child (ADDED_PATCH_CHANGES_ELEMENT);
1361 XMLNodeList p = added->children ();
1362 transform (p.begin(), p.end(), back_inserter (_added), boost::bind (&PatchChangeDiffCommand::unmarshal_patch_change, this, _1));
1366 XMLNode* removed = diff_command.child (REMOVED_PATCH_CHANGES_ELEMENT);
1368 XMLNodeList p = removed->children ();
1369 transform (p.begin(), p.end(), back_inserter (_removed), boost::bind (&PatchChangeDiffCommand::unmarshal_patch_change, this, _1));
1373 XMLNode* changed = diff_command.child (DIFF_PATCH_CHANGES_ELEMENT);
1375 XMLNodeList p = changed->children ();
1376 transform (p.begin(), p.end(), back_inserter (_changes), boost::bind (&PatchChangeDiffCommand::unmarshal_change, this, _1));
1383 MidiModel::PatchChangeDiffCommand::get_state ()
1385 XMLNode* diff_command = new XMLNode (PATCH_CHANGE_DIFF_COMMAND_ELEMENT);
1386 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
1388 XMLNode* added = diff_command->add_child (ADDED_PATCH_CHANGES_ELEMENT);
1389 for_each (_added.begin(), _added.end(),
1391 boost::bind (&XMLNode::add_child_nocopy, added, _1),
1392 boost::bind (&PatchChangeDiffCommand::marshal_patch_change, this, _1)
1396 XMLNode* removed = diff_command->add_child (REMOVED_PATCH_CHANGES_ELEMENT);
1397 for_each (_removed.begin(), _removed.end(),
1399 boost::bind (&XMLNode::add_child_nocopy, removed, _1),
1400 boost::bind (&PatchChangeDiffCommand::marshal_patch_change, this, _1)
1404 XMLNode* changes = diff_command->add_child (DIFF_PATCH_CHANGES_ELEMENT);
1405 for_each (_changes.begin(), _changes.end(),
1407 boost::bind (&XMLNode::add_child_nocopy, changes, _1),
1408 boost::bind (&PatchChangeDiffCommand::marshal_change, this, _1)
1412 return *diff_command;
1415 /** Write all of the model to a MidiSource (i.e. save the model).
1416 * This is different from manually using read to write to a source in that
1417 * note off events are written regardless of the track mode. This is so the
1418 * user can switch a recorded track (with note durations from some instrument)
1419 * to percussive, save, reload, then switch it back to sustained without
1420 * destroying the original note durations.
1422 * Similarly, control events are written without interpolation (as with the
1426 MidiModel::write_to (boost::shared_ptr<MidiSource> source)
1428 ReadLock lock(read_lock());
1430 const bool old_percussive = percussive();
1431 set_percussive(false);
1433 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1436 source->drop_model();
1437 source->mark_streaming_midi_write_started (note_mode());
1439 for (Evoral::Sequence<TimeType>::const_iterator i = begin(0, true); i != end(); ++i) {
1440 source->append_event_unlocked_beats(*i);
1443 set_percussive(old_percussive);
1444 source->mark_streaming_write_completed();
1451 /** very similar to ::write_to() but writes to the model's own
1452 existing midi_source, without making it call MidiSource::drop_model().
1453 the caller is a MidiSource that needs to catch up with the state
1457 MidiModel::sync_to_source ()
1459 ReadLock lock(read_lock());
1461 const bool old_percussive = percussive();
1462 set_percussive(false);
1464 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1467 ms->mark_streaming_midi_write_started (note_mode());
1469 for (Evoral::Sequence<TimeType>::const_iterator i = begin(0, true); i != end(); ++i) {
1470 ms->append_event_unlocked_beats(*i);
1473 set_percussive (old_percussive);
1474 ms->mark_streaming_write_completed ();
1481 /** Write part or all of the model to a MidiSource (i.e. save the model).
1482 * This is different from manually using read to write to a source in that
1483 * note off events are written regardless of the track mode. This is so the
1484 * user can switch a recorded track (with note durations from some instrument)
1485 * to percussive, save, reload, then switch it back to sustained without
1486 * destroying the original note durations.
1489 MidiModel::write_section_to (boost::shared_ptr<MidiSource> source, Evoral::MusicalTime begin_time, Evoral::MusicalTime end_time)
1491 ReadLock lock(read_lock());
1492 MidiStateTracker mst;
1494 const bool old_percussive = percussive();
1495 set_percussive(false);
1497 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1500 source->drop_model();
1501 source->mark_streaming_midi_write_started (note_mode());
1503 for (Evoral::Sequence<TimeType>::const_iterator i = begin(0, true); i != end(); ++i) {
1504 const Evoral::Event<Evoral::MusicalTime>& ev (*i);
1506 if (ev.time() >= begin_time && ev.time() < end_time) {
1508 const Evoral::MIDIEvent<Evoral::MusicalTime>* mev =
1509 static_cast<const Evoral::MIDIEvent<Evoral::MusicalTime>* > (&ev);
1516 if (mev->is_note_off()) {
1518 if (!mst.active (mev->note(), mev->channel())) {
1519 /* the matching note-on was outside the
1520 time range we were given, so just
1521 ignore this note-off.
1526 source->append_event_unlocked_beats (*i);
1527 mst.remove (mev->note(), mev->channel());
1529 } else if (mev->is_note_on()) {
1530 mst.add (mev->note(), mev->channel());
1531 source->append_event_unlocked_beats(*i);
1533 source->append_event_unlocked_beats(*i);
1538 mst.resolve_notes (*source, end_time);
1540 set_percussive(old_percussive);
1541 source->mark_streaming_write_completed();
1549 MidiModel::get_state()
1551 XMLNode *node = new XMLNode("MidiModel");
1555 Evoral::Sequence<MidiModel::TimeType>::NotePtr
1556 MidiModel::find_note (NotePtr other)
1558 Notes::iterator l = notes().lower_bound(other);
1560 if (l != notes().end()) {
1561 for (; (*l)->time() == other->time(); ++l) {
1562 /* NB: compare note contents, not note pointers.
1563 If "other" was a ptr to a note already in
1564 the model, we wouldn't be looking for it,
1567 if (**l == *other) {
1576 Evoral::Sequence<MidiModel::TimeType>::NotePtr
1577 MidiModel::find_note (gint note_id)
1579 /* used only for looking up notes when reloading history from disk,
1580 so we don't care about performance *too* much.
1583 for (Notes::iterator l = notes().begin(); l != notes().end(); ++l) {
1584 if ((*l)->id() == note_id) {
1592 MidiModel::PatchChangePtr
1593 MidiModel::find_patch_change (Evoral::event_id_t id)
1595 for (PatchChanges::iterator i = patch_changes().begin(); i != patch_changes().end(); ++i) {
1596 if ((*i)->id() == id) {
1601 return PatchChangePtr ();
1604 boost::shared_ptr<Evoral::Event<MidiModel::TimeType> >
1605 MidiModel::find_sysex (gint sysex_id)
1607 /* used only for looking up notes when reloading history from disk,
1608 so we don't care about performance *too* much.
1611 for (SysExes::iterator l = sysexes().begin(); l != sysexes().end(); ++l) {
1612 if ((*l)->id() == sysex_id) {
1617 return boost::shared_ptr<Evoral::Event<TimeType> > ();
1620 /** Lock and invalidate the source.
1621 * This should be used by commands and editing things
1623 MidiModel::WriteLock
1624 MidiModel::edit_lock()
1626 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1629 Glib::Threads::Mutex::Lock* source_lock = new Glib::Threads::Mutex::Lock (ms->mutex());
1630 ms->invalidate(); // Release cached iterator's read lock on model
1631 return WriteLock(new WriteLockImpl(source_lock, _lock, _control_lock));
1634 /** Lock just the model, the source lock must already be held.
1635 * This should only be called from libardour/evoral places
1637 MidiModel::WriteLock
1638 MidiModel::write_lock()
1640 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1643 assert (!ms->mutex().trylock ());
1644 return WriteLock(new WriteLockImpl(0, _lock, _control_lock));
1648 MidiModel::resolve_overlaps_unlocked (const NotePtr note, void* arg)
1650 using namespace Evoral;
1652 if (_writing || insert_merge_policy() == InsertMergeRelax) {
1656 NoteDiffCommand* cmd = static_cast<NoteDiffCommand*>(arg);
1658 TimeType sa = note->time();
1659 TimeType ea = note->end_time();
1661 const Pitches& p (pitches (note->channel()));
1662 NotePtr search_note(new Note<TimeType>(0, 0, 0, note->note()));
1663 set<NotePtr> to_be_deleted;
1664 bool set_note_length = false;
1665 bool set_note_time = false;
1666 TimeType note_time = note->time();
1667 TimeType note_length = note->length();
1669 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 checking overlaps for note %2 @ %3\n", this, (int)note->note(), note->time()));
1671 for (Pitches::const_iterator i = p.lower_bound (search_note);
1672 i != p.end() && (*i)->note() == note->note(); ++i) {
1674 TimeType sb = (*i)->time();
1675 TimeType eb = (*i)->end_time();
1676 OverlapType overlap = OverlapNone;
1678 if ((sb > sa) && (eb <= ea)) {
1679 overlap = OverlapInternal;
1680 } else if ((eb >= sa) && (eb <= ea)) {
1681 overlap = OverlapStart;
1682 } else if ((sb > sa) && (sb <= ea)) {
1683 overlap = OverlapEnd;
1684 } else if ((sa >= sb) && (sa <= eb) && (ea <= eb)) {
1685 overlap = OverlapExternal;
1691 DEBUG_TRACE (DEBUG::Sequence, string_compose (
1692 "\toverlap is %1 for (%2,%3) vs (%4,%5)\n",
1693 enum_2_string(overlap), sa, ea, sb, eb));
1695 if (insert_merge_policy() == InsertMergeReject) {
1696 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 just reject\n", this));
1702 cerr << "OverlapStart\n";
1703 /* existing note covers start of new note */
1704 switch (insert_merge_policy()) {
1705 case InsertMergeReplace:
1706 to_be_deleted.insert (*i);
1708 case InsertMergeTruncateExisting:
1710 cmd->change (*i, NoteDiffCommand::Length, (note->time() - (*i)->time()));
1712 (*i)->set_length (note->time() - (*i)->time());
1714 case InsertMergeTruncateAddition:
1715 set_note_time = true;
1716 set_note_length = true;
1717 note_time = (*i)->time() + (*i)->length();
1718 note_length = min (note_length, (*i)->length() - ((*i)->end_time() - note->time()));
1720 case InsertMergeExtend:
1722 cmd->change ((*i), NoteDiffCommand::Length, note->end_time() - (*i)->time());
1724 (*i)->set_length (note->end_time() - (*i)->time());
1725 return -1; /* do not add the new note */
1735 cerr << "OverlapEnd\n";
1736 /* existing note covers end of new note */
1737 switch (insert_merge_policy()) {
1738 case InsertMergeReplace:
1739 to_be_deleted.insert (*i);
1742 case InsertMergeTruncateExisting:
1743 /* resetting the start time of the existing note
1744 is a problem because of time ordering.
1748 case InsertMergeTruncateAddition:
1749 set_note_length = true;
1750 note_length = min (note_length, ((*i)->time() - note->time()));
1753 case InsertMergeExtend:
1754 /* we can't reset the time of the existing note because
1755 that will corrupt time ordering. So remove the
1756 existing note and change the position/length
1757 of the new note (which has not been added yet)
1759 to_be_deleted.insert (*i);
1760 set_note_length = true;
1761 note_length = min (note_length, (*i)->end_time() - note->time());
1770 case OverlapExternal:
1771 cerr << "OverlapExt\n";
1772 /* existing note overlaps all the new note */
1773 switch (insert_merge_policy()) {
1774 case InsertMergeReplace:
1775 to_be_deleted.insert (*i);
1777 case InsertMergeTruncateExisting:
1778 case InsertMergeTruncateAddition:
1779 case InsertMergeExtend:
1780 /* cannot add in this case */
1789 case OverlapInternal:
1790 cerr << "OverlapInt\n";
1791 /* new note fully overlaps an existing note */
1792 switch (insert_merge_policy()) {
1793 case InsertMergeReplace:
1794 case InsertMergeTruncateExisting:
1795 case InsertMergeTruncateAddition:
1796 case InsertMergeExtend:
1797 /* delete the existing note, the new one will cover it */
1798 to_be_deleted.insert (*i);
1814 for (set<NotePtr>::iterator i = to_be_deleted.begin(); i != to_be_deleted.end(); ++i) {
1815 remove_note_unlocked (*i);
1818 cmd->side_effect_remove (*i);
1822 if (set_note_time) {
1824 cmd->change (note, NoteDiffCommand::StartTime, note_time);
1826 note->set_time (note_time);
1829 if (set_note_length) {
1831 cmd->change (note, NoteDiffCommand::Length, note_length);
1833 note->set_length (note_length);
1840 MidiModel::insert_merge_policy () const
1842 /* XXX ultimately this should be a per-track or even per-model policy */
1843 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1846 return ms->session().config.get_insert_merge_policy ();
1850 MidiModel::set_midi_source (boost::shared_ptr<MidiSource> s)
1852 boost::shared_ptr<MidiSource> old = _midi_source.lock ();
1858 _midi_source_connections.drop_connections ();
1862 s->InterpolationChanged.connect_same_thread (
1863 _midi_source_connections, boost::bind (&MidiModel::source_interpolation_changed, this, _1, _2)
1866 s->AutomationStateChanged.connect_same_thread (
1867 _midi_source_connections, boost::bind (&MidiModel::source_automation_state_changed, this, _1, _2)
1871 /** The source has signalled that the interpolation style for a parameter has changed. In order to
1872 * keep MidiSource and ControlList interpolation state the same, we pass this change onto the
1873 * appropriate ControlList.
1875 * The idea is that MidiSource and the MidiModel's ControlList states are kept in sync, and one
1876 * or the other is listened to by the GUI.
1879 MidiModel::source_interpolation_changed (Evoral::Parameter p, Evoral::ControlList::InterpolationStyle s)
1881 Glib::Threads::Mutex::Lock lm (_control_lock);
1882 control(p)->list()->set_interpolation (s);
1885 /** A ControlList has signalled that its interpolation style has changed. Again, in order to keep
1886 * MidiSource and ControlList interpolation state in sync, we pass this change onto our MidiSource.
1889 MidiModel::control_list_interpolation_changed (Evoral::Parameter p, Evoral::ControlList::InterpolationStyle s)
1891 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1894 ms->set_interpolation_of (p, s);
1898 MidiModel::source_automation_state_changed (Evoral::Parameter p, AutoState s)
1900 Glib::Threads::Mutex::Lock lm (_control_lock);
1901 boost::shared_ptr<AutomationList> al = boost::dynamic_pointer_cast<AutomationList> (control(p)->list ());
1902 al->set_automation_state (s);
1906 MidiModel::automation_list_automation_state_changed (Evoral::Parameter p, AutoState s)
1908 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1910 ms->set_automation_state_of (p, s);
1913 boost::shared_ptr<Evoral::Control>
1914 MidiModel::control_factory (Evoral::Parameter const & p)
1916 boost::shared_ptr<Evoral::Control> c = Automatable::control_factory (p);
1918 /* Set up newly created control's lists to the appropriate interpolation and
1919 automation state from our source.
1922 boost::shared_ptr<MidiSource> ms = _midi_source.lock ();
1925 c->list()->set_interpolation (ms->interpolation_of (p));
1927 boost::shared_ptr<AutomationList> al = boost::dynamic_pointer_cast<AutomationList> (c->list ());
1930 al->set_automation_state (ms->automation_state_of (p));
1935 boost::shared_ptr<const MidiSource>
1936 MidiModel::midi_source ()
1938 return _midi_source.lock ();
1941 /** Moves notes, patch changes, controllers and sys-ex to insert silence at the start of the model.
1942 * Adds commands to the session's current undo stack to reflect the movements.
1945 MidiModel::insert_silence_at_start (TimeType t)
1947 boost::shared_ptr<MidiSource> s = _midi_source.lock ();
1952 if (!notes().empty ()) {
1953 NoteDiffCommand* c = new_note_diff_command ("insert silence");
1955 for (Notes::const_iterator i = notes().begin(); i != notes().end(); ++i) {
1956 c->change (*i, NoteDiffCommand::StartTime, (*i)->time() + t);
1959 apply_command_as_subcommand (s->session(), c);
1964 if (!patch_changes().empty ()) {
1965 PatchChangeDiffCommand* c = new_patch_change_diff_command ("insert silence");
1967 for (PatchChanges::const_iterator i = patch_changes().begin(); i != patch_changes().end(); ++i) {
1968 c->change_time (*i, (*i)->time() + t);
1971 apply_command_as_subcommand (s->session(), c);
1976 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
1977 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
1978 XMLNode& before = ac->alist()->get_state ();
1979 i->second->list()->shift (0, t);
1980 XMLNode& after = ac->alist()->get_state ();
1981 s->session().add_command (new MementoCommand<AutomationList> (new MidiAutomationListBinder (s, i->first), &before, &after));
1986 if (!sysexes().empty()) {
1987 SysExDiffCommand* c = new_sysex_diff_command ("insert silence");
1989 for (SysExes::iterator i = sysexes().begin(); i != sysexes().end(); ++i) {
1990 c->change (*i, (*i)->time() + t);
1993 apply_command_as_subcommand (s->session(), c);
1997 /** Transpose notes in a time range by a given number of semitones. Notes
1998 * will be clamped at 0 and 127 if the transposition would make them exceed
2001 * @param from Start time.
2002 * @param end End time.
2003 * @param semitones Number of semitones to transpose by (+ve is higher, -ve is lower).
2006 MidiModel::transpose (TimeType from, TimeType to, int semitones)
2008 boost::shared_ptr<const MidiSource> s = midi_source ();
2010 NoteDiffCommand* c = new_note_diff_command (_("transpose"));
2012 for (Notes::iterator i = notes().begin(); i != notes().end(); ++i) {
2014 if ((*i)->time() >= to) {
2019 } else if ((*i)->time() >= from) {
2021 int new_note = (*i)->note() + semitones;
2025 } else if (new_note > 127) {
2029 c->change (*i, NoteDiffCommand::NoteNumber, (uint8_t) new_note);
2034 apply_command (s->session (), c);
2038 MidiModel::control_list_marked_dirty ()
2040 AutomatableSequence<Evoral::MusicalTime>::control_list_marked_dirty ();
2042 ContentsChanged (); /* EMIT SIGNAL */