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 boost::bind (&DeltaCommand::unmarshal_note, this, _1));
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 boost::bind (&DeltaCommand::unmarshal_note, this, _1));
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(),
293 boost::bind (&XMLNode::add_child_nocopy, *added_notes, _1),
294 boost::bind (&DeltaCommand::marshal_note, this, _1)));
296 XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
297 for_each(_removed_notes.begin(), _removed_notes.end(),
299 boost::bind (&XMLNode::add_child_nocopy, *removed_notes, _1),
300 boost::bind (&DeltaCommand::marshal_note, this, _1)));
302 return *delta_command;
305 /************** DIFF COMMAND ********************/
307 #define DIFF_NOTES_ELEMENT "ChangedNotes"
308 #define DIFF_COMMAND_ELEMENT "DiffCommand"
310 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
318 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
322 set_state(node, Stateful::loading_state_version);
326 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
329 NotePropertyChange change;
332 change.property = prop;
333 change.new_value = new_value;
337 change.old_value = note->note();
340 change.old_value = note->velocity();
343 fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
347 fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
351 change.old_value = note->channel();
355 _changes.push_back (change);
359 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
362 NotePropertyChange change;
365 change.property = prop;
366 change.new_time = new_time;
372 fatal << "MidiModel::DiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
375 change.old_time = note->time();
378 change.old_time = note->length();
382 _changes.push_back (change);
386 MidiModel::DiffCommand::operator()()
388 MidiModel::WriteLock lock(_model->edit_lock());
390 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
391 Property prop = i->property;
394 i->note->set_note (i->new_value);
397 i->note->set_velocity (i->new_value);
400 i->note->set_time (i->new_time);
403 i->note->set_length (i->new_time);
406 i->note->set_channel (i->new_value);
412 _model->ContentsChanged(); /* EMIT SIGNAL */
416 MidiModel::DiffCommand::undo()
418 MidiModel::WriteLock lock(_model->edit_lock());
420 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
421 Property prop = i->property;
424 i->note->set_note (i->old_value);
427 i->note->set_velocity (i->old_value);
430 i->note->set_time (i->old_time);
433 i->note->set_length (i->old_time);
436 i->note->set_channel (i->old_value);
442 _model->ContentsChanged(); /* EMIT SIGNAL */
446 MidiModel::DiffCommand::marshal_change(const NotePropertyChange& change)
448 XMLNode* xml_change = new XMLNode("change");
450 /* first, the change itself */
452 xml_change->add_property ("property", enum_2_string (change.property));
455 ostringstream old_value_str (ios::ate);
456 if (change.property == StartTime || change.property == Length) {
457 old_value_str << change.old_time;
459 old_value_str << (unsigned int) change.old_value;
461 xml_change->add_property ("old", old_value_str.str());
465 ostringstream new_value_str (ios::ate);
466 if (change.property == StartTime || change.property == Length) {
467 new_value_str << change.new_time;
469 new_value_str << (unsigned int) change.new_value;
471 xml_change->add_property ("new", new_value_str.str());
474 /* now the rest of the note */
476 const SMFSource* smf = dynamic_cast<const SMFSource*> (_model->midi_source());
478 if (change.property != NoteNumber) {
479 ostringstream note_str;
480 note_str << int(change.note->note());
481 xml_change->add_property("note", note_str.str());
484 if (change.property != Channel) {
485 ostringstream channel_str;
486 channel_str << int(change.note->channel());
487 xml_change->add_property("channel", channel_str.str());
490 if (change.property != StartTime) {
491 ostringstream time_str;
493 time_str << smf->round_to_file_precision (change.note->time());
495 time_str << change.note->time();
497 xml_change->add_property("time", time_str.str());
500 if (change.property != Length) {
501 ostringstream length_str;
503 length_str << smf->round_to_file_precision (change.note->length());
505 length_str << change.note->length();
507 xml_change->add_property ("length", length_str.str());
510 if (change.property != Velocity) {
511 ostringstream velocity_str;
512 velocity_str << int (change.note->velocity());
513 xml_change->add_property("velocity", velocity_str.str());
519 MidiModel::DiffCommand::NotePropertyChange
520 MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
523 NotePropertyChange change;
525 unsigned int channel;
526 unsigned int velocity;
527 Evoral::MusicalTime time;
528 Evoral::MusicalTime length;
530 if ((prop = xml_change->property("property")) != 0) {
531 change.property = (Property) string_2_enum (prop->value(), change.property);
533 fatal << "!!!" << endmsg;
537 if ((prop = xml_change->property ("old")) != 0) {
538 istringstream old_str (prop->value());
539 if (change.property == StartTime || change.property == Length) {
540 old_str >> change.old_time;
542 int integer_value_so_that_istream_does_the_right_thing;
543 old_str >> integer_value_so_that_istream_does_the_right_thing;
544 change.old_value = integer_value_so_that_istream_does_the_right_thing;
547 fatal << "!!!" << endmsg;
551 if ((prop = xml_change->property ("new")) != 0) {
552 istringstream new_str (prop->value());
553 if (change.property == StartTime || change.property == Length) {
554 new_str >> change.new_time;
556 int integer_value_so_that_istream_does_the_right_thing;
557 new_str >> integer_value_so_that_istream_does_the_right_thing;
558 change.new_value = integer_value_so_that_istream_does_the_right_thing;
561 fatal << "!!!" << endmsg;
565 if (change.property != NoteNumber) {
566 if ((prop = xml_change->property("note")) != 0) {
567 istringstream note_str(prop->value());
570 warning << "note information missing note value" << endmsg;
574 note = change.new_value;
577 if (change.property != Channel) {
578 if ((prop = xml_change->property("channel")) != 0) {
579 istringstream channel_str(prop->value());
580 channel_str >> channel;
582 warning << "note information missing channel" << endmsg;
586 channel = change.new_value;
589 if (change.property != StartTime) {
590 if ((prop = xml_change->property("time")) != 0) {
591 istringstream time_str(prop->value());
594 warning << "note information missing time" << endmsg;
598 time = change.new_time;
601 if (change.property != Length) {
602 if ((prop = xml_change->property("length")) != 0) {
603 istringstream length_str(prop->value());
604 length_str >> length;
606 warning << "note information missing length" << endmsg;
610 length = change.new_time;
613 if (change.property != Velocity) {
614 if ((prop = xml_change->property("velocity")) != 0) {
615 istringstream velocity_str(prop->value());
616 velocity_str >> velocity;
618 warning << "note information missing velocity" << endmsg;
622 velocity = change.new_value;
625 /* we must point at the instance of the note that is actually in the model.
626 so go look for it ...
629 boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
631 change.note = _model->find_note (new_note);
634 warning << "MIDI note " << *new_note << " not found in model - programmers should investigate this" << endmsg;
635 /* use the actual new note */
636 change.note = new_note;
643 MidiModel::DiffCommand::set_state(const XMLNode& diff_command, int /*version*/)
645 if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
651 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
654 XMLNodeList notes = changed_notes->children();
655 transform (notes.begin(), notes.end(), back_inserter(_changes),
656 boost::bind (&DiffCommand::unmarshal_change, this, _1));
664 MidiModel::DiffCommand::get_state ()
666 XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
667 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
669 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
670 for_each(_changes.begin(), _changes.end(),
672 boost::bind (&XMLNode::add_child_nocopy, *changes, _1),
673 boost::bind (&DiffCommand::marshal_change, this, _1)));
675 return *diff_command;
678 /** Write the model to a MidiSource (i.e. save the model).
679 * This is different from manually using read to write to a source in that
680 * note off events are written regardless of the track mode. This is so the
681 * user can switch a recorded track (with note durations from some instrument)
682 * to percussive, save, reload, then switch it back to sustained without
683 * destroying the original note durations.
686 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
688 ReadLock lock(read_lock());
690 const bool old_percussive = percussive();
691 set_percussive(false);
693 source->drop_model();
694 source->mark_streaming_midi_write_started(note_mode(), _midi_source->timeline_position());
696 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
697 source->append_event_unlocked_beats(*i);
700 set_percussive(old_percussive);
701 source->mark_streaming_write_completed();
709 MidiModel::get_state()
711 XMLNode *node = new XMLNode("MidiModel");
715 boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
716 MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
718 Notes::iterator l = notes().lower_bound(other);
720 if (l != notes().end()) {
721 for (; (*l)->time() == other->time(); ++l) {
728 return boost::shared_ptr<Evoral::Note<TimeType> >();
731 /** Lock and invalidate the source.
732 * This should be used by commands and editing things
735 MidiModel::edit_lock()
737 Glib::Mutex::Lock* source_lock = new Glib::Mutex::Lock(_midi_source->mutex());
738 _midi_source->invalidate(); // Release cached iterator's read lock on model
739 return WriteLock(new WriteLockImpl(source_lock, _lock, _control_lock));
742 /** Lock just the model, the source lock must already be held.
743 * This should only be called from libardour/evoral places
746 MidiModel::write_lock()
748 assert(!_midi_source->mutex().trylock());
749 return WriteLock(new WriteLockImpl(NULL, _lock, _control_lock));