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)
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 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
139 _model->_midi_source->invalidate(); // release model read lock
140 _model->write_lock();
142 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
143 _model->add_note_unlocked(*i);
146 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
147 _model->remove_note_unlocked(*i);
150 _model->write_unlock();
151 _model->ContentsChanged(); /* EMIT SIGNAL */
155 MidiModel::DeltaCommand::undo()
157 // This could be made much faster by using a priority_queue for added and
158 // removed notes (or sort here), and doing a single iteration over _model
160 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
161 _model->_midi_source->invalidate(); // release model read lock
162 _model->write_lock();
164 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
165 _model->remove_note_unlocked(*i);
168 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
169 _model->add_note_unlocked(*i);
172 _model->write_unlock();
173 _model->ContentsChanged(); /* EMIT SIGNAL */
177 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
179 XMLNode* xml_note = new XMLNode("note");
180 ostringstream note_str(ios::ate);
181 note_str << int(note->note());
182 xml_note->add_property("note", note_str.str());
184 ostringstream channel_str(ios::ate);
185 channel_str << int(note->channel());
186 xml_note->add_property("channel", channel_str.str());
188 ostringstream time_str(ios::ate);
189 time_str << int(note->time());
190 xml_note->add_property("time", time_str.str());
192 ostringstream length_str(ios::ate);
193 length_str <<(unsigned int) note->length();
194 xml_note->add_property("length", length_str.str());
196 ostringstream velocity_str(ios::ate);
197 velocity_str << (unsigned int) note->velocity();
198 xml_note->add_property("velocity", velocity_str.str());
203 boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
204 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
208 unsigned int channel;
211 unsigned int velocity;
213 if ((prop = xml_note->property("note")) != 0) {
214 istringstream note_str(prop->value());
217 warning << "note information missing note value" << endmsg;
221 if ((prop = xml_note->property("channel")) != 0) {
222 istringstream channel_str(prop->value());
223 channel_str >> channel;
225 warning << "note information missing channel" << endmsg;
229 if ((prop = xml_note->property("time")) != 0) {
230 istringstream time_str(prop->value());
233 warning << "note information missing time" << endmsg;
237 if ((prop = xml_note->property("length")) != 0) {
238 istringstream length_str(prop->value());
239 length_str >> length;
241 warning << "note information missing length" << endmsg;
245 if ((prop = xml_note->property("velocity")) != 0) {
246 istringstream velocity_str(prop->value());
247 velocity_str >> velocity;
249 warning << "note information missing velocity" << endmsg;
253 boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
254 channel, time, length, note, velocity));
258 #define ADDED_NOTES_ELEMENT "AddedNotes"
259 #define REMOVED_NOTES_ELEMENT "RemovedNotes"
260 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
263 MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
265 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
269 _added_notes.clear();
270 XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
272 XMLNodeList notes = added_notes->children();
273 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
274 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
277 _removed_notes.clear();
278 XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
280 XMLNodeList notes = removed_notes->children();
281 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
282 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
289 MidiModel::DeltaCommand::get_state()
291 XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
292 delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
294 XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
295 for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
296 sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
297 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
299 XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
300 for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
301 sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
302 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
304 return *delta_command;
307 /************** DIFF COMMAND ********************/
309 #define DIFF_NOTES_ELEMENT "ChangedNotes"
310 #define DIFF_COMMAND_ELEMENT "DiffCommand"
312 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
320 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
328 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
331 NotePropertyChange change;
334 change.property = prop;
335 change.new_value = new_value;
339 change.old_value = note->note();
342 change.old_value = note->velocity();
345 fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
349 fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
353 change.old_value = note->channel();
357 _changes.push_back (change);
361 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
364 NotePropertyChange change;
367 change.property = prop;
368 change.new_time = new_time;
374 fatal << "MidiModel::DiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
377 change.old_time = note->time();
380 change.old_time = note->length();
384 _changes.push_back (change);
388 MidiModel::DiffCommand::operator()()
390 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
391 _model->_midi_source->invalidate(); // release model read lock
392 _model->write_lock();
394 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
395 Property prop = i->property;
398 i->note->set_note (i->new_value);
401 i->note->set_velocity (i->new_value);
404 i->note->set_time (i->new_time);
407 i->note->set_length (i->new_time);
410 i->note->set_channel (i->new_value);
415 _model->write_unlock();
416 _model->ContentsChanged(); /* EMIT SIGNAL */
420 MidiModel::DiffCommand::undo()
422 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
423 _model->_midi_source->invalidate(); // release model read lock
424 _model->write_lock();
426 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
427 Property prop = i->property;
430 i->note->set_note (i->old_value);
433 i->note->set_velocity (i->old_value);
436 i->note->set_time (i->old_time);
439 i->note->set_length (i->old_time);
442 i->note->set_channel (i->old_value);
447 _model->write_unlock();
448 _model->ContentsChanged(); /* EMIT SIGNAL */
452 MidiModel::DiffCommand::marshal_change(const NotePropertyChange& change)
454 XMLNode* xml_change = new XMLNode("change");
456 /* first, the change itself */
458 xml_change->add_property ("property", enum_2_string (change.property));
461 ostringstream old_value_str (ios::ate);
462 if (change.property == StartTime || change.property == Length) {
463 old_value_str << change.old_time;
465 old_value_str << (unsigned int) change.old_value;
467 xml_change->add_property ("old", old_value_str.str());
471 ostringstream new_value_str (ios::ate);
472 if (change.property == StartTime || change.property == Length) {
473 new_value_str << change.new_time;
475 new_value_str << (unsigned int) change.new_value;
477 xml_change->add_property ("new", new_value_str.str());
480 /* now the rest of the note */
482 const SMFSource* smf = dynamic_cast<const SMFSource*> (_model->midi_source());
484 if (change.property != NoteNumber) {
485 ostringstream note_str;
486 note_str << int(change.note->note());
487 xml_change->add_property("note", note_str.str());
490 if (change.property != Channel) {
491 ostringstream channel_str;
492 channel_str << int(change.note->channel());
493 xml_change->add_property("channel", channel_str.str());
496 if (change.property != StartTime) {
497 ostringstream time_str;
499 time_str << smf->round_to_file_precision (change.note->time());
501 time_str << change.note->time();
503 xml_change->add_property("time", time_str.str());
506 if (change.property != Length) {
507 ostringstream length_str;
509 length_str << smf->round_to_file_precision (change.note->length());
511 length_str << change.note->length();
513 xml_change->add_property ("length", length_str.str());
516 if (change.property != Velocity) {
517 ostringstream velocity_str;
518 velocity_str << int (change.note->velocity());
519 xml_change->add_property("velocity", velocity_str.str());
525 MidiModel::DiffCommand::NotePropertyChange
526 MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
529 NotePropertyChange change;
531 unsigned int channel;
532 unsigned int velocity;
533 Evoral::MusicalTime time;
534 Evoral::MusicalTime length;
536 if ((prop = xml_change->property("property")) != 0) {
537 change.property = (Property) string_2_enum (prop->value(), change.property);
539 fatal << "!!!" << endmsg;
543 if ((prop = xml_change->property ("old")) != 0) {
544 istringstream old_str (prop->value());
545 if (change.property == StartTime || change.property == Length) {
546 old_str >> change.old_time;
548 int integer_value_so_that_istream_does_the_right_thing;
549 old_str >> integer_value_so_that_istream_does_the_right_thing;
550 change.old_value = integer_value_so_that_istream_does_the_right_thing;
553 fatal << "!!!" << endmsg;
557 if ((prop = xml_change->property ("new")) != 0) {
558 istringstream new_str (prop->value());
559 if (change.property == StartTime || change.property == Length) {
560 new_str >> change.new_time;
562 int integer_value_so_that_istream_does_the_right_thing;
563 new_str >> integer_value_so_that_istream_does_the_right_thing;
564 change.new_value = integer_value_so_that_istream_does_the_right_thing;
567 fatal << "!!!" << endmsg;
571 if (change.property != NoteNumber) {
572 if ((prop = xml_change->property("note")) != 0) {
573 istringstream note_str(prop->value());
576 warning << "note information missing note value" << endmsg;
580 note = change.new_value;
583 if (change.property != Channel) {
584 if ((prop = xml_change->property("channel")) != 0) {
585 istringstream channel_str(prop->value());
586 channel_str >> channel;
588 warning << "note information missing channel" << endmsg;
592 channel = change.new_value;
595 if (change.property != StartTime) {
596 if ((prop = xml_change->property("time")) != 0) {
597 istringstream time_str(prop->value());
600 warning << "note information missing time" << endmsg;
604 time = change.new_time;
607 if (change.property != Length) {
608 if ((prop = xml_change->property("length")) != 0) {
609 istringstream length_str(prop->value());
610 length_str >> length;
612 warning << "note information missing length" << endmsg;
616 length = change.new_time;
619 if (change.property != Velocity) {
620 if ((prop = xml_change->property("velocity")) != 0) {
621 istringstream velocity_str(prop->value());
622 velocity_str >> velocity;
624 warning << "note information missing velocity" << endmsg;
628 velocity = change.new_value;
631 /* we must point at the instance of the note that is actually in the model.
632 so go look for it ...
635 boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
637 change.note = _model->find_note (new_note);
640 warning << "MIDI note " << *new_note << " not found in model - programmers should investigate this" << endmsg;
641 /* use the actual new note */
642 change.note = new_note;
649 MidiModel::DiffCommand::set_state(const XMLNode& diff_command)
651 if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
657 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
660 XMLNodeList notes = changed_notes->children();
662 transform (notes.begin(), notes.end(), back_inserter(_changes),
663 sigc::mem_fun(*this, &DiffCommand::unmarshal_change));
670 MidiModel::DiffCommand::get_state ()
672 XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
673 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
675 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
676 for_each(_changes.begin(), _changes.end(), sigc::compose(
677 sigc::mem_fun(*changes, &XMLNode::add_child_nocopy),
678 sigc::mem_fun(*this, &DiffCommand::marshal_change)));
680 return *diff_command;
683 /** Write the model to a MidiSource (i.e. save the model).
684 * This is different from manually using read to write to a source in that
685 * note off events are written regardless of the track mode. This is so the
686 * user can switch a recorded track (with note durations from some instrument)
687 * to percussive, save, reload, then switch it back to sustained without
688 * destroying the original note durations.
691 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
695 const bool old_percussive = percussive();
696 set_percussive(false);
698 source->drop_model();
700 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
701 source->append_event_unlocked_beats(*i);
704 set_percussive(old_percussive);
713 MidiModel::get_state()
715 XMLNode *node = new XMLNode("MidiModel");
719 boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
720 MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
722 for (Notes::iterator x = notes().begin(); x != notes().end(); ++x) {
727 /* XXX optimize by using a stored iterator and break out
728 when passed start time.
732 return boost::shared_ptr<Evoral::Note<TimeType> > ();