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/smf_source.h"
34 #include "ardour/types.h"
35 #include "ardour/session.h"
38 using namespace ARDOUR;
41 MidiModel::MidiModel(MidiSource* s, size_t size)
42 : AutomatableSequence<TimeType>(s->session(), size)
47 /** Start a new Delta command.
49 * This has no side-effects on the model or Session, the returned command
50 * can be held on to for as long as the caller wishes, or discarded without
51 * formality, until apply_command is called and ownership is taken.
53 MidiModel::DeltaCommand*
54 MidiModel::new_delta_command(const string name)
56 DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
60 /** Start a new Diff command.
62 * This has no side-effects on the model or Session, the returned command
63 * can be held on to for as long as the caller wishes, or discarded without
64 * formality, until apply_command is called and ownership is taken.
66 MidiModel::DiffCommand*
67 MidiModel::new_diff_command(const string name)
69 DiffCommand* cmd = new DiffCommand(_midi_source->model(), name);
75 * Ownership of cmd is taken, it must not be deleted by the caller.
76 * The command will constitute one item on the undo stack.
79 MidiModel::apply_command(Session& session, Command* cmd)
81 session.begin_reversible_command(cmd->name());
83 session.commit_reversible_command(cmd);
87 /** Apply a command as part of a larger reversible transaction
89 * Ownership of cmd is taken, it must not be deleted by the caller.
90 * The command will constitute one item on the undo stack.
93 MidiModel::apply_command_as_subcommand(Session& session, Command* cmd)
96 session.add_command(cmd);
103 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
111 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
115 set_state(node, Stateful::loading_state_version);
119 MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
121 _removed_notes.remove(note);
122 _added_notes.push_back(note);
126 MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
128 _added_notes.remove(note);
129 _removed_notes.push_back(note);
133 MidiModel::DeltaCommand::operator()()
135 // This could be made much faster by using a priority_queue for added and
136 // removed notes (or sort here), and doing a single iteration over _model
138 MidiModel::WriteLock lock(_model->edit_lock());
140 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
141 _model->add_note_unlocked(*i);
144 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
145 _model->remove_note_unlocked(*i);
149 _model->ContentsChanged(); /* EMIT SIGNAL */
153 MidiModel::DeltaCommand::undo()
155 // This could be made much faster by using a priority_queue for added and
156 // removed notes (or sort here), and doing a single iteration over _model
158 MidiModel::WriteLock lock(_model->edit_lock());;
160 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
161 _model->remove_note_unlocked(*i);
164 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
165 _model->add_note_unlocked(*i);
169 _model->ContentsChanged(); /* EMIT SIGNAL */
173 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
175 XMLNode* xml_note = new XMLNode("note");
176 ostringstream note_str(ios::ate);
177 note_str << int(note->note());
178 xml_note->add_property("note", note_str.str());
180 ostringstream channel_str(ios::ate);
181 channel_str << int(note->channel());
182 xml_note->add_property("channel", channel_str.str());
184 ostringstream time_str(ios::ate);
185 time_str << int(note->time());
186 xml_note->add_property("time", time_str.str());
188 ostringstream length_str(ios::ate);
189 length_str <<(unsigned int) note->length();
190 xml_note->add_property("length", length_str.str());
192 ostringstream velocity_str(ios::ate);
193 velocity_str << (unsigned int) note->velocity();
194 xml_note->add_property("velocity", velocity_str.str());
199 boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
200 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
204 unsigned int channel;
207 unsigned int velocity;
209 if ((prop = xml_note->property("note")) != 0) {
210 istringstream note_str(prop->value());
213 warning << "note information missing note value" << endmsg;
217 if ((prop = xml_note->property("channel")) != 0) {
218 istringstream channel_str(prop->value());
219 channel_str >> channel;
221 warning << "note information missing channel" << endmsg;
225 if ((prop = xml_note->property("time")) != 0) {
226 istringstream time_str(prop->value());
229 warning << "note information missing time" << endmsg;
233 if ((prop = xml_note->property("length")) != 0) {
234 istringstream length_str(prop->value());
235 length_str >> length;
237 warning << "note information missing length" << endmsg;
241 if ((prop = xml_note->property("velocity")) != 0) {
242 istringstream velocity_str(prop->value());
243 velocity_str >> velocity;
245 warning << "note information missing velocity" << endmsg;
249 boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
250 channel, time, length, note, velocity));
254 #define ADDED_NOTES_ELEMENT "AddedNotes"
255 #define REMOVED_NOTES_ELEMENT "RemovedNotes"
256 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
259 MidiModel::DeltaCommand::set_state (const XMLNode& delta_command, int /*version*/)
261 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
265 _added_notes.clear();
266 XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
268 XMLNodeList notes = added_notes->children();
269 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
270 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
273 _removed_notes.clear();
274 XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
276 XMLNodeList notes = removed_notes->children();
277 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
278 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
285 MidiModel::DeltaCommand::get_state()
287 XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
288 delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
290 XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
291 for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
292 sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
293 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
295 XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
296 for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
297 sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
298 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
300 return *delta_command;
303 /************** DIFF COMMAND ********************/
305 #define DIFF_NOTES_ELEMENT "ChangedNotes"
306 #define DIFF_COMMAND_ELEMENT "DiffCommand"
308 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
316 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
320 set_state(node, Stateful::loading_state_version);
324 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
327 NotePropertyChange change;
330 change.property = prop;
331 change.new_value = new_value;
335 change.old_value = note->note();
338 change.old_value = note->velocity();
341 fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
345 fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
349 change.old_value = note->channel();
353 _changes.push_back (change);
357 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
360 NotePropertyChange change;
363 change.property = prop;
364 change.new_time = new_time;
370 fatal << "MidiModel::DiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
373 change.old_time = note->time();
376 change.old_time = note->length();
380 _changes.push_back (change);
384 MidiModel::DiffCommand::operator()()
386 MidiModel::WriteLock lock(_model->edit_lock());
388 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
389 Property prop = i->property;
392 i->note->set_note (i->new_value);
395 i->note->set_velocity (i->new_value);
398 i->note->set_time (i->new_time);
401 i->note->set_length (i->new_time);
404 i->note->set_channel (i->new_value);
410 _model->ContentsChanged(); /* EMIT SIGNAL */
414 MidiModel::DiffCommand::undo()
416 MidiModel::WriteLock lock(_model->edit_lock());
418 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
419 Property prop = i->property;
422 i->note->set_note (i->old_value);
425 i->note->set_velocity (i->old_value);
428 i->note->set_time (i->old_time);
431 i->note->set_length (i->old_time);
434 i->note->set_channel (i->old_value);
440 _model->ContentsChanged(); /* EMIT SIGNAL */
444 MidiModel::DiffCommand::marshal_change(const NotePropertyChange& change)
446 XMLNode* xml_change = new XMLNode("change");
448 /* first, the change itself */
450 xml_change->add_property ("property", enum_2_string (change.property));
453 ostringstream old_value_str (ios::ate);
454 if (change.property == StartTime || change.property == Length) {
455 old_value_str << change.old_time;
457 old_value_str << (unsigned int) change.old_value;
459 xml_change->add_property ("old", old_value_str.str());
463 ostringstream new_value_str (ios::ate);
464 if (change.property == StartTime || change.property == Length) {
465 new_value_str << change.new_time;
467 new_value_str << (unsigned int) change.new_value;
469 xml_change->add_property ("new", new_value_str.str());
472 /* now the rest of the note */
474 const SMFSource* smf = dynamic_cast<const SMFSource*> (_model->midi_source());
476 if (change.property != NoteNumber) {
477 ostringstream note_str;
478 note_str << int(change.note->note());
479 xml_change->add_property("note", note_str.str());
482 if (change.property != Channel) {
483 ostringstream channel_str;
484 channel_str << int(change.note->channel());
485 xml_change->add_property("channel", channel_str.str());
488 if (change.property != StartTime) {
489 ostringstream time_str;
491 time_str << smf->round_to_file_precision (change.note->time());
493 time_str << change.note->time();
495 xml_change->add_property("time", time_str.str());
498 if (change.property != Length) {
499 ostringstream length_str;
501 length_str << smf->round_to_file_precision (change.note->length());
503 length_str << change.note->length();
505 xml_change->add_property ("length", length_str.str());
508 if (change.property != Velocity) {
509 ostringstream velocity_str;
510 velocity_str << int (change.note->velocity());
511 xml_change->add_property("velocity", velocity_str.str());
517 MidiModel::DiffCommand::NotePropertyChange
518 MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
521 NotePropertyChange change;
523 unsigned int channel;
524 unsigned int velocity;
525 Evoral::MusicalTime time;
526 Evoral::MusicalTime length;
528 if ((prop = xml_change->property("property")) != 0) {
529 change.property = (Property) string_2_enum (prop->value(), change.property);
531 fatal << "!!!" << endmsg;
535 if ((prop = xml_change->property ("old")) != 0) {
536 istringstream old_str (prop->value());
537 if (change.property == StartTime || change.property == Length) {
538 old_str >> change.old_time;
540 int integer_value_so_that_istream_does_the_right_thing;
541 old_str >> integer_value_so_that_istream_does_the_right_thing;
542 change.old_value = integer_value_so_that_istream_does_the_right_thing;
545 fatal << "!!!" << endmsg;
549 if ((prop = xml_change->property ("new")) != 0) {
550 istringstream new_str (prop->value());
551 if (change.property == StartTime || change.property == Length) {
552 new_str >> change.new_time;
554 int integer_value_so_that_istream_does_the_right_thing;
555 new_str >> integer_value_so_that_istream_does_the_right_thing;
556 change.new_value = integer_value_so_that_istream_does_the_right_thing;
559 fatal << "!!!" << endmsg;
563 if (change.property != NoteNumber) {
564 if ((prop = xml_change->property("note")) != 0) {
565 istringstream note_str(prop->value());
568 warning << "note information missing note value" << endmsg;
572 note = change.new_value;
575 if (change.property != Channel) {
576 if ((prop = xml_change->property("channel")) != 0) {
577 istringstream channel_str(prop->value());
578 channel_str >> channel;
580 warning << "note information missing channel" << endmsg;
584 channel = change.new_value;
587 if (change.property != StartTime) {
588 if ((prop = xml_change->property("time")) != 0) {
589 istringstream time_str(prop->value());
592 warning << "note information missing time" << endmsg;
596 time = change.new_time;
599 if (change.property != Length) {
600 if ((prop = xml_change->property("length")) != 0) {
601 istringstream length_str(prop->value());
602 length_str >> length;
604 warning << "note information missing length" << endmsg;
608 length = change.new_time;
611 if (change.property != Velocity) {
612 if ((prop = xml_change->property("velocity")) != 0) {
613 istringstream velocity_str(prop->value());
614 velocity_str >> velocity;
616 warning << "note information missing velocity" << endmsg;
620 velocity = change.new_value;
623 /* we must point at the instance of the note that is actually in the model.
624 so go look for it ...
627 boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
629 change.note = _model->find_note (new_note);
632 warning << "MIDI note " << *new_note << " not found in model - programmers should investigate this" << endmsg;
633 /* use the actual new note */
634 change.note = new_note;
641 MidiModel::DiffCommand::set_state(const XMLNode& diff_command, int /*version*/)
643 if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
649 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
652 XMLNodeList notes = changed_notes->children();
654 transform (notes.begin(), notes.end(), back_inserter(_changes),
655 sigc::mem_fun(*this, &DiffCommand::unmarshal_change));
662 MidiModel::DiffCommand::get_state ()
664 XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
665 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
667 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
668 for_each(_changes.begin(), _changes.end(), sigc::compose(
669 sigc::mem_fun(*changes, &XMLNode::add_child_nocopy),
670 sigc::mem_fun(*this, &DiffCommand::marshal_change)));
672 return *diff_command;
675 /** Write the model to a MidiSource (i.e. save the model).
676 * This is different from manually using read to write to a source in that
677 * note off events are written regardless of the track mode. This is so the
678 * user can switch a recorded track (with note durations from some instrument)
679 * to percussive, save, reload, then switch it back to sustained without
680 * destroying the original note durations.
683 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
685 ReadLock lock(read_lock());
687 const bool old_percussive = percussive();
688 set_percussive(false);
690 source->drop_model();
691 source->mark_streaming_midi_write_started(note_mode(), _midi_source->timeline_position());
693 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
694 source->append_event_unlocked_beats(*i);
697 set_percussive(old_percussive);
698 source->mark_streaming_write_completed();
706 MidiModel::get_state()
708 XMLNode *node = new XMLNode("MidiModel");
712 boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
713 MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
715 Notes::iterator l = notes().lower_bound(other);
717 if (l != notes().end()) {
718 for (; (*l)->time() == other->time(); ++l) {
725 return boost::shared_ptr<Evoral::Note<TimeType> >();
728 /** Lock and invalidate the source.
729 * This should be used by commands and editing things
732 MidiModel::edit_lock()
734 Glib::Mutex::Lock* source_lock = new Glib::Mutex::Lock(_midi_source->mutex());
735 _midi_source->invalidate(); // Release cached iterator's read lock on model
736 return WriteLock(new WriteLockImpl(source_lock, _lock, _control_lock));
739 /** Lock just the model, the source lock must already be held.
740 * This should only be called from libardour/evoral places
743 MidiModel::write_lock()
745 assert(!_midi_source->mutex().trylock());
746 return WriteLock(new WriteLockImpl(NULL, _lock, _control_lock));