2 Copyright (C) 2007 Paul Davis
3 Written by Dave Robillard, 2007
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/enumwriter.h>
28 #include <midi++/events.h>
30 #include <ardour/midi_model.h>
31 #include <ardour/midi_source.h>
32 #include <ardour/types.h>
33 #include <ardour/session.h>
36 using namespace ARDOUR;
38 void MidiModel::write_lock() {
40 _automation_lock.lock();
43 void MidiModel::write_unlock() {
44 _lock.writer_unlock();
45 _automation_lock.unlock();
48 void MidiModel::read_lock() const {
50 /*_automation_lock.lock();*/
53 void MidiModel::read_unlock() const {
54 _lock.reader_unlock();
55 /*_automation_lock.unlock();*/
58 // Read iterator (const_iterator)
60 MidiModel::const_iterator::const_iterator(const MidiModel& model, double t)
62 , _is_end( (t == DBL_MAX) || model.empty())
65 //cerr << "Created MIDI iterator @ " << t << " (is end: " << _is_end << ")" << endl;
72 _note_iter = model.notes().end();
73 // find first note which begins after t
74 for (MidiModel::Notes::const_iterator i = model.notes().begin(); i != model.notes().end(); ++i) {
75 if ((*i)->time() >= t) {
81 MidiControlIterator earliest_control(boost::shared_ptr<AutomationList>(), DBL_MAX, 0.0);
83 _control_iters.reserve(model.controls().size());
84 for (Automatable::Controls::const_iterator i = model.controls().begin();
85 i != model.controls().end(); ++i) {
88 i->first.type() == MidiCCAutomation ||
89 i->first.type() == MidiPgmChangeAutomation ||
90 i->first.type() == MidiPitchBenderAutomation ||
91 i->first.type() == MidiChannelAftertouchAutomation);
94 bool ret = i->second->list()->rt_safe_earliest_event_unlocked(t, DBL_MAX, x, y);
96 //cerr << "MIDI Iterator: CC " << i->first.id() << " (size " << i->second->list()->size()
97 // << ") has no events past " << t << endl;
102 assert(y >= i->first.min());
103 assert(y <= i->first.max());
105 const MidiControlIterator new_iter(i->second->list(), x, y);
107 //cerr << "MIDI Iterator: CC " << i->first.id() << " added (" << x << ", " << y << ")" << endl;
108 _control_iters.push_back(new_iter);
110 if (x < earliest_control.x) {
111 earliest_control = new_iter;
112 _control_iter = _control_iters.end();
117 if (_note_iter != model.notes().end()) {
118 _event = MIDI::Event((*_note_iter)->on_event(), false);
119 _active_notes.push(*_note_iter);
120 //cerr << " new const iterator: size active notes: " << _active_notes.size() << " is empty: " << _active_notes.empty() << endl;
124 if (earliest_control.automation_list && earliest_control.x < _event.time())
125 model.control_to_midi_event(_event, earliest_control);
127 _control_iter = _control_iters.end();
129 if (_event.size() == 0) {
130 //cerr << "Created MIDI iterator @ " << t << " is at end." << endl;
133 // FIXME: possible race condition here....
135 _model->read_unlock();
139 //printf("New MIDI Iterator = %X @ %lf\n", _event.type(), _event.time());
143 MidiModel::const_iterator::~const_iterator()
146 _model->read_unlock();
149 const MidiModel::const_iterator& MidiModel::const_iterator::operator++()
152 throw std::logic_error("Attempt to iterate past end of MidiModel");
154 /*cerr << "const_iterator::operator++: _event type:" << hex << "0x" << int(_event.type())
155 << " buffer: 0x" << int(_event.buffer()[0]) << " 0x" << int(_event.buffer()[1])
156 << " 0x" << int(_event.buffer()[2]) << endl;*/
158 if (! (_event.is_note() || _event.is_cc() || _event.is_pgm_change() || _event.is_pitch_bender() || _event.is_channel_aftertouch()) ) {
159 cerr << "FAILED event buffer: " << hex << int(_event.buffer()[0]) << int(_event.buffer()[1]) << int(_event.buffer()[2]) << endl;
161 assert((_event.is_note() || _event.is_cc() || _event.is_pgm_change() || _event.is_pitch_bender() || _event.is_channel_aftertouch()));
163 // Increment past current control event
164 if (!_event.is_note() && _control_iter != _control_iters.end() && _control_iter->automation_list.get()) {
165 double x = 0.0, y = 0.0;
166 const bool ret = _control_iter->automation_list->rt_safe_earliest_event_unlocked(
167 _control_iter->x, DBL_MAX, x, y, false);
168 cerr << "control_iter x:" << _control_iter->x << " y:" << _control_iter->y << endl;
171 cerr << "Incremented " << _control_iter->automation_list->parameter().id() << " to " << x << endl;
172 _control_iter->x = x;
173 _control_iter->y = y;
175 cerr << "Hit end of " << _control_iter->automation_list->parameter().id() << endl;
176 _control_iter->automation_list.reset();
177 _control_iter->x = DBL_MAX;
181 // Now find and point at the earliest event
183 const std::vector<MidiControlIterator>::iterator old_control_iter = _control_iter;
184 _control_iter = _control_iters.begin();
186 for (std::vector<MidiControlIterator>::iterator i = _control_iters.begin();
187 i != _control_iters.end(); ++i) {
188 if (i->x < _control_iter->x) {
193 enum Type {NIL, NOTE_ON, NOTE_OFF, AUTOMATION};
198 // Next earliest note on
199 if (_note_iter != _model->notes().end()) {
201 t = (*_note_iter)->time();
204 // Use the next earliest note off iff it's earlier than the note on
205 if (_model->note_mode() == Sustained && (! _active_notes.empty())) {
206 if (type == NIL || _active_notes.top()->end_time() <= (*_note_iter)->time()) {
208 t = _active_notes.top()->end_time();
212 // Use the next earliest controller iff it's earlier than the note event
213 if (_control_iter != _control_iters.end()
214 && _control_iter->x != DBL_MAX
215 )//&& _control_iter != old_control_iter)
216 if (type == NIL || _control_iter->x < t)
219 if (type == NOTE_ON) {
220 //cerr << "********** MIDI Iterator = note on" << endl;
221 _event = MIDI::Event((*_note_iter)->on_event(), false);
222 _active_notes.push(*_note_iter);
224 } else if (type == NOTE_OFF) {
225 //cerr << "********** MIDI Iterator = note off" << endl;
226 _event = MIDI::Event(_active_notes.top()->off_event(), false);
228 } else if (type == AUTOMATION) {
229 //cerr << "********** MIDI Iterator = Automation" << endl;
230 _model->control_to_midi_event(_event, *_control_iter);
232 //cerr << "********** MIDI Iterator = End" << endl;
236 assert(_is_end || _event.size()> 0);
241 bool MidiModel::const_iterator::operator==(const const_iterator& other) const
243 if (_is_end || other._is_end)
244 return (_is_end == other._is_end);
246 return (_event == other._event);
249 MidiModel::const_iterator& MidiModel::const_iterator::operator=(const const_iterator& other)
251 if (_locked && _model != other._model)
252 _model->read_unlock();
254 assert( ! other._event.owns_buffer());
256 _model = other._model;
257 _event = other._event;
258 _active_notes = other._active_notes;
259 _is_end = other._is_end;
260 _locked = other._locked;
261 _note_iter = other._note_iter;
262 _control_iters = other._control_iters;
263 _control_iter = other._control_iter;
265 assert( ! _event.owns_buffer());
272 MidiModel::MidiModel(MidiSource *s, size_t size)
273 : Automatable(s->session(), "midi model")
275 , _note_mode(Sustained)
278 , _end_iter(*this, DBL_MAX)
279 , _next_read(UINT32_MAX)
280 , _read_iter(*this, DBL_MAX)
283 assert(_end_iter._is_end);
284 assert( ! _end_iter._locked);
287 /** Read events in frame range \a start .. \a start+cnt into \a dst,
288 * adding \a stamp_offset to each event's timestamp.
289 * \return number of events written to \a dst
291 size_t MidiModel::read(MidiRingBuffer& dst, nframes_t start, nframes_t nframes,
292 nframes_t stamp_offset, nframes_t negative_stamp_offset) const
294 //cerr << this << " MM::read @ " << start << " frames: " << nframes << " -> " << stamp_offset << endl;
295 //cerr << this << " MM # notes: " << n_notes() << endl;
297 size_t read_events = 0;
299 if (start != _next_read) {
300 _read_iter = const_iterator(*this, (double)start);
301 //cerr << "Repositioning iterator from " << _next_read << " to " << start << endl;
303 //cerr << "Using cached iterator at " << _next_read << endl;
306 _next_read = start + nframes;
308 while (_read_iter != end() && _read_iter->time() < start + nframes) {
309 assert(_read_iter->size()> 0);
310 dst.write(_read_iter->time() + stamp_offset - negative_stamp_offset,
311 _read_iter->size(), _read_iter->buffer());
314 cerr << this << " MidiModel::read event @ " << _read_iter->time()
315 << " type: " << hex << int(_read_iter->type()) << dec
316 << " note: " << int(_read_iter->note())
317 << " velocity: " << int(_read_iter->velocity())
328 bool MidiModel::control_to_midi_event(MIDI::Event& ev,
329 const MidiControlIterator& iter) const
331 switch (iter.automation_list->parameter().type()) {
332 case MidiCCAutomation:
334 ev.set_buffer((Byte*)malloc(3), true);
336 assert(iter.automation_list);
337 assert(iter.automation_list->parameter().channel() < 16);
338 assert(iter.automation_list->parameter().id() <= INT8_MAX);
339 assert(iter.y <= INT8_MAX);
340 ev.buffer()[0] = MIDI_CMD_CONTROL + iter.automation_list->parameter().channel();
341 ev.buffer()[1] = (Byte)iter.automation_list->parameter().id();
342 ev.buffer()[2] = (Byte)iter.y;
347 case MidiPgmChangeAutomation:
349 ev.set_buffer((Byte*)malloc(3), true);
351 assert(iter.automation_list);
352 assert(iter.automation_list->parameter().channel() < 16);
353 assert(iter.automation_list->parameter().id() <= INT8_MAX);
354 assert(iter.y <= INT8_MAX);
355 ev.buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.automation_list->parameter().channel();
356 ev.buffer()[1] = (Byte)iter.y;
362 case MidiPitchBenderAutomation:
364 ev.set_buffer((Byte*)malloc(3), true);
366 assert(iter.automation_list);
367 assert(iter.automation_list->parameter().channel() < 16);
368 assert(iter.automation_list->parameter().id() <= INT8_MAX);
369 assert(iter.y < (1<<14));
370 ev.buffer()[0] = MIDI_CMD_BENDER + iter.automation_list->parameter().channel();
371 ev.buffer()[1] = ((Byte)iter.y) & 0x7F; // LSB
372 ev.buffer()[2] = (((Byte)iter.y) >> 7) & 0x7F; // MSB
377 case MidiChannelAftertouchAutomation:
379 ev.set_buffer((Byte*)malloc(3), true);
381 assert(iter.automation_list);
382 assert(iter.automation_list->parameter().channel() < 16);
383 assert(iter.automation_list->parameter().id() <= INT8_MAX);
384 assert(iter.y <= INT8_MAX);
386 = MIDI_CMD_CHANNEL_PRESSURE + iter.automation_list->parameter().channel();
387 ev.buffer()[1] = (Byte)iter.y;
399 /** Clear all events from the model.
401 void MidiModel::clear()
408 _lock.writer_unlock();
412 /** Begin a write of events to the model.
414 * If \a mode is Sustained, complete notes with duration are constructed as note
415 * on/off events are received. Otherwise (Percussive), only note on events are
416 * stored; note off events are discarded entirely and all contained notes will
419 void MidiModel::start_write()
421 //cerr << "MM " << this << " START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
424 for (int i = 0; i < 16; ++i)
425 _write_notes[i].clear();
427 _dirty_automations.clear();
431 /** Finish a write of events to the model.
433 * If \a delete_stuck is true and the current mode is Sustained, note on events
434 * that were never resolved with a corresonding note off will be deleted.
435 * Otherwise they will remain as notes with duration 0.
437 void MidiModel::end_write(bool delete_stuck)
442 //cerr << "MM " << this << " END WRITE: " << _notes.size() << " NOTES\n";
444 if (_note_mode == Sustained && delete_stuck) {
445 for (Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
446 if ((*n)->duration() == 0) {
447 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
449 // we have to break here because erase invalidates the iterator
457 for (int i = 0; i < 16; ++i) {
458 if (!_write_notes[i].empty()) {
459 cerr << "WARNING: MidiModel::end_write: Channel " << i << " has "
460 << _write_notes[i].size() << " stuck notes" << endl;
462 _write_notes[i].clear();
465 for (AutomationLists::const_iterator i = _dirty_automations.begin(); i != _dirty_automations.end(); ++i) {
467 (*i)->lookup_cache().left = -1;
468 (*i)->search_cache().left = -1;
475 /** Append \a in_event to model. NOT realtime safe.
477 * Timestamps of events in \a buf are expected to be relative to
478 * the start of this model (t=0) and MUST be monotonically increasing
479 * and MUST be >= the latest event currently in the model.
481 void MidiModel::append(const MIDI::Event& ev)
486 cerr << "MidiModel append event type: "
487 << hex << "0x" << (int)ev.type() << endl;
489 assert(_notes.empty() || ev.time() >= _notes.back()->time());
492 if (ev.is_note_on()) {
493 append_note_on_unlocked(ev.channel(), ev.time(), ev.note(),
495 } else if (ev.is_note_off()) {
496 append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
497 } else if (ev.is_cc()) {
498 append_automation_event_unlocked(MidiCCAutomation, ev.channel(),
499 ev.time(), ev.cc_number(), ev.cc_value());
500 } else if (ev.is_pgm_change()) {
501 append_automation_event_unlocked(MidiPgmChangeAutomation, ev.channel(),
502 ev.time(), ev.pgm_number(), 0);
503 } else if (ev.is_pitch_bender()) {
504 append_automation_event_unlocked(MidiPitchBenderAutomation,
505 ev.channel(), ev.time(), ev.pitch_bender_lsb(),
506 ev.pitch_bender_msb());
507 } else if (ev.is_channel_aftertouch()) {
508 append_automation_event_unlocked(MidiChannelAftertouchAutomation,
509 ev.channel(), ev.time(), ev.channel_aftertouch(), 0);
511 printf("WARNING: MidiModel: Unknown event type %X\n", ev.type());
517 void MidiModel::append_note_on_unlocked(uint8_t chan, double time,
518 uint8_t note_num, uint8_t velocity)
520 /*cerr << "MidiModel " << this << " chan " << (int)chan <<
521 " note " << (int)note_num << " on @ " << time << endl;*/
527 _notes.push_back(boost::shared_ptr<Note>(new Note(chan, time, 0, note_num, velocity)));
528 if (_note_mode == Sustained) {
529 //cerr << "MM Sustained: Appending active note on " << (unsigned)(uint8_t)note_num << endl;
530 _write_notes[chan].push_back(_notes.size() - 1);
532 cerr << "MM Percussive: NOT appending active note on" << endl;
536 void MidiModel::append_note_off_unlocked(uint8_t chan, double time,
539 /*cerr << "MidiModel " << this << " chan " << (int)chan <<
540 " note " << (int)note_num << " off @ " << time << endl;*/
546 if (_note_mode == Percussive) {
547 cerr << "MidiModel Ignoring note off (percussive mode)" << endl;
551 /* FIXME: make _write_notes fixed size (127 noted) for speed */
553 /* FIXME: note off velocity for that one guy out there who actually has
554 * keys that send it */
556 bool resolved = false;
558 for (WriteNotes::iterator n = _write_notes[chan].begin(); n
559 != _write_notes[chan].end(); ++n) {
560 Note& note = *_notes[*n].get();
561 //cerr << (unsigned)(uint8_t)note.note() << " ? " << (unsigned)note_num << endl;
562 if (note.note() == note_num) {
563 assert(time >= note.time());
564 note.set_duration(time - note.time());
565 _write_notes[chan].erase(n);
566 //cerr << "MM resolved note, duration: " << note.duration() << endl;
573 cerr << "MidiModel " << this << " spurious note off chan " << (int)chan
574 << ", note " << (int)note_num << " @ " << time << endl;
577 void MidiModel::append_automation_event_unlocked(AutomationType type,
578 uint8_t chan, double time, uint8_t first_byte, uint8_t second_byte)
580 //cerr << "MidiModel " << this << " chan " << (int)chan <<
581 // " CC " << (int)number << " = " << (int)value << " @ " << time << endl;
591 case MidiCCAutomation:
593 value = double(second_byte);
595 case MidiChannelAftertouchAutomation:
596 case MidiPgmChangeAutomation:
598 value = double(first_byte);
600 case MidiPitchBenderAutomation:
602 value = double((0x7F & second_byte) << 7 | (0x7F & first_byte));
608 Parameter param(type, id, chan);
609 boost::shared_ptr<AutomationControl> control = Automatable::control(param, true);
610 control->list()->fast_simple_add(time, value);
611 /*cerr << "control list size after fast simple add: " << control->list()->size() << endl;*/
614 void MidiModel::add_note_unlocked(const boost::shared_ptr<Note> note)
616 //cerr << "MidiModel " << this << " add note " << (int)note.note() << " @ " << note.time() << endl;
618 Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note,
619 note_time_comparator);
620 _notes.insert(i, note);
623 void MidiModel::remove_note_unlocked(const boost::shared_ptr<const Note> note)
626 //cerr << "MidiModel " << this << " remove note " << (int)note.note() << " @ " << note.time() << endl;
627 for (Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
629 const Note& _note = *note;
630 // TODO: There is still the issue, that after restarting ardour
631 // persisted undo does not work, because of rounding errors in the
632 // event times after saving/restoring to/from MIDI files
633 cerr << "======================================= " << endl;
634 cerr << int(_n.note()) << "@" << int(_n.time()) << "[" << int(_n.channel()) << "] --" << int(_n.duration()) << "-- #" << int(_n.velocity()) << endl;
635 cerr << int(_note.note()) << "@" << int(_note.time()) << "[" << int(_note.channel()) << "] --" << int(_note.duration()) << "-- #" << int(_note.velocity()) << endl;
636 cerr << "Equal: " << bool(_n == _note) << endl;
637 cerr << endl << endl;
640 // we have to break here, because erase invalidates all iterators, ie. n itself
646 /** Slow! for debugging only. */
648 bool MidiModel::is_sorted() const {
650 for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n)
651 if ((*n)->time() < t)
660 /** Start a new command.
662 * This has no side-effects on the model or Session, the returned command
663 * can be held on to for as long as the caller wishes, or discarded without
664 * formality, until apply_command is called and ownership is taken.
666 MidiModel::DeltaCommand* MidiModel::new_delta_command(const string name)
668 DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
674 * Ownership of cmd is taken, it must not be deleted by the caller.
675 * The command will constitute one item on the undo stack.
677 void MidiModel::apply_command(Command* cmd)
679 _session.begin_reversible_command(cmd->name());
682 _session.commit_reversible_command(cmd);
688 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
689 const std::string& name)
696 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
703 void MidiModel::DeltaCommand::add(const boost::shared_ptr<Note> note)
705 //cerr << "MEC: apply" << endl;
706 _removed_notes.remove(note);
707 _added_notes.push_back(note);
710 void MidiModel::DeltaCommand::remove(const boost::shared_ptr<Note> note)
712 //cerr << "MEC: remove" << endl;
713 _added_notes.remove(note);
714 _removed_notes.push_back(note);
717 void MidiModel::DeltaCommand::operator()()
719 // This could be made much faster by using a priority_queue for added and
720 // removed notes (or sort here), and doing a single iteration over _model
722 // Need to reset iterator to drop the read lock it holds, or we'll deadlock
723 const bool reset_iter = (_model->_read_iter.locked());
724 const double iter_time = _model->_read_iter->time();
727 _model->_read_iter = _model->end(); // drop read lock
729 assert( ! _model->_read_iter.locked());
731 _model->write_lock();
733 for (std::list< boost::shared_ptr<Note> >::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
734 _model->add_note_unlocked(*i);
736 for (std::list< boost::shared_ptr<Note> >::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
737 _model->remove_note_unlocked(*i);
739 _model->write_unlock();
742 _model->_read_iter = const_iterator(*_model.get(), iter_time);
744 _model->ContentsChanged(); /* EMIT SIGNAL */
747 void MidiModel::DeltaCommand::undo()
749 // This could be made much faster by using a priority_queue for added and
750 // removed notes (or sort here), and doing a single iteration over _model
752 // Need to reset iterator to drop the read lock it holds, or we'll deadlock
753 const bool reset_iter = (_model->_read_iter.locked());
754 const double iter_time = _model->_read_iter->time();
757 _model->_read_iter = _model->end(); // drop read lock
759 assert( ! _model->_read_iter.locked());
761 _model->write_lock();
763 for (std::list< boost::shared_ptr<Note> >::iterator i = _added_notes.begin(); i
764 != _added_notes.end(); ++i)
765 _model->remove_note_unlocked(*i);
767 for (std::list< boost::shared_ptr<Note> >::iterator i =
768 _removed_notes.begin(); i != _removed_notes.end(); ++i)
769 _model->add_note_unlocked(*i);
771 _model->write_unlock();
774 _model->_read_iter = const_iterator(*_model.get(), iter_time);
776 _model->ContentsChanged(); /* EMIT SIGNAL */
779 XMLNode & MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr<Note> note)
781 XMLNode *xml_note = new XMLNode("note");
782 ostringstream note_str(ios::ate);
783 note_str << int(note->note());
784 xml_note->add_property("note", note_str.str());
786 ostringstream channel_str(ios::ate);
787 channel_str << int(note->channel());
788 xml_note->add_property("channel", channel_str.str());
790 ostringstream time_str(ios::ate);
791 time_str << int(note->time());
792 xml_note->add_property("time", time_str.str());
794 ostringstream duration_str(ios::ate);
795 duration_str <<(unsigned int) note->duration();
796 xml_note->add_property("duration", duration_str.str());
798 ostringstream velocity_str(ios::ate);
799 velocity_str << (unsigned int) note->velocity();
800 xml_note->add_property("velocity", velocity_str.str());
805 boost::shared_ptr<Note> MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
808 istringstream note_str(xml_note->property("note")->value());
811 unsigned int channel;
812 istringstream channel_str(xml_note->property("channel")->value());
813 channel_str >> channel;
816 istringstream time_str(xml_note->property("time")->value());
819 unsigned int duration;
820 istringstream duration_str(xml_note->property("duration")->value());
821 duration_str >> duration;
823 unsigned int velocity;
824 istringstream velocity_str(xml_note->property("velocity")->value());
825 velocity_str >> velocity;
827 boost::shared_ptr<Note> note_ptr(new Note(channel, time, duration, note, velocity));
831 #define ADDED_NOTES_ELEMENT "added_notes"
832 #define REMOVED_NOTES_ELEMENT "removed_notes"
833 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
835 int MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
837 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
841 _added_notes.clear();
842 XMLNode *added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
843 XMLNodeList notes = added_notes->children();
844 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
845 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
847 _removed_notes.clear();
848 XMLNode *removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
849 notes = removed_notes->children();
850 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
851 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
856 XMLNode& MidiModel::DeltaCommand::get_state()
858 XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
859 delta_command->add_property("midi_source", _model->midi_source()->id().to_s());
861 XMLNode *added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
862 for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
863 sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
864 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
866 XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
867 for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
868 sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
869 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
871 return *delta_command;
874 struct EventTimeComparator {
875 typedef const MIDI::Event* value_type;
876 inline bool operator()(const MIDI::Event* a, const MIDI::Event* b) const {
877 return a->time() >= b->time();
881 /** Write the model to a MidiSource (i.e. save the model).
882 * This is different from manually using read to write to a source in that
883 * note off events are written regardless of the track mode. This is so the
884 * user can switch a recorded track (with note durations from some instrument)
885 * to percussive, save, reload, then switch it back to sustained without
886 * destroying the original note durations.
888 bool MidiModel::write_to(boost::shared_ptr<MidiSource> source)
892 const NoteMode old_note_mode = _note_mode;
893 _note_mode = Sustained;
895 for (const_iterator i = begin(); i != end(); ++i)
896 source->append_event_unlocked(Frames, *i);
898 _note_mode = old_note_mode;
906 XMLNode& MidiModel::get_state()
908 XMLNode *node = new XMLNode("MidiModel");