Fix crash with debug enabled.
[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 #include <algorithm>
20 #include <cmath>
21 #include <iostream>
22 #include <limits>
23 #include <stdexcept>
24 #include <stdint.h>
25 #include <cstdio>
26
27 #include "pbd/compose.h"
28
29 #include "evoral/Control.hpp"
30 #include "evoral/ControlList.hpp"
31 #include "evoral/ControlSet.hpp"
32 #include "evoral/EventSink.hpp"
33 #include "evoral/MIDIParameters.hpp"
34 #include "evoral/Sequence.hpp"
35 #include "evoral/TypeMap.hpp"
36 #include "evoral/midi_util.h"
37
38 using namespace std;
39 using namespace PBD;
40
41 /** Minimum time between MIDI outputs from a single interpolated controller,
42     expressed in beats.  This is to limit the rate at which MIDI messages
43     are generated.  It is only applied to interpolated controllers.
44
45     XXX: This is a hack.  The time should probably be expressed in
46     seconds rather than beats, and should be configurable etc. etc.
47 */
48 static double const time_between_interpolated_controller_outputs = 1.0 / 256;
49
50 namespace Evoral {
51
52 // Read iterator (const_iterator)
53
54 template<typename Time>
55 Sequence<Time>::const_iterator::const_iterator()
56         : _seq(NULL)
57         , _is_end(true)
58         , _control_iter(_control_iters.end())
59 {
60         _event = boost::shared_ptr< Event<Time> >(new Event<Time>());
61 }
62
63 /** @param force_discrete true to force ControlLists to use discrete evaluation, otherwise false to get them to use their configured mode */
64 template<typename Time>
65 Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq, Time t, bool force_discrete, std::set<Evoral::Parameter> const & filtered)
66         : _seq(&seq)
67         , _active_patch_change_message (0)
68         , _type(NIL)
69         , _is_end((t == DBL_MAX) || seq.empty())
70         , _note_iter(seq.notes().end())
71         , _sysex_iter(seq.sysexes().end())
72         , _patch_change_iter(seq.patch_changes().end())
73         , _control_iter(_control_iters.end())
74         , _force_discrete (force_discrete)
75 {
76         DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
77
78         if (!_is_end) {
79                 _lock = seq.read_lock();
80         } else {
81                 return;
82         }
83
84         typename Sequence<Time>::ReadLock lock(seq.read_lock());
85
86         // Find first note which begins at or after t
87         _note_iter = seq.note_lower_bound(t);
88
89         // Find first sysex event at or after t
90         for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
91                         i != seq.sysexes().end(); ++i) {
92                 if ((*i)->time() >= t) {
93                         _sysex_iter = i;
94                         break;
95                 }
96         }
97         assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
98
99         // Find first patch event at or after t
100         for (typename Sequence<Time>::PatchChanges::const_iterator i = seq.patch_changes().begin(); i != seq.patch_changes().end(); ++i) {
101                 if ((*i)->time() >= t) {
102                         _patch_change_iter = i;
103                         break;
104                 }
105         }
106         assert (_patch_change_iter == seq.patch_changes().end() || (*_patch_change_iter)->time() >= t);
107
108         // Find first control event after t
109         ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
110         _control_iters.reserve(seq._controls.size());
111         bool   found                  = false;
112         size_t earliest_control_index = 0;
113         for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
114
115                 if (filtered.find (i->first) != filtered.end()) {
116                         /* this parameter is filtered, so don't bother setting up an iterator for it */
117                         continue;
118                 }
119                 
120                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
121                 double x, y;
122                 bool ret;
123                 if (_force_discrete) {
124                         ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t, x, y, true);
125                 } else {
126                         ret = i->second->list()->rt_safe_earliest_event_unlocked(t, x, y, true);
127                 }
128                 if (!ret) {
129                         DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
130                                                                       i->first.id(), i->second->list()->size(), t));
131                         continue;
132                 }
133
134                 assert(x >= 0);
135
136                 if (y < i->first.min() || y > i->first.max()) {
137                         cerr << "ERROR: Controller value " << y
138                                 << " out of range [" << i->first.min() << "," << i->first.max()
139                                 << "], event ignored" << endl;
140                         continue;
141                 }
142
143                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
144
145                 const ControlIterator new_iter(i->second->list(), x, y);
146                 _control_iters.push_back(new_iter);
147
148                 // Found a new earliest_control
149                 if (x < earliest_control.x) {
150                         earliest_control = new_iter;
151                         earliest_control_index = _control_iters.size() - 1;
152                         found = true;
153                 }
154         }
155
156         if (found) {
157                 _control_iter = _control_iters.begin() + earliest_control_index;
158                 assert(_control_iter != _control_iters.end());
159         } else {
160                 _control_iter = _control_iters.end();
161         }
162
163         // Now find the earliest event overall and point to it
164         Time earliest_t = t;
165
166         if (_note_iter != seq.notes().end()) {
167                 _type = NOTE_ON;
168                 earliest_t = (*_note_iter)->time();
169         }
170
171         if (_sysex_iter != seq.sysexes().end()
172                         && ((*_sysex_iter)->time() < earliest_t || _type == NIL)) {
173                 _type = SYSEX;
174                 earliest_t = (*_sysex_iter)->time();
175         }
176
177         if (_patch_change_iter != seq.patch_changes().end() && ((*_patch_change_iter)->time() < earliest_t || _type == NIL)) {
178                 _type = PATCH_CHANGE;
179                 earliest_t = (*_patch_change_iter)->time ();
180         }
181
182         if (_control_iter != _control_iters.end()
183                         && earliest_control.list && earliest_control.x >= t
184                         && (earliest_control.x < earliest_t || _type == NIL)) {
185                 _type = CONTROL;
186                 earliest_t = earliest_control.x;
187         }
188
189         switch (_type) {
190         case NOTE_ON:
191                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at note on event @ %1\n", earliest_t));
192                 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_note_iter)->on_event(), true));
193                 _active_notes.push(*_note_iter);
194                 break;
195         case SYSEX:
196                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at sysex event @ %1\n", earliest_t));
197                 _event = boost::shared_ptr< Event<Time> >(
198                                 new Event<Time>(*(*_sysex_iter), true));
199                 break;
200         case CONTROL:
201                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at control event @ %1\n", earliest_t));
202                 seq.control_to_midi_event(_event, earliest_control);
203                 break;
204         case PATCH_CHANGE:
205                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at patch change event @ %1\n", earliest_t));
206                 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_patch_change_iter)->message (_active_patch_change_message), true));
207                 break;
208         default:
209                 break;
210         }
211
212         if (_type == NIL || !_event || _event->size() == 0) {
213                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at end @ %1\n", t));
214                 _type   = NIL;
215                 _is_end = true;
216         } else {
217                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("New iterator = 0x%1 : 0x%2 @ %3\n",
218                                                               (int)_event->event_type(),
219                                                               (int)((MIDIEvent<Time>*)_event.get())->type(),
220                                                               _event->time()));
221                 
222                 assert(midi_event_is_valid(_event->buffer(), _event->size()));
223         }
224
225 }
226
227 template<typename Time>
228 Sequence<Time>::const_iterator::~const_iterator()
229 {
230 }
231
232 template<typename Time>
233 void
234 Sequence<Time>::const_iterator::invalidate()
235 {
236         while (!_active_notes.empty()) {
237                 _active_notes.pop();
238         }
239         _type = NIL;
240         _is_end = true;
241         if (_seq) {
242                 _note_iter = _seq->notes().end();
243                 _sysex_iter = _seq->sysexes().end();
244                 _patch_change_iter = _seq->patch_changes().end();
245                 _active_patch_change_message = 0;
246         }
247         _control_iter = _control_iters.end();
248         _lock.reset();
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         DEBUG_TRACE(DEBUG::Sequence, "Sequence::const_iterator++\n");
260         assert(_event && _event->buffer() && _event->size() > 0);
261
262         const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
263
264         if (!(     ev.is_note()
265                         || ev.is_cc()
266                         || ev.is_pgm_change()
267                         || ev.is_pitch_bender()
268                         || ev.is_channel_pressure()
269                         || ev.is_sysex()) ) {
270                 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
271                         << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
272         }
273
274         double x   = 0.0;
275         double y   = 0.0;
276         bool   ret = false;
277
278         // Increment past current event
279         switch (_type) {
280         case NOTE_ON:
281                 ++_note_iter;
282                 break;
283         case NOTE_OFF:
284                 break;
285         case CONTROL:
286                 // Increment current controller iterator
287                 if (_force_discrete || _control_iter->list->interpolation() == ControlList::Discrete) {
288                         ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (
289                                 _control_iter->x, x, y, false
290                                 );
291                 } else {
292                         ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
293                                 _control_iter->x + time_between_interpolated_controller_outputs, x, y, false
294                                 );
295                 }
296                 assert(!ret || x > _control_iter->x);
297                 if (ret) {
298                         _control_iter->x = x;
299                         _control_iter->y = y;
300                 } else {
301                         _control_iter->list.reset();
302                         _control_iter->x = DBL_MAX;
303                         _control_iter->y = DBL_MAX;
304                 }
305
306                 // Find the controller with the next earliest event time
307                 _control_iter = _control_iters.begin();
308                 for (ControlIterators::iterator i = _control_iters.begin();
309                                 i != _control_iters.end(); ++i) {
310                         if (i->x < _control_iter->x) {
311                                 _control_iter = i;
312                         }
313                 }
314                 break;
315         case SYSEX:
316                 ++_sysex_iter;
317                 break;
318         case PATCH_CHANGE:
319                 ++_active_patch_change_message;
320                 if (_active_patch_change_message == (*_patch_change_iter)->messages()) {
321                         ++_patch_change_iter;
322                         _active_patch_change_message = 0;
323                 }
324                 break;
325         default:
326                 assert(false);
327         }
328
329         // Now find the earliest event overall and point to it
330         _type = NIL;
331         Time earliest_t = std::numeric_limits<Time>::max();
332
333         // Next earliest note on
334         if (_note_iter != _seq->notes().end()) {
335                 _type = NOTE_ON;
336                 earliest_t = (*_note_iter)->time();
337         }
338
339         // Use the next note off iff it's earlier or the same time as the note on
340         if (!_seq->percussive() && (!_active_notes.empty())) {
341                 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
342                         _type = NOTE_OFF;
343                         earliest_t = _active_notes.top()->end_time();
344                 }
345         }
346
347         // Use the next earliest controller iff it's earlier than the note event
348         if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
349                 if (_type == NIL || _control_iter->x < earliest_t) {
350                         _type = CONTROL;
351                         earliest_t = _control_iter->x;
352                 }
353         }
354
355         // Use the next earliest SysEx iff it's earlier than the controller
356         if (_sysex_iter != _seq->sysexes().end()) {
357                 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
358                         _type = SYSEX;
359                         earliest_t = (*_sysex_iter)->time();
360                 }
361         }
362
363         // Use the next earliest patch change iff it's earlier than the SysEx
364         if (_patch_change_iter != _seq->patch_changes().end()) {
365                 if (_type == NIL || (*_patch_change_iter)->time() < earliest_t) {
366                         _type = PATCH_CHANGE;
367                         earliest_t = (*_patch_change_iter)->time();
368                 }
369         }
370
371         // Set event to reflect new position
372         switch (_type) {
373         case NOTE_ON:
374                 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
375                 *_event = (*_note_iter)->on_event();
376                 _active_notes.push(*_note_iter);
377                 break;
378         case NOTE_OFF:
379                 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
380                 assert(!_active_notes.empty());
381                 *_event = _active_notes.top()->off_event();
382                 _active_notes.pop();
383                 break;
384         case CONTROL:
385                 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
386                 _seq->control_to_midi_event(_event, *_control_iter);
387                 break;
388         case SYSEX:
389                 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
390                 *_event = *(*_sysex_iter);
391                 break;
392         case PATCH_CHANGE:
393                 DEBUG_TRACE(DEBUG::Sequence, "iterator = patch change\n");
394                 *_event = (*_patch_change_iter)->message (_active_patch_change_message);
395                 break;
396         default:
397                 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
398                 _is_end = true;
399         }
400
401         assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
402
403         return *this;
404 }
405
406 template<typename Time>
407 bool
408 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
409 {
410         if (_seq != other._seq) {
411                 return false;
412         } else if (_is_end || other._is_end) {
413                 return (_is_end == other._is_end);
414         } else if (_type != other._type) {
415                 return false;
416         } else {
417                 return (_event == other._event);
418         }
419 }
420
421 template<typename Time>
422 typename Sequence<Time>::const_iterator&
423 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
424 {
425         _seq           = other._seq;
426         _event         = other._event;
427         _active_notes  = other._active_notes;
428         _type          = other._type;
429         _is_end        = other._is_end;
430         _note_iter     = other._note_iter;
431         _sysex_iter    = other._sysex_iter;
432         _patch_change_iter = other._patch_change_iter;
433         _control_iters = other._control_iters;
434         _force_discrete = other._force_discrete;
435         _active_patch_change_message = other._active_patch_change_message;
436
437         if (other._lock)
438                 _lock = _seq->read_lock();
439         else
440                 _lock.reset();
441
442         if (other._control_iter == other._control_iters.end()) {
443                 _control_iter = _control_iters.end();
444         } else {
445                 const size_t index = other._control_iter - other._control_iters.begin();
446                 _control_iter  = _control_iters.begin() + index;
447         }
448
449         return *this;
450 }
451
452 // Sequence
453
454 template<typename Time>
455 Sequence<Time>::Sequence(const TypeMap& type_map)
456         : _edited(false)
457         , _overlapping_pitches_accepted (true)
458         , _overlap_pitch_resolution (FirstOnFirstOff)
459         , _writing(false)
460         , _type_map(type_map)
461         , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
462         , _percussive(false)
463         , _lowest_note(127)
464         , _highest_note(0)
465 {
466         DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
467         assert(_end_iter._is_end);
468         assert( ! _end_iter._lock);
469
470         for (int i = 0; i < 16; ++i) {
471                 _bank[i] = 0;
472         }
473 }
474
475 template<typename Time>
476 Sequence<Time>::Sequence(const Sequence<Time>& other)
477         : ControlSet (other)
478         , _edited(false)
479         , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
480         , _overlap_pitch_resolution (other._overlap_pitch_resolution)
481         , _writing(false)
482         , _type_map(other._type_map)
483         , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
484         , _percussive(other._percussive)
485         , _lowest_note(other._lowest_note)
486         , _highest_note(other._highest_note)
487 {
488         for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
489                 NotePtr n (new Note<Time> (**i));
490                 _notes.insert (n);
491         }
492
493         for (typename SysExes::const_iterator i = other._sysexes.begin(); i != other._sysexes.end(); ++i) {
494                 boost::shared_ptr<Event<Time> > n (new Event<Time> (**i, true));
495                 _sysexes.push_back (n);
496         }
497
498         for (typename PatchChanges::const_iterator i = other._patch_changes.begin(); i != other._patch_changes.end(); ++i) {
499                 PatchChangePtr n (new PatchChange<Time> (**i));
500                 _patch_changes.insert (n);
501         }
502
503         for (int i = 0; i < 16; ++i) {
504                 _bank[i] = other._bank[i];
505         }
506
507         DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
508         assert(_end_iter._is_end);
509         assert(! _end_iter._lock);
510 }
511
512 /** Write the controller event pointed to by \a iter to \a ev.
513  * The buffer of \a ev will be allocated or resized as necessary.
514  * The event_type of \a ev should be set to the expected output type.
515  * \return true on success
516  */
517 template<typename Time>
518 bool
519 Sequence<Time>::control_to_midi_event(
520                 boost::shared_ptr< Event<Time> >& ev,
521                 const ControlIterator&            iter) const
522 {
523         assert(iter.list.get());
524         const uint32_t event_type = iter.list->parameter().type();
525
526         // initialize the event pointer with a new event, if necessary
527         if (!ev) {
528                 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
529         }
530
531         uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
532         ev->set_event_type(_type_map.midi_event_type(midi_type));
533         switch (midi_type) {
534         case MIDI_CMD_CONTROL:
535                 assert(iter.list.get());
536                 assert(iter.list->parameter().channel() < 16);
537                 assert(iter.list->parameter().id() <= INT8_MAX);
538                 assert(iter.y <= INT8_MAX);
539
540                 ev->time() = iter.x;
541                 ev->realloc(3);
542                 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
543                 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
544                 ev->buffer()[2] = (uint8_t)iter.y;
545                 break;
546
547         case MIDI_CMD_PGM_CHANGE:
548                 assert(iter.list.get());
549                 assert(iter.list->parameter().channel() < 16);
550                 assert(iter.y <= INT8_MAX);
551
552                 ev->time() = iter.x;
553                 ev->realloc(2);
554                 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
555                 ev->buffer()[1] = (uint8_t)iter.y;
556                 break;
557
558         case MIDI_CMD_BENDER:
559                 assert(iter.list.get());
560                 assert(iter.list->parameter().channel() < 16);
561                 assert(iter.y < (1<<14));
562
563                 ev->time() = iter.x;
564                 ev->realloc(3);
565                 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
566                 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
567                 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
568                 break;
569
570         case MIDI_CMD_CHANNEL_PRESSURE:
571                 assert(iter.list.get());
572                 assert(iter.list->parameter().channel() < 16);
573                 assert(iter.y <= INT8_MAX);
574
575                 ev->time() = iter.x;
576                 ev->realloc(2);
577                 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
578                 ev->buffer()[1] = (uint8_t)iter.y;
579                 break;
580
581         default:
582                 return false;
583         }
584
585         return true;
586 }
587
588 /** Clear all events from the model.
589  */
590 template<typename Time>
591 void
592 Sequence<Time>::clear()
593 {
594         WriteLock lock(write_lock());
595         _notes.clear();
596         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
597                 li->second->list()->clear();
598 }
599
600 /** Begin a write of events to the model.
601  *
602  * If \a mode is Sustained, complete notes with length are constructed as note
603  * on/off events are received.  Otherwise (Percussive), only note on events are
604  * stored; note off events are discarded entirely and all contained notes will
605  * have length 0.
606  */
607 template<typename Time>
608 void
609 Sequence<Time>::start_write()
610 {
611         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
612         WriteLock lock(write_lock());
613         _writing = true;
614         for (int i = 0; i < 16; ++i) {
615                 _write_notes[i].clear();
616         }
617 }
618
619 /** Finish a write of events to the model.
620  *
621  * If \a delete_stuck is true and the current mode is Sustained, note on events
622  * that were never resolved with a corresonding note off will be deleted.
623  * Otherwise they will remain as notes with length 0.
624  */
625 template<typename Time>
626 void
627 Sequence<Time>::end_write (bool delete_stuck)
628 {
629         WriteLock lock(write_lock());
630
631         if (!_writing) {
632                 return;
633         }
634
635         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes)\n", this, _notes.size()));
636
637         if (!_percussive && delete_stuck) {
638                 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
639                         typename Notes::iterator next = n;
640                         ++next;
641                         
642                         if ((*n)->length() == 0) {
643                                 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
644                                 _notes.erase(n);
645                         }
646                         
647                         n = next;
648                 }
649         }
650
651         for (int i = 0; i < 16; ++i) {
652                 if (!_write_notes[i].empty()) {
653                         cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
654                                         << _write_notes[i].size() << " stuck notes" << endl;
655                 }
656                 _write_notes[i].clear();
657         }
658
659         _writing = false;
660 }
661
662
663 template<typename Time>
664 bool
665 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
666 {
667         /* This is the core method to add notes to a Sequence 
668          */
669
670         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3\n", this, (int)note->note(), note->time()));
671
672         if (resolve_overlaps_unlocked (note, arg)) {
673                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 DISALLOWED: note %2 @ %3\n", this, (int)note->note(), note->time()));
674                 return false;
675         }
676
677         if (note->id() < 0) {
678                 note->set_id (Evoral::next_event_id());
679         } 
680
681         if (note->note() < _lowest_note)
682                 _lowest_note = note->note();
683         if (note->note() > _highest_note)
684                 _highest_note = note->note();
685
686         _notes.insert (note);
687         _pitches[note->channel()].insert (note);
688  
689         _edited = true;
690
691        return true;
692 }
693
694 template<typename Time>
695 void
696 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
697 {
698         bool erased = false;
699
700         _edited = true;
701
702         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note %2 @ %3\n", this, (int)note->note(), note->time()));
703
704         typename Sequence<Time>::Notes::iterator i = note_lower_bound(note->time());
705         while (i != _notes.end() && (*i)->time() == note->time()) {
706
707                 typename Sequence<Time>::Notes::iterator tmp = i;
708                 ++tmp;
709
710                 if (*i == note) {
711
712                         NotePtr n = *i;
713                         
714                         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
715                         _notes.erase (i);
716
717                         if (n->note() == _lowest_note || n->note() == _highest_note) {
718
719                                 _lowest_note = 127;
720                                 _highest_note = 0;
721
722                                 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
723                                         if ((*ii)->note() < _lowest_note)
724                                                 _lowest_note = (*ii)->note();
725                                         if ((*ii)->note() > _highest_note)
726                                                 _highest_note = (*ii)->note();
727                                 }
728                         }
729                         
730                         erased = true;
731                 }
732
733                 i = tmp;
734         }
735
736         Pitches& p (pitches (note->channel()));
737         
738         NotePtr search_note(new Note<Time>(0, 0, 0, note->note(), 0));
739
740         typename Pitches::iterator j = p.lower_bound (search_note);
741         while (j != p.end() && (*j)->note() == note->note()) {
742                 typename Pitches::iterator tmp = j;
743                 ++tmp;
744                 
745                 if (*j == note) {
746                         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*j)->note(), (*j)->time()));
747                         p.erase (j);
748                 }
749
750                 j = tmp;
751         }
752         
753         if (!erased) {
754                 cerr << "Unable to find note to erase" << endl;
755         }
756 }
757
758 template<typename Time>
759 void
760 Sequence<Time>::add_patch_change_unlocked (PatchChangePtr p)
761 {
762         _patch_changes.insert (p);
763         if (p->id () < 0) {
764                 p->set_id (Evoral::next_event_id ());
765         }
766 }
767
768 template<typename Time>
769 void
770 Sequence<Time>::remove_patch_change_unlocked (const constPatchChangePtr p)
771 {
772         typename Sequence<Time>::PatchChanges::iterator i = patch_change_lower_bound (p->time ());
773         while (i != _patch_changes.end() && (*i)->time() == p->time()) {
774
775                 typename Sequence<Time>::PatchChanges::iterator tmp = i;
776                 ++tmp;
777
778                 if (*i == p) {
779                         _patch_changes.erase (i);
780                 }
781
782                 i = tmp;
783         }
784 }
785
786 /** Append \a ev to model.  NOT realtime safe.
787  *
788  * The timestamp of event is expected to be relative to
789  * the start of this model (t=0) and MUST be monotonically increasing
790  * and MUST be >= the latest event currently in the model.
791  */
792 template<typename Time>
793 void
794 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
795 {
796         WriteLock lock(write_lock());
797
798         const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
799
800         assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
801         assert(_writing);
802
803         if (!midi_event_is_valid(ev.buffer(), ev.size())) {
804                 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
805                 return;
806         }
807
808         if (ev.is_note_on()) {
809                 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
810                 append_note_on_unlocked (note, evid);
811         } else if (ev.is_note_off()) {
812                 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
813                 /* XXX note: event ID is discarded because we merge the on+off events into
814                    a single note object
815                 */
816                 append_note_off_unlocked (note);
817         } else if (ev.is_sysex()) {
818                 append_sysex_unlocked(ev, evid);
819         } else if (ev.is_cc() && (ev.cc_number() == MIDI_CTL_MSB_BANK || ev.cc_number() == MIDI_CTL_LSB_BANK)) {
820                 /* note bank numbers in our _bank[] array, so that we can write an event when the program change arrives */
821                 if (ev.cc_number() == MIDI_CTL_MSB_BANK) {
822                         _bank[ev.channel()] &= (0x7f << 7);
823                         _bank[ev.channel()] |= ev.cc_value() << 7;
824                 } else {
825                         _bank[ev.channel()] &= 0x7f;
826                         _bank[ev.channel()] |= ev.cc_value();
827                 }
828         } else if (ev.is_cc()) {
829                 append_control_unlocked(
830                         Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
831                         ev.time(), ev.cc_value(), evid);
832         } else if (ev.is_pgm_change()) {
833                 /* write a patch change with this program change and any previously set-up bank number */
834                 append_patch_change_unlocked (PatchChange<Time> (ev.time(), ev.channel(), ev.pgm_number(), _bank[ev.channel()]), evid);
835         } else if (ev.is_pitch_bender()) {
836                 append_control_unlocked(
837                         Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
838                         ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
839                                            | (0x7F & ev.pitch_bender_lsb())),
840                         evid);
841         } else if (ev.is_channel_pressure()) {
842                 append_control_unlocked(
843                         Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
844                         ev.time(), ev.channel_pressure(), evid);
845         } else if (!_type_map.type_is_midi(ev.event_type())) {
846                 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
847                 for (size_t i=0; i < ev.size(); ++i) {
848                         printf("%X ", ev.buffer()[i]);
849                 }
850                 printf("\n");
851         } else {
852                 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
853         }
854
855         _edited = true;
856 }
857
858 template<typename Time>
859 void
860 Sequence<Time>::append_note_on_unlocked (NotePtr note, event_id_t evid)
861 {
862         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this, 
863                                                       (int) note->channel(), (int) note->note(), 
864                                                       note->time(), (int) note->velocity()));
865         assert(note->note() <= 127);
866         assert(note->channel() < 16);
867         assert(_writing);
868
869         if (note->id() < 0) {
870                 note->set_id (evid);
871         }
872
873         if (note->velocity() == 0) {
874                 append_note_off_unlocked (note);
875                 return;
876         }
877
878         add_note_unlocked (note);
879         
880         if (!_percussive) {
881                 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sustained: Appending active note on %1 channel %2\n",
882                                                               (unsigned)(uint8_t)note->note(), note->channel()));
883                 _write_notes[note->channel()].insert (note);
884         } else {
885                 DEBUG_TRACE(DEBUG::Sequence, "Percussive: NOT appending active note on\n");
886         }
887 }
888
889 template<typename Time>
890 void
891 Sequence<Time>::append_note_off_unlocked (NotePtr note)
892 {
893         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n",
894                                                       this, (int)note->channel(), 
895                                                       (int)note->note(), note->time(), (int)note->velocity()));
896         assert(note->note() <= 127);
897         assert(note->channel() < 16);
898         assert(_writing);
899         _edited = true;
900
901         if (_percussive) {
902                 DEBUG_TRACE(DEBUG::Sequence, "Sequence Ignoring note off (percussive mode)\n");
903                 return;
904         }
905
906         bool resolved = false;
907
908         /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
909            matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
910            whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
911            format.
912         */
913
914         /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
915
916         for (typename WriteNotes::iterator n = _write_notes[note->channel()].begin(); n != _write_notes[note->channel()].end(); ) {
917
918                 typename WriteNotes::iterator tmp = n;
919                 ++tmp;
920                 
921                 NotePtr nn = *n;
922                 if (note->note() == nn->note() && nn->channel() == note->channel()) {
923                         assert(note->time() >= nn->time());
924
925                         nn->set_length (note->time() - nn->time());
926                         nn->set_off_velocity (note->velocity());
927
928                         _write_notes[note->channel()].erase(n);
929                         DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note, length: %1\n", note->length()));
930                         resolved = true;
931                         break;
932                 }
933
934                 n = tmp;
935         }
936
937         if (!resolved) {
938                 cerr << this << " spurious note off chan " << (int)note->channel()
939                      << ", note " << (int)note->note() << " @ " << note->time() << endl;
940         }
941 }
942
943 template<typename Time>
944 void
945 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t /* evid */)
946 {
947         DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3\t=\t%4 # controls: %5\n",
948                                                       this, _type_map.to_symbol(param), time, value, _controls.size()));
949         boost::shared_ptr<Control> c = control(param, true);
950         c->list()->add (time, value);
951         /* XXX control events should use IDs */
952 }
953
954 template<typename Time>
955 void
956 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t /* evid */)
957 {
958 #ifdef DEBUG_SEQUENCE
959         cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
960         for (size_t i=0; i < ev.size(); ++i) {
961                 cerr << int(ev.buffer()[i]) << " ";
962         } cerr << "]" << endl;
963 #endif
964
965         boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
966         /* XXX sysex events should use IDs */
967         _sysexes.push_back(event);
968 }
969
970 template<typename Time>
971 void
972 Sequence<Time>::append_patch_change_unlocked (const PatchChange<Time>& ev, event_id_t id)
973 {
974         PatchChangePtr p (new PatchChange<Time> (ev));
975         
976         if (p->id() < 0) {
977                 p->set_id (id);
978         }
979         
980         _patch_changes.insert (p);
981 }
982
983 template<typename Time>
984 bool
985 Sequence<Time>::contains (const NotePtr& note) const
986 {
987         ReadLock lock (read_lock());
988         return contains_unlocked (note);
989 }
990
991 template<typename Time>
992 bool
993 Sequence<Time>::contains_unlocked (const NotePtr& note) const
994 {
995         const Pitches& p (pitches (note->channel()));
996         NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
997
998         for (typename Pitches::const_iterator i = p.lower_bound (search_note); 
999              i != p.end() && (*i)->note() == note->note(); ++i) {
1000
1001                 if (**i == *note) {
1002                         return true;
1003                 }
1004         }
1005
1006         return false;
1007 }
1008
1009 template<typename Time>
1010 bool
1011 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
1012 {
1013         ReadLock lock (read_lock());
1014         return overlaps_unlocked (note, without);
1015 }
1016
1017 template<typename Time>
1018 bool
1019 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
1020 {
1021         Time sa = note->time();
1022         Time ea  = note->end_time();
1023          
1024         const Pitches& p (pitches (note->channel()));
1025         NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
1026
1027         for (typename Pitches::const_iterator i = p.lower_bound (search_note); 
1028              i != p.end() && (*i)->note() == note->note(); ++i) {
1029
1030                 if (without && (**i) == *without) {
1031                         continue;
1032                 }
1033
1034                 Time sb = (*i)->time();
1035                 Time eb = (*i)->end_time();
1036
1037                 if (((sb > sa) && (eb <= ea)) ||
1038                     ((eb >= sa) && (eb <= ea)) ||
1039                     ((sb > sa) && (sb <= ea)) ||
1040                     ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
1041                         return true;
1042                 }
1043         }
1044
1045         return false;
1046 }
1047
1048 template<typename Time>
1049 void
1050 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
1051 {
1052         _notes = n;
1053 }
1054
1055 /** Return the earliest note with time >= t */
1056 template<typename Time>
1057 typename Sequence<Time>::Notes::const_iterator
1058 Sequence<Time>::note_lower_bound (Time t) const
1059 {
1060         NotePtr search_note(new Note<Time>(0, t, 0, 0, 0));
1061         typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
1062         assert(i == _notes.end() || (*i)->time() >= t);
1063         return i;
1064 }
1065
1066 /** Return the earliest patch change with time >= t */
1067 template<typename Time>
1068 typename Sequence<Time>::PatchChanges::const_iterator
1069 Sequence<Time>::patch_change_lower_bound (Time t) const
1070 {
1071         PatchChangePtr search (new PatchChange<Time> (t, 0, 0, 0));
1072         typename Sequence<Time>::PatchChanges::const_iterator i = _patch_changes.lower_bound (search);
1073         assert (i == _patch_changes.end() || (*i)->time() >= t);
1074         return i;
1075 }
1076
1077 template<typename Time>
1078 void
1079 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1080 {
1081         switch (op) {
1082         case PitchEqual:
1083         case PitchLessThan:
1084         case PitchLessThanOrEqual:
1085         case PitchGreater:
1086         case PitchGreaterThanOrEqual:
1087                 get_notes_by_pitch (n, op, val, chan_mask);
1088                 break;
1089                 
1090         case VelocityEqual:
1091         case VelocityLessThan:
1092         case VelocityLessThanOrEqual:
1093         case VelocityGreater:
1094         case VelocityGreaterThanOrEqual:
1095                 get_notes_by_velocity (n, op, val, chan_mask);
1096                 break;
1097         }
1098 }
1099
1100 template<typename Time>
1101 void
1102 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1103 {
1104         for (uint8_t c = 0; c < 16; ++c) {
1105
1106                 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
1107                         continue;
1108                 }
1109
1110                 const Pitches& p (pitches (c));
1111                 NotePtr search_note(new Note<Time>(0, 0, 0, val, 0));
1112                 typename Pitches::const_iterator i;
1113                 switch (op) {
1114                 case PitchEqual:
1115                         i = p.lower_bound (search_note);
1116                         while (i != p.end() && (*i)->note() == val) {
1117                                 n.insert (*i);
1118                         }
1119                         break;
1120                 case PitchLessThan:
1121                         i = p.upper_bound (search_note);
1122                         while (i != p.end() && (*i)->note() < val) {
1123                                 n.insert (*i);
1124                         }
1125                         break;
1126                 case PitchLessThanOrEqual:
1127                         i = p.upper_bound (search_note);
1128                         while (i != p.end() && (*i)->note() <= val) {
1129                                 n.insert (*i);
1130                         }
1131                         break;
1132                 case PitchGreater:
1133                         i = p.lower_bound (search_note);
1134                         while (i != p.end() && (*i)->note() > val) {
1135                                 n.insert (*i);
1136                         }
1137                         break;
1138                 case PitchGreaterThanOrEqual:
1139                         i = p.lower_bound (search_note);
1140                         while (i != p.end() && (*i)->note() >= val) {
1141                                 n.insert (*i);
1142                         }
1143                         break;
1144                         
1145                 default:
1146                         //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1147                         abort ();
1148                         /* NOTREACHED*/
1149                 }
1150         }
1151 }
1152
1153 template<typename Time>
1154 void
1155 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1156 {
1157         ReadLock lock (read_lock());
1158         
1159         for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1160
1161                 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1162                         continue;
1163                 }
1164
1165                 switch (op) {
1166                 case VelocityEqual:
1167                         if ((*i)->velocity() == val) {
1168                                 n.insert (*i);
1169                         }
1170                         break;
1171                 case VelocityLessThan:
1172                         if ((*i)->velocity() < val) {
1173                                 n.insert (*i);
1174                         }
1175                         break;
1176                 case VelocityLessThanOrEqual:
1177                         if ((*i)->velocity() <= val) {
1178                                 n.insert (*i);
1179                         }
1180                         break;
1181                 case VelocityGreater:
1182                         if ((*i)->velocity() > val) {
1183                                 n.insert (*i);
1184                         }
1185                         break;
1186                 case VelocityGreaterThanOrEqual:
1187                         if ((*i)->velocity() >= val) {
1188                                 n.insert (*i);
1189                         }
1190                         break;
1191                 default:
1192                         // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1193                         abort ();
1194                         /* NOTREACHED*/
1195
1196                 }
1197         }
1198 }
1199
1200 template<typename Time>
1201 void
1202 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1203 {
1204         _overlap_pitch_resolution = opr;
1205
1206         /* XXX todo: clean up existing overlaps in source data? */
1207 }
1208
1209 template<typename Time>
1210 void
1211 Sequence<Time>::control_list_marked_dirty ()
1212 {
1213         set_edited (true);
1214 }
1215
1216 template class Sequence<Evoral::MusicalTime>;
1217
1218 } // namespace Evoral
1219