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