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
28 #include "evoral/Note.hpp"
31 #include "pbd/compose.h"
32 #include "pbd/error.h"
34 #include "evoral/Control.hpp"
35 #include "evoral/ControlList.hpp"
36 #include "evoral/ControlSet.hpp"
37 #include "evoral/EventSink.hpp"
38 #include "evoral/ParameterDescriptor.hpp"
39 #include "evoral/Sequence.hpp"
40 #include "evoral/TypeMap.hpp"
41 #include "evoral/midi_util.h"
48 /** Minimum time between MIDI outputs from a single interpolated controller,
49 expressed in beats. This is to limit the rate at which MIDI messages
50 are generated. It is only applied to interpolated controllers.
52 XXX: This is a hack. The time should probably be expressed in
53 seconds rather than beats, and should be configurable etc. etc.
55 static double const time_between_interpolated_controller_outputs = 1.0 / 256;
59 // Read iterator (const_iterator)
61 template<typename Time>
62 Sequence<Time>::const_iterator::const_iterator()
64 , _event(boost::shared_ptr< Event<Time> >(new Event<Time>()))
65 , _active_patch_change_message (0)
68 , _control_iter(_control_iters.end())
69 , _force_discrete(false)
73 /** @param force_discrete true to force ControlLists to use discrete evaluation, otherwise false to get them to use their configured mode */
74 template<typename Time>
75 Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq, Time t, bool force_discrete, std::set<Evoral::Parameter> const & filtered)
77 , _active_patch_change_message (0)
79 , _is_end((t == DBL_MAX) || seq.empty())
80 , _note_iter(seq.notes().end())
81 , _sysex_iter(seq.sysexes().end())
82 , _patch_change_iter(seq.patch_changes().end())
83 , _control_iter(_control_iters.end())
84 , _force_discrete (force_discrete)
86 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
92 _lock = seq.read_lock();
94 // Find first note which begins at or after t
95 _note_iter = seq.note_lower_bound(t);
97 // Find first sysex event at or after t
98 for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
99 i != seq.sysexes().end(); ++i) {
100 if ((*i)->time() >= t) {
105 assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
107 // Find first patch event at or after t
108 for (typename Sequence<Time>::PatchChanges::const_iterator i = seq.patch_changes().begin(); i != seq.patch_changes().end(); ++i) {
109 if ((*i)->time() >= t) {
110 _patch_change_iter = i;
114 assert (_patch_change_iter == seq.patch_changes().end() || (*_patch_change_iter)->time() >= t);
116 // Find first control event after t
117 _control_iters.reserve(seq._controls.size());
119 size_t earliest_control_index = 0;
120 double earliest_control_x = DBL_MAX;
121 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
123 if (filtered.find (i->first) != filtered.end()) {
124 /* this parameter is filtered, so don't bother setting up an iterator for it */
128 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
131 if (_force_discrete || i->second->list()->interpolation() == ControlList::Discrete) {
132 ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t.to_double(), x, y, true);
134 ret = i->second->list()->rt_safe_earliest_event_unlocked(t.to_double(), x, y, true);
137 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
138 i->first.id(), i->second->list()->size(), t));
144 const ParameterDescriptor& desc = seq.type_map().descriptor(i->first);
145 if (y < desc.lower || y > desc.upper) {
146 cerr << "ERROR: Controller value " << y
147 << " out of range [" << desc.lower << "," << desc.upper
148 << "], event ignored" << endl;
152 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
154 const ControlIterator new_iter(i->second->list(), x, y);
155 _control_iters.push_back(new_iter);
157 // Found a new earliest_control
158 if (x < earliest_control_x) {
159 earliest_control_x = x;
160 earliest_control_index = _control_iters.size() - 1;
166 _control_iter = _control_iters.begin() + earliest_control_index;
167 assert(_control_iter != _control_iters.end());
168 assert(_control_iter->list);
170 _control_iter = _control_iters.end();
173 // Choose the earliest event overall to point to
176 // Allocate a new event for storing the current event in MIDI format
177 _event = boost::shared_ptr< Event<Time> >(
178 new Event<Time>(0, Time(), 4, NULL, true));
180 // Set event from chosen sub-iterator
184 DEBUG_TRACE(DEBUG::Sequence,
185 string_compose("Starting at end @ %1\n", t));
187 DEBUG_TRACE(DEBUG::Sequence,
188 string_compose("Starting at type 0x%1 : 0x%2 @ %3\n",
189 (int)_event->event_type(),
190 (int)((MIDIEvent<Time>*)_event.get())->type(),
195 template<typename Time>
196 Sequence<Time>::const_iterator::~const_iterator()
200 template<typename Time>
202 Sequence<Time>::const_iterator::invalidate()
204 while (!_active_notes.empty()) {
210 _note_iter = _seq->notes().end();
211 _sysex_iter = _seq->sysexes().end();
212 _patch_change_iter = _seq->patch_changes().end();
213 _active_patch_change_message = 0;
215 _control_iters.clear();
216 _control_iter = _control_iters.end();
220 template<typename Time>
222 Sequence<Time>::const_iterator::choose_next(Time earliest_t)
226 // Next earliest note on
227 if (_note_iter != _seq->notes().end()) {
229 earliest_t = (*_note_iter)->time();
232 // Use the next note off iff it's earlier or the same time as the note on
233 if ((!_active_notes.empty())) {
234 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
236 earliest_t = _active_notes.top()->end_time();
240 // Use the next earliest controller iff it's earlier than the note event
241 if (_control_iter != _control_iters.end() &&
242 _control_iter->list && _control_iter->x != DBL_MAX) {
243 if (_type == NIL || _control_iter->x < earliest_t.to_double()) {
245 earliest_t = Time(_control_iter->x);
249 // Use the next earliest SysEx iff it's earlier than the controller
250 if (_sysex_iter != _seq->sysexes().end()) {
251 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
253 earliest_t = (*_sysex_iter)->time();
257 // Use the next earliest patch change iff it's earlier than the SysEx
258 if (_patch_change_iter != _seq->patch_changes().end()) {
259 if (_type == NIL || (*_patch_change_iter)->time() < earliest_t) {
260 _type = PATCH_CHANGE;
261 earliest_t = (*_patch_change_iter)->time();
267 template<typename Time>
269 Sequence<Time>::const_iterator::set_event()
273 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
274 *_event = (*_note_iter)->on_event();
275 _active_notes.push(*_note_iter);
278 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
279 assert(!_active_notes.empty());
280 *_event = _active_notes.top()->off_event();
284 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
285 *_event = *(*_sysex_iter);
288 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
289 _seq->control_to_midi_event(_event, *_control_iter);
292 DEBUG_TRACE(DEBUG::Sequence, "iterator = program change\n");
293 *_event = (*_patch_change_iter)->message (_active_patch_change_message);
300 if (_type == NIL || !_event || _event->size() == 0) {
301 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
305 assert(midi_event_is_valid(_event->buffer(), _event->size()));
309 template<typename Time>
310 const typename Sequence<Time>::const_iterator&
311 Sequence<Time>::const_iterator::operator++()
314 throw std::logic_error("Attempt to iterate past end of Sequence");
317 assert(_event && _event->buffer() && _event->size() > 0);
319 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
323 || ev.is_pgm_change()
324 || ev.is_pitch_bender()
325 || ev.is_channel_pressure()
326 || ev.is_sysex()) ) {
327 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
328 << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
335 // Increment past current event
343 // Increment current controller iterator
344 if (_force_discrete || _control_iter->list->interpolation() == ControlList::Discrete) {
345 ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (
346 _control_iter->x, x, y, false);
348 ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
349 _control_iter->x + time_between_interpolated_controller_outputs, x, y, false);
351 assert(!ret || x > _control_iter->x);
353 _control_iter->x = x;
354 _control_iter->y = y;
356 _control_iter->list.reset();
357 _control_iter->x = DBL_MAX;
358 _control_iter->y = DBL_MAX;
361 // Find the controller with the next earliest event time
362 _control_iter = _control_iters.begin();
363 for (ControlIterators::iterator i = _control_iters.begin();
364 i != _control_iters.end(); ++i) {
365 if (i->x < _control_iter->x) {
374 ++_active_patch_change_message;
375 if (_active_patch_change_message == (*_patch_change_iter)->messages()) {
376 ++_patch_change_iter;
377 _active_patch_change_message = 0;
384 // Choose the earliest event overall to point to
385 choose_next(std::numeric_limits<Time>::max());
387 // Set event from chosen sub-iterator
390 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
395 template<typename Time>
397 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
399 if (_seq != other._seq) {
401 } else if (_is_end || other._is_end) {
402 return (_is_end == other._is_end);
403 } else if (_type != other._type) {
406 return (_event == other._event);
410 template<typename Time>
411 typename Sequence<Time>::const_iterator&
412 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
415 _event = other._event;
416 _active_notes = other._active_notes;
418 _is_end = other._is_end;
419 _note_iter = other._note_iter;
420 _sysex_iter = other._sysex_iter;
421 _patch_change_iter = other._patch_change_iter;
422 _control_iters = other._control_iters;
423 _force_discrete = other._force_discrete;
424 _active_patch_change_message = other._active_patch_change_message;
427 _lock = _seq->read_lock();
432 if (other._control_iter == other._control_iters.end()) {
433 _control_iter = _control_iters.end();
435 const size_t index = other._control_iter - other._control_iters.begin();
436 _control_iter = _control_iters.begin() + index;
444 template<typename Time>
445 Sequence<Time>::Sequence(const TypeMap& type_map)
447 , _overlapping_pitches_accepted (true)
448 , _overlap_pitch_resolution (FirstOnFirstOff)
450 , _type_map(type_map)
451 , _end_iter(*this, std::numeric_limits<Time>::max(), false, std::set<Evoral::Parameter> ())
456 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
457 assert(_end_iter._is_end);
458 assert( ! _end_iter._lock);
460 for (int i = 0; i < 16; ++i) {
465 template<typename Time>
466 Sequence<Time>::Sequence(const Sequence<Time>& other)
469 , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
470 , _overlap_pitch_resolution (other._overlap_pitch_resolution)
472 , _type_map(other._type_map)
473 , _end_iter(*this, std::numeric_limits<Time>::max(), false, std::set<Evoral::Parameter> ())
474 , _percussive(other._percussive)
475 , _lowest_note(other._lowest_note)
476 , _highest_note(other._highest_note)
478 for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
479 NotePtr n (new Note<Time> (**i));
483 for (typename SysExes::const_iterator i = other._sysexes.begin(); i != other._sysexes.end(); ++i) {
484 boost::shared_ptr<Event<Time> > n (new Event<Time> (**i, true));
488 for (typename PatchChanges::const_iterator i = other._patch_changes.begin(); i != other._patch_changes.end(); ++i) {
489 PatchChangePtr n (new PatchChange<Time> (**i));
490 _patch_changes.insert (n);
493 for (int i = 0; i < 16; ++i) {
494 _bank[i] = other._bank[i];
497 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
498 assert(_end_iter._is_end);
499 assert(! _end_iter._lock);
502 /** Write the controller event pointed to by \a iter to \a ev.
503 * The buffer of \a ev will be allocated or resized as necessary.
504 * The event_type of \a ev should be set to the expected output type.
505 * \return true on success
507 template<typename Time>
509 Sequence<Time>::control_to_midi_event(
510 boost::shared_ptr< Event<Time> >& ev,
511 const ControlIterator& iter) const
513 assert(iter.list.get());
514 const uint32_t event_type = iter.list->parameter().type();
516 // initialize the event pointer with a new event, if necessary
518 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, Time(), 3, NULL, true));
521 uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
522 ev->set_event_type(_type_map.midi_event_type(midi_type));
524 case MIDI_CMD_CONTROL:
525 assert(iter.list.get());
526 assert(iter.list->parameter().channel() < 16);
527 assert(iter.list->parameter().id() <= INT8_MAX);
528 assert(iter.y <= INT8_MAX);
530 ev->set_time(Time(iter.x));
532 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
533 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
534 ev->buffer()[2] = (uint8_t)iter.y;
537 case MIDI_CMD_PGM_CHANGE:
538 assert(iter.list.get());
539 assert(iter.list->parameter().channel() < 16);
540 assert(iter.y <= INT8_MAX);
542 ev->set_time(Time(iter.x));
544 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
545 ev->buffer()[1] = (uint8_t)iter.y;
548 case MIDI_CMD_BENDER:
549 assert(iter.list.get());
550 assert(iter.list->parameter().channel() < 16);
551 assert(iter.y < (1<<14));
553 ev->set_time(Time(iter.x));
555 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
556 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
557 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
560 case MIDI_CMD_CHANNEL_PRESSURE:
561 assert(iter.list.get());
562 assert(iter.list->parameter().channel() < 16);
563 assert(iter.y <= INT8_MAX);
565 ev->set_time(Time(iter.x));
567 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
568 ev->buffer()[1] = (uint8_t)iter.y;
578 /** Clear all events from the model.
580 template<typename Time>
582 Sequence<Time>::clear()
584 WriteLock lock(write_lock());
586 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
587 li->second->list()->clear();
590 /** Begin a write of events to the model.
592 * If \a mode is Sustained, complete notes with length are constructed as note
593 * on/off events are received. Otherwise (Percussive), only note on events are
594 * stored; note off events are discarded entirely and all contained notes will
597 template<typename Time>
599 Sequence<Time>::start_write()
601 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
602 WriteLock lock(write_lock());
604 for (int i = 0; i < 16; ++i) {
605 _write_notes[i].clear();
609 /** Finish a write of events to the model.
611 * If \a delete_stuck is true and the current mode is Sustained, note on events
612 * that were never resolved with a corresonding note off will be deleted.
613 * Otherwise they will remain as notes with length 0.
615 template<typename Time>
617 Sequence<Time>::end_write (StuckNoteOption option, Time when)
619 WriteLock lock(write_lock());
625 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes) delete stuck option %3 @ %4\n", this, _notes.size(), option, when));
627 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
628 typename Notes::iterator next = n;
631 if (!(*n)->length()) {
635 case DeleteStuckNotes:
636 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
639 case ResolveStuckNotes:
640 if (when <= (*n)->time()) {
641 cerr << "WARNING: Stuck note resolution - end time @ "
642 << when << " is before note on: " << (**n) << endl;
645 (*n)->set_length (when - (*n)->time());
646 cerr << "WARNING: resolved note-on with no note-off to generate " << (**n) << endl;
655 for (int i = 0; i < 16; ++i) {
656 _write_notes[i].clear();
663 template<typename Time>
665 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
667 /* This is the core method to add notes to a Sequence
670 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3 dur %4\n", this, (int)note->note(), note->time(), note->length()));
672 if (resolve_overlaps_unlocked (note, arg)) {
673 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 DISALLOWED: note %2 @ %3\n", this, (int)note->note(), note->time()));
677 if (note->id() < 0) {
678 note->set_id (Evoral::next_event_id());
681 if (note->note() < _lowest_note)
682 _lowest_note = note->note();
683 if (note->note() > _highest_note)
684 _highest_note = note->note();
686 _notes.insert (note);
687 _pitches[note->channel()].insert (note);
694 template<typename Time>
696 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
699 bool id_matched = false;
701 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note #%2 %3 @ %4\n", this, note->id(), (int)note->note(), note->time()));
703 /* first try searching for the note using the time index, which is
704 * faster since the container is "indexed" by time. (technically, this
705 * means that lower_bound() can do a binary search rather than linear)
707 * this may not work, for reasons explained below.
710 typename Sequence<Time>::Notes::iterator i;
712 for (i = note_lower_bound(note->time()); i != _notes.end() && (*i)->time() == note->time(); ++i) {
716 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note #%2 %3 @ %4\n", this, (*i)->id(), (int)(*i)->note(), (*i)->time()));
719 if (note->note() == _lowest_note || note->note() == _highest_note) {
724 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
725 if ((*ii)->note() < _lowest_note)
726 _lowest_note = (*ii)->note();
727 if ((*ii)->note() > _highest_note)
728 _highest_note = (*ii)->note();
737 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\ttime-based lookup did not find note #%2 %3 @ %4\n", this, note->id(), (int)note->note(), note->time()));
741 /* if the note's time property was changed in tandem with some
742 * other property as the next operation after it was added to
743 * the sequence, then at the point where we call this to undo
744 * the add, the note we are targetting currently has a
745 * different time property than the one we we passed via
748 * in this scenario, we have no choice other than to linear
749 * search the list of notes and find the note by ID.
752 for (i = _notes.begin(); i != _notes.end(); ++i) {
754 if ((*i)->id() == note->id()) {
756 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\tID-based pass, erasing note #%2 %3 @ %4\n", this, (*i)->id(), (int)(*i)->note(), (*i)->time()));
759 if (note->note() == _lowest_note || note->note() == _highest_note) {
764 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
765 if ((*ii)->note() < _lowest_note)
766 _lowest_note = (*ii)->note();
767 if ((*ii)->note() > _highest_note)
768 _highest_note = (*ii)->note();
781 Pitches& p (pitches (note->channel()));
783 typename Pitches::iterator j;
785 /* if we had to ID-match above, we can't expect to find it in
786 * pitches via note comparison either. so do another linear
787 * search to locate it. otherwise, we can use the note index
788 * to potentially speed things up.
793 for (j = p.begin(); j != p.end(); ++j) {
794 if ((*j)->id() == note->id()) {
802 /* Now find the same note in the "pitches" list (which indexes
803 * notes by channel+time. We care only about its note number
804 * so the search_note has all other properties unset.
807 NotePtr search_note (new Note<Time>(0, Time(), Time(), note->note(), 0));
809 for (j = p.lower_bound (search_note); j != p.end() && (*j)->note() == note->note(); ++j) {
812 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*j)->note(), (*j)->time()));
820 warning << string_compose ("erased note %1 not found in pitches for channel %2", *note, (int) note->channel()) << endmsg;
826 cerr << "Unable to find note to erase matching " << *note.get() << endmsg;
830 template<typename Time>
832 Sequence<Time>::remove_patch_change_unlocked (const constPatchChangePtr p)
834 typename Sequence<Time>::PatchChanges::iterator i = patch_change_lower_bound (p->time ());
836 while (i != _patch_changes.end() && ((*i)->time() == p->time())) {
838 typename Sequence<Time>::PatchChanges::iterator tmp = i;
842 _patch_changes.erase (i);
849 template<typename Time>
851 Sequence<Time>::remove_sysex_unlocked (const SysExPtr sysex)
853 typename Sequence<Time>::SysExes::iterator i = sysex_lower_bound (sysex->time ());
854 while (i != _sysexes.end() && (*i)->time() == sysex->time()) {
856 typename Sequence<Time>::SysExes::iterator tmp = i;
867 /** Append \a ev to model. NOT realtime safe.
869 * The timestamp of event is expected to be relative to
870 * the start of this model (t=0) and MUST be monotonically increasing
871 * and MUST be >= the latest event currently in the model.
873 template<typename Time>
875 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
877 WriteLock lock(write_lock());
879 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
881 assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
884 if (!midi_event_is_valid(ev.buffer(), ev.size())) {
885 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
889 if (ev.is_note_on() && ev.velocity() > 0) {
890 append_note_on_unlocked (ev, evid);
891 } else if (ev.is_note_off() || (ev.is_note_on() && ev.velocity() == 0)) {
892 /* XXX note: event ID is discarded because we merge the on+off events into
895 append_note_off_unlocked (ev);
896 } else if (ev.is_sysex()) {
897 append_sysex_unlocked(ev, evid);
898 } else if (ev.is_cc() && (ev.cc_number() == MIDI_CTL_MSB_BANK || ev.cc_number() == MIDI_CTL_LSB_BANK)) {
899 /* note bank numbers in our _bank[] array, so that we can write an event when the program change arrives */
900 if (ev.cc_number() == MIDI_CTL_MSB_BANK) {
901 _bank[ev.channel()] &= ~(0x7f << 7);
902 _bank[ev.channel()] |= ev.cc_value() << 7;
904 _bank[ev.channel()] &= ~0x7f;
905 _bank[ev.channel()] |= ev.cc_value();
907 } else if (ev.is_cc()) {
908 append_control_unlocked(
909 Parameter(ev.event_type(), ev.channel(), ev.cc_number()),
910 ev.time(), ev.cc_value(), evid);
911 } else if (ev.is_pgm_change()) {
912 /* write a patch change with this program change and any previously set-up bank number */
913 append_patch_change_unlocked (PatchChange<Time> (ev.time(), ev.channel(), ev.pgm_number(), _bank[ev.channel()]), evid);
914 } else if (ev.is_pitch_bender()) {
915 append_control_unlocked(
916 Parameter(ev.event_type(), ev.channel()),
917 ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
918 | (0x7F & ev.pitch_bender_lsb())),
920 } else if (ev.is_channel_pressure()) {
921 append_control_unlocked(
922 Parameter(ev.event_type(), ev.channel()),
923 ev.time(), ev.channel_pressure(), evid);
924 } else if (!_type_map.type_is_midi(ev.event_type())) {
925 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
926 for (size_t i=0; i < ev.size(); ++i) {
927 printf("%X ", ev.buffer()[i]);
931 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
937 template<typename Time>
939 Sequence<Time>::append_note_on_unlocked (const MIDIEvent<Time>& ev, event_id_t evid)
941 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this,
942 (int)ev.channel(), (int)ev.note(),
943 ev.time(), (int)ev.velocity()));
946 if (ev.note() > 127) {
947 error << string_compose (_("invalid note on number (%1) ignored"), (int) ev.note()) << endmsg;
949 } else if (ev.channel() >= 16) {
950 error << string_compose (_("invalid note on channel (%1) ignored"), (int) ev.channel()) << endmsg;
952 } else if (ev.velocity() == 0) {
953 // Note on with velocity 0 handled as note off by caller
954 error << string_compose (_("invalid note on velocity (%1) ignored"), (int) ev.velocity()) << endmsg;
958 NotePtr note(new Note<Time>(ev.channel(), ev.time(), Time(), ev.note(), ev.velocity()));
961 add_note_unlocked (note);
963 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Appending active note on %1 channel %2\n",
964 (unsigned)(uint8_t)note->note(), note->channel()));
965 _write_notes[note->channel()].insert (note);
969 template<typename Time>
971 Sequence<Time>::append_note_off_unlocked (const MIDIEvent<Time>& ev)
973 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 OFF @ %4 v=%5\n",
974 this, (int)ev.channel(),
975 (int)ev.note(), ev.time(), (int)ev.velocity()));
978 if (ev.note() > 127) {
979 error << string_compose (_("invalid note off number (%1) ignored"), (int) ev.note()) << endmsg;
981 } else if (ev.channel() >= 16) {
982 error << string_compose (_("invalid note off channel (%1) ignored"), (int) ev.channel()) << endmsg;
988 bool resolved = false;
990 /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
991 matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
992 whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
996 /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
998 for (typename WriteNotes::iterator n = _write_notes[ev.channel()].begin(); n != _write_notes[ev.channel()].end(); ) {
1000 typename WriteNotes::iterator tmp = n;
1004 if (ev.note() == nn->note() && nn->channel() == ev.channel()) {
1005 assert(ev.time() >= nn->time());
1007 nn->set_length (ev.time() - nn->time());
1008 nn->set_off_velocity (ev.velocity());
1010 _write_notes[ev.channel()].erase(n);
1011 DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note @ %2 length: %1\n", nn->length(), nn->time()));
1020 cerr << this << " spurious note off chan " << (int)ev.channel()
1021 << ", note " << (int)ev.note() << " @ " << ev.time() << endl;
1025 template<typename Time>
1027 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t /* evid */)
1029 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3 = %4 # controls: %5\n",
1030 this, _type_map.to_symbol(param), time, value, _controls.size()));
1031 boost::shared_ptr<Control> c = control(param, true);
1032 c->list()->add (time.to_double(), value);
1033 /* XXX control events should use IDs */
1036 template<typename Time>
1038 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t /* evid */)
1040 #ifdef DEBUG_SEQUENCE
1041 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
1042 for (size_t i=0; i < ev.size(); ++i) {
1043 cerr << int(ev.buffer()[i]) << " ";
1044 } cerr << "]" << endl;
1047 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
1048 /* XXX sysex events should use IDs */
1049 _sysexes.insert(event);
1052 template<typename Time>
1054 Sequence<Time>::append_patch_change_unlocked (const PatchChange<Time>& ev, event_id_t id)
1056 PatchChangePtr p (new PatchChange<Time> (ev));
1062 _patch_changes.insert (p);
1065 template<typename Time>
1067 Sequence<Time>::add_patch_change_unlocked (PatchChangePtr p)
1070 p->set_id (Evoral::next_event_id ());
1073 _patch_changes.insert (p);
1076 template<typename Time>
1078 Sequence<Time>::add_sysex_unlocked (SysExPtr s)
1081 s->set_id (Evoral::next_event_id ());
1084 _sysexes.insert (s);
1087 template<typename Time>
1089 Sequence<Time>::contains (const NotePtr& note) const
1091 ReadLock lock (read_lock());
1092 return contains_unlocked (note);
1095 template<typename Time>
1097 Sequence<Time>::contains_unlocked (const NotePtr& note) const
1099 const Pitches& p (pitches (note->channel()));
1100 NotePtr search_note(new Note<Time>(0, Time(), Time(), note->note()));
1102 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
1103 i != p.end() && (*i)->note() == note->note(); ++i) {
1113 template<typename Time>
1115 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
1117 ReadLock lock (read_lock());
1118 return overlaps_unlocked (note, without);
1121 template<typename Time>
1123 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
1125 Time sa = note->time();
1126 Time ea = note->end_time();
1128 const Pitches& p (pitches (note->channel()));
1129 NotePtr search_note(new Note<Time>(0, Time(), Time(), note->note()));
1131 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
1132 i != p.end() && (*i)->note() == note->note(); ++i) {
1134 if (without && (**i) == *without) {
1138 Time sb = (*i)->time();
1139 Time eb = (*i)->end_time();
1141 if (((sb > sa) && (eb <= ea)) ||
1142 ((eb >= sa) && (eb <= ea)) ||
1143 ((sb > sa) && (sb <= ea)) ||
1144 ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
1152 template<typename Time>
1154 Sequence<Time>::set_notes (const typename Sequence<Time>::Notes& n)
1159 // CONST iterator implementations (x3)
1161 /** Return the earliest note with time >= t */
1162 template<typename Time>
1163 typename Sequence<Time>::Notes::const_iterator
1164 Sequence<Time>::note_lower_bound (Time t) const
1166 NotePtr search_note(new Note<Time>(0, t, Time(), 0, 0));
1167 typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
1168 assert(i == _notes.end() || (*i)->time() >= t);
1172 /** Return the earliest patch change with time >= t */
1173 template<typename Time>
1174 typename Sequence<Time>::PatchChanges::const_iterator
1175 Sequence<Time>::patch_change_lower_bound (Time t) const
1177 PatchChangePtr search (new PatchChange<Time> (t, 0, 0, 0));
1178 typename Sequence<Time>::PatchChanges::const_iterator i = _patch_changes.lower_bound (search);
1179 assert (i == _patch_changes.end() || (*i)->time() >= t);
1183 /** Return the earliest sysex with time >= t */
1184 template<typename Time>
1185 typename Sequence<Time>::SysExes::const_iterator
1186 Sequence<Time>::sysex_lower_bound (Time t) const
1188 SysExPtr search (new Event<Time> (0, t));
1189 typename Sequence<Time>::SysExes::const_iterator i = _sysexes.lower_bound (search);
1190 assert (i == _sysexes.end() || (*i)->time() >= t);
1194 // NON-CONST iterator implementations (x3)
1196 /** Return the earliest note with time >= t */
1197 template<typename Time>
1198 typename Sequence<Time>::Notes::iterator
1199 Sequence<Time>::note_lower_bound (Time t)
1201 NotePtr search_note(new Note<Time>(0, t, Time(), 0, 0));
1202 typename Sequence<Time>::Notes::iterator i = _notes.lower_bound(search_note);
1203 assert(i == _notes.end() || (*i)->time() >= t);
1207 /** Return the earliest patch change with time >= t */
1208 template<typename Time>
1209 typename Sequence<Time>::PatchChanges::iterator
1210 Sequence<Time>::patch_change_lower_bound (Time t)
1212 PatchChangePtr search (new PatchChange<Time> (t, 0, 0, 0));
1213 typename Sequence<Time>::PatchChanges::iterator i = _patch_changes.lower_bound (search);
1214 assert (i == _patch_changes.end() || (*i)->time() >= t);
1218 /** Return the earliest sysex with time >= t */
1219 template<typename Time>
1220 typename Sequence<Time>::SysExes::iterator
1221 Sequence<Time>::sysex_lower_bound (Time t)
1223 SysExPtr search (new Event<Time> (0, t));
1224 typename Sequence<Time>::SysExes::iterator i = _sysexes.lower_bound (search);
1225 assert (i == _sysexes.end() || (*i)->time() >= t);
1229 template<typename Time>
1231 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1236 case PitchLessThanOrEqual:
1238 case PitchGreaterThanOrEqual:
1239 get_notes_by_pitch (n, op, val, chan_mask);
1243 case VelocityLessThan:
1244 case VelocityLessThanOrEqual:
1245 case VelocityGreater:
1246 case VelocityGreaterThanOrEqual:
1247 get_notes_by_velocity (n, op, val, chan_mask);
1252 template<typename Time>
1254 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1256 for (uint8_t c = 0; c < 16; ++c) {
1258 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
1262 const Pitches& p (pitches (c));
1263 NotePtr search_note(new Note<Time>(0, Time(), Time(), val, 0));
1264 typename Pitches::const_iterator i;
1267 i = p.lower_bound (search_note);
1268 while (i != p.end() && (*i)->note() == val) {
1273 i = p.upper_bound (search_note);
1274 while (i != p.end() && (*i)->note() < val) {
1278 case PitchLessThanOrEqual:
1279 i = p.upper_bound (search_note);
1280 while (i != p.end() && (*i)->note() <= val) {
1285 i = p.lower_bound (search_note);
1286 while (i != p.end() && (*i)->note() > val) {
1290 case PitchGreaterThanOrEqual:
1291 i = p.lower_bound (search_note);
1292 while (i != p.end() && (*i)->note() >= val) {
1298 //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1299 abort(); /* NOTREACHED*/
1304 template<typename Time>
1306 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1308 ReadLock lock (read_lock());
1310 for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1312 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1318 if ((*i)->velocity() == val) {
1322 case VelocityLessThan:
1323 if ((*i)->velocity() < val) {
1327 case VelocityLessThanOrEqual:
1328 if ((*i)->velocity() <= val) {
1332 case VelocityGreater:
1333 if ((*i)->velocity() > val) {
1337 case VelocityGreaterThanOrEqual:
1338 if ((*i)->velocity() >= val) {
1343 // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1344 abort(); /* NOTREACHED*/
1350 template<typename Time>
1352 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1354 _overlap_pitch_resolution = opr;
1356 /* XXX todo: clean up existing overlaps in source data? */
1359 template<typename Time>
1361 Sequence<Time>::control_list_marked_dirty ()
1366 template<typename Time>
1368 Sequence<Time>::dump (ostream& str) const
1370 typename Sequence<Time>::const_iterator i;
1371 str << "+++ dump\n";
1372 for (i = begin(); i != end(); ++i) {
1375 str << "--- dump\n";
1378 template class Sequence<Evoral::Beats>;
1380 } // namespace Evoral