* converted two especially obnoxious assertions into warnings since they hinder my...
[ardour.git] / libs / evoral / src / Sequence.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  * 
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  * 
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  * 
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #define __STDC_LIMIT_MACROS 1
20
21 #include <iostream>
22 #include <cmath>
23 #include <algorithm>
24 #include <stdexcept>
25 #include <stdint.h>
26 #include <evoral/Sequence.hpp>
27 #include <evoral/ControlList.hpp>
28 #include <evoral/Control.hpp>
29 #include <evoral/ControlSet.hpp>
30 #include <evoral/EventSink.hpp>
31 #include <evoral/MIDIParameters.hpp>
32 #include <evoral/TypeMap.hpp>
33
34 using namespace std;
35
36 namespace Evoral {
37
38 void Sequence::write_lock() {
39         _lock.writer_lock();
40         _control_lock.lock();
41 }
42
43 void Sequence::write_unlock() {
44         _lock.writer_unlock();
45         _control_lock.unlock();
46 }
47
48 void Sequence::read_lock() const {
49         _lock.reader_lock();
50 }
51
52 void Sequence::read_unlock() const {
53         _lock.reader_unlock();
54 }
55
56 struct null_ostream : public std::ostream {
57         null_ostream(): std::ios(0), std::ostream(0) {}
58 };
59 static null_ostream nullout;
60
61 //static ostream& debugout = cout;
62 static ostream& debugout = nullout;
63 static ostream& errorout = cerr;
64
65 // Read iterator (const_iterator)
66
67 Sequence::const_iterator::const_iterator(const Sequence& seq, EventTime t)
68         : _seq(&seq)
69         , _is_end( (t == DBL_MAX) || seq.empty() )
70         , _locked( !_is_end )
71 {
72         debugout << "Created Iterator @ " << t << " (is end: " << _is_end << ")" << endl;
73
74         if (_is_end) {
75                 return;
76         }
77
78         seq.read_lock();
79
80         // find first note which begins after t
81         _note_iter = seq.notes().end();
82         for (Sequence::Notes::const_iterator i = seq.notes().begin(); i != seq.notes().end(); ++i) {
83                 if ((*i)->time() >= t) {
84                         _note_iter = i;
85                         break;
86                 }
87         }
88
89         ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
90
91         _control_iters.reserve(seq._controls.size());
92         
93         // find the earliest control event available
94         for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
95                 debugout << "Iterator: control: " << seq._type_map.to_symbol(i->first) << endl;
96                 double x, y;
97                 bool ret = i->second->list()->rt_safe_earliest_event_unlocked(t, DBL_MAX, x, y);
98                 if (!ret) {
99                         debugout << "Iterator: CC " << i->first.id() << " (size " << i->second->list()->size()
100                                 << ") has no events past " << t << endl;
101                         continue;
102                 }
103
104                 assert(x >= 0);
105
106                 /*if (y < i->first.min() || y > i->first.max()) {
107                         errorout << "ERROR: Controller " << i->first.symbol() << " value " << y
108                                 << " out of range [" << i->first.min() << "," << i->first.max()
109                                 << "], event ignored" << endl;
110                         continue;
111                 }*/
112
113                 const ControlIterator new_iter(i->second->list(), x, y);
114
115                 debugout << "Iterator: CC " << i->first.id() << " added (" << x << ", " << y << ")" << endl;
116                 _control_iters.push_back(new_iter);
117
118                 // if the x of the current control is less than earliest_control
119                 // we have a new earliest_control
120                 if (x < earliest_control.x) {
121                         earliest_control = new_iter;
122                         _control_iter = _control_iters.end();
123                         --_control_iter;
124                         // now _control_iter points to the last Element in _control_iters
125                 }
126         }
127
128         if (_note_iter != seq.notes().end()
129                         && (*_note_iter)->on_event().time() >= t
130                         && (!earliest_control.list
131                                 || (*_note_iter)->on_event().time() < earliest_control.x)) {
132                 debugout << "Reading note on event @ " << (*_note_iter)->on_event().time() << endl;
133                 _event = boost::shared_ptr<Event>(new Event((*_note_iter)->on_event(), true));
134                 _active_notes.push(*_note_iter);
135                 ++_note_iter;
136                 _control_iter = _control_iters.end();
137         } else if (earliest_control.list) {
138                 debugout << "Reading control event @ " << earliest_control.x << endl;
139                 seq.control_to_midi_event(_event, earliest_control);
140         }
141
142         if ( (! _event.get()) || _event->size() == 0) {
143                 debugout << "New iterator @ " << t << " is at end." << endl;
144                 _is_end = true;
145
146                 // eliminate possible race condition here (ugly)
147                 static Glib::Mutex mutex;
148                 Glib::Mutex::Lock lock(mutex);
149                 if (_locked) {
150                         _seq->read_unlock();
151                         _locked = false;
152                 }
153         } else {
154                 debugout << "New Iterator = " << _event->event_type();
155                 debugout << " : " << hex << (int)((MIDIEvent*)_event.get())->type();
156                 debugout << " @ " <<  _event->time() << endl;
157         }
158
159         //assert(_is_end || (_event->buffer() && _event->buffer()[0] != '\0'));
160 }
161
162 Sequence::const_iterator::~const_iterator()
163 {
164         if (_locked) {
165                 _seq->read_unlock();
166         }
167 }
168
169 const
170 Sequence::const_iterator& Sequence::const_iterator::operator++()
171 {
172         if (_is_end) {
173                 throw std::logic_error("Attempt to iterate past end of Sequence");
174         }
175         
176         debugout << "Iterator ++" << endl;
177         assert(_event->buffer() && _event->size() > 0);
178         
179         const MIDIEvent& ev = *((MIDIEvent*)_event.get());
180
181         //debugout << "const_iterator::operator++: " << _event->to_string() << endl;
182
183         if (! (ev.is_note() || ev.is_cc() || ev.is_pgm_change()
184                                 || ev.is_pitch_bender() || ev.is_channel_pressure()) ) {
185                 errorout << "Unknown event type: " << hex << int(ev.buffer()[0])
186                         << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
187         }
188         assert((ev.is_note() || ev.is_cc() || ev.is_pgm_change() || ev.is_pitch_bender() || ev.is_channel_pressure()));
189
190         // Increment past current control event
191         if (!ev.is_note() && _control_iter != _control_iters.end() && _control_iter->list.get()) {
192                 double x = 0.0, y = 0.0;
193                 const bool ret = _control_iter->list->rt_safe_earliest_event_unlocked(
194                                 _control_iter->x, DBL_MAX, x, y, false);
195
196                 if ( !(!ret || x > _control_iter->x) ) {
197                         cerr << "Warning: Assertion failed: !ret || x > _control_iter->x in Sequence.cpp" << endl;
198                 }
199
200                 if (ret) {
201                         _control_iter->x = x;
202                         _control_iter->y = y;
203                 } else {
204                         _control_iter->list.reset();
205                         _control_iter->x = DBL_MAX;
206                         _control_iter->y = DBL_MAX;
207                 }
208         }
209
210         _control_iter = _control_iters.begin();
211
212         // find the _control_iter with the earliest event time
213         for (ControlIterators::iterator i = _control_iters.begin(); i != _control_iters.end(); ++i) {
214                 if (i->x < _control_iter->x) {
215                         _control_iter = i;
216                 }
217         }
218
219         enum Type {NIL, NOTE_ON, NOTE_OFF, CONTROL};
220
221         Type type = NIL;
222         EventTime t = 0;
223
224         // Next earliest note on
225         if (_note_iter != _seq->notes().end()) {
226                 type = NOTE_ON;
227                 t = (*_note_iter)->time();
228         }
229
230         // Use the next earliest note off iff it's earlier than the note on
231         if (!_seq->percussive() && (! _active_notes.empty())) {
232                 if (type == NIL || _active_notes.top()->end_time() <= t) {
233                         type = NOTE_OFF;
234                         t = _active_notes.top()->end_time();
235                 }
236         }
237
238         // Use the next earliest controller iff it's earlier than the note event
239         if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
240                 if (type == NIL || _control_iter->x < t) {
241                         type = CONTROL;
242                 }
243         }
244
245         if (type == NOTE_ON) {
246                 debugout << "Iterator = note on" << endl;
247                 *_event = (*_note_iter)->on_event();
248                 _active_notes.push(*_note_iter);
249                 ++_note_iter;
250         } else if (type == NOTE_OFF) {
251                 debugout << "Iterator = note off" << endl;
252                 *_event = _active_notes.top()->off_event();
253                 _active_notes.pop();
254         } else if (type == CONTROL) {
255                 debugout << "Iterator = control" << endl;
256                 _seq->control_to_midi_event(_event, *_control_iter);
257         } else {
258                 debugout << "Iterator = End" << endl;
259                 _is_end = true;
260         }
261
262         assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
263
264         return *this;
265 }
266
267 bool
268 Sequence::const_iterator::operator==(const const_iterator& other) const
269 {
270         if (_is_end || other._is_end) {
271                 return (_is_end == other._is_end);
272         } else {
273                 return (_event == other._event);
274         }
275 }
276
277 Sequence::const_iterator&
278 Sequence::const_iterator::operator=(const const_iterator& other)
279 {
280         if (_locked && _seq != other._seq) {
281                 _seq->read_unlock();
282         }
283
284         _seq           = other._seq;
285         _active_notes  = other._active_notes;
286         _is_end        = other._is_end;
287         _locked        = other._locked;
288         _note_iter     = other._note_iter;
289         _control_iters = other._control_iters;
290         size_t index   = other._control_iter - other._control_iters.begin();
291         _control_iter  = _control_iters.begin() + index;
292         
293         if (!_is_end && other._event) {
294                 if (_event) {
295                         *_event = *other._event.get();
296                 } else {
297                         _event = boost::shared_ptr<Event>(new Event(*other._event, true));
298                 }
299         } else {
300                 if (_event) {
301                         _event->clear();
302                 }
303         }
304
305         return *this;
306 }
307
308 // Sequence
309
310 Sequence::Sequence(const TypeMap& type_map, size_t size)
311         : _read_iter(*this, DBL_MAX)
312         , _edited(false)
313         , _type_map(type_map)
314         , _notes(size)
315         , _writing(false)
316         , _end_iter(*this, DBL_MAX)
317         , _next_read(UINT32_MAX)
318         , _percussive(false)
319         , _lowest_note(127)
320         , _highest_note(0)
321 {
322         debugout << "Sequence (size " << size << ") constructed: " << this << endl;
323         assert(_end_iter._is_end);
324         assert( ! _end_iter._locked);
325 }
326
327 /** Read events in frame range \a start .. \a start+cnt into \a dst,
328  * adding \a offset to each event's timestamp.
329  * \return number of events written to \a dst
330  */
331 size_t
332 Sequence::read(EventSink& dst, timestamp_t start, timedur_t nframes, timestamp_t offset) const
333 {
334         debugout << this << " read @ " << start << " * " << nframes << " + " << offset << endl;
335         debugout << this << " # notes: " << n_notes() << endl;
336         debugout << this << " # controls: " << _controls.size() << endl;
337
338         size_t read_events = 0;
339
340         if (start != _next_read) {
341                 debugout << "Repositioning iterator from " << _next_read << " to " << start << endl;
342                 _read_iter = const_iterator(*this, (double)start);
343         } else {
344                 debugout << "Using cached iterator at " << _next_read << endl;
345         }
346
347         _next_read = (FrameTime) floor (start + nframes);
348
349         while (_read_iter != end() && _read_iter->time() < start + nframes) {
350                 assert(_read_iter->size() > 0);
351                 assert(_read_iter->buffer());
352                 dst.write(_read_iter->time() + offset,
353                           _read_iter->event_type(),
354                           _read_iter->size(), 
355                           _read_iter->buffer());
356                 
357                  debugout << this << " read event type " << _read_iter->event_type()
358                          << " @ " << _read_iter->time() << " : ";
359                  for (size_t i = 0; i < _read_iter->size(); ++i)
360                          debugout << hex << (int)_read_iter->buffer()[i];
361                  debugout << endl;
362                 
363                 ++_read_iter;
364                 ++read_events;
365         }
366
367         return read_events;
368 }
369
370 /** Write the controller event pointed to by \a iter to \a ev.
371  * The buffer of \a ev will be allocated or resized as necessary.
372  * The event_type of \a ev should be set to the expected output type.
373  * \return true on success
374  */
375 bool
376 Sequence::control_to_midi_event(boost::shared_ptr<Event>& ev, const ControlIterator& iter) const
377 {
378         assert(iter.list.get());
379         const uint32_t event_type = iter.list->parameter().type();
380         if (!ev) {
381                 ev = boost::shared_ptr<Event>(new Event(event_type, 0, 3, NULL, true));
382         }
383         
384         uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
385         ev->set_event_type(_type_map.midi_event_type(midi_type));
386         switch (midi_type) {
387         case MIDI_CMD_CONTROL:
388                 assert(iter.list.get());
389                 assert(iter.list->parameter().channel() < 16);
390                 assert(iter.list->parameter().id() <= INT8_MAX);
391                 assert(iter.y <= INT8_MAX);
392                 
393                 ev->time() = iter.x;
394                 ev->realloc(3);
395                 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
396                 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
397                 ev->buffer()[2] = (uint8_t)iter.y;
398                 break;
399
400         case MIDI_CMD_PGM_CHANGE:
401                 assert(iter.list.get());
402                 assert(iter.list->parameter().channel() < 16);
403                 assert(iter.y <= INT8_MAX);
404                 
405                 ev->time() = iter.x;
406                 ev->realloc(2);
407                 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
408                 ev->buffer()[1] = (uint8_t)iter.y;
409                 break;
410
411         case MIDI_CMD_BENDER:
412                 assert(iter.list.get());
413                 assert(iter.list->parameter().channel() < 16);
414                 assert(iter.y < (1<<14));
415                 
416                 ev->time() = iter.x;
417                 ev->realloc(3);
418                 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
419                 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
420                 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
421                 break;
422
423         case MIDI_CMD_CHANNEL_PRESSURE:
424                 assert(iter.list.get());
425                 assert(iter.list->parameter().channel() < 16);
426                 assert(iter.y <= INT8_MAX);
427
428                 ev->time() = iter.x;
429                 ev->realloc(2);
430                 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
431                 ev->buffer()[1] = (uint8_t)iter.y;
432                 break;
433
434         default:
435                 return false;
436         }
437
438         return true;
439 }
440
441 /** Clear all events from the model.
442  */
443 void
444 Sequence::clear()
445 {
446         _lock.writer_lock();
447         _notes.clear();
448         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
449                 li->second->list()->clear();
450         _next_read = 0;
451         _read_iter = end();
452         _lock.writer_unlock();
453 }
454
455 /** Begin a write of events to the model.
456  *
457  * If \a mode is Sustained, complete notes with length are constructed as note
458  * on/off events are received.  Otherwise (Percussive), only note on events are
459  * stored; note off events are discarded entirely and all contained notes will
460  * have length 0.
461  */
462 void
463 Sequence::start_write()
464 {
465         debugout << this << " START WRITE, PERCUSSIVE = " << _percussive << endl;
466         write_lock();
467         _writing = true;
468         for (int i = 0; i < 16; ++i)
469                 _write_notes[i].clear();
470         
471         _dirty_controls.clear();
472         write_unlock();
473 }
474
475 /** Finish a write of events to the model.
476  *
477  * If \a delete_stuck is true and the current mode is Sustained, note on events
478  * that were never resolved with a corresonding note off will be deleted.
479  * Otherwise they will remain as notes with length 0.
480  */
481 void
482 Sequence::end_write(bool delete_stuck)
483 {
484         write_lock();
485         assert(_writing);
486
487         debugout << this << " END WRITE: " << _notes.size() << " NOTES\n";
488
489         if (!_percussive && delete_stuck) {
490                 for (Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
491                         if ((*n)->length() == 0) {
492                                 errorout << "WARNING: Stuck note lost: " << (*n)->note() << endl;
493                                 n = _notes.erase(n);
494                                 // we have to break here because erase invalidates the iterator
495                                 break;
496                         } else {
497                                 ++n;
498                         }
499                 }
500         }
501
502         for (int i = 0; i < 16; ++i) {
503                 if (!_write_notes[i].empty()) {
504                         errorout << "WARNING: Sequence::end_write: Channel " << i << " has "
505                                         << _write_notes[i].size() << " stuck notes" << endl;
506                 }
507                 _write_notes[i].clear();
508         }
509
510         for (ControlLists::const_iterator i = _dirty_controls.begin(); i != _dirty_controls.end(); ++i) {
511                 (*i)->mark_dirty();
512         }
513         
514         _writing = false;
515         write_unlock();
516 }
517
518 /** Append \a ev to model.  NOT realtime safe.
519  *
520  * Timestamps of events in \a buf are expected to be relative to
521  * the start of this model (t=0) and MUST be monotonically increasing
522  * and MUST be >= the latest event currently in the model.
523  */
524 void
525 Sequence::append(const Event& event)
526 {
527         write_lock();
528         _edited = true;
529         
530         const MIDIEvent& ev = (const MIDIEvent&)event;
531
532         assert(_notes.empty() || ev.time() >= _notes.back()->time());
533         assert(_writing);
534
535         if (ev.is_note_on()) {
536                 append_note_on_unlocked(ev.channel(), ev.time(), ev.note(),
537                                 ev.velocity());
538         } else if (ev.is_note_off()) {
539                 append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
540         } else if (!_type_map.type_is_midi(ev.event_type())) {
541                 printf("WARNING: Sequence: Unknown event type %X\n", ev.event_type());
542         } else if (ev.is_cc()) {
543                 append_control_unlocked(
544                                 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
545                                 ev.time(), ev.cc_value());
546         } else if (ev.is_pgm_change()) {
547                 append_control_unlocked(
548                                 Evoral::MIDI::ProgramChange(ev.event_type(), ev.channel()),
549                                 ev.time(), ev.pgm_number());
550         } else if (ev.is_pitch_bender()) {
551                 append_control_unlocked(
552                                 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
553                                 ev.time(), double(  (0x7F & ev.pitch_bender_msb()) << 7
554                                         | (0x7F & ev.pitch_bender_lsb()) ));
555         } else if (ev.is_channel_pressure()) {
556                 append_control_unlocked(
557                                 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
558                                 ev.time(), ev.channel_pressure());
559         } else {
560                 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
561         }
562
563         write_unlock();
564 }
565
566 void
567 Sequence::append_note_on_unlocked(uint8_t chan, EventTime time, uint8_t note_num, uint8_t velocity)
568 {
569         debugout << this << " c" << (int)chan << " note " << (int)note_num << " off @ " << time << endl;
570         assert(note_num <= 127);
571         assert(chan < 16);
572         assert(_writing);
573         _edited = true;
574
575         if (note_num < _lowest_note)
576                 _lowest_note = note_num;
577         if (note_num > _highest_note)
578                 _highest_note = note_num;
579
580         boost::shared_ptr<Note> new_note(new Note(chan, time, 0, note_num, velocity));
581         _notes.push_back(new_note);
582         if (!_percussive) {
583                 debugout << "Sustained: Appending active note on " << (unsigned)(uint8_t)note_num << endl;
584                 _write_notes[chan].push_back(_notes.size() - 1);
585         } else {
586                 debugout << "Percussive: NOT appending active note on" << endl;
587          }
588 }
589
590 void
591 Sequence::append_note_off_unlocked(uint8_t chan, EventTime time, uint8_t note_num)
592 {
593         debugout << this << " c" << (int)chan << " note " << (int)note_num << " off @ " << time << endl;
594         assert(note_num <= 127);
595         assert(chan < 16);
596         assert(_writing);
597         _edited = true;
598
599         if (_percussive) {
600                 debugout << "Sequence Ignoring note off (percussive mode)" << endl;
601                 return;
602         }
603
604         /* FIXME: make _write_notes fixed size (127 noted) for speed */
605
606         /* FIXME: note off velocity for that one guy out there who actually has
607          * keys that send it */
608
609         bool resolved = false;
610
611         for (WriteNotes::iterator n = _write_notes[chan].begin(); n
612                         != _write_notes[chan].end(); ++n) {
613                 Note& note = *_notes[*n].get();
614                 if (note.note() == note_num) {
615                         assert(time >= note.time());
616                         note.set_length(time - note.time());
617                         _write_notes[chan].erase(n);
618                         debugout << "resolved note, length: " << note.length() << endl;
619                         resolved = true;
620                         break;
621                 }
622         }
623
624         if (!resolved) {
625                 errorout << this << " spurious note off chan " << (int)chan
626                                 << ", note " << (int)note_num << " @ " << time << endl;
627         }
628 }
629
630 void
631 Sequence::append_control_unlocked(const Parameter& param, EventTime time, double value)
632 {
633         debugout << this << " " << _type_map.to_symbol(param) << " @ " << time << " \t= \t" << value
634                         << " # controls: " << _controls.size() << endl;
635         control(param, true)->list()->rt_add(time, value);
636 }
637
638
639 void
640 Sequence::add_note_unlocked(const boost::shared_ptr<Note> note)
641 {
642         debugout << this << " add note " << (int)note->note() << " @ " << note->time() << endl;
643         _edited = true;
644         Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note,
645                         note_time_comparator);
646         _notes.insert(i, note);
647 }
648
649 void
650 Sequence::remove_note_unlocked(const boost::shared_ptr<const Note> note)
651 {
652         _edited = true;
653         debugout << this << " remove note " << (int)note->note() << " @ " << note->time() << endl;
654         for (Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
655                 Note& _n = *(*n);
656                 const Note& _note = *note;
657                 // TODO: There is still the issue, that after restarting ardour
658                 // persisted undo does not work, because of rounding errors in the
659                 // event times after saving/restoring to/from MIDI files
660                 /*cerr << "======================================= " << endl;
661                 cerr << int(_n.note()) << "@" << int(_n.time()) << "[" << int(_n.channel()) << "] --" << int(_n.length()) << "-- #" << int(_n.velocity()) << endl;
662                 cerr << int(_note.note()) << "@" << int(_note.time()) << "[" << int(_note.channel()) << "] --" << int(_note.length()) << "-- #" << int(_note.velocity()) << endl;
663                 cerr << "Equal: " << bool(_n == _note) << endl;
664                 cerr << endl << endl;*/
665                 if (_n == _note) {
666                         _notes.erase(n);
667                         // we have to break here, because erase invalidates all iterators, ie. n itself
668                         break;
669                 }
670         }
671 }
672
673 /** Slow!  for debugging only. */
674 #ifndef NDEBUG
675 bool
676 Sequence::is_sorted() const {
677         bool t = 0;
678         for (Notes::const_iterator n = _notes.begin(); n != _notes.end(); ++n)
679                 if ((*n)->time() < t)
680                         return false;
681                 else
682                         t = (*n)->time();
683
684         return true;
685 }
686 #endif
687
688 } // namespace Evoral
689