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