3b1729cfe8a19e2b1fd909a4395c8a48d25f5b5d
[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(format("New iterator = %1% : %2% @ %3%\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, size_t size)
431         : _edited(false)
432         , _type_map(type_map)
433         , _notes(size)
434         , _writing(false)
435         , _end_iter(*this, DBL_MAX)
436         , _percussive(false)
437         , _lowest_note(127)
438         , _highest_note(0)
439 {
440         DUMP(format("Sequence (size %1%) constructed: %2%\n") % size % this);
441         assert(_end_iter._is_end);
442         assert( ! _end_iter._locked);
443 }
444
445 /** Write the controller event pointed to by \a iter to \a ev.
446  * The buffer of \a ev will be allocated or resized as necessary.
447  * The event_type of \a ev should be set to the expected output type.
448  * \return true on success
449  */
450 template<typename Time>
451 bool
452 Sequence<Time>::control_to_midi_event(
453                 boost::shared_ptr< Event<Time> >& ev,
454                 const ControlIterator&            iter) const
455 {
456         assert(iter.list.get());
457         const uint32_t event_type = iter.list->parameter().type();
458         
459         // initialize the event pointer with a new event, if necessary
460         if (!ev) {
461                 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
462         }
463         
464         uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
465         ev->set_event_type(_type_map.midi_event_type(midi_type));
466         switch (midi_type) {
467         case MIDI_CMD_CONTROL:
468                 assert(iter.list.get());
469                 assert(iter.list->parameter().channel() < 16);
470                 assert(iter.list->parameter().id() <= INT8_MAX);
471                 assert(iter.y <= INT8_MAX);
472                 
473                 ev->time() = iter.x;
474                 ev->realloc(3);
475                 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
476                 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
477                 ev->buffer()[2] = (uint8_t)iter.y;
478                 break;
479
480         case MIDI_CMD_PGM_CHANGE:
481                 assert(iter.list.get());
482                 assert(iter.list->parameter().channel() < 16);
483                 assert(iter.y <= INT8_MAX);
484                 
485                 ev->time() = iter.x;
486                 ev->realloc(2);
487                 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
488                 ev->buffer()[1] = (uint8_t)iter.y;
489                 break;
490
491         case MIDI_CMD_BENDER:
492                 assert(iter.list.get());
493                 assert(iter.list->parameter().channel() < 16);
494                 assert(iter.y < (1<<14));
495                 
496                 ev->time() = iter.x;
497                 ev->realloc(3);
498                 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
499                 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
500                 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
501                 break;
502
503         case MIDI_CMD_CHANNEL_PRESSURE:
504                 assert(iter.list.get());
505                 assert(iter.list->parameter().channel() < 16);
506                 assert(iter.y <= INT8_MAX);
507
508                 ev->time() = iter.x;
509                 ev->realloc(2);
510                 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
511                 ev->buffer()[1] = (uint8_t)iter.y;
512                 break;
513
514         default:
515                 return false;
516         }
517
518         return true;
519 }
520
521 /** Clear all events from the model.
522  */
523 template<typename Time>
524 void
525 Sequence<Time>::clear()
526 {
527         _lock.writer_lock();
528         _notes.clear();
529         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
530                 li->second->list()->clear();
531         _lock.writer_unlock();
532 }
533
534 /** Begin a write of events to the model.
535  *
536  * If \a mode is Sustained, complete notes with length are constructed as note
537  * on/off events are received.  Otherwise (Percussive), only note on events are
538  * stored; note off events are discarded entirely and all contained notes will
539  * have length 0.
540  */
541 template<typename Time>
542 void
543 Sequence<Time>::start_write()
544 {
545         DUMP(format("%1% : start_write (percussive = %2%)\n") % this % _percussive);
546         write_lock();
547         _writing = true;
548         for (int i = 0; i < 16; ++i) {
549                 _write_notes[i].clear();
550         }
551         _dirty_controls.clear();
552         write_unlock();
553 }
554
555 /** Finish a write of events to the model.
556  *
557  * If \a delete_stuck is true and the current mode is Sustained, note on events
558  * that were never resolved with a corresonding note off will be deleted.
559  * Otherwise they will remain as notes with length 0.
560  */
561 template<typename Time>
562 void
563 Sequence<Time>::end_write(bool delete_stuck)
564 {
565         write_lock();
566         assert(_writing);
567
568         DUMP(format("%1% : end_write (%2% notes)\n") % this % _notes.size());
569
570         if (!_percussive && delete_stuck) {
571                 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
572                         if ((*n)->length() == 0) {
573                                 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
574                                 n = _notes.erase(n);
575                                 // we have to break here because erase invalidates the iterator
576                                 break;
577                         } else {
578                                 ++n;
579                         }
580                 }
581         }
582
583         for (int i = 0; i < 16; ++i) {
584                 if (!_write_notes[i].empty()) {
585                         cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
586                                         << _write_notes[i].size() << " stuck notes" << endl;
587                 }
588                 _write_notes[i].clear();
589         }
590
591         for (ControlLists::const_iterator i = _dirty_controls.begin(); i != _dirty_controls.end(); ++i) {
592                 (*i)->mark_dirty();
593         }
594         
595         _writing = false;
596         write_unlock();
597 }
598
599 /** Append \a ev to model.  NOT realtime safe.
600  *
601  * Timestamps of events in \a buf are expected to be relative to
602  * the start of this model (t=0) and MUST be monotonically increasing
603  * and MUST be >= the latest event currently in the model.
604  */
605 template<typename Time>
606 void
607 Sequence<Time>::append(const Event<Time>& event)
608 {
609         write_lock();
610         _edited = true;
611         
612         const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
613
614         assert(_notes.empty() || ev.time() >= _notes.back()->time());
615         assert(_writing);
616
617         if (!midi_event_is_valid(ev.buffer(), ev.size())) {
618                 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
619                 write_unlock();
620                 return;
621         }
622
623         if (ev.is_note_on()) {
624                 append_note_on_unlocked(ev.channel(), ev.time(), ev.note(), ev.velocity());
625         } else if (ev.is_note_off()) {
626                 append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
627         } else if (ev.is_sysex()) {
628                 append_sysex_unlocked(ev);
629         } else if (!_type_map.type_is_midi(ev.event_type())) {
630                 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
631                 for (size_t i=0; i < ev.size(); ++i) {
632                         printf("%X ", ev.buffer()[i]);
633                 }
634                 printf("\n");
635         } else if (ev.is_cc()) {
636                 append_control_unlocked(
637                                 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
638                                 ev.time(), ev.cc_value());
639         } else if (ev.is_pgm_change()) {
640                 append_control_unlocked(
641                                 Evoral::MIDI::ProgramChange(ev.event_type(), ev.channel()),
642                                 ev.time(), ev.pgm_number());
643         } else if (ev.is_pitch_bender()) {
644                 append_control_unlocked(
645                                 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
646                                 ev.time(), double(  (0x7F & ev.pitch_bender_msb()) << 7
647                                         | (0x7F & ev.pitch_bender_lsb()) ));
648         } else if (ev.is_channel_pressure()) {
649                 append_control_unlocked(
650                                 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
651                                 ev.time(), ev.channel_pressure());
652         } else {
653                 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
654         }
655
656         write_unlock();
657 }
658
659 template<typename Time>
660 void
661 Sequence<Time>::append_note_on_unlocked(uint8_t chan, Time time, uint8_t note_num, uint8_t velocity)
662 {
663         DUMP(format("%1% c=%2% note %3% on @ %4% v=%5%\n")
664                         % this % (int)chan % (int)note_num % time % (int)velocity);
665         assert(note_num <= 127);
666         assert(chan < 16);
667         assert(_writing);
668         _edited = true;
669
670         if (velocity == 0) {
671                 append_note_off_unlocked(chan, time, note_num);
672                 return;
673         }
674
675         if (note_num < _lowest_note)
676                 _lowest_note = note_num;
677         if (note_num > _highest_note)
678                 _highest_note = note_num;
679
680         boost::shared_ptr< Note<Time> > new_note(new Note<Time>(chan, time, 0, note_num, velocity));
681         _notes.push_back(new_note);
682         if (!_percussive) {
683                 DUMP(format("Sustained: Appending active note on %1% channel %2%\n")
684                                 % (unsigned)(uint8_t)note_num % chan);
685                 _write_notes[chan].push_back(_notes.size() - 1);
686         } else {
687                 DUMP("Percussive: NOT appending active note on\n");
688          }
689 }
690
691 template<typename Time>
692 void
693 Sequence<Time>::append_note_off_unlocked(uint8_t chan, Time time, uint8_t note_num)
694 {
695         DUMP(format("%1% c=%2% note %3% off @ %4%\n")
696                         % this % (int)chan % (int)note_num % time);
697         assert(note_num <= 127);
698         assert(chan < 16);
699         assert(_writing);
700         _edited = true;
701
702         if (_percussive) {
703                 DUMP("Sequence Ignoring note off (percussive mode)\n");
704                 return;
705         }
706
707         /* FIXME: make _write_notes fixed size (127 noted) for speed */
708
709         /* FIXME: note off velocity for that one guy out there who actually has
710          * keys that send it */
711
712         bool resolved = false;
713
714         for (WriteNotes::iterator n = _write_notes[chan].begin(); n
715                         != _write_notes[chan].end(); ++n) {
716                 Note<Time>& note = *_notes[*n].get();
717                 if (note.note() == note_num) {
718                         assert(time >= note.time());
719                         note.set_length(time - note.time());
720                         _write_notes[chan].erase(n);
721                         DUMP(format("resolved note, length: %1%\n") % note.length());
722                         resolved = true;
723                         break;
724                 }
725         }
726
727         if (!resolved) {
728                 cerr << this << " spurious note off chan " << (int)chan
729                                 << ", note " << (int)note_num << " @ " << time << endl;
730         }
731 }
732
733 template<typename Time>
734 void
735 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value)
736 {
737         DUMP(format("%1% %2% @ %3%\t=\t%4% # controls: %5%\n")
738                         % this % _type_map.to_symbol(param) % time % value % _controls.size());
739         boost::shared_ptr<Control> c = control(param, true);
740         c->list()->rt_add(time, value);
741 }
742
743 template<typename Time>
744 void
745 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev)
746 {
747         #ifdef DEBUG_SEQUENCE
748         cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
749         for (size_t i=0; i < ev.size(); ++i) {
750                 cerr << int(ev.buffer()[i]) << " ";
751         } cerr << "]" << endl;
752         #endif
753
754         boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
755         _sysexes.push_back(event);
756 }
757
758 template<typename Time>
759 void
760 Sequence<Time>::add_note_unlocked(const boost::shared_ptr< Note<Time> > note)
761 {
762         DUMP(format("%1% add note %2% @ %3%\n") % this % (int)note->note() % note->time());
763         _edited = true;
764         typename Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note,
765                         note_time_comparator);
766         _notes.insert(i, note);
767 }
768
769 template<typename Time>
770 void
771 Sequence<Time>::remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note)
772 {
773         _edited = true;
774         DUMP(format("%1% remove note %2% @ %3%\n") % this % (int)note->note() % note->time());
775         for (typename Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
776                 if (*(*n) == *note) {
777                         _notes.erase(n);
778                         break;
779                 }
780         }
781 }
782
783 template<typename Time>
784 void
785 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
786 {
787         _notes = n;
788 }
789
790 template class Sequence<Evoral::MusicalTime>;
791
792 } // namespace Evoral
793