1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave 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
19 #define __STDC_LIMIT_MACROS 1
28 #include "pbd/compose.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 // Read iterator (const_iterator)
46 template<typename Time>
47 Sequence<Time>::const_iterator::const_iterator()
50 , _control_iter(_control_iters.end())
52 _event = boost::shared_ptr< Event<Time> >(new Event<Time>());
55 /** @param force_discrete true to force ControlLists to use discrete evaluation, otherwise false to get them to use their configured mode */
56 template<typename Time>
57 Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq, Time t, bool force_discrete, std::set<Evoral::Parameter> const & filtered)
60 , _is_end((t == DBL_MAX) || seq.empty())
61 , _note_iter(seq.notes().end())
62 , _sysex_iter(seq.sysexes().end())
63 , _control_iter(_control_iters.end())
64 , _force_discrete (force_discrete)
66 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
69 _lock = seq.read_lock();
74 typename Sequence<Time>::ReadLock lock(seq.read_lock());
76 // Find first note which begins at or after t
77 _note_iter = seq.note_lower_bound(t);
79 // Find first sysex event at or after t
80 for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
81 i != seq.sysexes().end(); ++i) {
82 if ((*i)->time() >= t) {
87 assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
89 // Find first control event after t
90 ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
91 _control_iters.reserve(seq._controls.size());
93 size_t earliest_control_index = 0;
94 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
96 if (filtered.find (i->first) != filtered.end()) {
97 /* this parameter is filtered, so don't bother setting up an iterator for it */
101 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
104 if (_force_discrete) {
105 ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t, x, y, true);
107 ret = i->second->list()->rt_safe_earliest_event_unlocked(t, x, y, true);
110 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
111 i->first.id(), i->second->list()->size(), t));
117 if (y < i->first.min() || y > i->first.max()) {
118 cerr << "ERROR: Controller value " << y
119 << " out of range [" << i->first.min() << "," << i->first.max()
120 << "], event ignored" << endl;
124 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
126 const ControlIterator new_iter(i->second->list(), x, y);
127 _control_iters.push_back(new_iter);
129 // Found a new earliest_control
130 if (x < earliest_control.x) {
131 earliest_control = new_iter;
132 earliest_control_index = _control_iters.size() - 1;
138 _control_iter = _control_iters.begin() + earliest_control_index;
139 assert(_control_iter != _control_iters.end());
141 _control_iter = _control_iters.end();
144 // Now find the earliest event overall and point to it
147 if (_note_iter != seq.notes().end()) {
149 earliest_t = (*_note_iter)->time();
152 if (_sysex_iter != seq.sysexes().end()
153 && ((*_sysex_iter)->time() < earliest_t || _type == NIL)) {
155 earliest_t = (*_sysex_iter)->time();
158 if (_control_iter != _control_iters.end()
159 && earliest_control.list && earliest_control.x >= t
160 && (earliest_control.x < earliest_t || _type == NIL)) {
162 earliest_t = earliest_control.x;
167 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at note on event @ %1\n", earliest_t));
168 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_note_iter)->on_event(), true));
169 _active_notes.push(*_note_iter);
172 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at sysex event @ %1\n", earliest_t));
173 _event = boost::shared_ptr< Event<Time> >(
174 new Event<Time>(*(*_sysex_iter), true));
177 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at control event @ %1\n", earliest_t));
178 seq.control_to_midi_event(_event, earliest_control);
184 if (_type == NIL || !_event || _event->size() == 0) {
185 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at end @ %1\n", t));
189 DEBUG_TRACE (DEBUG::Sequence, string_compose ("New iterator = 0x%x : 0x%x @ %f\n",
190 (int)_event->event_type(),
191 (int)((MIDIEvent<Time>*)_event.get())->type(),
193 assert(midi_event_is_valid(_event->buffer(), _event->size()));
197 template<typename Time>
198 Sequence<Time>::const_iterator::~const_iterator()
202 template<typename Time>
204 Sequence<Time>::const_iterator::invalidate()
206 while (!_active_notes.empty()) {
212 _note_iter = _seq->notes().end();
213 _sysex_iter = _seq->sysexes().end();
215 _control_iter = _control_iters.end();
219 template<typename Time>
220 const typename Sequence<Time>::const_iterator&
221 Sequence<Time>::const_iterator::operator++()
224 throw std::logic_error("Attempt to iterate past end of Sequence");
227 DEBUG_TRACE(DEBUG::Sequence, "Sequence::const_iterator++\n");
228 assert(_event && _event->buffer() && _event->size() > 0);
230 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
234 || ev.is_pgm_change()
235 || ev.is_pitch_bender()
236 || ev.is_channel_pressure()
237 || ev.is_sysex()) ) {
238 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
239 << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
246 // Increment past current event
254 // Increment current controller iterator
255 if (_force_discrete) {
256 ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (_control_iter->x, x, y, false);
258 ret = _control_iter->list->rt_safe_earliest_event_unlocked (_control_iter->x, x, y, false);
260 assert(!ret || x > _control_iter->x);
262 _control_iter->x = x;
263 _control_iter->y = y;
265 _control_iter->list.reset();
266 _control_iter->x = DBL_MAX;
267 _control_iter->y = DBL_MAX;
270 // Find the controller with the next earliest event time
271 _control_iter = _control_iters.begin();
272 for (ControlIterators::iterator i = _control_iters.begin();
273 i != _control_iters.end(); ++i) {
274 if (i->x < _control_iter->x) {
286 // Now find the earliest event overall and point to it
288 Time earliest_t = std::numeric_limits<Time>::max();
290 // Next earliest note on
291 if (_note_iter != _seq->notes().end()) {
293 earliest_t = (*_note_iter)->time();
296 // Use the next note off iff it's earlier or the same time as the note on
297 if (!_seq->percussive() && (!_active_notes.empty())) {
298 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
300 earliest_t = _active_notes.top()->end_time();
304 // Use the next earliest controller iff it's earlier than the note event
305 if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
306 if (_type == NIL || _control_iter->x < earliest_t) {
308 earliest_t = _control_iter->x;
312 // Use the next earliest SysEx iff it's earlier than the controller
313 if (_sysex_iter != _seq->sysexes().end()) {
314 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
316 earliest_t = (*_sysex_iter)->time();
320 // Set event to reflect new position
323 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
324 *_event = (*_note_iter)->on_event();
325 _active_notes.push(*_note_iter);
328 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
329 assert(!_active_notes.empty());
330 *_event = _active_notes.top()->off_event();
334 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
335 _seq->control_to_midi_event(_event, *_control_iter);
338 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
339 *_event = *(*_sysex_iter);
342 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
346 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
351 template<typename Time>
353 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
355 if (_seq != other._seq) {
357 } else if (_is_end || other._is_end) {
358 return (_is_end == other._is_end);
359 } else if (_type != other._type) {
362 return (_event == other._event);
366 template<typename Time>
367 typename Sequence<Time>::const_iterator&
368 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
371 _event = other._event;
372 _active_notes = other._active_notes;
374 _is_end = other._is_end;
375 _note_iter = other._note_iter;
376 _sysex_iter = other._sysex_iter;
377 _control_iters = other._control_iters;
378 _force_discrete = other._force_discrete;
381 _lock = _seq->read_lock();
385 if (other._control_iter == other._control_iters.end()) {
386 _control_iter = _control_iters.end();
388 const size_t index = other._control_iter - other._control_iters.begin();
389 _control_iter = _control_iters.begin() + index;
397 template<typename Time>
398 Sequence<Time>::Sequence(const TypeMap& type_map)
400 , _overlapping_pitches_accepted (true)
401 , _overlap_pitch_resolution (FirstOnFirstOff)
403 , _type_map(type_map)
404 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
409 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
410 assert(_end_iter._is_end);
411 assert( ! _end_iter._lock);
414 template<typename Time>
415 Sequence<Time>::Sequence(const Sequence<Time>& other)
418 , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
419 , _overlap_pitch_resolution (other._overlap_pitch_resolution)
421 , _type_map(other._type_map)
422 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
423 , _percussive(other._percussive)
424 , _lowest_note(other._lowest_note)
425 , _highest_note(other._highest_note)
427 for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
428 NotePtr n (new Note<Time> (**i));
432 for (typename SysExes::const_iterator i = other._sysexes.begin(); i != other._sysexes.end(); ++i) {
433 boost::shared_ptr<Event<Time> > n (new Event<Time> (**i, true));
434 _sysexes.push_back (n);
437 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
438 assert(_end_iter._is_end);
439 assert(! _end_iter._lock);
442 /** Write the controller event pointed to by \a iter to \a ev.
443 * The buffer of \a ev will be allocated or resized as necessary.
444 * The event_type of \a ev should be set to the expected output type.
445 * \return true on success
447 template<typename Time>
449 Sequence<Time>::control_to_midi_event(
450 boost::shared_ptr< Event<Time> >& ev,
451 const ControlIterator& iter) const
453 assert(iter.list.get());
454 const uint32_t event_type = iter.list->parameter().type();
456 // initialize the event pointer with a new event, if necessary
458 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
461 uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
462 ev->set_event_type(_type_map.midi_event_type(midi_type));
464 case MIDI_CMD_CONTROL:
465 assert(iter.list.get());
466 assert(iter.list->parameter().channel() < 16);
467 assert(iter.list->parameter().id() <= INT8_MAX);
468 assert(iter.y <= INT8_MAX);
472 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
473 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
474 ev->buffer()[2] = (uint8_t)iter.y;
477 case MIDI_CMD_PGM_CHANGE:
478 assert(iter.list.get());
479 assert(iter.list->parameter().channel() < 16);
480 assert(iter.y <= INT8_MAX);
484 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
485 ev->buffer()[1] = (uint8_t)iter.y;
488 case MIDI_CMD_BENDER:
489 assert(iter.list.get());
490 assert(iter.list->parameter().channel() < 16);
491 assert(iter.y < (1<<14));
495 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
496 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
497 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
500 case MIDI_CMD_CHANNEL_PRESSURE:
501 assert(iter.list.get());
502 assert(iter.list->parameter().channel() < 16);
503 assert(iter.y <= INT8_MAX);
507 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
508 ev->buffer()[1] = (uint8_t)iter.y;
518 /** Clear all events from the model.
520 template<typename Time>
522 Sequence<Time>::clear()
524 WriteLock lock(write_lock());
526 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
527 li->second->list()->clear();
530 /** Begin a write of events to the model.
532 * If \a mode is Sustained, complete notes with length are constructed as note
533 * on/off events are received. Otherwise (Percussive), only note on events are
534 * stored; note off events are discarded entirely and all contained notes will
537 template<typename Time>
539 Sequence<Time>::start_write()
541 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
542 WriteLock lock(write_lock());
544 for (int i = 0; i < 16; ++i) {
545 _write_notes[i].clear();
549 /** Finish a write of events to the model.
551 * If \a delete_stuck is true and the current mode is Sustained, note on events
552 * that were never resolved with a corresonding note off will be deleted.
553 * Otherwise they will remain as notes with length 0.
555 template<typename Time>
557 Sequence<Time>::end_write (bool delete_stuck)
559 WriteLock lock(write_lock());
565 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes)\n", this, _notes.size()));
567 if (!_percussive && delete_stuck) {
568 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
569 typename Notes::iterator next = n;
571 if ((*n)->length() == 0) {
572 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
580 for (int i = 0; i < 16; ++i) {
581 if (!_write_notes[i].empty()) {
582 cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
583 << _write_notes[i].size() << " stuck notes" << endl;
585 _write_notes[i].clear();
592 template<typename Time>
594 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
596 /* This is the core method to add notes to a Sequence
599 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3\n", this, (int)note->note(), note->time()));
601 if (resolve_overlaps_unlocked (note, arg)) {
602 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 DISALLOWED: note %2 @ %3\n", this, (int)note->note(), note->time()));
606 if (note->id() < 0) {
607 note->set_id (Evoral::next_event_id());
610 if (note->note() < _lowest_note)
611 _lowest_note = note->note();
612 if (note->note() > _highest_note)
613 _highest_note = note->note();
615 _notes.insert (note);
616 _pitches[note->channel()].insert (note);
623 template<typename Time>
625 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
631 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note %2 @ %3\n", this, (int)note->note(), note->time()));
633 for (typename Sequence<Time>::Notes::iterator i = note_lower_bound(note->time());
634 i != _notes.end() && (*i)->time() == note->time(); ++i) {
638 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
641 if ((*i)->note() == _lowest_note || (*i)->note() == _highest_note) {
646 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
647 if ((*ii)->note() < _lowest_note)
648 _lowest_note = (*ii)->note();
649 if ((*ii)->note() > _highest_note)
650 _highest_note = (*ii)->note();
658 Pitches& p (pitches (note->channel()));
660 NotePtr search_note(new Note<Time>(0, 0, 0, note->note(), 0));
662 for (typename Pitches::iterator i = p.lower_bound (search_note);
663 i != p.end() && (*i)->note() == note->note(); ++i) {
665 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
671 cerr << "Unable to find note to erase" << endl;
675 /** Append \a ev to model. NOT realtime safe.
677 * The timestamp of event is expected to be relative to
678 * the start of this model (t=0) and MUST be monotonically increasing
679 * and MUST be >= the latest event currently in the model.
681 template<typename Time>
683 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
685 WriteLock lock(write_lock());
687 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
689 assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
692 if (!midi_event_is_valid(ev.buffer(), ev.size())) {
693 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
697 if (ev.is_note_on()) {
698 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
699 append_note_on_unlocked (note, evid);
700 } else if (ev.is_note_off()) {
701 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
702 /* XXX note: event ID is discarded because we merge the on+off events into
705 append_note_off_unlocked (note);
706 } else if (ev.is_sysex()) {
707 append_sysex_unlocked(ev, evid);
708 } else if (ev.is_cc()) {
709 append_control_unlocked(
710 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
711 ev.time(), ev.cc_value(), evid);
712 } else if (ev.is_pgm_change()) {
713 append_control_unlocked(
714 Evoral::MIDI::ProgramChange(ev.event_type(), ev.channel()),
715 ev.time(), ev.pgm_number(), evid);
716 } else if (ev.is_pitch_bender()) {
717 append_control_unlocked(
718 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
719 ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
720 | (0x7F & ev.pitch_bender_lsb())),
722 } else if (ev.is_channel_pressure()) {
723 append_control_unlocked(
724 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
725 ev.time(), ev.channel_pressure(), evid);
726 } else if (!_type_map.type_is_midi(ev.event_type())) {
727 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
728 for (size_t i=0; i < ev.size(); ++i) {
729 printf("%X ", ev.buffer()[i]);
733 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
739 template<typename Time>
741 Sequence<Time>::append_note_on_unlocked (NotePtr note, event_id_t evid)
743 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this,
744 (int) note->channel(), (int) note->note(),
745 note->time(), (int) note->velocity()));
746 assert(note->note() <= 127);
747 assert(note->channel() < 16);
750 if (note->id() < 0) {
754 if (note->velocity() == 0) {
755 append_note_off_unlocked (note);
759 add_note_unlocked (note);
762 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sustained: Appending active note on %1 channel %2\n",
763 (unsigned)(uint8_t)note->note(), note->channel()));
764 _write_notes[note->channel()].insert (note);
766 DEBUG_TRACE(DEBUG::Sequence, "Percussive: NOT appending active note on\n");
770 template<typename Time>
772 Sequence<Time>::append_note_off_unlocked (NotePtr note)
774 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n",
775 this, (int)note->channel(),
776 (int)note->note(), note->time(), (int)note->velocity()));
777 assert(note->note() <= 127);
778 assert(note->channel() < 16);
783 DEBUG_TRACE(DEBUG::Sequence, "Sequence Ignoring note off (percussive mode)\n");
787 bool resolved = false;
789 /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
790 matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
791 whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
795 /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
797 for (typename WriteNotes::iterator n = _write_notes[note->channel()].begin(); n != _write_notes[note->channel()].end(); ++n) {
799 if (note->note() == nn->note() && nn->channel() == note->channel()) {
800 assert(note->time() >= nn->time());
802 nn->set_length (note->time() - nn->time());
803 nn->set_off_velocity (note->velocity());
805 _write_notes[note->channel()].erase(n);
806 DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note, length: %1\n", note->length()));
813 cerr << this << " spurious note off chan " << (int)note->channel()
814 << ", note " << (int)note->note() << " @ " << note->time() << endl;
818 template<typename Time>
820 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t evid)
822 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3\t=\t%4 # controls: %5\n",
823 this, _type_map.to_symbol(param), time, value, _controls.size()));
824 boost::shared_ptr<Control> c = control(param, true);
825 c->list()->rt_add(time, value);
826 /* XXX control events should use IDs */
829 template<typename Time>
831 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t evid)
833 #ifdef DEBUG_SEQUENCE
834 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
835 for (size_t i=0; i < ev.size(); ++i) {
836 cerr << int(ev.buffer()[i]) << " ";
837 } cerr << "]" << endl;
840 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
841 /* XXX sysex events should use IDs */
842 _sysexes.push_back(event);
845 template<typename Time>
847 Sequence<Time>::contains (const NotePtr& note) const
849 ReadLock lock (read_lock());
850 return contains_unlocked (note);
853 template<typename Time>
855 Sequence<Time>::contains_unlocked (const NotePtr& note) const
857 const Pitches& p (pitches (note->channel()));
858 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
860 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
861 i != p.end() && (*i)->note() == note->note(); ++i) {
871 template<typename Time>
873 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
875 ReadLock lock (read_lock());
876 return overlaps_unlocked (note, without);
879 template<typename Time>
881 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
883 Time sa = note->time();
884 Time ea = note->end_time();
886 const Pitches& p (pitches (note->channel()));
887 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
889 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
890 i != p.end() && (*i)->note() == note->note(); ++i) {
892 if (without && (**i) == *without) {
896 Time sb = (*i)->time();
897 Time eb = (*i)->end_time();
899 if (((sb > sa) && (eb <= ea)) ||
900 ((eb >= sa) && (eb <= ea)) ||
901 ((sb > sa) && (sb <= ea)) ||
902 ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
910 template<typename Time>
912 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
917 /** Return the earliest note with time >= t */
918 template<typename Time>
919 typename Sequence<Time>::Notes::const_iterator
920 Sequence<Time>::note_lower_bound (Time t) const
922 NotePtr search_note(new Note<Time>(0, t, 0, 0, 0));
923 typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
924 assert(i == _notes.end() || (*i)->time() >= t);
928 template<typename Time>
930 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
935 case PitchLessThanOrEqual:
937 case PitchGreaterThanOrEqual:
938 get_notes_by_pitch (n, op, val, chan_mask);
942 case VelocityLessThan:
943 case VelocityLessThanOrEqual:
944 case VelocityGreater:
945 case VelocityGreaterThanOrEqual:
946 get_notes_by_velocity (n, op, val, chan_mask);
951 template<typename Time>
953 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
955 for (uint8_t c = 0; c < 16; ++c) {
957 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
961 const Pitches& p (pitches (c));
962 NotePtr search_note(new Note<Time>(0, 0, 0, val, 0));
963 typename Pitches::const_iterator i;
966 i = p.lower_bound (search_note);
967 while (i != p.end() && (*i)->note() == val) {
972 i = p.upper_bound (search_note);
973 while (i != p.end() && (*i)->note() < val) {
977 case PitchLessThanOrEqual:
978 i = p.upper_bound (search_note);
979 while (i != p.end() && (*i)->note() <= val) {
984 i = p.lower_bound (search_note);
985 while (i != p.end() && (*i)->note() > val) {
989 case PitchGreaterThanOrEqual:
990 i = p.lower_bound (search_note);
991 while (i != p.end() && (*i)->note() >= val) {
997 //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1004 template<typename Time>
1006 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1008 ReadLock lock (read_lock());
1010 for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1012 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1018 if ((*i)->velocity() == val) {
1022 case VelocityLessThan:
1023 if ((*i)->velocity() < val) {
1027 case VelocityLessThanOrEqual:
1028 if ((*i)->velocity() <= val) {
1032 case VelocityGreater:
1033 if ((*i)->velocity() > val) {
1037 case VelocityGreaterThanOrEqual:
1038 if ((*i)->velocity() >= val) {
1043 // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1051 template<typename Time>
1053 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1055 _overlap_pitch_resolution = opr;
1057 /* XXX todo: clean up existing overlaps in source data? */
1060 template<typename Time>
1062 Sequence<Time>::control_list_marked_dirty ()
1067 template class Sequence<Evoral::MusicalTime>;
1069 } // namespace Evoral