Fix failed assertion when end_write is called multiple times.
[ardour.git] / libs / evoral / src / Sequence.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #define __STDC_LIMIT_MACROS 1
20 #include <algorithm>
21 #include <cmath>
22 #include <iostream>
23 #include <limits>
24 #include <stdexcept>
25 #include <stdint.h>
26 #include <cstdio>
27 #include "evoral/Control.hpp"
28 #include "evoral/ControlList.hpp"
29 #include "evoral/ControlSet.hpp"
30 #include "evoral/EventSink.hpp"
31 #include "evoral/MIDIParameters.hpp"
32 #include "evoral/Sequence.hpp"
33 #include "evoral/TypeMap.hpp"
34 #include "evoral/midi_util.h"
35
36 // #define DEBUG_SEQUENCE 1
37 #ifdef DEBUG_SEQUENCE
38         #include <boost/format.hpp>
39         using boost::format;
40         #define DUMP(x) cerr << (x);
41 #else
42         #define DUMP(x)
43 #endif
44
45 using namespace std;
46
47 namespace Evoral {
48
49 template<typename Time>
50 void Sequence<Time>::write_lock() {
51         _lock.writer_lock();
52         _control_lock.lock();
53 }
54
55 template<typename Time>
56 void Sequence<Time>::write_unlock() {
57         _lock.writer_unlock();
58         _control_lock.unlock();
59 }
60
61 template<typename Time>
62 void Sequence<Time>::read_lock() const {
63         _lock.reader_lock();
64 }
65
66 template<typename Time>
67 void Sequence<Time>::read_unlock() const {
68         _lock.reader_unlock();
69 }
70
71
72 // Read iterator (const_iterator)
73
74 template<typename Time>
75 Sequence<Time>::const_iterator::const_iterator()
76         : _seq(NULL)
77         , _is_end(true)
78         , _locked(false)
79         , _control_iter(_control_iters.end())
80 {
81         _event = boost::shared_ptr< Event<Time> >(new Event<Time>());
82 }
83
84 template<typename Time>
85 Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq, Time t)
86         : _seq(&seq)
87         , _type(NIL)
88         , _is_end((t == DBL_MAX) || seq.empty())
89         , _locked(!_is_end)
90         , _note_iter(seq.notes().end())
91         , _sysex_iter(seq.sysexes().end())
92         , _control_iter(_control_iters.end())
93 {
94         DUMP(format("Created Iterator @ %1% (is end: %2%)\n)") % t % _is_end);
95
96         if (_is_end) {
97                 return;
98         }
99
100         seq.read_lock();
101
102         // Find first note which begins at or after t
103         _note_iter = seq.note_lower_bound(t);
104
105         // Find first sysex event at or after t
106         for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
107                         i != seq.sysexes().end(); ++i) {
108                 if ((*i)->time() >= t) {
109                         _sysex_iter = i;
110                         break;
111                 }
112         }
113         assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
114
115         // Find first control event after t
116         ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
117         _control_iters.reserve(seq._controls.size());
118         bool   found                  = false;
119         size_t earliest_control_index = 0;
120         for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
121                 DUMP(format("Iterator: control: %1%\n") % seq._type_map.to_symbol(i->first));
122                 double x, y;
123                 bool ret = i->second->list()->rt_safe_earliest_event_unlocked(t, DBL_MAX, x, y);
124                 if (!ret) {
125                         DUMP(format("Iterator: CC %1% (size %2%) has no events past %3%\n")
126                                         % i->first.id() % i->second->list()->size() % t);
127                         continue;
128                 }
129
130                 assert(x >= 0);
131
132                 if (y < i->first.min() || y > i->first.max()) {
133                         cerr << "ERROR: Controller value " << y
134                                 << " out of range [" << i->first.min() << "," << i->first.max()
135                                 << "], event ignored" << endl;
136                         continue;
137                 }
138
139                 DUMP(format("Iterator: CC %1% added (%2%, %3%)\n") % i->first.id() % x % y);
140
141                 const ControlIterator new_iter(i->second->list(), x, y);
142                 _control_iters.push_back(new_iter);
143
144                 // Found a new earliest_control
145                 if (x < earliest_control.x) {
146                         earliest_control = new_iter;
147                         earliest_control_index = _control_iters.size() - 1;
148                         found = true;
149                 }
150         }
151
152         if (found) {
153                 _control_iter = _control_iters.begin() + earliest_control_index;
154         } else {
155                 _control_iter = _control_iters.end();
156         }
157
158         // Now find the earliest event overall and point to it
159         Time earliest_t = t;
160
161         if (_note_iter != seq.notes().end()) {
162                 _type = NOTE_ON;
163                 earliest_t = (*_note_iter)->time();
164         }
165
166         if (_sysex_iter != seq.sysexes().end() && (*_sysex_iter)->time() < earliest_t) {
167                 _type = SYSEX;
168                 earliest_t = (*_sysex_iter)->time();
169         }
170
171         if (_control_iter != _control_iters.end()
172                         && earliest_control.list && earliest_control.x >= t
173                         && earliest_control.x < earliest_t) {
174                 _type = CONTROL;
175                 earliest_t = earliest_control.x;
176         }
177
178         switch (_type) {
179         case NOTE_ON:
180                 DUMP(format("Starting at note on event @ %1%\n") % earliest_t);
181                 _event = boost::shared_ptr< Event<Time> >(
182                                 new Event<Time>((*_note_iter)->on_event(), true));
183                 _active_notes.push(*_note_iter);
184                 break;
185         case SYSEX:
186                 DUMP(format("Starting at sysex event @ %1%\n") % earliest_t);
187                 _event = boost::shared_ptr< Event<Time> >(
188                                 new Event<Time>(*(*_sysex_iter), true));
189                 break;
190         case CONTROL:
191                 DUMP(format("Starting at control event @ %1%\n") % earliest_t);
192                 seq.control_to_midi_event(_event, earliest_control);
193                 break;
194         default:
195                 break;
196         }
197
198         if (_type == NIL || !_event || _event->size() == 0) {
199                 DUMP(format("Starting at end @ %1%\n") % t);
200                 _type   = NIL;
201                 _is_end = true;
202                 _locked = false;
203                 _seq->read_unlock();
204         } else {
205                 DUMP(printf("New iterator = 0x%x : 0x%x @ %f\n",
206                             (int)_event->event_type(),
207                             (int)((MIDIEvent<Time>*)_event.get())->type(),
208                             _event->time()));
209                 assert(midi_event_is_valid(_event->buffer(), _event->size()));
210         }
211 }
212
213 template<typename Time>
214 Sequence<Time>::const_iterator::~const_iterator()
215 {
216         if (_locked) {
217                 _seq->read_unlock();
218         }
219 }
220
221 template<typename Time>
222 void
223 Sequence<Time>::const_iterator::invalidate()
224 {
225         while (!_active_notes.empty()) {
226                 _active_notes.pop();
227         }
228         _type = NIL;
229         _is_end = true;
230         if (_seq) {
231                 _note_iter = _seq->notes().end();
232                 _sysex_iter = _seq->sysexes().end();
233         }
234         _control_iter = _control_iters.end();
235         if (_locked) {
236                 _seq->read_unlock();
237                 _locked = false;
238         }
239 }
240
241 template<typename Time>
242 const typename Sequence<Time>::const_iterator&
243 Sequence<Time>::const_iterator::operator++()
244 {
245         if (_is_end) {
246                 throw std::logic_error("Attempt to iterate past end of Sequence");
247         }
248
249         DUMP("Sequence::const_iterator++\n");
250         assert(_event && _event->buffer() && _event->size() > 0);
251
252         const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
253
254         if (!(     ev.is_note()
255                         || ev.is_cc()
256                         || ev.is_pgm_change()
257                         || ev.is_pitch_bender()
258                         || ev.is_channel_pressure()
259                         || ev.is_sysex()) ) {
260                 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
261                         << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
262         }
263
264         double x   = 0.0;
265         double y   = 0.0;
266         bool   ret = false;
267
268         // Increment past current event
269         switch (_type) {
270         case NOTE_ON:
271                 ++_note_iter;
272                 break;
273         case NOTE_OFF:
274                 break;
275         case CONTROL:
276                 // Increment current controller iterator
277                 ret = _control_iter->list->rt_safe_earliest_event_unlocked(
278                                 _control_iter->x, DBL_MAX, x, y, false);
279                 assert(!ret || x > _control_iter->x);
280                 if (ret) {
281                         _control_iter->x = x;
282                         _control_iter->y = y;
283                 } else {
284                         _control_iter->list.reset();
285                         _control_iter->x = DBL_MAX;
286                         _control_iter->y = DBL_MAX;
287                 }
288
289                 // Find the controller with the next earliest event time
290                 _control_iter = _control_iters.begin();
291                 for (ControlIterators::iterator i = _control_iters.begin();
292                                 i != _control_iters.end(); ++i) {
293                         if (i->x < _control_iter->x) {
294                                 _control_iter = i;
295                         }
296                 }
297                 break;
298         case SYSEX:
299                 ++_sysex_iter;
300                 break;
301         default:
302                 assert(false);
303         }
304
305         // Now find the earliest event overall and point to it
306         _type = NIL;
307         Time earliest_t = std::numeric_limits<Time>::max();
308
309         // Next earliest note on
310         if (_note_iter != _seq->notes().end()) {
311                 _type = NOTE_ON;
312                 earliest_t = (*_note_iter)->time();
313         }
314
315         // Use the next note off iff it's earlier or the same time as the note on
316         if (!_seq->percussive() && (!_active_notes.empty())) {
317                 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
318                         _type = NOTE_OFF;
319                         earliest_t = _active_notes.top()->end_time();
320                 }
321         }
322
323         // Use the next earliest controller iff it's earlier than the note event
324         if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
325                 if (_type == NIL || _control_iter->x < earliest_t) {
326                         _type = CONTROL;
327                         earliest_t = _control_iter->x;
328                 }
329         }
330
331         // Use the next earliest SysEx iff it's earlier than the controller
332         if (_sysex_iter != _seq->sysexes().end()) {
333                 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
334                         _type = SYSEX;
335                         earliest_t = (*_sysex_iter)->time();
336                 }
337         }
338
339         // Set event to reflect new position
340         switch (_type) {
341         case NOTE_ON:
342                 DUMP("iterator = note on\n");
343                 *_event = (*_note_iter)->on_event();
344                 _active_notes.push(*_note_iter);
345                 break;
346         case NOTE_OFF:
347                 DUMP("iterator = note off\n");
348                 assert(!_active_notes.empty());
349                 *_event = _active_notes.top()->off_event();
350                 _active_notes.pop();
351                 break;
352         case CONTROL:
353                 DUMP("iterator = control\n");
354                 _seq->control_to_midi_event(_event, *_control_iter);
355                 break;
356         case SYSEX:
357                 DUMP("iterator = sysex\n");
358                 *_event = *(*_sysex_iter);
359                 break;
360         default:
361                 DUMP("iterator = end\n");
362                 _is_end = true;
363         }
364
365         assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
366
367         return *this;
368 }
369
370 template<typename Time>
371 bool
372 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
373 {
374         if (_seq != other._seq) {
375                 return false;
376         } else if (_is_end || other._is_end) {
377                 return (_is_end == other._is_end);
378         } else if (_type != other._type) {
379                 return false;
380         } else {
381                 return (_event == other._event);
382         }
383 }
384
385 template<typename Time>
386 typename Sequence<Time>::const_iterator&
387 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
388 {
389         if (_seq != other._seq) {
390                 if (_locked) {
391                         _seq->read_unlock();
392                 }
393                 if (other._locked) {
394                    other._seq->read_lock();
395                 }
396         } else if (!_locked && other._locked) {
397                 _seq->read_lock();
398         }
399
400         _seq           = other._seq;
401         _event         = other._event;
402         _active_notes  = other._active_notes;
403         _type          = other._type;
404         _is_end        = other._is_end;
405         _locked        = other._locked;
406         _note_iter     = other._note_iter;
407         _sysex_iter    = other._sysex_iter;
408         _control_iters = other._control_iters;
409
410         if (other._control_iter == other._control_iters.end()) {
411                 _control_iter = _control_iters.end();
412         } else {
413                 const size_t index = other._control_iter - other._control_iters.begin();
414                 _control_iter  = _control_iters.begin() + index;
415         }
416
417         return *this;
418 }
419
420 // Sequence
421
422 template<typename Time>
423 Sequence<Time>::Sequence(const TypeMap& type_map)
424         : _edited(false)
425         , _type_map(type_map)
426         , _writing(false)
427         , _end_iter(*this, DBL_MAX)
428         , _percussive(false)
429         , _lowest_note(127)
430         , _highest_note(0)
431 {
432         DUMP(format("Sequence (size %1%) constructed: %2%\n") % size % this);
433         assert(_end_iter._is_end);
434         assert( ! _end_iter._locked);
435 }
436
437 /** Write the controller event pointed to by \a iter to \a ev.
438  * The buffer of \a ev will be allocated or resized as necessary.
439  * The event_type of \a ev should be set to the expected output type.
440  * \return true on success
441  */
442 template<typename Time>
443 bool
444 Sequence<Time>::control_to_midi_event(
445                 boost::shared_ptr< Event<Time> >& ev,
446                 const ControlIterator&            iter) const
447 {
448         assert(iter.list.get());
449         const uint32_t event_type = iter.list->parameter().type();
450
451         // initialize the event pointer with a new event, if necessary
452         if (!ev) {
453                 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
454         }
455
456         uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
457         ev->set_event_type(_type_map.midi_event_type(midi_type));
458         switch (midi_type) {
459         case MIDI_CMD_CONTROL:
460                 assert(iter.list.get());
461                 assert(iter.list->parameter().channel() < 16);
462                 assert(iter.list->parameter().id() <= INT8_MAX);
463                 assert(iter.y <= INT8_MAX);
464
465                 ev->time() = iter.x;
466                 ev->realloc(3);
467                 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
468                 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
469                 ev->buffer()[2] = (uint8_t)iter.y;
470                 break;
471
472         case MIDI_CMD_PGM_CHANGE:
473                 assert(iter.list.get());
474                 assert(iter.list->parameter().channel() < 16);
475                 assert(iter.y <= INT8_MAX);
476
477                 ev->time() = iter.x;
478                 ev->realloc(2);
479                 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
480                 ev->buffer()[1] = (uint8_t)iter.y;
481                 break;
482
483         case MIDI_CMD_BENDER:
484                 assert(iter.list.get());
485                 assert(iter.list->parameter().channel() < 16);
486                 assert(iter.y < (1<<14));
487
488                 ev->time() = iter.x;
489                 ev->realloc(3);
490                 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
491                 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
492                 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
493                 break;
494
495         case MIDI_CMD_CHANNEL_PRESSURE:
496                 assert(iter.list.get());
497                 assert(iter.list->parameter().channel() < 16);
498                 assert(iter.y <= INT8_MAX);
499
500                 ev->time() = iter.x;
501                 ev->realloc(2);
502                 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
503                 ev->buffer()[1] = (uint8_t)iter.y;
504                 break;
505
506         default:
507                 return false;
508         }
509
510         return true;
511 }
512
513 /** Clear all events from the model.
514  */
515 template<typename Time>
516 void
517 Sequence<Time>::clear()
518 {
519         _lock.writer_lock();
520         _notes.clear();
521         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
522                 li->second->list()->clear();
523         _lock.writer_unlock();
524 }
525
526 /** Begin a write of events to the model.
527  *
528  * If \a mode is Sustained, complete notes with length are constructed as note
529  * on/off events are received.  Otherwise (Percussive), only note on events are
530  * stored; note off events are discarded entirely and all contained notes will
531  * have length 0.
532  */
533 template<typename Time>
534 void
535 Sequence<Time>::start_write()
536 {
537         DUMP(format("%1% : start_write (percussive = %2%)\n") % this % _percussive);
538         write_lock();
539         _writing = true;
540         for (int i = 0; i < 16; ++i) {
541                 _write_notes[i].clear();
542         }
543         _dirty_controls.clear();
544         write_unlock();
545 }
546
547 /** Finish a write of events to the model.
548  *
549  * If \a delete_stuck is true and the current mode is Sustained, note on events
550  * that were never resolved with a corresonding note off will be deleted.
551  * Otherwise they will remain as notes with length 0.
552  */
553 template<typename Time>
554 void
555 Sequence<Time>::end_write(bool delete_stuck)
556 {
557         write_lock();
558
559         if (!_writing) {
560                 write_unlock();
561                 return;
562         }
563
564         DUMP(format("%1% : end_write (%2% notes)\n") % this % _notes.size());
565
566         if (!_percussive && delete_stuck) {
567                 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
568                         typename Notes::iterator next = n;
569                         ++next;
570                         if ((*n)->length() == 0) {
571                                 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
572                                 _notes.erase(n);
573                         }
574                         n = next;
575                 }
576         }
577
578         for (int i = 0; i < 16; ++i) {
579                 if (!_write_notes[i].empty()) {
580                         cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
581                                         << _write_notes[i].size() << " stuck notes" << endl;
582                 }
583                 _write_notes[i].clear();
584         }
585
586         for (ControlLists::const_iterator i = _dirty_controls.begin(); i != _dirty_controls.end(); ++i) {
587                 (*i)->mark_dirty();
588         }
589
590         _writing = false;
591         write_unlock();
592 }
593
594 /** Append \a ev to model.  NOT realtime safe.
595  *
596  * Timestamps of events in \a buf are expected to be relative to
597  * the start of this model (t=0) and MUST be monotonically increasing
598  * and MUST be >= the latest event currently in the model.
599  */
600 template<typename Time>
601 void
602 Sequence<Time>::append(const Event<Time>& event)
603 {
604         write_lock();
605         _edited = true;
606
607         const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
608
609         assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
610         assert(_writing);
611
612         if (!midi_event_is_valid(ev.buffer(), ev.size())) {
613                 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
614                 write_unlock();
615                 return;
616         }
617
618         if (ev.is_note_on()) {
619                 append_note_on_unlocked(ev.channel(), ev.time(), ev.note(), ev.velocity());
620         } else if (ev.is_note_off()) {
621                 append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
622         } else if (ev.is_sysex()) {
623                 append_sysex_unlocked(ev);
624         } else if (!_type_map.type_is_midi(ev.event_type())) {
625                 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
626                 for (size_t i=0; i < ev.size(); ++i) {
627                         printf("%X ", ev.buffer()[i]);
628                 }
629                 printf("\n");
630         } else if (ev.is_cc()) {
631                 append_control_unlocked(
632                                 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
633                                 ev.time(), ev.cc_value());
634         } else if (ev.is_pgm_change()) {
635                 append_control_unlocked(
636                                 Evoral::MIDI::ProgramChange(ev.event_type(), ev.channel()),
637                                 ev.time(), ev.pgm_number());
638         } else if (ev.is_pitch_bender()) {
639                 append_control_unlocked(
640                                 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
641                                 ev.time(), double(  (0x7F & ev.pitch_bender_msb()) << 7
642                                         | (0x7F & ev.pitch_bender_lsb()) ));
643         } else if (ev.is_channel_pressure()) {
644                 append_control_unlocked(
645                                 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
646                                 ev.time(), ev.channel_pressure());
647         } else {
648                 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
649         }
650
651         write_unlock();
652 }
653
654 template<typename Time>
655 void
656 Sequence<Time>::append_note_on_unlocked(uint8_t chan, Time time, uint8_t note_num, uint8_t velocity)
657 {
658         DUMP(format("%1% c=%2% note %3% on @ %4% v=%5%\n")
659                         % this % (int)chan % (int)note_num % time % (int)velocity);
660         assert(note_num <= 127);
661         assert(chan < 16);
662         assert(_writing);
663         _edited = true;
664
665         if (velocity == 0) {
666                 append_note_off_unlocked(chan, time, note_num);
667                 return;
668         }
669
670         if (note_num < _lowest_note)
671                 _lowest_note = note_num;
672         if (note_num > _highest_note)
673                 _highest_note = note_num;
674
675         boost::shared_ptr< Note<Time> > new_note(new Note<Time>(chan, time, 0, note_num, velocity));
676         _notes.insert(new_note);
677         if (!_percussive) {
678                 DUMP(format("Sustained: Appending active note on %1% channel %2%\n")
679                                 % (unsigned)(uint8_t)note_num % chan);
680                 _write_notes[chan].insert(new_note);
681         } else {
682                 DUMP("Percussive: NOT appending active note on\n");
683         }
684 }
685
686 template<typename Time>
687 void
688 Sequence<Time>::append_note_off_unlocked(uint8_t chan, Time time, uint8_t note_num)
689 {
690         DUMP(format("%1% c=%2% note %3% off @ %4%\n")
691                         % this % (int)chan % (int)note_num % time);
692         assert(note_num <= 127);
693         assert(chan < 16);
694         assert(_writing);
695         _edited = true;
696
697         if (_percussive) {
698                 DUMP("Sequence Ignoring note off (percussive mode)\n");
699                 return;
700         }
701
702         // TODO: support note off velocity
703
704         bool resolved = false;
705         for (typename WriteNotes::iterator n = _write_notes[chan].begin();
706                         n != _write_notes[chan].end(); ++n) {
707                 boost::shared_ptr< Note<Time> > note = *n;
708                 if (note->note() == note_num) {
709                         assert(time >= note->time());
710                         note->set_length(time - note->time());
711                         _write_notes[chan].erase(n);
712                         DUMP(format("resolved note, length: %1%\n") % note.length());
713                         resolved = true;
714                         break;
715                 }
716         }
717
718         if (!resolved) {
719                 cerr << this << " spurious note off chan " << (int)chan
720                                 << ", note " << (int)note_num << " @ " << time << endl;
721         }
722 }
723
724 template<typename Time>
725 void
726 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value)
727 {
728         DUMP(format("%1% %2% @ %3%\t=\t%4% # controls: %5%\n")
729                         % this % _type_map.to_symbol(param) % time % value % _controls.size());
730         boost::shared_ptr<Control> c = control(param, true);
731         c->list()->rt_add(time, value);
732 }
733
734 template<typename Time>
735 void
736 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev)
737 {
738         #ifdef DEBUG_SEQUENCE
739         cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
740         for (size_t i=0; i < ev.size(); ++i) {
741                 cerr << int(ev.buffer()[i]) << " ";
742         } cerr << "]" << endl;
743         #endif
744
745         boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
746         _sysexes.push_back(event);
747 }
748
749 template<typename Time>
750 void
751 Sequence<Time>::add_note_unlocked(const boost::shared_ptr< Note<Time> > note)
752 {
753         DUMP(format("%1% add note %2% @ %3%\n") % this % (int)note->note() % note->time());
754         _edited = true;
755         typename Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note,
756                         note_time_comparator);
757         _notes.insert(i, note);
758 }
759
760 template<typename Time>
761 void
762 Sequence<Time>::remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note)
763 {
764         _edited = true;
765         DUMP(format("%1% remove note %2% @ %3%\n") % this % (int)note->note() % note->time());
766         for (typename Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
767                 if (*(*n) == *note) {
768                         _notes.erase(n);
769                         break;
770                 }
771         }
772 }
773
774 template<typename Time>
775 void
776 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
777 {
778         _notes = n;
779 }
780
781 /** Return the earliest note with time >= t */
782 template<typename Time>
783 typename Sequence<Time>::Notes::const_iterator
784 Sequence<Time>::note_lower_bound (Time t) const
785 {
786         boost::shared_ptr< Note<Time> > search_note(new Note<Time>(0, t, 0, 0, 0));
787         typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
788         assert(i == _notes.end() || (*i)->time() >= t);
789         return i;
790 }
791
792 template class Sequence<Evoral::MusicalTime>;
793
794 } // namespace Evoral
795