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,
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,
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 MidiModel::WriteLock lock(_model->edit_lock());
391 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
392 Property prop = i->property;
395 i->note->set_note (i->new_value);
398 i->note->set_velocity (i->new_value);
401 i->note->set_time (i->new_time);
404 i->note->set_length (i->new_time);
407 i->note->set_channel (i->new_value);
413 _model->ContentsChanged(); /* EMIT SIGNAL */
417 MidiModel::DiffCommand::undo()
419 MidiModel::WriteLock lock(_model->edit_lock());
421 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
422 Property prop = i->property;
425 i->note->set_note (i->old_value);
428 i->note->set_velocity (i->old_value);
431 i->note->set_time (i->old_time);
434 i->note->set_length (i->old_time);
437 i->note->set_channel (i->old_value);
443 _model->ContentsChanged(); /* EMIT SIGNAL */
447 MidiModel::DiffCommand::marshal_change(const NoteChange& change)
449 XMLNode* xml_change = new XMLNode("change");
451 /* first, the change itself */
453 xml_change->add_property ("property", enum_2_string (change.property));
456 ostringstream old_value_str (ios::ate);
457 if (change.property == StartTime || change.property == Length) {
458 old_value_str << change.old_time;
460 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 if (change.property == StartTime || change.property == Length) {
468 new_value_str << change.new_time;
470 new_value_str << (unsigned int) change.new_value;
472 xml_change->add_property ("new", new_value_str.str());
475 /* now the rest of the note */
477 const SMFSource* smf = dynamic_cast<const SMFSource*> (_model->midi_source());
479 if (change.property != NoteNumber) {
480 ostringstream note_str;
481 note_str << int(change.note->note());
482 xml_change->add_property("note", note_str.str());
485 if (change.property != Channel) {
486 ostringstream channel_str;
487 channel_str << int(change.note->channel());
488 xml_change->add_property("channel", channel_str.str());
491 if (change.property != StartTime) {
492 ostringstream time_str;
494 time_str << smf->round_to_file_precision (change.note->time());
496 time_str << change.note->time();
498 xml_change->add_property("time", time_str.str());
501 if (change.property != Length) {
502 ostringstream length_str;
504 length_str << smf->round_to_file_precision (change.note->length());
506 length_str << change.note->length();
508 xml_change->add_property ("length", length_str.str());
511 if (change.property != Velocity) {
512 ostringstream velocity_str;
513 velocity_str << int (change.note->velocity());
514 xml_change->add_property("velocity", velocity_str.str());
520 MidiModel::DiffCommand::NoteChange
521 MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
526 unsigned int channel;
527 unsigned int velocity;
528 Evoral::MusicalTime time;
529 Evoral::MusicalTime length;
531 if ((prop = xml_change->property("property")) != 0) {
532 change.property = (Property) string_2_enum (prop->value(), change.property);
534 fatal << "!!!" << endmsg;
538 if ((prop = xml_change->property ("old")) != 0) {
539 istringstream old_str (prop->value());
540 if (change.property == StartTime || change.property == Length) {
541 old_str >> change.old_time;
543 int integer_value_so_that_istream_does_the_right_thing;
544 old_str >> integer_value_so_that_istream_does_the_right_thing;
545 change.old_value = integer_value_so_that_istream_does_the_right_thing;
548 fatal << "!!!" << endmsg;
552 if ((prop = xml_change->property ("new")) != 0) {
553 istringstream new_str (prop->value());
554 if (change.property == StartTime || change.property == Length) {
555 new_str >> change.new_time;
557 int integer_value_so_that_istream_does_the_right_thing;
558 new_str >> integer_value_so_that_istream_does_the_right_thing;
559 change.new_value = integer_value_so_that_istream_does_the_right_thing;
562 fatal << "!!!" << endmsg;
566 if (change.property != NoteNumber) {
567 if ((prop = xml_change->property("note")) != 0) {
568 istringstream note_str(prop->value());
571 warning << "note information missing note value" << endmsg;
575 note = change.new_value;
578 if (change.property != Channel) {
579 if ((prop = xml_change->property("channel")) != 0) {
580 istringstream channel_str(prop->value());
581 channel_str >> channel;
583 warning << "note information missing channel" << endmsg;
587 channel = change.new_value;
590 if (change.property != StartTime) {
591 if ((prop = xml_change->property("time")) != 0) {
592 istringstream time_str(prop->value());
595 warning << "note information missing time" << endmsg;
599 time = change.new_time;
602 if (change.property != Length) {
603 if ((prop = xml_change->property("length")) != 0) {
604 istringstream length_str(prop->value());
605 length_str >> length;
607 warning << "note information missing length" << endmsg;
611 length = change.new_time;
614 if (change.property != Velocity) {
615 if ((prop = xml_change->property("velocity")) != 0) {
616 istringstream velocity_str(prop->value());
617 velocity_str >> velocity;
619 warning << "note information missing velocity" << endmsg;
623 velocity = change.new_value;
626 /* we must point at the instance of the note that is actually in the model.
627 so go look for it ...
630 boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
632 change.note = _model->find_note (new_note);
635 warning << "MIDI note " << *new_note << " not found in model - programmers should investigate this" << endmsg;
636 /* use the actual new note */
637 change.note = new_note;
644 MidiModel::DiffCommand::set_state(const XMLNode& diff_command, int /*version*/)
646 if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
652 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
655 XMLNodeList notes = changed_notes->children();
656 transform (notes.begin(), notes.end(), back_inserter(_changes),
657 boost::bind (&DiffCommand::unmarshal_change, this, _1));
665 MidiModel::DiffCommand::get_state ()
667 XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
668 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
670 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
671 for_each(_changes.begin(), _changes.end(),
673 boost::bind (&XMLNode::add_child_nocopy, *changes, _1),
674 boost::bind (&DiffCommand::marshal_change, this, _1)));
676 return *diff_command;
679 /** Write all of the model to a MidiSource (i.e. save the model).
680 * This is different from manually using read to write to a source in that
681 * note off events are written regardless of the track mode. This is so the
682 * user can switch a recorded track (with note durations from some instrument)
683 * to percussive, save, reload, then switch it back to sustained without
684 * destroying the original note durations.
687 MidiModel::write_to (boost::shared_ptr<MidiSource> source)
689 ReadLock lock(read_lock());
691 const bool old_percussive = percussive();
692 set_percussive(false);
694 source->drop_model();
695 source->mark_streaming_midi_write_started(note_mode(), _midi_source->timeline_position());
697 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
698 source->append_event_unlocked_beats(*i);
701 set_percussive(old_percussive);
702 source->mark_streaming_write_completed();
709 /** Write part or all of the model to a MidiSource (i.e. save the model).
710 * This is different from manually using read to write to a source in that
711 * note off events are written regardless of the track mode. This is so the
712 * user can switch a recorded track (with note durations from some instrument)
713 * to percussive, save, reload, then switch it back to sustained without
714 * destroying the original note durations.
717 MidiModel::write_section_to (boost::shared_ptr<MidiSource> source, Evoral::MusicalTime begin_time, Evoral::MusicalTime end_time)
719 ReadLock lock(read_lock());
720 MidiStateTracker mst;
721 Evoral::MusicalTime extra_note_on_time = end_time;
723 const bool old_percussive = percussive();
724 set_percussive(false);
726 source->drop_model();
727 source->mark_streaming_midi_write_started(note_mode(), _midi_source->timeline_position());
729 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
730 const Evoral::Event<Evoral::MusicalTime>& ev (*i);
732 if (ev.time() >= begin_time && ev.time() < end_time) {
734 const Evoral::MIDIEvent<Evoral::MusicalTime>* mev =
735 static_cast<const Evoral::MIDIEvent<Evoral::MusicalTime>* > (&ev);
742 if (mev->is_note_off()) {
744 if (!mst.active (mev->note(), mev->channel())) {
746 /* add a note-on at the start of the range we're writing
747 to the file. velocity is just an arbitary reasonable value.
750 Evoral::MIDIEvent<Evoral::MusicalTime> on (mev->event_type(), extra_note_on_time, 3, 0, true);
751 on.set_type (mev->type());
752 on.set_note (mev->note());
753 on.set_channel (mev->channel());
754 on.set_velocity (mev->velocity());
756 cerr << "Add note on for odd note off, note = " << (int) on.note() << endl;
757 source->append_event_unlocked_beats (on);
758 mst.add (on.note(), on.channel());
760 extra_note_on_time += 1.0/128.0;
763 cerr << "MIDI Note off (note = " << (int) mev->note() << endl;
764 source->append_event_unlocked_beats (*i);
765 mst.remove (mev->note(), mev->channel());
768 } else if (mev->is_note_on()) {
769 cerr << "MIDI Note on (note = " << (int) mev->note() << endl;
770 mst.add (mev->note(), mev->channel());
771 source->append_event_unlocked_beats(*i);
774 cerr << "MIDI other event type\n";
775 source->append_event_unlocked_beats(*i);
780 mst.resolve_notes (*source, end_time);
782 set_percussive(old_percussive);
783 source->mark_streaming_write_completed();
791 MidiModel::get_state()
793 XMLNode *node = new XMLNode("MidiModel");
797 boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
798 MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
800 Notes::iterator l = notes().lower_bound(other);
802 if (l != notes().end()) {
803 for (; (*l)->time() == other->time(); ++l) {
810 return boost::shared_ptr<Evoral::Note<TimeType> >();
813 /** Lock and invalidate the source.
814 * This should be used by commands and editing things
817 MidiModel::edit_lock()
819 Glib::Mutex::Lock* source_lock = new Glib::Mutex::Lock(_midi_source->mutex());
820 _midi_source->invalidate(); // Release cached iterator's read lock on model
821 return WriteLock(new WriteLockImpl(source_lock, _lock, _control_lock));
824 /** Lock just the model, the source lock must already be held.
825 * This should only be called from libardour/evoral places
828 MidiModel::write_lock()
830 assert(!_midi_source->mutex().trylock());
831 return WriteLock(new WriteLockImpl(NULL, _lock, _control_lock));