1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
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
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.
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
27 #include "pbd/compose.h"
28 #include "pbd/error.h"
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"
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.
48 XXX: This is a hack. The time should probably be expressed in
49 seconds rather than beats, and should be configurable etc. etc.
51 static double const time_between_interpolated_controller_outputs = 1.0 / 256;
55 // Read iterator (const_iterator)
57 template<typename Time>
58 Sequence<Time>::const_iterator::const_iterator()
61 , _control_iter(_control_iters.end())
63 _event = boost::shared_ptr< Event<Time> >(new Event<Time>());
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)
70 , _active_patch_change_message (0)
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)
79 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
85 _lock = seq.read_lock();
87 // Find first note which begins at or after t
88 _note_iter = seq.note_lower_bound(t);
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) {
98 assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
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;
107 assert (_patch_change_iter == seq.patch_changes().end() || (*_patch_change_iter)->time() >= t);
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());
113 size_t earliest_control_index = 0;
114 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
116 if (filtered.find (i->first) != filtered.end()) {
117 /* this parameter is filtered, so don't bother setting up an iterator for it */
121 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
124 if (_force_discrete) {
125 ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t, x, y, true);
127 ret = i->second->list()->rt_safe_earliest_event_unlocked(t, x, y, true);
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));
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;
144 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
146 const ControlIterator new_iter(i->second->list(), x, y);
147 _control_iters.push_back(new_iter);
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;
158 _control_iter = _control_iters.begin() + earliest_control_index;
159 assert(_control_iter != _control_iters.end());
161 _control_iter = _control_iters.end();
164 // Now find the earliest event overall and point to it
167 if (_note_iter != seq.notes().end()) {
169 earliest_t = (*_note_iter)->time();
172 if (_sysex_iter != seq.sysexes().end()
173 && ((*_sysex_iter)->time() < earliest_t || _type == NIL)) {
175 earliest_t = (*_sysex_iter)->time();
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 ();
183 if (_control_iter != _control_iters.end()
184 && earliest_control.list && earliest_control.x >= t
185 && (earliest_control.x < earliest_t || _type == NIL)) {
187 earliest_t = earliest_control.x;
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);
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));
202 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at control event @ %1\n", earliest_t));
203 seq.control_to_midi_event(_event, earliest_control);
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));
213 if (_type == NIL || !_event || _event->size() == 0) {
214 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at end @ %1\n", t));
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(),
223 assert(midi_event_is_valid(_event->buffer(), _event->size()));
228 template<typename Time>
229 Sequence<Time>::const_iterator::~const_iterator()
233 template<typename Time>
235 Sequence<Time>::const_iterator::invalidate()
237 while (!_active_notes.empty()) {
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;
248 _control_iter = _control_iters.end();
252 template<typename Time>
253 const typename Sequence<Time>::const_iterator&
254 Sequence<Time>::const_iterator::operator++()
257 throw std::logic_error("Attempt to iterate past end of Sequence");
260 DEBUG_TRACE(DEBUG::Sequence, "Sequence::const_iterator++\n");
261 assert(_event && _event->buffer() && _event->size() > 0);
263 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
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;
279 // Increment past current event
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
293 ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
294 _control_iter->x + time_between_interpolated_controller_outputs, x, y, false
297 assert(!ret || x > _control_iter->x);
299 _control_iter->x = x;
300 _control_iter->y = y;
302 _control_iter->list.reset();
303 _control_iter->x = DBL_MAX;
304 _control_iter->y = DBL_MAX;
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) {
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;
330 // Now find the earliest event overall and point to it
332 Time earliest_t = std::numeric_limits<Time>::max();
334 // Next earliest note on
335 if (_note_iter != _seq->notes().end()) {
337 earliest_t = (*_note_iter)->time();
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())) {
346 if ((!_active_notes.empty())) {
348 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
350 earliest_t = _active_notes.top()->end_time();
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) {
358 earliest_t = _control_iter->x;
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) {
366 earliest_t = (*_sysex_iter)->time();
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();
378 // Set event to reflect new position
381 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
382 *_event = (*_note_iter)->on_event();
383 _active_notes.push(*_note_iter);
386 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
387 assert(!_active_notes.empty());
388 *_event = _active_notes.top()->off_event();
392 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
393 _seq->control_to_midi_event(_event, *_control_iter);
396 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
397 *_event = *(*_sysex_iter);
400 DEBUG_TRACE(DEBUG::Sequence, "iterator = patch change\n");
401 *_event = (*_patch_change_iter)->message (_active_patch_change_message);
404 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
408 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
413 template<typename Time>
415 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
417 if (_seq != other._seq) {
419 } else if (_is_end || other._is_end) {
420 return (_is_end == other._is_end);
421 } else if (_type != other._type) {
424 return (_event == other._event);
428 template<typename Time>
429 typename Sequence<Time>::const_iterator&
430 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
433 _event = other._event;
434 _active_notes = other._active_notes;
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;
445 _lock = _seq->read_lock();
450 if (other._control_iter == other._control_iters.end()) {
451 _control_iter = _control_iters.end();
453 const size_t index = other._control_iter - other._control_iters.begin();
454 _control_iter = _control_iters.begin() + index;
462 template<typename Time>
463 Sequence<Time>::Sequence(const TypeMap& type_map)
465 , _overlapping_pitches_accepted (true)
466 , _overlap_pitch_resolution (FirstOnFirstOff)
468 , _type_map(type_map)
469 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
474 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
475 assert(_end_iter._is_end);
476 assert( ! _end_iter._lock);
478 for (int i = 0; i < 16; ++i) {
483 template<typename Time>
484 Sequence<Time>::Sequence(const Sequence<Time>& other)
487 , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
488 , _overlap_pitch_resolution (other._overlap_pitch_resolution)
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)
496 for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
497 NotePtr n (new Note<Time> (**i));
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));
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);
511 for (int i = 0; i < 16; ++i) {
512 _bank[i] = other._bank[i];
515 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
516 assert(_end_iter._is_end);
517 assert(! _end_iter._lock);
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
525 template<typename Time>
527 Sequence<Time>::control_to_midi_event(
528 boost::shared_ptr< Event<Time> >& ev,
529 const ControlIterator& iter) const
531 assert(iter.list.get());
532 const uint32_t event_type = iter.list->parameter().type();
534 // initialize the event pointer with a new event, if necessary
536 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
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));
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);
548 ev->set_time(iter.x);
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;
555 case MIDI_CMD_PGM_CHANGE:
556 assert(iter.list.get());
557 assert(iter.list->parameter().channel() < 16);
558 assert(iter.y <= INT8_MAX);
560 ev->set_time(iter.x);
562 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
563 ev->buffer()[1] = (uint8_t)iter.y;
566 case MIDI_CMD_BENDER:
567 assert(iter.list.get());
568 assert(iter.list->parameter().channel() < 16);
569 assert(iter.y < (1<<14));
571 ev->set_time(iter.x);
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
578 case MIDI_CMD_CHANNEL_PRESSURE:
579 assert(iter.list.get());
580 assert(iter.list->parameter().channel() < 16);
581 assert(iter.y <= INT8_MAX);
583 ev->set_time(iter.x);
585 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
586 ev->buffer()[1] = (uint8_t)iter.y;
596 /** Clear all events from the model.
598 template<typename Time>
600 Sequence<Time>::clear()
602 WriteLock lock(write_lock());
604 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
605 li->second->list()->clear();
608 /** Begin a write of events to the model.
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
615 template<typename Time>
617 Sequence<Time>::start_write()
619 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
620 WriteLock lock(write_lock());
622 for (int i = 0; i < 16; ++i) {
623 _write_notes[i].clear();
627 /** Finish a write of events to the model.
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.
633 template<typename Time>
635 Sequence<Time>::end_write (StuckNoteOption option, Time when)
637 WriteLock lock(write_lock());
643 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes) delete stuck option %3 @ %4\n", this, _notes.size(), option, when));
645 #ifdef PERCUSSIVE_IGNORE_NOTE_OFFS
648 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
649 typename Notes::iterator next = n;
652 if ((*n)->length() == 0) {
656 case DeleteStuckNotes:
657 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
660 case ResolveStuckNotes:
661 if (when <= (*n)->time()) {
662 cerr << "WARNING: Stuck note resolution - end time @ "
663 << when << " is before note on: " << (**n) << endl;
666 (*n)->set_length (when - (*n)->time());
667 cerr << "WARNING: resolved note-on with no note-off to generate " << (**n) << endl;
675 #ifdef PERCUSSIVE_IGNORE_NOTE_OFFS
679 for (int i = 0; i < 16; ++i) {
680 _write_notes[i].clear();
687 template<typename Time>
689 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
691 /* This is the core method to add notes to a Sequence
694 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3 dur %4\n", this, (int)note->note(), note->time(), note->length()));
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()));
701 if (note->id() < 0) {
702 note->set_id (Evoral::next_event_id());
705 if (note->note() < _lowest_note)
706 _lowest_note = note->note();
707 if (note->note() > _highest_note)
708 _highest_note = note->note();
710 _notes.insert (note);
711 _pitches[note->channel()].insert (note);
718 template<typename Time>
720 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
726 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note %2 @ %3\n", this, (int)note->note(), note->time()));
728 typename Sequence<Time>::Notes::iterator i = note_lower_bound(note->time());
729 while (i != _notes.end() && (*i)->time() == note->time()) {
731 typename Sequence<Time>::Notes::iterator tmp = i;
738 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
741 if (n->note() == _lowest_note || n->note() == _highest_note) {
746 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
747 if ((*ii)->note() < _lowest_note)
748 _lowest_note = (*ii)->note();
749 if ((*ii)->note() > _highest_note)
750 _highest_note = (*ii)->note();
760 Pitches& p (pitches (note->channel()));
762 NotePtr search_note(new Note<Time>(0, 0, 0, note->note(), 0));
764 typename Pitches::iterator j = p.lower_bound (search_note);
765 while (j != p.end() && (*j)->note() == note->note()) {
766 typename Pitches::iterator tmp = j;
770 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*j)->note(), (*j)->time()));
778 cerr << "Unable to find note to erase matching " << *note.get() << endl;
782 template<typename Time>
784 Sequence<Time>::remove_patch_change_unlocked (const constPatchChangePtr p)
786 typename Sequence<Time>::PatchChanges::iterator i = patch_change_lower_bound (p->time ());
787 while (i != _patch_changes.end() && (*i)->time() == p->time()) {
789 typename Sequence<Time>::PatchChanges::iterator tmp = i;
793 _patch_changes.erase (i);
800 template<typename Time>
802 Sequence<Time>::remove_sysex_unlocked (const SysExPtr sysex)
804 typename Sequence<Time>::SysExes::iterator i = sysex_lower_bound (sysex->time ());
805 while (i != _sysexes.end() && (*i)->time() == sysex->time()) {
807 typename Sequence<Time>::SysExes::iterator tmp = i;
818 /** Append \a ev to model. NOT realtime safe.
820 * The timestamp of event is expected to be relative to
821 * the start of this model (t=0) and MUST be monotonically increasing
822 * and MUST be >= the latest event currently in the model.
824 template<typename Time>
826 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
828 WriteLock lock(write_lock());
830 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
832 assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
835 if (!midi_event_is_valid(ev.buffer(), ev.size())) {
836 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
840 if (ev.is_note_on()) {
841 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
842 append_note_on_unlocked (note, evid);
843 } else if (ev.is_note_off()) {
844 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
845 /* XXX note: event ID is discarded because we merge the on+off events into
848 append_note_off_unlocked (note);
849 } else if (ev.is_sysex()) {
850 append_sysex_unlocked(ev, evid);
851 } else if (ev.is_cc() && (ev.cc_number() == MIDI_CTL_MSB_BANK || ev.cc_number() == MIDI_CTL_LSB_BANK)) {
852 /* note bank numbers in our _bank[] array, so that we can write an event when the program change arrives */
853 if (ev.cc_number() == MIDI_CTL_MSB_BANK) {
854 _bank[ev.channel()] &= ~(0x7f << 7);
855 _bank[ev.channel()] |= ev.cc_value() << 7;
857 _bank[ev.channel()] &= ~0x7f;
858 _bank[ev.channel()] |= ev.cc_value();
860 } else if (ev.is_cc()) {
861 append_control_unlocked(
862 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
863 ev.time(), ev.cc_value(), evid);
864 } else if (ev.is_pgm_change()) {
865 /* write a patch change with this program change and any previously set-up bank number */
866 append_patch_change_unlocked (PatchChange<Time> (ev.time(), ev.channel(), ev.pgm_number(), _bank[ev.channel()]), evid);
867 } else if (ev.is_pitch_bender()) {
868 append_control_unlocked(
869 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
870 ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
871 | (0x7F & ev.pitch_bender_lsb())),
873 } else if (ev.is_channel_pressure()) {
874 append_control_unlocked(
875 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
876 ev.time(), ev.channel_pressure(), evid);
877 } else if (!_type_map.type_is_midi(ev.event_type())) {
878 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
879 for (size_t i=0; i < ev.size(); ++i) {
880 printf("%X ", ev.buffer()[i]);
884 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
890 template<typename Time>
892 Sequence<Time>::append_note_on_unlocked (NotePtr note, event_id_t evid)
894 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this,
895 (int) note->channel(), (int) note->note(),
896 note->time(), (int) note->velocity()));
899 if (note->note() > 127) {
900 error << string_compose (_("illegal note number (%1) used in Note on event - event will be ignored"), (int) note->note()) << endmsg;
903 if (note->channel() >= 16) {
904 error << string_compose (_("illegal channel number (%1) used in Note on event - event will be ignored"), (int) note->channel()) << endmsg;
908 if (note->id() < 0) {
912 if (note->velocity() == 0) {
913 append_note_off_unlocked (note);
917 add_note_unlocked (note);
919 #ifdef PERCUSSIVE_IGNORE_NOTE_OFFS
923 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sustained: Appending active note on %1 channel %2\n",
924 (unsigned)(uint8_t)note->note(), note->channel()));
925 _write_notes[note->channel()].insert (note);
927 #ifdef PERCUSSIVE_IGNORE_NOTE_OFFS
929 DEBUG_TRACE(DEBUG::Sequence, "Percussive: NOT appending active note on\n");
935 template<typename Time>
937 Sequence<Time>::append_note_off_unlocked (NotePtr note)
939 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 OFF @ %4 v=%5\n",
940 this, (int)note->channel(),
941 (int)note->note(), note->time(), (int)note->velocity()));
944 if (note->note() > 127) {
945 error << string_compose (_("illegal note number (%1) used in Note off event - event will be ignored"), (int) note->note()) << endmsg;
948 if (note->channel() >= 16) {
949 error << string_compose (_("illegal channel number (%1) used in Note off event - event will be ignored"), (int) note->channel()) << endmsg;
955 #ifdef PERCUSSIVE_IGNORE_NOTE_OFFS
957 DEBUG_TRACE(DEBUG::Sequence, "Sequence Ignoring note off (percussive mode)\n");
962 bool resolved = false;
964 /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
965 matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
966 whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
970 /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
972 for (typename WriteNotes::iterator n = _write_notes[note->channel()].begin(); n != _write_notes[note->channel()].end(); ) {
974 typename WriteNotes::iterator tmp = n;
978 if (note->note() == nn->note() && nn->channel() == note->channel()) {
979 assert(note->time() >= nn->time());
981 nn->set_length (note->time() - nn->time());
982 nn->set_off_velocity (note->velocity());
984 _write_notes[note->channel()].erase(n);
985 DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note @ %2 length: %1\n", nn->length(), nn->time()));
994 cerr << this << " spurious note off chan " << (int)note->channel()
995 << ", note " << (int)note->note() << " @ " << note->time() << endl;
999 template<typename Time>
1001 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t /* evid */)
1003 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3\t=\t%4 # controls: %5\n",
1004 this, _type_map.to_symbol(param), time, value, _controls.size()));
1005 boost::shared_ptr<Control> c = control(param, true);
1006 c->list()->add (time, value);
1007 /* XXX control events should use IDs */
1010 template<typename Time>
1012 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t /* evid */)
1014 #ifdef DEBUG_SEQUENCE
1015 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
1016 for (size_t i=0; i < ev.size(); ++i) {
1017 cerr << int(ev.buffer()[i]) << " ";
1018 } cerr << "]" << endl;
1021 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
1022 /* XXX sysex events should use IDs */
1023 _sysexes.insert(event);
1026 template<typename Time>
1028 Sequence<Time>::append_patch_change_unlocked (const PatchChange<Time>& ev, event_id_t id)
1030 PatchChangePtr p (new PatchChange<Time> (ev));
1036 _patch_changes.insert (p);
1039 template<typename Time>
1041 Sequence<Time>::add_patch_change_unlocked (PatchChangePtr p)
1044 p->set_id (Evoral::next_event_id ());
1047 _patch_changes.insert (p);
1050 template<typename Time>
1052 Sequence<Time>::add_sysex_unlocked (SysExPtr s)
1055 s->set_id (Evoral::next_event_id ());
1058 _sysexes.insert (s);
1061 template<typename Time>
1063 Sequence<Time>::contains (const NotePtr& note) const
1065 ReadLock lock (read_lock());
1066 return contains_unlocked (note);
1069 template<typename Time>
1071 Sequence<Time>::contains_unlocked (const NotePtr& note) const
1073 const Pitches& p (pitches (note->channel()));
1074 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
1076 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
1077 i != p.end() && (*i)->note() == note->note(); ++i) {
1087 template<typename Time>
1089 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
1091 ReadLock lock (read_lock());
1092 return overlaps_unlocked (note, without);
1095 template<typename Time>
1097 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
1099 Time sa = note->time();
1100 Time ea = note->end_time();
1102 const Pitches& p (pitches (note->channel()));
1103 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
1105 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
1106 i != p.end() && (*i)->note() == note->note(); ++i) {
1108 if (without && (**i) == *without) {
1112 Time sb = (*i)->time();
1113 Time eb = (*i)->end_time();
1115 if (((sb > sa) && (eb <= ea)) ||
1116 ((eb >= sa) && (eb <= ea)) ||
1117 ((sb > sa) && (sb <= ea)) ||
1118 ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
1126 template<typename Time>
1128 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
1133 /** Return the earliest note with time >= t */
1134 template<typename Time>
1135 typename Sequence<Time>::Notes::const_iterator
1136 Sequence<Time>::note_lower_bound (Time t) const
1138 NotePtr search_note(new Note<Time>(0, t, 0, 0, 0));
1139 typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
1140 assert(i == _notes.end() || (*i)->time() >= t);
1144 /** Return the earliest patch change with time >= t */
1145 template<typename Time>
1146 typename Sequence<Time>::PatchChanges::const_iterator
1147 Sequence<Time>::patch_change_lower_bound (Time t) const
1149 PatchChangePtr search (new PatchChange<Time> (t, 0, 0, 0));
1150 typename Sequence<Time>::PatchChanges::const_iterator i = _patch_changes.lower_bound (search);
1151 assert (i == _patch_changes.end() || (*i)->time() >= t);
1155 /** Return the earliest sysex with time >= t */
1156 template<typename Time>
1157 typename Sequence<Time>::SysExes::const_iterator
1158 Sequence<Time>::sysex_lower_bound (Time t) const
1160 SysExPtr search (new Event<Time> (0, t));
1161 typename Sequence<Time>::SysExes::const_iterator i = _sysexes.lower_bound (search);
1162 assert (i == _sysexes.end() || (*i)->time() >= t);
1166 template<typename Time>
1168 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1173 case PitchLessThanOrEqual:
1175 case PitchGreaterThanOrEqual:
1176 get_notes_by_pitch (n, op, val, chan_mask);
1180 case VelocityLessThan:
1181 case VelocityLessThanOrEqual:
1182 case VelocityGreater:
1183 case VelocityGreaterThanOrEqual:
1184 get_notes_by_velocity (n, op, val, chan_mask);
1189 template<typename Time>
1191 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1193 for (uint8_t c = 0; c < 16; ++c) {
1195 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
1199 const Pitches& p (pitches (c));
1200 NotePtr search_note(new Note<Time>(0, 0, 0, val, 0));
1201 typename Pitches::const_iterator i;
1204 i = p.lower_bound (search_note);
1205 while (i != p.end() && (*i)->note() == val) {
1210 i = p.upper_bound (search_note);
1211 while (i != p.end() && (*i)->note() < val) {
1215 case PitchLessThanOrEqual:
1216 i = p.upper_bound (search_note);
1217 while (i != p.end() && (*i)->note() <= val) {
1222 i = p.lower_bound (search_note);
1223 while (i != p.end() && (*i)->note() > val) {
1227 case PitchGreaterThanOrEqual:
1228 i = p.lower_bound (search_note);
1229 while (i != p.end() && (*i)->note() >= val) {
1235 //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1242 template<typename Time>
1244 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1246 ReadLock lock (read_lock());
1248 for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1250 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1256 if ((*i)->velocity() == val) {
1260 case VelocityLessThan:
1261 if ((*i)->velocity() < val) {
1265 case VelocityLessThanOrEqual:
1266 if ((*i)->velocity() <= val) {
1270 case VelocityGreater:
1271 if ((*i)->velocity() > val) {
1275 case VelocityGreaterThanOrEqual:
1276 if ((*i)->velocity() >= val) {
1281 // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1289 template<typename Time>
1291 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1293 _overlap_pitch_resolution = opr;
1295 /* XXX todo: clean up existing overlaps in source data? */
1298 template<typename Time>
1300 Sequence<Time>::control_list_marked_dirty ()
1305 template<typename Time>
1307 Sequence<Time>::dump (ostream& str) const
1309 typename Sequence<Time>::const_iterator i;
1310 str << "+++ dump\n";
1311 for (i = begin(); i != end(); ++i) {
1314 str << "--- dump\n";
1317 template class Sequence<Evoral::MusicalTime>;
1319 } // namespace Evoral