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