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
27 #include "pbd/compose.h"
29 #include "evoral/Control.hpp"
30 #include "evoral/ControlList.hpp"
31 #include "evoral/ControlSet.hpp"
32 #include "evoral/EventSink.hpp"
33 #include "evoral/MIDIParameters.hpp"
34 #include "evoral/Sequence.hpp"
35 #include "evoral/TypeMap.hpp"
36 #include "evoral/midi_util.h"
41 /** Minimum time between MIDI outputs from a single interpolated controller,
42 expressed in beats. This is to limit the rate at which MIDI messages
43 are generated. It is only applied to interpolated controllers.
45 XXX: This is a hack. The time should probably be expressed in
46 seconds rather than beats, and should be configurable etc. etc.
48 static double const time_between_interpolated_controller_outputs = 1.0 / 256;
52 // Read iterator (const_iterator)
54 template<typename Time>
55 Sequence<Time>::const_iterator::const_iterator()
58 , _control_iter(_control_iters.end())
60 _event = boost::shared_ptr< Event<Time> >(new Event<Time>());
63 /** @param force_discrete true to force ControlLists to use discrete evaluation, otherwise false to get them to use their configured mode */
64 template<typename Time>
65 Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq, Time t, bool force_discrete, std::set<Evoral::Parameter> const & filtered)
68 , _is_end((t == DBL_MAX) || seq.empty())
69 , _note_iter(seq.notes().end())
70 , _sysex_iter(seq.sysexes().end())
71 , _control_iter(_control_iters.end())
72 , _force_discrete (force_discrete)
74 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
77 _lock = seq.read_lock();
82 typename Sequence<Time>::ReadLock lock(seq.read_lock());
84 // Find first note which begins at or after t
85 _note_iter = seq.note_lower_bound(t);
87 // Find first sysex event at or after t
88 for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
89 i != seq.sysexes().end(); ++i) {
90 if ((*i)->time() >= t) {
95 assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
97 // Find first control event after t
98 ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
99 _control_iters.reserve(seq._controls.size());
101 size_t earliest_control_index = 0;
102 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
104 if (filtered.find (i->first) != filtered.end()) {
105 /* this parameter is filtered, so don't bother setting up an iterator for it */
109 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
112 if (_force_discrete) {
113 ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t, x, y, true);
115 ret = i->second->list()->rt_safe_earliest_event_unlocked(t, x, y, true);
118 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
119 i->first.id(), i->second->list()->size(), t));
125 if (y < i->first.min() || y > i->first.max()) {
126 cerr << "ERROR: Controller value " << y
127 << " out of range [" << i->first.min() << "," << i->first.max()
128 << "], event ignored" << endl;
132 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
134 const ControlIterator new_iter(i->second->list(), x, y);
135 _control_iters.push_back(new_iter);
137 // Found a new earliest_control
138 if (x < earliest_control.x) {
139 earliest_control = new_iter;
140 earliest_control_index = _control_iters.size() - 1;
146 _control_iter = _control_iters.begin() + earliest_control_index;
147 assert(_control_iter != _control_iters.end());
149 _control_iter = _control_iters.end();
152 // Now find the earliest event overall and point to it
155 if (_note_iter != seq.notes().end()) {
157 earliest_t = (*_note_iter)->time();
160 if (_sysex_iter != seq.sysexes().end()
161 && ((*_sysex_iter)->time() < earliest_t || _type == NIL)) {
163 earliest_t = (*_sysex_iter)->time();
166 if (_control_iter != _control_iters.end()
167 && earliest_control.list && earliest_control.x >= t
168 && (earliest_control.x < earliest_t || _type == NIL)) {
170 earliest_t = earliest_control.x;
175 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at note on event @ %1\n", earliest_t));
176 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_note_iter)->on_event(), true));
177 _active_notes.push(*_note_iter);
180 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at sysex event @ %1\n", earliest_t));
181 _event = boost::shared_ptr< Event<Time> >(
182 new Event<Time>(*(*_sysex_iter), true));
185 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at control event @ %1\n", earliest_t));
186 seq.control_to_midi_event(_event, earliest_control);
192 if (_type == NIL || !_event || _event->size() == 0) {
193 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at end @ %1\n", t));
197 DEBUG_TRACE (DEBUG::Sequence, string_compose ("New iterator = 0x%x : 0x%x @ %f\n",
198 (int)_event->event_type(),
199 (int)((MIDIEvent<Time>*)_event.get())->type(),
201 assert(midi_event_is_valid(_event->buffer(), _event->size()));
205 template<typename Time>
206 Sequence<Time>::const_iterator::~const_iterator()
210 template<typename Time>
212 Sequence<Time>::const_iterator::invalidate()
214 while (!_active_notes.empty()) {
220 _note_iter = _seq->notes().end();
221 _sysex_iter = _seq->sysexes().end();
223 _control_iter = _control_iters.end();
227 template<typename Time>
228 const typename Sequence<Time>::const_iterator&
229 Sequence<Time>::const_iterator::operator++()
232 throw std::logic_error("Attempt to iterate past end of Sequence");
235 DEBUG_TRACE(DEBUG::Sequence, "Sequence::const_iterator++\n");
236 assert(_event && _event->buffer() && _event->size() > 0);
238 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
242 || ev.is_pgm_change()
243 || ev.is_pitch_bender()
244 || ev.is_channel_pressure()
245 || ev.is_sysex()) ) {
246 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
247 << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
254 // Increment past current event
262 // Increment current controller iterator
263 if (_force_discrete || _control_iter->list->interpolation() == ControlList::Discrete) {
264 ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (
265 _control_iter->x, x, y, false
268 ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
269 _control_iter->x + time_between_interpolated_controller_outputs, x, y, false
272 assert(!ret || x > _control_iter->x);
274 _control_iter->x = x;
275 _control_iter->y = y;
277 _control_iter->list.reset();
278 _control_iter->x = DBL_MAX;
279 _control_iter->y = DBL_MAX;
282 // Find the controller with the next earliest event time
283 _control_iter = _control_iters.begin();
284 for (ControlIterators::iterator i = _control_iters.begin();
285 i != _control_iters.end(); ++i) {
286 if (i->x < _control_iter->x) {
298 // Now find the earliest event overall and point to it
300 Time earliest_t = std::numeric_limits<Time>::max();
302 // Next earliest note on
303 if (_note_iter != _seq->notes().end()) {
305 earliest_t = (*_note_iter)->time();
308 // Use the next note off iff it's earlier or the same time as the note on
309 if (!_seq->percussive() && (!_active_notes.empty())) {
310 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
312 earliest_t = _active_notes.top()->end_time();
316 // Use the next earliest controller iff it's earlier than the note event
317 if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
318 if (_type == NIL || _control_iter->x < earliest_t) {
320 earliest_t = _control_iter->x;
324 // Use the next earliest SysEx iff it's earlier than the controller
325 if (_sysex_iter != _seq->sysexes().end()) {
326 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
328 earliest_t = (*_sysex_iter)->time();
332 // Set event to reflect new position
335 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
336 *_event = (*_note_iter)->on_event();
337 _active_notes.push(*_note_iter);
340 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
341 assert(!_active_notes.empty());
342 *_event = _active_notes.top()->off_event();
346 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
347 _seq->control_to_midi_event(_event, *_control_iter);
350 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
351 *_event = *(*_sysex_iter);
354 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
358 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
363 template<typename Time>
365 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
367 if (_seq != other._seq) {
369 } else if (_is_end || other._is_end) {
370 return (_is_end == other._is_end);
371 } else if (_type != other._type) {
374 return (_event == other._event);
378 template<typename Time>
379 typename Sequence<Time>::const_iterator&
380 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
383 _event = other._event;
384 _active_notes = other._active_notes;
386 _is_end = other._is_end;
387 _note_iter = other._note_iter;
388 _sysex_iter = other._sysex_iter;
389 _control_iters = other._control_iters;
390 _force_discrete = other._force_discrete;
393 _lock = _seq->read_lock();
397 if (other._control_iter == other._control_iters.end()) {
398 _control_iter = _control_iters.end();
400 const size_t index = other._control_iter - other._control_iters.begin();
401 _control_iter = _control_iters.begin() + index;
409 template<typename Time>
410 Sequence<Time>::Sequence(const TypeMap& type_map)
412 , _overlapping_pitches_accepted (true)
413 , _overlap_pitch_resolution (FirstOnFirstOff)
415 , _type_map(type_map)
416 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
421 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
422 assert(_end_iter._is_end);
423 assert( ! _end_iter._lock);
426 template<typename Time>
427 Sequence<Time>::Sequence(const Sequence<Time>& other)
430 , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
431 , _overlap_pitch_resolution (other._overlap_pitch_resolution)
433 , _type_map(other._type_map)
434 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
435 , _percussive(other._percussive)
436 , _lowest_note(other._lowest_note)
437 , _highest_note(other._highest_note)
439 for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
440 NotePtr n (new Note<Time> (**i));
444 for (typename SysExes::const_iterator i = other._sysexes.begin(); i != other._sysexes.end(); ++i) {
445 boost::shared_ptr<Event<Time> > n (new Event<Time> (**i, true));
446 _sysexes.push_back (n);
449 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
450 assert(_end_iter._is_end);
451 assert(! _end_iter._lock);
454 /** Write the controller event pointed to by \a iter to \a ev.
455 * The buffer of \a ev will be allocated or resized as necessary.
456 * The event_type of \a ev should be set to the expected output type.
457 * \return true on success
459 template<typename Time>
461 Sequence<Time>::control_to_midi_event(
462 boost::shared_ptr< Event<Time> >& ev,
463 const ControlIterator& iter) const
465 assert(iter.list.get());
466 const uint32_t event_type = iter.list->parameter().type();
468 // initialize the event pointer with a new event, if necessary
470 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
473 uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
474 ev->set_event_type(_type_map.midi_event_type(midi_type));
476 case MIDI_CMD_CONTROL:
477 assert(iter.list.get());
478 assert(iter.list->parameter().channel() < 16);
479 assert(iter.list->parameter().id() <= INT8_MAX);
480 assert(iter.y <= INT8_MAX);
484 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
485 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
486 ev->buffer()[2] = (uint8_t)iter.y;
489 case MIDI_CMD_PGM_CHANGE:
490 assert(iter.list.get());
491 assert(iter.list->parameter().channel() < 16);
492 assert(iter.y <= INT8_MAX);
496 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
497 ev->buffer()[1] = (uint8_t)iter.y;
500 case MIDI_CMD_BENDER:
501 assert(iter.list.get());
502 assert(iter.list->parameter().channel() < 16);
503 assert(iter.y < (1<<14));
507 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
508 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
509 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
512 case MIDI_CMD_CHANNEL_PRESSURE:
513 assert(iter.list.get());
514 assert(iter.list->parameter().channel() < 16);
515 assert(iter.y <= INT8_MAX);
519 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
520 ev->buffer()[1] = (uint8_t)iter.y;
530 /** Clear all events from the model.
532 template<typename Time>
534 Sequence<Time>::clear()
536 WriteLock lock(write_lock());
538 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
539 li->second->list()->clear();
542 /** Begin a write of events to the model.
544 * If \a mode is Sustained, complete notes with length are constructed as note
545 * on/off events are received. Otherwise (Percussive), only note on events are
546 * stored; note off events are discarded entirely and all contained notes will
549 template<typename Time>
551 Sequence<Time>::start_write()
553 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
554 WriteLock lock(write_lock());
556 for (int i = 0; i < 16; ++i) {
557 _write_notes[i].clear();
561 /** Finish a write of events to the model.
563 * If \a delete_stuck is true and the current mode is Sustained, note on events
564 * that were never resolved with a corresonding note off will be deleted.
565 * Otherwise they will remain as notes with length 0.
567 template<typename Time>
569 Sequence<Time>::end_write (bool delete_stuck)
571 WriteLock lock(write_lock());
577 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes)\n", this, _notes.size()));
579 if (!_percussive && delete_stuck) {
580 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
581 typename Notes::iterator next = n;
583 if ((*n)->length() == 0) {
584 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
592 for (int i = 0; i < 16; ++i) {
593 if (!_write_notes[i].empty()) {
594 cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
595 << _write_notes[i].size() << " stuck notes" << endl;
597 _write_notes[i].clear();
604 template<typename Time>
606 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
608 /* This is the core method to add notes to a Sequence
611 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3\n", this, (int)note->note(), note->time()));
613 if (resolve_overlaps_unlocked (note, arg)) {
614 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 DISALLOWED: note %2 @ %3\n", this, (int)note->note(), note->time()));
618 if (note->id() < 0) {
619 note->set_id (Evoral::next_event_id());
622 if (note->note() < _lowest_note)
623 _lowest_note = note->note();
624 if (note->note() > _highest_note)
625 _highest_note = note->note();
627 _notes.insert (note);
628 _pitches[note->channel()].insert (note);
635 template<typename Time>
637 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
643 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note %2 @ %3\n", this, (int)note->note(), note->time()));
645 for (typename Sequence<Time>::Notes::iterator i = note_lower_bound(note->time());
646 i != _notes.end() && (*i)->time() == note->time(); ++i) {
650 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
653 if ((*i)->note() == _lowest_note || (*i)->note() == _highest_note) {
658 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
659 if ((*ii)->note() < _lowest_note)
660 _lowest_note = (*ii)->note();
661 if ((*ii)->note() > _highest_note)
662 _highest_note = (*ii)->note();
670 Pitches& p (pitches (note->channel()));
672 NotePtr search_note(new Note<Time>(0, 0, 0, note->note(), 0));
674 for (typename Pitches::iterator i = p.lower_bound (search_note);
675 i != p.end() && (*i)->note() == note->note(); ++i) {
677 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
683 cerr << "Unable to find note to erase" << endl;
687 /** Append \a ev to model. NOT realtime safe.
689 * The timestamp of event is expected to be relative to
690 * the start of this model (t=0) and MUST be monotonically increasing
691 * and MUST be >= the latest event currently in the model.
693 template<typename Time>
695 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
697 WriteLock lock(write_lock());
699 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
701 assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
704 if (!midi_event_is_valid(ev.buffer(), ev.size())) {
705 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
709 if (ev.is_note_on()) {
710 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
711 append_note_on_unlocked (note, evid);
712 } else if (ev.is_note_off()) {
713 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
714 /* XXX note: event ID is discarded because we merge the on+off events into
717 append_note_off_unlocked (note);
718 } else if (ev.is_sysex()) {
719 append_sysex_unlocked(ev, evid);
720 } else if (ev.is_cc()) {
721 append_control_unlocked(
722 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
723 ev.time(), ev.cc_value(), evid);
724 } else if (ev.is_pgm_change()) {
725 append_control_unlocked(
726 Evoral::MIDI::ProgramChange(ev.event_type(), ev.channel()),
727 ev.time(), ev.pgm_number(), evid);
728 } else if (ev.is_pitch_bender()) {
729 append_control_unlocked(
730 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
731 ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
732 | (0x7F & ev.pitch_bender_lsb())),
734 } else if (ev.is_channel_pressure()) {
735 append_control_unlocked(
736 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
737 ev.time(), ev.channel_pressure(), evid);
738 } else if (!_type_map.type_is_midi(ev.event_type())) {
739 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
740 for (size_t i=0; i < ev.size(); ++i) {
741 printf("%X ", ev.buffer()[i]);
745 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
751 template<typename Time>
753 Sequence<Time>::append_note_on_unlocked (NotePtr note, event_id_t evid)
755 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this,
756 (int) note->channel(), (int) note->note(),
757 note->time(), (int) note->velocity()));
758 assert(note->note() <= 127);
759 assert(note->channel() < 16);
762 if (note->id() < 0) {
766 if (note->velocity() == 0) {
767 append_note_off_unlocked (note);
771 add_note_unlocked (note);
774 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sustained: Appending active note on %1 channel %2\n",
775 (unsigned)(uint8_t)note->note(), note->channel()));
776 _write_notes[note->channel()].insert (note);
778 DEBUG_TRACE(DEBUG::Sequence, "Percussive: NOT appending active note on\n");
782 template<typename Time>
784 Sequence<Time>::append_note_off_unlocked (NotePtr note)
786 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n",
787 this, (int)note->channel(),
788 (int)note->note(), note->time(), (int)note->velocity()));
789 assert(note->note() <= 127);
790 assert(note->channel() < 16);
795 DEBUG_TRACE(DEBUG::Sequence, "Sequence Ignoring note off (percussive mode)\n");
799 bool resolved = false;
801 /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
802 matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
803 whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
807 /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
809 for (typename WriteNotes::iterator n = _write_notes[note->channel()].begin(); n != _write_notes[note->channel()].end(); ++n) {
811 if (note->note() == nn->note() && nn->channel() == note->channel()) {
812 assert(note->time() >= nn->time());
814 nn->set_length (note->time() - nn->time());
815 nn->set_off_velocity (note->velocity());
817 _write_notes[note->channel()].erase(n);
818 DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note, length: %1\n", note->length()));
825 cerr << this << " spurious note off chan " << (int)note->channel()
826 << ", note " << (int)note->note() << " @ " << note->time() << endl;
830 template<typename Time>
832 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t /* evid */)
834 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3\t=\t%4 # controls: %5\n",
835 this, _type_map.to_symbol(param), time, value, _controls.size()));
836 boost::shared_ptr<Control> c = control(param, true);
837 c->list()->add (time, value);
838 /* XXX control events should use IDs */
841 template<typename Time>
843 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t /* evid */)
845 #ifdef DEBUG_SEQUENCE
846 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
847 for (size_t i=0; i < ev.size(); ++i) {
848 cerr << int(ev.buffer()[i]) << " ";
849 } cerr << "]" << endl;
852 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
853 /* XXX sysex events should use IDs */
854 _sysexes.push_back(event);
857 template<typename Time>
859 Sequence<Time>::contains (const NotePtr& note) const
861 ReadLock lock (read_lock());
862 return contains_unlocked (note);
865 template<typename Time>
867 Sequence<Time>::contains_unlocked (const NotePtr& note) const
869 const Pitches& p (pitches (note->channel()));
870 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
872 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
873 i != p.end() && (*i)->note() == note->note(); ++i) {
883 template<typename Time>
885 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
887 ReadLock lock (read_lock());
888 return overlaps_unlocked (note, without);
891 template<typename Time>
893 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
895 Time sa = note->time();
896 Time ea = note->end_time();
898 const Pitches& p (pitches (note->channel()));
899 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
901 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
902 i != p.end() && (*i)->note() == note->note(); ++i) {
904 if (without && (**i) == *without) {
908 Time sb = (*i)->time();
909 Time eb = (*i)->end_time();
911 if (((sb > sa) && (eb <= ea)) ||
912 ((eb >= sa) && (eb <= ea)) ||
913 ((sb > sa) && (sb <= ea)) ||
914 ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
922 template<typename Time>
924 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
929 /** Return the earliest note with time >= t */
930 template<typename Time>
931 typename Sequence<Time>::Notes::const_iterator
932 Sequence<Time>::note_lower_bound (Time t) const
934 NotePtr search_note(new Note<Time>(0, t, 0, 0, 0));
935 typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
936 assert(i == _notes.end() || (*i)->time() >= t);
940 template<typename Time>
942 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
947 case PitchLessThanOrEqual:
949 case PitchGreaterThanOrEqual:
950 get_notes_by_pitch (n, op, val, chan_mask);
954 case VelocityLessThan:
955 case VelocityLessThanOrEqual:
956 case VelocityGreater:
957 case VelocityGreaterThanOrEqual:
958 get_notes_by_velocity (n, op, val, chan_mask);
963 template<typename Time>
965 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
967 for (uint8_t c = 0; c < 16; ++c) {
969 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
973 const Pitches& p (pitches (c));
974 NotePtr search_note(new Note<Time>(0, 0, 0, val, 0));
975 typename Pitches::const_iterator i;
978 i = p.lower_bound (search_note);
979 while (i != p.end() && (*i)->note() == val) {
984 i = p.upper_bound (search_note);
985 while (i != p.end() && (*i)->note() < val) {
989 case PitchLessThanOrEqual:
990 i = p.upper_bound (search_note);
991 while (i != p.end() && (*i)->note() <= val) {
996 i = p.lower_bound (search_note);
997 while (i != p.end() && (*i)->note() > val) {
1001 case PitchGreaterThanOrEqual:
1002 i = p.lower_bound (search_note);
1003 while (i != p.end() && (*i)->note() >= val) {
1009 //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1016 template<typename Time>
1018 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1020 ReadLock lock (read_lock());
1022 for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1024 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1030 if ((*i)->velocity() == val) {
1034 case VelocityLessThan:
1035 if ((*i)->velocity() < val) {
1039 case VelocityLessThanOrEqual:
1040 if ((*i)->velocity() <= val) {
1044 case VelocityGreater:
1045 if ((*i)->velocity() > val) {
1049 case VelocityGreaterThanOrEqual:
1050 if ((*i)->velocity() >= val) {
1055 // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1063 template<typename Time>
1065 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1067 _overlap_pitch_resolution = opr;
1069 /* XXX todo: clean up existing overlaps in source data? */
1072 template<typename Time>
1074 Sequence<Time>::control_list_marked_dirty ()
1079 template class Sequence<Evoral::MusicalTime>;
1081 } // namespace Evoral