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;
73 _note_iter = model.notes().end();
74 // find first note which begins after t
75 for (MidiModel::Notes::const_iterator i = model.notes().begin(); i != model.notes().end(); ++i) {
76 if ((*i)->time() >= t) {
82 MidiControlIterator earliest_control(boost::shared_ptr<AutomationList>(), DBL_MAX, 0.0);
84 _control_iters.reserve(model.controls().size());
86 // find the earliest control event available
87 for (Automatable::Controls::const_iterator i = model.controls().begin();
88 i != model.controls().end(); ++i) {
91 i->first.type() == MidiCCAutomation ||
92 i->first.type() == MidiPgmChangeAutomation ||
93 i->first.type() == MidiPitchBenderAutomation ||
94 i->first.type() == MidiChannelAftertouchAutomation);
97 bool ret = i->second->list()->rt_safe_earliest_event_unlocked(t, DBL_MAX, x, y);
99 //cerr << "MIDI Iterator: CC " << i->first.id() << " (size " << i->second->list()->size()
100 // << ") has no events past " << t << endl;
106 if (y < i->first.min() || y > i->first.max()) {
107 cerr << "ERROR: Controller (" << i->first.to_string() << ") value '" << y
108 << "' out of range [" << i->first.min() << "," << i->first.max()
109 << "], event ignored" << endl;
113 const MidiControlIterator new_iter(i->second->list(), x, y);
115 //cerr << "MIDI Iterator: CC " << i->first.id() << " added (" << x << ", " << y << ")" << endl;
116 _control_iters.push_back(new_iter);
118 // if the x of the current control is less than earliest_control
119 // we have a new earliest_control
120 if (x < earliest_control.x) {
121 earliest_control = new_iter;
122 _control_iter = _control_iters.end();
124 // now _control_iter points to the last Element in _control_iters
128 if (_note_iter != model.notes().end()) {
129 _event = boost::shared_ptr<MIDI::Event>(new MIDI::Event((*_note_iter)->on_event(), true));
132 double time = DBL_MAX;
133 // in case we have no notes in the region, we still want to get controller messages
135 time = _event->time();
136 // if the note is going to make it this turn, advance _note_iter
137 if (earliest_control.x > time) {
138 _active_notes.push(*_note_iter);
143 // <=, because we probably would want to send control events first
144 if (earliest_control.automation_list.get() && earliest_control.x <= time) {
145 model.control_to_midi_event(_event, earliest_control);
147 _control_iter = _control_iters.end();
150 if ( (! _event.get()) || _event->size() == 0) {
151 //cerr << "Created MIDI iterator @ " << t << " is at end." << endl;
154 // eliminate possible race condition here (ugly)
155 static Glib::Mutex mutex;
156 Glib::Mutex::Lock lock(mutex);
158 _model->read_unlock();
162 //printf("New MIDI Iterator = %X @ %lf\n", _event->type(), _event->time());
165 assert(_is_end || (_event->buffer() && _event->buffer()[0] != '\0'));
168 MidiModel::const_iterator::~const_iterator()
171 _model->read_unlock();
175 const MidiModel::const_iterator& MidiModel::const_iterator::operator++()
178 throw std::logic_error("Attempt to iterate past end of MidiModel");
181 assert(_event->buffer() && _event->buffer()[0] != '\0');
183 /*cerr << "const_iterator::operator++: _event type:" << hex << "0x" << int(_event->type())
184 << " buffer: 0x" << int(_event->buffer()[0]) << " 0x" << int(_event->buffer()[1])
185 << " 0x" << int(_event->buffer()[2]) << endl;*/
187 if (! (_event->is_note() || _event->is_cc() || _event->is_pgm_change() || _event->is_pitch_bender() || _event->is_channel_aftertouch()) ) {
188 cerr << "FAILED event buffer: " << hex << int(_event->buffer()[0]) << int(_event->buffer()[1]) << int(_event->buffer()[2]) << endl;
190 assert((_event->is_note() || _event->is_cc() || _event->is_pgm_change() || _event->is_pitch_bender() || _event->is_channel_aftertouch()));
192 // Increment past current control event
193 if (!_event->is_note() && _control_iter != _control_iters.end() && _control_iter->automation_list.get()) {
194 double x = 0.0, y = 0.0;
195 const bool ret = _control_iter->automation_list->rt_safe_earliest_event_unlocked(
196 _control_iter->x, DBL_MAX, x, y, false);
199 _control_iter->x = x;
200 _control_iter->y = y;
202 _control_iter->automation_list.reset();
203 _control_iter->x = DBL_MAX;
207 const std::vector<MidiControlIterator>::iterator old_control_iter = _control_iter;
208 _control_iter = _control_iters.begin();
210 // find the _control_iter with the earliest event time
211 for (std::vector<MidiControlIterator>::iterator i = _control_iters.begin();
212 i != _control_iters.end(); ++i) {
213 if (i->x < _control_iter->x) {
218 enum Type {NIL, NOTE_ON, NOTE_OFF, AUTOMATION};
223 // Next earliest note on
224 if (_note_iter != _model->notes().end()) {
226 t = (*_note_iter)->time();
229 // Use the next earliest note off iff it's earlier than the note on
230 if (_model->note_mode() == Sustained && (! _active_notes.empty())) {
231 if (type == NIL || _active_notes.top()->end_time() <= (*_note_iter)->time()) {
233 t = _active_notes.top()->end_time();
237 // Use the next earliest controller iff it's earlier than the note event
238 if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX /*&& _control_iter != old_control_iter */) {
239 if (type == NIL || _control_iter->x < t) {
244 if (type == NOTE_ON) {
245 //cerr << "********** MIDI Iterator = note on" << endl;
246 *_event = (*_note_iter)->on_event();
247 _active_notes.push(*_note_iter);
249 } else if (type == NOTE_OFF) {
250 //cerr << "********** MIDI Iterator = note off" << endl;
251 *_event = _active_notes.top()->off_event();
253 } else if (type == AUTOMATION) {
254 //cerr << "********** MIDI Iterator = Automation" << endl;
255 _model->control_to_midi_event(_event, *_control_iter);
257 //cerr << "********** MIDI Iterator = End" << endl;
261 assert(_is_end || _event->size() > 0);
266 bool MidiModel::const_iterator::operator==(const const_iterator& other) const
268 if (_is_end || other._is_end) {
269 return (_is_end == other._is_end);
271 return (_event == other._event);
275 MidiModel::const_iterator& MidiModel::const_iterator::operator=(const const_iterator& other)
277 if (_locked && _model != other._model) {
278 _model->read_unlock();
281 _model = other._model;
282 _active_notes = other._active_notes;
283 _is_end = other._is_end;
284 _locked = other._locked;
285 _note_iter = other._note_iter;
286 _control_iters = other._control_iters;
287 size_t index = other._control_iter - other._control_iters.begin();
288 _control_iter = _control_iters.begin() + index;
291 _event = boost::shared_ptr<MIDI::Event>(new MIDI::Event(*other._event, true));
299 MidiModel::MidiModel(MidiSource *s, size_t size)
300 : Automatable(s->session(), "midi model")
302 , _note_mode(Sustained)
305 , _end_iter(*this, DBL_MAX)
306 , _next_read(UINT32_MAX)
307 , _read_iter(*this, DBL_MAX)
310 assert(_end_iter._is_end);
311 assert( ! _end_iter._locked);
314 /** Read events in frame range \a start .. \a start+cnt into \a dst,
315 * adding \a stamp_offset to each event's timestamp.
316 * \return number of events written to \a dst
318 size_t MidiModel::read(MidiRingBuffer& dst, nframes_t start, nframes_t nframes,
319 nframes_t stamp_offset, nframes_t negative_stamp_offset) const
321 //cerr << this << " MM::read @ " << start << " frames: " << nframes << " -> " << stamp_offset << endl;
322 //cerr << this << " MM # notes: " << n_notes() << endl;
324 size_t read_events = 0;
326 if (start != _next_read) {
327 _read_iter = const_iterator(*this, (double)start);
328 //cerr << "Repositioning iterator from " << _next_read << " to " << start << endl;
330 //cerr << "Using cached iterator at " << _next_read << endl;
333 _next_read = start + nframes;
335 while (_read_iter != end() && _read_iter->time() < start + nframes) {
336 assert(_read_iter->size() > 0);
337 assert(_read_iter->buffer());
338 dst.write(_read_iter->time() + stamp_offset - negative_stamp_offset,
340 _read_iter->buffer());
342 /*cerr << this << " MidiModel::read event @ " << _read_iter->time()
343 << " type: " << hex << int(_read_iter->type()) << dec
344 << " note: " << int(_read_iter->note())
345 << " velocity: " << int(_read_iter->velocity())
355 /** Write the controller event pointed to by \a iter to \a ev.
356 * The buffer of \a ev will be allocated or resized as necessary.
357 * \return true on success
360 MidiModel::control_to_midi_event(boost::shared_ptr<MIDI::Event>& ev, const MidiControlIterator& iter) const
362 assert(iter.automation_list.get());
364 ev = boost::shared_ptr<MIDI::Event>(new MIDI::Event(0, 3, NULL, true));
367 switch (iter.automation_list->parameter().type()) {
368 case MidiCCAutomation:
369 assert(iter.automation_list.get());
370 assert(iter.automation_list->parameter().channel() < 16);
371 assert(iter.automation_list->parameter().id() <= INT8_MAX);
372 assert(iter.y <= INT8_MAX);
376 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.automation_list->parameter().channel();
377 ev->buffer()[1] = (Byte)iter.automation_list->parameter().id();
378 ev->buffer()[2] = (Byte)iter.y;
381 case MidiPgmChangeAutomation:
382 assert(iter.automation_list.get());
383 assert(iter.automation_list->parameter().channel() < 16);
384 assert(iter.automation_list->parameter().id() == 0);
385 assert(iter.y <= INT8_MAX);
389 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.automation_list->parameter().channel();
390 ev->buffer()[1] = (Byte)iter.y;
393 case MidiPitchBenderAutomation:
394 assert(iter.automation_list.get());
395 assert(iter.automation_list->parameter().channel() < 16);
396 assert(iter.automation_list->parameter().id() == 0);
397 assert(iter.y < (1<<14));
401 ev->buffer()[0] = MIDI_CMD_BENDER + iter.automation_list->parameter().channel();
402 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
403 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
406 case MidiChannelAftertouchAutomation:
407 assert(iter.automation_list.get());
408 assert(iter.automation_list->parameter().channel() < 16);
409 assert(iter.automation_list->parameter().id() == 0);
410 assert(iter.y <= INT8_MAX);
415 = MIDI_CMD_CHANNEL_PRESSURE + iter.automation_list->parameter().channel();
416 ev->buffer()[1] = (Byte)iter.y;
427 /** Clear all events from the model.
429 void MidiModel::clear()
436 _lock.writer_unlock();
440 /** Begin a write of events to the model.
442 * If \a mode is Sustained, complete notes with duration are constructed as note
443 * on/off events are received. Otherwise (Percussive), only note on events are
444 * stored; note off events are discarded entirely and all contained notes will
447 void MidiModel::start_write()
449 //cerr << "MM " << this << " START WRITE, MODE = " << enum_2_string(_note_mode) << endl;
452 for (int i = 0; i < 16; ++i)
453 _write_notes[i].clear();
455 _dirty_automations.clear();
459 /** Finish a write of events to the model.
461 * If \a delete_stuck is true and the current mode is Sustained, note on events
462 * that were never resolved with a corresonding note off will be deleted.
463 * Otherwise they will remain as notes with duration 0.
465 void MidiModel::end_write(bool delete_stuck)
470 //cerr << "MM " << this << " END WRITE: " << _notes.size() << " NOTES\n";
472 if (_note_mode == Sustained && delete_stuck) {
473 for (Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
474 if ((*n)->duration() == 0) {
475 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
477 // we have to break here because erase invalidates the iterator
485 for (int i = 0; i < 16; ++i) {
486 if (!_write_notes[i].empty()) {
487 cerr << "WARNING: MidiModel::end_write: Channel " << i << " has "
488 << _write_notes[i].size() << " stuck notes" << endl;
490 _write_notes[i].clear();
493 for (AutomationLists::const_iterator i = _dirty_automations.begin(); i != _dirty_automations.end(); ++i) {
495 (*i)->lookup_cache().left = -1;
496 (*i)->search_cache().left = -1;
503 /** Append \a in_event to model. NOT realtime safe.
505 * Timestamps of events in \a buf are expected to be relative to
506 * the start of this model (t=0) and MUST be monotonically increasing
507 * and MUST be >= the latest event currently in the model.
509 void MidiModel::append(const MIDI::Event& ev)
514 assert(_notes.empty() || ev.time() >= _notes.back()->time());
517 if (ev.is_note_on()) {
518 append_note_on_unlocked(ev.channel(), ev.time(), ev.note(),
520 } else if (ev.is_note_off()) {
521 append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
522 } else if (ev.is_cc()) {
523 append_automation_event_unlocked(MidiCCAutomation, ev.channel(),
524 ev.time(), ev.cc_number(), ev.cc_value());
525 } else if (ev.is_pgm_change()) {
526 append_automation_event_unlocked(MidiPgmChangeAutomation, ev.channel(),
527 ev.time(), ev.pgm_number(), 0);
528 } else if (ev.is_pitch_bender()) {
529 append_automation_event_unlocked(MidiPitchBenderAutomation,
530 ev.channel(), ev.time(), ev.pitch_bender_lsb(),
531 ev.pitch_bender_msb());
532 } else if (ev.is_channel_aftertouch()) {
533 append_automation_event_unlocked(MidiChannelAftertouchAutomation,
534 ev.channel(), ev.time(), ev.channel_aftertouch(), 0);
536 printf("WARNING: MidiModel: Unknown event type %X\n", ev.type());
542 void MidiModel::append_note_on_unlocked(uint8_t chan, double time,
543 uint8_t note_num, uint8_t velocity)
545 /*cerr << "MidiModel " << this << " chan " << (int)chan <<
546 " note " << (int)note_num << " on @ " << time << endl;*/
548 assert(note_num <= 127);
553 boost::shared_ptr<Note> new_note(new Note(chan, time, 0, note_num, velocity));
554 _notes.push_back(new_note);
555 if (_note_mode == Sustained) {
556 //cerr << "MM Sustained: Appending active note on " << (unsigned)(uint8_t)note_num << endl;
557 _write_notes[chan].push_back(_notes.size() - 1);
559 cerr << "MM Percussive: NOT appending active note on" << endl;
563 void MidiModel::append_note_off_unlocked(uint8_t chan, double time,
566 /*cerr << "MidiModel " << this << " chan " << (int)chan <<
567 " note " << (int)note_num << " off @ " << time << endl;*/
569 assert(note_num <= 127);
574 if (_note_mode == Percussive) {
575 cerr << "MidiModel Ignoring note off (percussive mode)" << endl;
579 /* FIXME: make _write_notes fixed size (127 noted) for speed */
581 /* FIXME: note off velocity for that one guy out there who actually has
582 * keys that send it */
584 bool resolved = false;
586 for (WriteNotes::iterator n = _write_notes[chan].begin(); n
587 != _write_notes[chan].end(); ++n) {
588 Note& note = *_notes[*n].get();
589 if (note.note() == note_num) {
590 assert(time >= note.time());
591 note.set_duration(time - note.time());
592 _write_notes[chan].erase(n);
593 //cerr << "MM resolved note, duration: " << note.duration() << endl;
600 cerr << "MidiModel " << this << " spurious note off chan " << (int)chan
601 << ", note " << (int)note_num << " @ " << time << endl;
605 void MidiModel::append_automation_event_unlocked(AutomationType type,
606 uint8_t chan, double time, uint8_t first_byte, uint8_t second_byte)
608 //cerr << "MidiModel " << this << " chan " << (int)chan <<
609 // " CC " << (int)number << " = " << (int)value << " @ " << time << endl;
619 case MidiCCAutomation:
621 value = double(second_byte);
623 case MidiChannelAftertouchAutomation:
624 case MidiPgmChangeAutomation:
626 value = double(first_byte);
628 case MidiPitchBenderAutomation:
630 value = double((0x7F & second_byte) << 7 | (0x7F & first_byte));
636 Parameter param(type, id, chan);
637 boost::shared_ptr<AutomationControl> control = Automatable::control(param, true);
638 control->list()->rt_add(time, value);
641 void MidiModel::add_note_unlocked(const boost::shared_ptr<Note> note)
643 //cerr << "MidiModel " << this << " add note " << (int)note.note() << " @ " << note.time() << endl;
645 Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note,
646 note_time_comparator);
647 _notes.insert(i, note);
650 void MidiModel::remove_note_unlocked(const boost::shared_ptr<const Note> note)
653 //cerr << "MidiModel " << this << " remove note " << (int)note.note() << " @ " << note.time() << endl;
654 for (Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
656 const Note& _note = *note;
657 // TODO: There is still the issue, that after restarting ardour
658 // persisted undo does not work, because of rounding errors in the
659 // event times after saving/restoring to/from MIDI files
660 /*cerr << "======================================= " << endl;
661 cerr << int(_n.note()) << "@" << int(_n.time()) << "[" << int(_n.channel()) << "] --" << int(_n.duration()) << "-- #" << int(_n.velocity()) << endl;
662 cerr << int(_note.note()) << "@" << int(_note.time()) << "[" << int(_note.channel()) << "] --" << int(_note.duration()) << "-- #" << int(_note.velocity()) << endl;
663 cerr << "Equal: " << bool(_n == _note) << endl;
664 cerr << endl << endl;*/
667 // we have to break here, because erase invalidates all iterators, ie. n itself
673 /** Slow! for debugging only. */
675 bool MidiModel::is_sorted() const {
677 for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n)
678 if ((*n)->time() < t)
687 /** Start a new command.
689 * This has no side-effects on the model or Session, the returned command
690 * can be held on to for as long as the caller wishes, or discarded without
691 * formality, until apply_command is called and ownership is taken.
693 MidiModel::DeltaCommand* MidiModel::new_delta_command(const string name)
695 DeltaCommand* cmd = new DeltaCommand(_midi_source->model(), name);
701 * Ownership of cmd is taken, it must not be deleted by the caller.
702 * The command will constitute one item on the undo stack.
704 void MidiModel::apply_command(Command* cmd)
706 _session.begin_reversible_command(cmd->name());
709 _session.commit_reversible_command(cmd);
715 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
716 const std::string& name)
723 MidiModel::DeltaCommand::DeltaCommand(boost::shared_ptr<MidiModel> m,
730 void MidiModel::DeltaCommand::add(const boost::shared_ptr<Note> note)
732 //cerr << "MEC: apply" << endl;
733 _removed_notes.remove(note);
734 _added_notes.push_back(note);
737 void MidiModel::DeltaCommand::remove(const boost::shared_ptr<Note> note)
739 //cerr << "MEC: remove" << endl;
740 _added_notes.remove(note);
741 _removed_notes.push_back(note);
744 void MidiModel::DeltaCommand::operator()()
746 // This could be made much faster by using a priority_queue for added and
747 // removed notes (or sort here), and doing a single iteration over _model
749 // Need to reset iterator to drop the read lock it holds, or we'll deadlock
750 const bool reset_iter = (_model->_read_iter.locked());
751 double iter_time = -1.0;
754 if (_model->_read_iter.get_event_pointer().get()) {
755 iter_time = _model->_read_iter->time();
757 cerr << "MidiModel::DeltaCommand::operator(): WARNING: _read_iter points to no event" << endl;
759 _model->_read_iter = _model->end(); // drop read lock
762 assert( ! _model->_read_iter.locked());
764 _model->write_lock();
766 for (std::list< boost::shared_ptr<Note> >::iterator i = _added_notes.begin(); i != _added_notes.end(); ++i)
767 _model->add_note_unlocked(*i);
769 for (std::list< boost::shared_ptr<Note> >::iterator i = _removed_notes.begin(); i != _removed_notes.end(); ++i)
770 _model->remove_note_unlocked(*i);
772 _model->write_unlock();
774 if (reset_iter && iter_time != -1.0) {
775 _model->_read_iter = const_iterator(*_model.get(), iter_time);
778 _model->ContentsChanged(); /* EMIT SIGNAL */
781 void MidiModel::DeltaCommand::undo()
783 // This could be made much faster by using a priority_queue for added and
784 // removed notes (or sort here), and doing a single iteration over _model
786 // Need to reset iterator to drop the read lock it holds, or we'll deadlock
787 const bool reset_iter = (_model->_read_iter.locked());
788 double iter_time = -1.0;
791 if (_model->_read_iter.get_event_pointer().get()) {
792 iter_time = _model->_read_iter->time();
794 cerr << "MidiModel::DeltaCommand::undo(): WARNING: _read_iter points to no event" << endl;
796 _model->_read_iter = _model->end(); // drop read lock
799 assert( ! _model->_read_iter.locked());
801 _model->write_lock();
803 for (std::list< boost::shared_ptr<Note> >::iterator i = _added_notes.begin(); i
804 != _added_notes.end(); ++i)
805 _model->remove_note_unlocked(*i);
807 for (std::list< boost::shared_ptr<Note> >::iterator i =
808 _removed_notes.begin(); i != _removed_notes.end(); ++i)
809 _model->add_note_unlocked(*i);
811 _model->write_unlock();
813 if (reset_iter && iter_time != -1.0) {
814 _model->_read_iter = const_iterator(*_model.get(), iter_time);
817 _model->ContentsChanged(); /* EMIT SIGNAL */
820 XMLNode & MidiModel::DeltaCommand::marshal_note(const boost::shared_ptr<Note> note)
822 XMLNode *xml_note = new XMLNode("note");
823 ostringstream note_str(ios::ate);
824 note_str << int(note->note());
825 xml_note->add_property("note", note_str.str());
827 ostringstream channel_str(ios::ate);
828 channel_str << int(note->channel());
829 xml_note->add_property("channel", channel_str.str());
831 ostringstream time_str(ios::ate);
832 time_str << int(note->time());
833 xml_note->add_property("time", time_str.str());
835 ostringstream duration_str(ios::ate);
836 duration_str <<(unsigned int) note->duration();
837 xml_note->add_property("duration", duration_str.str());
839 ostringstream velocity_str(ios::ate);
840 velocity_str << (unsigned int) note->velocity();
841 xml_note->add_property("velocity", velocity_str.str());
846 boost::shared_ptr<Note> MidiModel::DeltaCommand::unmarshal_note(XMLNode *xml_note)
849 istringstream note_str(xml_note->property("note")->value());
852 unsigned int channel;
853 istringstream channel_str(xml_note->property("channel")->value());
854 channel_str >> channel;
857 istringstream time_str(xml_note->property("time")->value());
860 unsigned int duration;
861 istringstream duration_str(xml_note->property("duration")->value());
862 duration_str >> duration;
864 unsigned int velocity;
865 istringstream velocity_str(xml_note->property("velocity")->value());
866 velocity_str >> velocity;
868 boost::shared_ptr<Note> note_ptr(new Note(channel, time, duration, note, velocity));
872 #define ADDED_NOTES_ELEMENT "added_notes"
873 #define REMOVED_NOTES_ELEMENT "removed_notes"
874 #define DELTA_COMMAND_ELEMENT "DeltaCommand"
876 int MidiModel::DeltaCommand::set_state(const XMLNode& delta_command)
878 if (delta_command.name() != string(DELTA_COMMAND_ELEMENT)) {
882 _added_notes.clear();
883 XMLNode *added_notes = delta_command.child(ADDED_NOTES_ELEMENT);
884 XMLNodeList notes = added_notes->children();
885 transform(notes.begin(), notes.end(), back_inserter(_added_notes),
886 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
888 _removed_notes.clear();
889 XMLNode *removed_notes = delta_command.child(REMOVED_NOTES_ELEMENT);
890 notes = removed_notes->children();
891 transform(notes.begin(), notes.end(), back_inserter(_removed_notes),
892 sigc::mem_fun(*this, &DeltaCommand::unmarshal_note));
897 XMLNode& MidiModel::DeltaCommand::get_state()
899 XMLNode *delta_command = new XMLNode(DELTA_COMMAND_ELEMENT);
900 delta_command->add_property("midi_source", _model->midi_source()->id().to_s());
902 XMLNode *added_notes = delta_command->add_child(ADDED_NOTES_ELEMENT);
903 for_each(_added_notes.begin(), _added_notes.end(), sigc::compose(
904 sigc::mem_fun(*added_notes, &XMLNode::add_child_nocopy),
905 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
907 XMLNode *removed_notes = delta_command->add_child(REMOVED_NOTES_ELEMENT);
908 for_each(_removed_notes.begin(), _removed_notes.end(), sigc::compose(
909 sigc::mem_fun(*removed_notes, &XMLNode::add_child_nocopy),
910 sigc::mem_fun(*this, &DeltaCommand::marshal_note)));
912 return *delta_command;
915 struct EventTimeComparator {
916 typedef const MIDI::Event* value_type;
917 inline bool operator()(const MIDI::Event& a, const MIDI::Event& b) const {
918 return a.time() >= b.time();
922 /** Write the model to a MidiSource (i.e. save the model).
923 * This is different from manually using read to write to a source in that
924 * note off events are written regardless of the track mode. This is so the
925 * user can switch a recorded track (with note durations from some instrument)
926 * to percussive, save, reload, then switch it back to sustained without
927 * destroying the original note durations.
929 bool MidiModel::write_to(boost::shared_ptr<MidiSource> source)
933 const NoteMode old_note_mode = _note_mode;
934 _note_mode = Sustained;
936 for (const_iterator i = begin(); i != end(); ++i) {
937 source->append_event_unlocked(Frames, *i);
940 _note_mode = old_note_mode;
948 XMLNode& MidiModel::get_state()
950 XMLNode *node = new XMLNode("MidiModel");