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