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