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