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"
42 /** Minimum time between MIDI outputs from a single interpolated controller,
43 expressed in beats. This is to limit the rate at which MIDI messages
44 are generated. It is only applied to interpolated controllers.
46 XXX: This is a hack. The time should probably be expressed in
47 seconds rather than beats, and should be configurable etc. etc.
49 static double const time_between_interpolated_controller_outputs = 1.0 / 256;
53 // Read iterator (const_iterator)
55 template<typename Time>
56 Sequence<Time>::const_iterator::const_iterator()
59 , _control_iter(_control_iters.end())
61 _event = boost::shared_ptr< Event<Time> >(new Event<Time>());
64 /** @param force_discrete true to force ControlLists to use discrete evaluation, otherwise false to get them to use their configured mode */
65 template<typename Time>
66 Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq, Time t, bool force_discrete, std::set<Evoral::Parameter> const & filtered)
69 , _is_end((t == DBL_MAX) || seq.empty())
70 , _note_iter(seq.notes().end())
71 , _sysex_iter(seq.sysexes().end())
72 , _control_iter(_control_iters.end())
73 , _force_discrete (force_discrete)
75 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
78 _lock = seq.read_lock();
83 typename Sequence<Time>::ReadLock lock(seq.read_lock());
85 // Find first note which begins at or after t
86 _note_iter = seq.note_lower_bound(t);
88 // Find first sysex event at or after t
89 for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
90 i != seq.sysexes().end(); ++i) {
91 if ((*i)->time() >= t) {
96 assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
98 // Find first control event after t
99 ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
100 _control_iters.reserve(seq._controls.size());
102 size_t earliest_control_index = 0;
103 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
105 if (filtered.find (i->first) != filtered.end()) {
106 /* this parameter is filtered, so don't bother setting up an iterator for it */
110 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
113 if (_force_discrete) {
114 ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t, x, y, true);
116 ret = i->second->list()->rt_safe_earliest_event_unlocked(t, x, y, true);
119 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
120 i->first.id(), i->second->list()->size(), t));
126 if (y < i->first.min() || y > i->first.max()) {
127 cerr << "ERROR: Controller value " << y
128 << " out of range [" << i->first.min() << "," << i->first.max()
129 << "], event ignored" << endl;
133 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
135 const ControlIterator new_iter(i->second->list(), x, y);
136 _control_iters.push_back(new_iter);
138 // Found a new earliest_control
139 if (x < earliest_control.x) {
140 earliest_control = new_iter;
141 earliest_control_index = _control_iters.size() - 1;
147 _control_iter = _control_iters.begin() + earliest_control_index;
148 assert(_control_iter != _control_iters.end());
150 _control_iter = _control_iters.end();
153 // Now find the earliest event overall and point to it
156 if (_note_iter != seq.notes().end()) {
158 earliest_t = (*_note_iter)->time();
161 if (_sysex_iter != seq.sysexes().end()
162 && ((*_sysex_iter)->time() < earliest_t || _type == NIL)) {
164 earliest_t = (*_sysex_iter)->time();
167 if (_control_iter != _control_iters.end()
168 && earliest_control.list && earliest_control.x >= t
169 && (earliest_control.x < earliest_t || _type == NIL)) {
171 earliest_t = earliest_control.x;
176 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at note on event @ %1\n", earliest_t));
177 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_note_iter)->on_event(), true));
178 _active_notes.push(*_note_iter);
181 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at sysex event @ %1\n", earliest_t));
182 _event = boost::shared_ptr< Event<Time> >(
183 new Event<Time>(*(*_sysex_iter), true));
186 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at control event @ %1\n", earliest_t));
187 seq.control_to_midi_event(_event, earliest_control);
193 if (_type == NIL || !_event || _event->size() == 0) {
194 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at end @ %1\n", t));
198 DEBUG_TRACE (DEBUG::Sequence, string_compose ("New iterator = 0x%x : 0x%x @ %f\n",
199 (int)_event->event_type(),
200 (int)((MIDIEvent<Time>*)_event.get())->type(),
202 assert(midi_event_is_valid(_event->buffer(), _event->size()));
206 template<typename Time>
207 Sequence<Time>::const_iterator::~const_iterator()
211 template<typename Time>
213 Sequence<Time>::const_iterator::invalidate()
215 while (!_active_notes.empty()) {
221 _note_iter = _seq->notes().end();
222 _sysex_iter = _seq->sysexes().end();
224 _control_iter = _control_iters.end();
228 template<typename Time>
229 const typename Sequence<Time>::const_iterator&
230 Sequence<Time>::const_iterator::operator++()
233 throw std::logic_error("Attempt to iterate past end of Sequence");
236 DEBUG_TRACE(DEBUG::Sequence, "Sequence::const_iterator++\n");
237 assert(_event && _event->buffer() && _event->size() > 0);
239 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
243 || ev.is_pgm_change()
244 || ev.is_pitch_bender()
245 || ev.is_channel_pressure()
246 || ev.is_sysex()) ) {
247 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
248 << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
255 // Increment past current event
263 // Increment current controller iterator
264 if (_force_discrete || _control_iter->list->interpolation() == ControlList::Discrete) {
265 ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (
266 _control_iter->x, x, y, false
269 ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
270 _control_iter->x + time_between_interpolated_controller_outputs, x, y, false
273 assert(!ret || x > _control_iter->x);
275 _control_iter->x = x;
276 _control_iter->y = y;
278 _control_iter->list.reset();
279 _control_iter->x = DBL_MAX;
280 _control_iter->y = DBL_MAX;
283 // Find the controller with the next earliest event time
284 _control_iter = _control_iters.begin();
285 for (ControlIterators::iterator i = _control_iters.begin();
286 i != _control_iters.end(); ++i) {
287 if (i->x < _control_iter->x) {
299 // Now find the earliest event overall and point to it
301 Time earliest_t = std::numeric_limits<Time>::max();
303 // Next earliest note on
304 if (_note_iter != _seq->notes().end()) {
306 earliest_t = (*_note_iter)->time();
309 // Use the next note off iff it's earlier or the same time as the note on
310 if (!_seq->percussive() && (!_active_notes.empty())) {
311 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
313 earliest_t = _active_notes.top()->end_time();
317 // Use the next earliest controller iff it's earlier than the note event
318 if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
319 if (_type == NIL || _control_iter->x < earliest_t) {
321 earliest_t = _control_iter->x;
325 // Use the next earliest SysEx iff it's earlier than the controller
326 if (_sysex_iter != _seq->sysexes().end()) {
327 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
329 earliest_t = (*_sysex_iter)->time();
333 // Set event to reflect new position
336 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
337 *_event = (*_note_iter)->on_event();
338 _active_notes.push(*_note_iter);
341 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
342 assert(!_active_notes.empty());
343 *_event = _active_notes.top()->off_event();
347 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
348 _seq->control_to_midi_event(_event, *_control_iter);
351 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
352 *_event = *(*_sysex_iter);
355 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
359 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
364 template<typename Time>
366 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
368 if (_seq != other._seq) {
370 } else if (_is_end || other._is_end) {
371 return (_is_end == other._is_end);
372 } else if (_type != other._type) {
375 return (_event == other._event);
379 template<typename Time>
380 typename Sequence<Time>::const_iterator&
381 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
384 _event = other._event;
385 _active_notes = other._active_notes;
387 _is_end = other._is_end;
388 _note_iter = other._note_iter;
389 _sysex_iter = other._sysex_iter;
390 _control_iters = other._control_iters;
391 _force_discrete = other._force_discrete;
394 _lock = _seq->read_lock();
398 if (other._control_iter == other._control_iters.end()) {
399 _control_iter = _control_iters.end();
401 const size_t index = other._control_iter - other._control_iters.begin();
402 _control_iter = _control_iters.begin() + index;
410 template<typename Time>
411 Sequence<Time>::Sequence(const TypeMap& type_map)
413 , _overlapping_pitches_accepted (true)
414 , _overlap_pitch_resolution (FirstOnFirstOff)
416 , _type_map(type_map)
417 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
422 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
423 assert(_end_iter._is_end);
424 assert( ! _end_iter._lock);
427 template<typename Time>
428 Sequence<Time>::Sequence(const Sequence<Time>& other)
431 , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
432 , _overlap_pitch_resolution (other._overlap_pitch_resolution)
434 , _type_map(other._type_map)
435 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
436 , _percussive(other._percussive)
437 , _lowest_note(other._lowest_note)
438 , _highest_note(other._highest_note)
440 for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
441 NotePtr n (new Note<Time> (**i));
445 for (typename SysExes::const_iterator i = other._sysexes.begin(); i != other._sysexes.end(); ++i) {
446 boost::shared_ptr<Event<Time> > n (new Event<Time> (**i, true));
447 _sysexes.push_back (n);
450 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
451 assert(_end_iter._is_end);
452 assert(! _end_iter._lock);
455 /** Write the controller event pointed to by \a iter to \a ev.
456 * The buffer of \a ev will be allocated or resized as necessary.
457 * The event_type of \a ev should be set to the expected output type.
458 * \return true on success
460 template<typename Time>
462 Sequence<Time>::control_to_midi_event(
463 boost::shared_ptr< Event<Time> >& ev,
464 const ControlIterator& iter) const
466 assert(iter.list.get());
467 const uint32_t event_type = iter.list->parameter().type();
469 // initialize the event pointer with a new event, if necessary
471 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
474 uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
475 ev->set_event_type(_type_map.midi_event_type(midi_type));
477 case MIDI_CMD_CONTROL:
478 assert(iter.list.get());
479 assert(iter.list->parameter().channel() < 16);
480 assert(iter.list->parameter().id() <= INT8_MAX);
481 assert(iter.y <= INT8_MAX);
485 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
486 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
487 ev->buffer()[2] = (uint8_t)iter.y;
490 case MIDI_CMD_PGM_CHANGE:
491 assert(iter.list.get());
492 assert(iter.list->parameter().channel() < 16);
493 assert(iter.y <= INT8_MAX);
497 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
498 ev->buffer()[1] = (uint8_t)iter.y;
501 case MIDI_CMD_BENDER:
502 assert(iter.list.get());
503 assert(iter.list->parameter().channel() < 16);
504 assert(iter.y < (1<<14));
508 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
509 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
510 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
513 case MIDI_CMD_CHANNEL_PRESSURE:
514 assert(iter.list.get());
515 assert(iter.list->parameter().channel() < 16);
516 assert(iter.y <= INT8_MAX);
520 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
521 ev->buffer()[1] = (uint8_t)iter.y;
531 /** Clear all events from the model.
533 template<typename Time>
535 Sequence<Time>::clear()
537 WriteLock lock(write_lock());
539 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
540 li->second->list()->clear();
543 /** Begin a write of events to the model.
545 * If \a mode is Sustained, complete notes with length are constructed as note
546 * on/off events are received. Otherwise (Percussive), only note on events are
547 * stored; note off events are discarded entirely and all contained notes will
550 template<typename Time>
552 Sequence<Time>::start_write()
554 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
555 WriteLock lock(write_lock());
557 for (int i = 0; i < 16; ++i) {
558 _write_notes[i].clear();
562 /** Finish a write of events to the model.
564 * If \a delete_stuck is true and the current mode is Sustained, note on events
565 * that were never resolved with a corresonding note off will be deleted.
566 * Otherwise they will remain as notes with length 0.
568 template<typename Time>
570 Sequence<Time>::end_write (bool delete_stuck)
572 WriteLock lock(write_lock());
578 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes)\n", this, _notes.size()));
580 if (!_percussive && delete_stuck) {
581 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
582 typename Notes::iterator next = n;
584 if ((*n)->length() == 0) {
585 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
593 for (int i = 0; i < 16; ++i) {
594 if (!_write_notes[i].empty()) {
595 cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
596 << _write_notes[i].size() << " stuck notes" << endl;
598 _write_notes[i].clear();
605 template<typename Time>
607 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
609 /* This is the core method to add notes to a Sequence
612 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3\n", this, (int)note->note(), note->time()));
614 if (resolve_overlaps_unlocked (note, arg)) {
615 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 DISALLOWED: note %2 @ %3\n", this, (int)note->note(), note->time()));
619 if (note->id() < 0) {
620 note->set_id (Evoral::next_event_id());
623 if (note->note() < _lowest_note)
624 _lowest_note = note->note();
625 if (note->note() > _highest_note)
626 _highest_note = note->note();
628 _notes.insert (note);
629 _pitches[note->channel()].insert (note);
636 template<typename Time>
638 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
644 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note %2 @ %3\n", this, (int)note->note(), note->time()));
646 for (typename Sequence<Time>::Notes::iterator i = note_lower_bound(note->time());
647 i != _notes.end() && (*i)->time() == note->time(); ++i) {
651 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
654 if ((*i)->note() == _lowest_note || (*i)->note() == _highest_note) {
659 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
660 if ((*ii)->note() < _lowest_note)
661 _lowest_note = (*ii)->note();
662 if ((*ii)->note() > _highest_note)
663 _highest_note = (*ii)->note();
671 Pitches& p (pitches (note->channel()));
673 NotePtr search_note(new Note<Time>(0, 0, 0, note->note(), 0));
675 for (typename Pitches::iterator i = p.lower_bound (search_note);
676 i != p.end() && (*i)->note() == note->note(); ++i) {
678 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
684 cerr << "Unable to find note to erase" << endl;
688 /** Append \a ev to model. NOT realtime safe.
690 * The timestamp of event is expected to be relative to
691 * the start of this model (t=0) and MUST be monotonically increasing
692 * and MUST be >= the latest event currently in the model.
694 template<typename Time>
696 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
698 WriteLock lock(write_lock());
700 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
702 assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
705 if (!midi_event_is_valid(ev.buffer(), ev.size())) {
706 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
710 if (ev.is_note_on()) {
711 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
712 append_note_on_unlocked (note, evid);
713 } else if (ev.is_note_off()) {
714 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
715 /* XXX note: event ID is discarded because we merge the on+off events into
718 append_note_off_unlocked (note);
719 } else if (ev.is_sysex()) {
720 append_sysex_unlocked(ev, evid);
721 } else if (ev.is_cc()) {
722 append_control_unlocked(
723 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
724 ev.time(), ev.cc_value(), evid);
725 } else if (ev.is_pgm_change()) {
726 append_control_unlocked(
727 Evoral::MIDI::ProgramChange(ev.event_type(), ev.channel()),
728 ev.time(), ev.pgm_number(), evid);
729 } else if (ev.is_pitch_bender()) {
730 append_control_unlocked(
731 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
732 ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
733 | (0x7F & ev.pitch_bender_lsb())),
735 } else if (ev.is_channel_pressure()) {
736 append_control_unlocked(
737 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
738 ev.time(), ev.channel_pressure(), evid);
739 } else if (!_type_map.type_is_midi(ev.event_type())) {
740 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
741 for (size_t i=0; i < ev.size(); ++i) {
742 printf("%X ", ev.buffer()[i]);
746 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
752 template<typename Time>
754 Sequence<Time>::append_note_on_unlocked (NotePtr note, event_id_t evid)
756 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this,
757 (int) note->channel(), (int) note->note(),
758 note->time(), (int) note->velocity()));
759 assert(note->note() <= 127);
760 assert(note->channel() < 16);
763 if (note->id() < 0) {
767 if (note->velocity() == 0) {
768 append_note_off_unlocked (note);
772 add_note_unlocked (note);
775 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sustained: Appending active note on %1 channel %2\n",
776 (unsigned)(uint8_t)note->note(), note->channel()));
777 _write_notes[note->channel()].insert (note);
779 DEBUG_TRACE(DEBUG::Sequence, "Percussive: NOT appending active note on\n");
783 template<typename Time>
785 Sequence<Time>::append_note_off_unlocked (NotePtr note)
787 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n",
788 this, (int)note->channel(),
789 (int)note->note(), note->time(), (int)note->velocity()));
790 assert(note->note() <= 127);
791 assert(note->channel() < 16);
796 DEBUG_TRACE(DEBUG::Sequence, "Sequence Ignoring note off (percussive mode)\n");
800 bool resolved = false;
802 /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
803 matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
804 whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
808 /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
810 for (typename WriteNotes::iterator n = _write_notes[note->channel()].begin(); n != _write_notes[note->channel()].end(); ++n) {
812 if (note->note() == nn->note() && nn->channel() == note->channel()) {
813 assert(note->time() >= nn->time());
815 nn->set_length (note->time() - nn->time());
816 nn->set_off_velocity (note->velocity());
818 _write_notes[note->channel()].erase(n);
819 DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note, length: %1\n", note->length()));
826 cerr << this << " spurious note off chan " << (int)note->channel()
827 << ", note " << (int)note->note() << " @ " << note->time() << endl;
831 template<typename Time>
833 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t /* evid */)
835 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3\t=\t%4 # controls: %5\n",
836 this, _type_map.to_symbol(param), time, value, _controls.size()));
837 boost::shared_ptr<Control> c = control(param, true);
838 c->list()->add (time, value);
839 /* XXX control events should use IDs */
842 template<typename Time>
844 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t /* evid */)
846 #ifdef DEBUG_SEQUENCE
847 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
848 for (size_t i=0; i < ev.size(); ++i) {
849 cerr << int(ev.buffer()[i]) << " ";
850 } cerr << "]" << endl;
853 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
854 /* XXX sysex events should use IDs */
855 _sysexes.push_back(event);
858 template<typename Time>
860 Sequence<Time>::contains (const NotePtr& note) const
862 ReadLock lock (read_lock());
863 return contains_unlocked (note);
866 template<typename Time>
868 Sequence<Time>::contains_unlocked (const NotePtr& note) const
870 const Pitches& p (pitches (note->channel()));
871 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
873 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
874 i != p.end() && (*i)->note() == note->note(); ++i) {
884 template<typename Time>
886 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
888 ReadLock lock (read_lock());
889 return overlaps_unlocked (note, without);
892 template<typename Time>
894 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
896 Time sa = note->time();
897 Time ea = note->end_time();
899 const Pitches& p (pitches (note->channel()));
900 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
902 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
903 i != p.end() && (*i)->note() == note->note(); ++i) {
905 if (without && (**i) == *without) {
909 Time sb = (*i)->time();
910 Time eb = (*i)->end_time();
912 if (((sb > sa) && (eb <= ea)) ||
913 ((eb >= sa) && (eb <= ea)) ||
914 ((sb > sa) && (sb <= ea)) ||
915 ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
923 template<typename Time>
925 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
930 /** Return the earliest note with time >= t */
931 template<typename Time>
932 typename Sequence<Time>::Notes::const_iterator
933 Sequence<Time>::note_lower_bound (Time t) const
935 NotePtr search_note(new Note<Time>(0, t, 0, 0, 0));
936 typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
937 assert(i == _notes.end() || (*i)->time() >= t);
941 template<typename Time>
943 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
948 case PitchLessThanOrEqual:
950 case PitchGreaterThanOrEqual:
951 get_notes_by_pitch (n, op, val, chan_mask);
955 case VelocityLessThan:
956 case VelocityLessThanOrEqual:
957 case VelocityGreater:
958 case VelocityGreaterThanOrEqual:
959 get_notes_by_velocity (n, op, val, chan_mask);
964 template<typename Time>
966 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
968 for (uint8_t c = 0; c < 16; ++c) {
970 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
974 const Pitches& p (pitches (c));
975 NotePtr search_note(new Note<Time>(0, 0, 0, val, 0));
976 typename Pitches::const_iterator i;
979 i = p.lower_bound (search_note);
980 while (i != p.end() && (*i)->note() == val) {
985 i = p.upper_bound (search_note);
986 while (i != p.end() && (*i)->note() < val) {
990 case PitchLessThanOrEqual:
991 i = p.upper_bound (search_note);
992 while (i != p.end() && (*i)->note() <= val) {
997 i = p.lower_bound (search_note);
998 while (i != p.end() && (*i)->note() > val) {
1002 case PitchGreaterThanOrEqual:
1003 i = p.lower_bound (search_note);
1004 while (i != p.end() && (*i)->note() >= val) {
1010 //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1017 template<typename Time>
1019 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1021 ReadLock lock (read_lock());
1023 for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1025 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1031 if ((*i)->velocity() == val) {
1035 case VelocityLessThan:
1036 if ((*i)->velocity() < val) {
1040 case VelocityLessThanOrEqual:
1041 if ((*i)->velocity() <= val) {
1045 case VelocityGreater:
1046 if ((*i)->velocity() > val) {
1050 case VelocityGreaterThanOrEqual:
1051 if ((*i)->velocity() >= val) {
1056 // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1064 template<typename Time>
1066 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1068 _overlap_pitch_resolution = opr;
1070 /* XXX todo: clean up existing overlaps in source data? */
1073 template<typename Time>
1075 Sequence<Time>::control_list_marked_dirty ()
1080 template class Sequence<Evoral::MusicalTime>;
1082 } // namespace Evoral