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