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