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/types.h"
34 #include "ardour/session.h"
37 using namespace ARDOUR;
40 MidiModel::MidiModel(MidiSource* s, size_t size)
41 : AutomatableSequence<TimeType>(s->session(), size)
46 /** Start a new Delta command.
48 * This has no side-effects on the model or Session, the returned command
49 * can be held on to for as long as the caller wishes, or discarded without
50 * formality, until apply_command is called and ownership is taken.
52 MidiModel::DeltaCommand*
53 MidiModel::new_delta_command(const string name)
55 DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
59 /** Start a new Diff command.
61 * This has no side-effects on the model or Session, the returned command
62 * can be held on to for as long as the caller wishes, or discarded without
63 * formality, until apply_command is called and ownership is taken.
65 MidiModel::DiffCommand*
66 MidiModel::new_diff_command(const string name)
68 DiffCommand* cmd = new DiffCommand(_midi_source->model(), name);
74 * Ownership of cmd is taken, it must not be deleted by the caller.
75 * The command will constitute one item on the undo stack.
78 MidiModel::apply_command(Session& session, Command* cmd)
80 session.begin_reversible_command(cmd->name());
82 session.commit_reversible_command(cmd);
86 /** Apply a command as part of a larger reversible transaction
88 * Ownership of cmd is taken, it must not be deleted by the caller.
89 * The command will constitute one item on the undo stack.
92 MidiModel::apply_command_as_subcommand(Session& session, Command* cmd)
95 session.add_command(cmd);
102 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
110 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
118 MidiModel::DeltaCommand::add(const boost::shared_ptr< Evoral::Note<TimeType> > note)
120 _removed_notes.remove(note);
121 _added_notes.push_back(note);
125 MidiModel::DeltaCommand::remove(const boost::shared_ptr< Evoral::Note<TimeType> > note)
127 _added_notes.remove(note);
128 _removed_notes.push_back(note);
132 MidiModel::DeltaCommand::operator()()
134 // This could be made much faster by using a priority_queue for added and
135 // removed notes (or sort here), and doing a single iteration over _model
137 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
138 _model->_midi_source->invalidate(); // release model read lock
139 _model->write_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);
149 _model->write_unlock();
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 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
160 _model->_midi_source->invalidate(); // release model read lock
161 _model->write_lock();
163 for (NoteList::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i) {
164 _model->remove_note_unlocked(*i);
167 for (NoteList::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i) {
168 _model->add_note_unlocked(*i);
171 _model->write_unlock();
172 _model->ContentsChanged(); /* EMIT SIGNAL */
176 MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr< Evoral::Note<TimeType> > note)
178 XMLNode* xml_note = new XMLNode("note");
179 ostringstream note_str(ios::ate);
180 note_str << int(note->note());
181 xml_note->add_property("note", note_str.str());
183 ostringstream channel_str(ios::ate);
184 channel_str << int(note->channel());
185 xml_note->add_property("channel", channel_str.str());
187 ostringstream time_str(ios::ate);
188 time_str << int(note->time());
189 xml_note->add_property("time", time_str.str());
191 ostringstream length_str(ios::ate);
192 length_str <<(unsigned int) note->length();
193 xml_note->add_property("length", length_str.str());
195 ostringstream velocity_str(ios::ate);
196 velocity_str << (unsigned int) note->velocity();
197 xml_note->add_property("velocity", velocity_str.str());
202 boost::shared_ptr< Evoral::Note<MidiModel::TimeType> >
203 MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
207 unsigned int channel;
210 unsigned int velocity;
212 if ((prop = xml_note->property("note")) != 0) {
213 istringstream note_str(prop->value());
216 warning << "note information missing note value" << endmsg;
220 if ((prop = xml_note->property("channel")) != 0) {
221 istringstream channel_str(prop->value());
222 channel_str >> channel;
224 warning << "note information missing channel" << endmsg;
228 if ((prop = xml_note->property("time")) != 0) {
229 istringstream time_str(prop->value());
232 warning << "note information missing time" << endmsg;
236 if ((prop = xml_note->property("length")) != 0) {
237 istringstream length_str(prop->value());
238 length_str >> length;
240 warning << "note information missing length" << endmsg;
244 if ((prop = xml_note->property("velocity")) != 0) {
245 istringstream velocity_str(prop->value());
246 velocity_str >> velocity;
248 warning << "note information missing velocity" << endmsg;
252 boost::shared_ptr< Evoral::Note<TimeType> > note_ptr(new Evoral::Note<TimeType>(
253 channel, time, length, note, velocity));
257 #define ADDED_NOTES_ELEMENT "AddedNotes"
258 #define REMOVED_NOTES_ELEMENT "RemovedNotes"
259 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
262 MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
264 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
268 _added_notes.clear();
269 XMLNode* added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
270 XMLNodeList notes = added_notes->children();
271 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
272 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
274 _removed_notes.clear();
275 XMLNode* removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
276 notes = removed_notes->children();
277 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
278 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
284 MidiModel::DeltaCommand::get_state()
286 XMLNode* delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
287 delta_command->add_property("midi-source", _model->midi_source()->id().to_s());
289 XMLNode* added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
290 for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
291 sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
292 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
294 XMLNode* removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
295 for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
296 sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
297 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
299 return *delta_command;
302 /************** DIFF COMMAND ********************/
304 #define DIFF_NOTES_ELEMENT "ChangedNotes"
305 #define DIFF_COMMAND_ELEMENT "DiffCommand"
307 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const std::string& name)
315 MidiModel::DiffCommand::DiffCommand(boost::shared_ptr<MidiModel> m, const XMLNode& node)
323 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
326 NotePropertyChange change;
329 change.property = prop;
330 change.new_value = new_value;
334 change.old_value = note->note();
337 change.old_value = note->velocity();
340 fatal << "MidiModel::DiffCommand::change() with integer argument called for start time" << endmsg;
344 fatal << "MidiModel::DiffCommand::change() with integer argument called for length" << endmsg;
348 change.old_value = note->channel();
352 _changes.push_back (change);
356 MidiModel::DiffCommand::change(const boost::shared_ptr< Evoral::Note<TimeType> > note, Property prop,
359 NotePropertyChange change;
362 change.property = prop;
363 change.new_time = new_time;
369 fatal << "MidiModel::DiffCommand::change() with time argument called for note, channel or velocity" << endmsg;
372 change.old_time = note->time();
375 change.old_time = note->length();
379 _changes.push_back (change);
383 MidiModel::DiffCommand::operator()()
385 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
386 _model->_midi_source->invalidate(); // release model read lock
387 _model->write_lock();
389 for (ChangeList::iterator i = _changes.begin(); i != _changes.end(); ++i) {
390 Property prop = i->property;
393 i->note->set_note (i->new_value);
396 i->note->set_velocity (i->new_value);
399 i->note->set_time (i->new_time);
402 i->note->set_length (i->new_time);
405 i->note->set_channel (i->new_value);
410 _model->write_unlock();
411 _model->ContentsChanged(); /* EMIT SIGNAL */
415 MidiModel::DiffCommand::undo()
417 Glib::Mutex::Lock lm (_model->_midi_source->mutex());
418 _model->_midi_source->invalidate(); // release model read lock
419 _model->write_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);
442 _model->write_unlock();
443 _model->ContentsChanged(); /* EMIT SIGNAL */
447 MidiModel::DiffCommand::marshal_change(const NotePropertyChange& 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 old_value_str << (unsigned int) change.old_value;
458 xml_change->add_property ("old", old_value_str.str());
462 ostringstream new_value_str (ios::ate);
463 new_value_str << (unsigned int) change.old_value;
464 xml_change->add_property ("new", new_value_str.str());
467 /* now the rest of the note */
469 if (change.property != NoteNumber) {
470 ostringstream note_str(ios::ate);
471 note_str << int(change.note->note());
472 xml_change->add_property("note", note_str.str());
475 if (change.property != Channel) {
476 ostringstream channel_str(ios::ate);
477 channel_str << int(change.note->channel());
478 xml_change->add_property("channel", channel_str.str());
481 if (change.property != StartTime) {
482 ostringstream time_str(ios::ate);
483 time_str << int(change.note->time());
484 xml_change->add_property("time", time_str.str());
487 if (change.property != Length) {
488 ostringstream length_str(ios::ate);
489 length_str <<(unsigned int) change.note->length();
490 xml_change->add_property("length", length_str.str());
493 if (change.property != Velocity) {
494 ostringstream velocity_str(ios::ate);
495 velocity_str << (unsigned int) change.note->velocity();
496 xml_change->add_property("velocity", velocity_str.str());
502 MidiModel::DiffCommand::NotePropertyChange
503 MidiModel::DiffCommand::unmarshal_change(XMLNode *xml_change)
506 NotePropertyChange change;
508 unsigned int channel;
511 unsigned int velocity;
513 if ((prop = xml_change->property("property")) != 0) {
514 change.property = (Property) string_2_enum (prop->value(), change.property);
516 fatal << "!!!" << endmsg;
520 if ((prop = xml_change->property ("old")) != 0) {
521 istringstream old_str (prop->value());
522 old_str >> change.old_value;
524 fatal << "!!!" << endmsg;
528 if ((prop = xml_change->property ("new")) != 0) {
529 istringstream new_str (prop->value());
530 new_str >> change.new_value;
532 fatal << "!!!" << endmsg;
536 if (change.property != NoteNumber) {
537 if ((prop = xml_change->property("note")) != 0) {
538 istringstream note_str(prop->value());
541 warning << "note information missing note value" << endmsg;
545 note = change.new_value;
548 if (change.property != Channel) {
549 if ((prop = xml_change->property("channel")) != 0) {
550 istringstream channel_str(prop->value());
551 channel_str >> channel;
553 warning << "note information missing channel" << endmsg;
557 channel = change.new_value;
560 if (change.property != StartTime) {
561 if ((prop = xml_change->property("time")) != 0) {
562 istringstream time_str(prop->value());
565 warning << "note information missing time" << endmsg;
569 time = change.new_value;
572 if (change.property != Length) {
573 if ((prop = xml_change->property("length")) != 0) {
574 istringstream length_str(prop->value());
575 length_str >> length;
577 warning << "note information missing length" << endmsg;
581 length = change.new_value;
584 if (change.property != Velocity) {
585 if ((prop = xml_change->property("velocity")) != 0) {
586 istringstream velocity_str(prop->value());
587 velocity_str >> velocity;
589 warning << "note information missing velocity" << endmsg;
593 velocity = change.new_value;
596 /* we must point at the instance of the note that is actually in the model.
597 so go look for it ...
600 boost::shared_ptr<Evoral::Note<TimeType> > new_note (new Evoral::Note<TimeType> (channel, time, length, note, velocity));
602 change.note = _model->find_note (new_note);
605 warning << "MIDI note not found in model - programmers should investigate this" << endmsg;
606 /* use the actual new note */
607 change.note = new_note;
614 MidiModel::DiffCommand::set_state(const XMLNode& diff_command)
616 if (diff_command.name() != string(DIFF_COMMAND_ELEMENT)) {
622 XMLNode* changed_notes = diff_command.child(DIFF_NOTES_ELEMENT);
623 XMLNodeList notes = changed_notes->children();
625 transform (notes.begin(), notes.end(), back_inserter(_changes),
626 sigc::mem_fun(*this, &DiffCommand::unmarshal_change));
632 MidiModel::DiffCommand::get_state ()
634 XMLNode* diff_command = new XMLNode(DIFF_COMMAND_ELEMENT);
635 diff_command->add_property("midi-source", _model->midi_source()->id().to_s());
637 XMLNode* changes = diff_command->add_child(DIFF_NOTES_ELEMENT);
638 for_each(_changes.begin(), _changes.end(), sigc::compose(
639 sigc::mem_fun(*changes, &XMLNode::add_child_nocopy),
640 sigc::mem_fun(*this, &DiffCommand::marshal_change)));
642 return *diff_command;
645 /** Write the model to a MidiSource (i.e. save the model).
646 * This is different from manually using read to write to a source in that
647 * note off events are written regardless of the track mode. This is so the
648 * user can switch a recorded track (with note durations from some instrument)
649 * to percussive, save, reload, then switch it back to sustained without
650 * destroying the original note durations.
653 MidiModel::write_to(boost::shared_ptr<MidiSource> source)
657 const bool old_percussive = percussive();
658 set_percussive(false);
660 source->drop_model();
662 for (Evoral::Sequence<TimeType>::const_iterator i = begin(); i != end(); ++i) {
663 source->append_event_unlocked_beats(*i);
666 set_percussive(old_percussive);
675 MidiModel::get_state()
677 XMLNode *node = new XMLNode("MidiModel");
681 boost::shared_ptr<Evoral::Note<MidiModel::TimeType> >
682 MidiModel::find_note (boost::shared_ptr<Evoral::Note<TimeType> > other)
684 Notes::iterator i = find (notes().begin(), notes().end(), other);
686 if (i == notes().end()) {
687 return boost::shared_ptr<Evoral::Note<TimeType> > ();