1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
5 * Evoral is free software; you can redistribute it and/or modify it under the
6 * terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "pbd/compose.h"
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)
67 , _active_patch_change_message (0)
69 , _is_end((t == DBL_MAX) || seq.empty())
70 , _note_iter(seq.notes().end())
71 , _sysex_iter(seq.sysexes().end())
72 , _patch_change_iter(seq.patch_changes().end())
73 , _control_iter(_control_iters.end())
74 , _force_discrete (force_discrete)
76 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
82 _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 patch event at or after t
98 for (typename Sequence<Time>::PatchChanges::const_iterator i = seq.patch_changes().begin(); i != seq.patch_changes().end(); ++i) {
99 if ((*i)->time() >= t) {
100 _patch_change_iter = i;
104 assert (_patch_change_iter == seq.patch_changes().end() || (*_patch_change_iter)->time() >= t);
106 // Find first control event after t
107 ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
108 _control_iters.reserve(seq._controls.size());
110 size_t earliest_control_index = 0;
111 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
113 if (filtered.find (i->first) != filtered.end()) {
114 /* this parameter is filtered, so don't bother setting up an iterator for it */
118 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
121 if (_force_discrete) {
122 ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t, x, y, true);
124 ret = i->second->list()->rt_safe_earliest_event_unlocked(t, x, y, true);
127 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
128 i->first.id(), i->second->list()->size(), t));
134 if (y < i->first.min() || y > i->first.max()) {
135 cerr << "ERROR: Controller value " << y
136 << " out of range [" << i->first.min() << "," << i->first.max()
137 << "], event ignored" << endl;
141 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
143 const ControlIterator new_iter(i->second->list(), x, y);
144 _control_iters.push_back(new_iter);
146 // Found a new earliest_control
147 if (x < earliest_control.x) {
148 earliest_control = new_iter;
149 earliest_control_index = _control_iters.size() - 1;
155 _control_iter = _control_iters.begin() + earliest_control_index;
156 assert(_control_iter != _control_iters.end());
158 _control_iter = _control_iters.end();
161 // Now find the earliest event overall and point to it
164 if (_note_iter != seq.notes().end()) {
166 earliest_t = (*_note_iter)->time();
169 if (_sysex_iter != seq.sysexes().end()
170 && ((*_sysex_iter)->time() < earliest_t || _type == NIL)) {
172 earliest_t = (*_sysex_iter)->time();
175 if (_patch_change_iter != seq.patch_changes().end() && ((*_patch_change_iter)->time() < earliest_t || _type == NIL)) {
176 _type = PATCH_CHANGE;
177 earliest_t = (*_patch_change_iter)->time ();
180 if (_control_iter != _control_iters.end()
181 && earliest_control.list && earliest_control.x >= t
182 && (earliest_control.x < earliest_t || _type == NIL)) {
184 earliest_t = earliest_control.x;
189 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at note on event @ %1\n", earliest_t));
190 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_note_iter)->on_event(), true));
191 _active_notes.push(*_note_iter);
194 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at sysex event @ %1\n", earliest_t));
195 _event = boost::shared_ptr< Event<Time> >(
196 new Event<Time>(*(*_sysex_iter), true));
199 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at control event @ %1\n", earliest_t));
200 seq.control_to_midi_event(_event, earliest_control);
203 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at patch change event @ %1\n", earliest_t));
204 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_patch_change_iter)->message (_active_patch_change_message), true));
210 if (_type == NIL || !_event || _event->size() == 0) {
211 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at end @ %1\n", t));
215 DEBUG_TRACE (DEBUG::Sequence, string_compose ("New iterator = 0x%1 : 0x%2 @ %3\n",
216 (int)_event->event_type(),
217 (int)((MIDIEvent<Time>*)_event.get())->type(),
220 assert(midi_event_is_valid(_event->buffer(), _event->size()));
225 template<typename Time>
226 Sequence<Time>::const_iterator::~const_iterator()
230 template<typename Time>
232 Sequence<Time>::const_iterator::invalidate()
234 while (!_active_notes.empty()) {
240 _note_iter = _seq->notes().end();
241 _sysex_iter = _seq->sysexes().end();
242 _patch_change_iter = _seq->patch_changes().end();
243 _active_patch_change_message = 0;
245 _control_iter = _control_iters.end();
249 template<typename Time>
250 const typename Sequence<Time>::const_iterator&
251 Sequence<Time>::const_iterator::operator++()
254 throw std::logic_error("Attempt to iterate past end of Sequence");
257 DEBUG_TRACE(DEBUG::Sequence, "Sequence::const_iterator++\n");
258 assert(_event && _event->buffer() && _event->size() > 0);
260 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
264 || ev.is_pgm_change()
265 || ev.is_pitch_bender()
266 || ev.is_channel_pressure()
267 || ev.is_sysex()) ) {
268 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
269 << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
276 // Increment past current event
284 // Increment current controller iterator
285 if (_force_discrete || _control_iter->list->interpolation() == ControlList::Discrete) {
286 ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (
287 _control_iter->x, x, y, false
290 ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
291 _control_iter->x + time_between_interpolated_controller_outputs, x, y, false
294 assert(!ret || x > _control_iter->x);
296 _control_iter->x = x;
297 _control_iter->y = y;
299 _control_iter->list.reset();
300 _control_iter->x = DBL_MAX;
301 _control_iter->y = DBL_MAX;
304 // Find the controller with the next earliest event time
305 _control_iter = _control_iters.begin();
306 for (ControlIterators::iterator i = _control_iters.begin();
307 i != _control_iters.end(); ++i) {
308 if (i->x < _control_iter->x) {
317 ++_active_patch_change_message;
318 if (_active_patch_change_message == (*_patch_change_iter)->messages()) {
319 ++_patch_change_iter;
320 _active_patch_change_message = 0;
327 // Now find the earliest event overall and point to it
329 Time earliest_t = std::numeric_limits<Time>::max();
331 // Next earliest note on
332 if (_note_iter != _seq->notes().end()) {
334 earliest_t = (*_note_iter)->time();
337 // Use the next note off iff it's earlier or the same time as the note on
338 if (!_seq->percussive() && (!_active_notes.empty())) {
339 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
341 earliest_t = _active_notes.top()->end_time();
345 // Use the next earliest controller iff it's earlier than the note event
346 if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
347 if (_type == NIL || _control_iter->x < earliest_t) {
349 earliest_t = _control_iter->x;
353 // Use the next earliest SysEx iff it's earlier than the controller
354 if (_sysex_iter != _seq->sysexes().end()) {
355 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
357 earliest_t = (*_sysex_iter)->time();
361 // Use the next earliest patch change iff it's earlier than the SysEx
362 if (_patch_change_iter != _seq->patch_changes().end()) {
363 if (_type == NIL || (*_patch_change_iter)->time() < earliest_t) {
364 _type = PATCH_CHANGE;
365 earliest_t = (*_patch_change_iter)->time();
369 // Set event to reflect new position
372 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
373 *_event = (*_note_iter)->on_event();
374 _active_notes.push(*_note_iter);
377 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
378 assert(!_active_notes.empty());
379 *_event = _active_notes.top()->off_event();
383 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
384 _seq->control_to_midi_event(_event, *_control_iter);
387 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
388 *_event = *(*_sysex_iter);
391 DEBUG_TRACE(DEBUG::Sequence, "iterator = patch change\n");
392 *_event = (*_patch_change_iter)->message (_active_patch_change_message);
395 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
399 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
404 template<typename Time>
406 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
408 if (_seq != other._seq) {
410 } else if (_is_end || other._is_end) {
411 return (_is_end == other._is_end);
412 } else if (_type != other._type) {
415 return (_event == other._event);
419 template<typename Time>
420 typename Sequence<Time>::const_iterator&
421 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
424 _event = other._event;
425 _active_notes = other._active_notes;
427 _is_end = other._is_end;
428 _note_iter = other._note_iter;
429 _sysex_iter = other._sysex_iter;
430 _patch_change_iter = other._patch_change_iter;
431 _control_iters = other._control_iters;
432 _force_discrete = other._force_discrete;
433 _active_patch_change_message = other._active_patch_change_message;
436 _lock = _seq->read_lock();
441 if (other._control_iter == other._control_iters.end()) {
442 _control_iter = _control_iters.end();
444 const size_t index = other._control_iter - other._control_iters.begin();
445 _control_iter = _control_iters.begin() + index;
453 template<typename Time>
454 Sequence<Time>::Sequence(const TypeMap& type_map)
456 , _overlapping_pitches_accepted (true)
457 , _overlap_pitch_resolution (FirstOnFirstOff)
459 , _type_map(type_map)
460 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
465 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
466 assert(_end_iter._is_end);
467 assert( ! _end_iter._lock);
469 for (int i = 0; i < 16; ++i) {
474 template<typename Time>
475 Sequence<Time>::Sequence(const Sequence<Time>& other)
478 , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
479 , _overlap_pitch_resolution (other._overlap_pitch_resolution)
481 , _type_map(other._type_map)
482 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
483 , _percussive(other._percussive)
484 , _lowest_note(other._lowest_note)
485 , _highest_note(other._highest_note)
487 for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
488 NotePtr n (new Note<Time> (**i));
492 for (typename SysExes::const_iterator i = other._sysexes.begin(); i != other._sysexes.end(); ++i) {
493 boost::shared_ptr<Event<Time> > n (new Event<Time> (**i, true));
494 _sysexes.push_back (n);
497 for (typename PatchChanges::const_iterator i = other._patch_changes.begin(); i != other._patch_changes.end(); ++i) {
498 PatchChangePtr n (new PatchChange<Time> (**i));
499 _patch_changes.insert (n);
502 for (int i = 0; i < 16; ++i) {
503 _bank[i] = other._bank[i];
506 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
507 assert(_end_iter._is_end);
508 assert(! _end_iter._lock);
511 /** Write the controller event pointed to by \a iter to \a ev.
512 * The buffer of \a ev will be allocated or resized as necessary.
513 * The event_type of \a ev should be set to the expected output type.
514 * \return true on success
516 template<typename Time>
518 Sequence<Time>::control_to_midi_event(
519 boost::shared_ptr< Event<Time> >& ev,
520 const ControlIterator& iter) const
522 assert(iter.list.get());
523 const uint32_t event_type = iter.list->parameter().type();
525 // initialize the event pointer with a new event, if necessary
527 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
530 uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
531 ev->set_event_type(_type_map.midi_event_type(midi_type));
533 case MIDI_CMD_CONTROL:
534 assert(iter.list.get());
535 assert(iter.list->parameter().channel() < 16);
536 assert(iter.list->parameter().id() <= INT8_MAX);
537 assert(iter.y <= INT8_MAX);
541 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
542 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
543 ev->buffer()[2] = (uint8_t)iter.y;
546 case MIDI_CMD_PGM_CHANGE:
547 assert(iter.list.get());
548 assert(iter.list->parameter().channel() < 16);
549 assert(iter.y <= INT8_MAX);
553 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
554 ev->buffer()[1] = (uint8_t)iter.y;
557 case MIDI_CMD_BENDER:
558 assert(iter.list.get());
559 assert(iter.list->parameter().channel() < 16);
560 assert(iter.y < (1<<14));
564 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
565 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
566 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
569 case MIDI_CMD_CHANNEL_PRESSURE:
570 assert(iter.list.get());
571 assert(iter.list->parameter().channel() < 16);
572 assert(iter.y <= INT8_MAX);
576 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
577 ev->buffer()[1] = (uint8_t)iter.y;
587 /** Clear all events from the model.
589 template<typename Time>
591 Sequence<Time>::clear()
593 WriteLock lock(write_lock());
595 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
596 li->second->list()->clear();
599 /** Begin a write of events to the model.
601 * If \a mode is Sustained, complete notes with length are constructed as note
602 * on/off events are received. Otherwise (Percussive), only note on events are
603 * stored; note off events are discarded entirely and all contained notes will
606 template<typename Time>
608 Sequence<Time>::start_write()
610 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
611 WriteLock lock(write_lock());
613 for (int i = 0; i < 16; ++i) {
614 _write_notes[i].clear();
618 /** Finish a write of events to the model.
620 * If \a delete_stuck is true and the current mode is Sustained, note on events
621 * that were never resolved with a corresonding note off will be deleted.
622 * Otherwise they will remain as notes with length 0.
624 template<typename Time>
626 Sequence<Time>::end_write (Time when, bool delete_stuck, bool resolve)
628 WriteLock lock(write_lock());
636 assert (!delete_stuck);
639 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes)\n", this, _notes.size()));
643 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
644 typename Notes::iterator next = n;
647 if ((*n)->length() == 0) {
649 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
651 } else if (resolve) {
652 if (when <= (*n)->time()) {
653 cerr << "WARNING: Stuck note resolution - end time @ "
654 << when << " is before note on: " << (**n) << endl;
657 (*n)->set_length (when - (*n)->time());
658 cerr << "WARNING: resolved note-on with no note-off to generate " << (**n) << endl;
667 for (int i = 0; i < 16; ++i) {
668 _write_notes[i].clear();
675 template<typename Time>
677 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
679 /* This is the core method to add notes to a Sequence
682 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3\n", this, (int)note->note(), note->time()));
684 if (resolve_overlaps_unlocked (note, arg)) {
685 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 DISALLOWED: note %2 @ %3\n", this, (int)note->note(), note->time()));
689 if (note->id() < 0) {
690 note->set_id (Evoral::next_event_id());
693 if (note->note() < _lowest_note)
694 _lowest_note = note->note();
695 if (note->note() > _highest_note)
696 _highest_note = note->note();
698 _notes.insert (note);
699 _pitches[note->channel()].insert (note);
706 template<typename Time>
708 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
714 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note %2 @ %3\n", this, (int)note->note(), note->time()));
716 typename Sequence<Time>::Notes::iterator i = note_lower_bound(note->time());
717 while (i != _notes.end() && (*i)->time() == note->time()) {
719 typename Sequence<Time>::Notes::iterator tmp = i;
726 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
729 if (n->note() == _lowest_note || n->note() == _highest_note) {
734 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
735 if ((*ii)->note() < _lowest_note)
736 _lowest_note = (*ii)->note();
737 if ((*ii)->note() > _highest_note)
738 _highest_note = (*ii)->note();
748 Pitches& p (pitches (note->channel()));
750 NotePtr search_note(new Note<Time>(0, 0, 0, note->note(), 0));
752 typename Pitches::iterator j = p.lower_bound (search_note);
753 while (j != p.end() && (*j)->note() == note->note()) {
754 typename Pitches::iterator tmp = j;
758 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*j)->note(), (*j)->time()));
766 cerr << "Unable to find note to erase matching " << *note.get() << endl;
770 template<typename Time>
772 Sequence<Time>::add_patch_change_unlocked (PatchChangePtr p)
774 _patch_changes.insert (p);
776 p->set_id (Evoral::next_event_id ());
780 template<typename Time>
782 Sequence<Time>::remove_patch_change_unlocked (const constPatchChangePtr p)
784 typename Sequence<Time>::PatchChanges::iterator i = patch_change_lower_bound (p->time ());
785 while (i != _patch_changes.end() && (*i)->time() == p->time()) {
787 typename Sequence<Time>::PatchChanges::iterator tmp = i;
791 _patch_changes.erase (i);
798 /** Append \a ev to model. NOT realtime safe.
800 * The timestamp of event is expected to be relative to
801 * the start of this model (t=0) and MUST be monotonically increasing
802 * and MUST be >= the latest event currently in the model.
804 template<typename Time>
806 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
808 WriteLock lock(write_lock());
810 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
812 assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
815 if (!midi_event_is_valid(ev.buffer(), ev.size())) {
816 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
820 if (ev.is_note_on()) {
821 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
822 append_note_on_unlocked (note, evid);
823 } else if (ev.is_note_off()) {
824 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
825 /* XXX note: event ID is discarded because we merge the on+off events into
828 append_note_off_unlocked (note);
829 } else if (ev.is_sysex()) {
830 append_sysex_unlocked(ev, evid);
831 } else if (ev.is_cc() && (ev.cc_number() == MIDI_CTL_MSB_BANK || ev.cc_number() == MIDI_CTL_LSB_BANK)) {
832 /* note bank numbers in our _bank[] array, so that we can write an event when the program change arrives */
833 if (ev.cc_number() == MIDI_CTL_MSB_BANK) {
834 _bank[ev.channel()] &= ~(0x7f << 7);
835 _bank[ev.channel()] |= ev.cc_value() << 7;
837 _bank[ev.channel()] &= ~0x7f;
838 _bank[ev.channel()] |= ev.cc_value();
840 } else if (ev.is_cc()) {
841 append_control_unlocked(
842 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
843 ev.time(), ev.cc_value(), evid);
844 } else if (ev.is_pgm_change()) {
845 /* write a patch change with this program change and any previously set-up bank number */
846 append_patch_change_unlocked (PatchChange<Time> (ev.time(), ev.channel(), ev.pgm_number(), _bank[ev.channel()]), evid);
847 } else if (ev.is_pitch_bender()) {
848 append_control_unlocked(
849 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
850 ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
851 | (0x7F & ev.pitch_bender_lsb())),
853 } else if (ev.is_channel_pressure()) {
854 append_control_unlocked(
855 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
856 ev.time(), ev.channel_pressure(), evid);
857 } else if (!_type_map.type_is_midi(ev.event_type())) {
858 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
859 for (size_t i=0; i < ev.size(); ++i) {
860 printf("%X ", ev.buffer()[i]);
864 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
870 template<typename Time>
872 Sequence<Time>::append_note_on_unlocked (NotePtr note, event_id_t evid)
874 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this,
875 (int) note->channel(), (int) note->note(),
876 note->time(), (int) note->velocity()));
877 assert(note->note() <= 127);
878 assert(note->channel() < 16);
881 if (note->id() < 0) {
885 if (note->velocity() == 0) {
886 append_note_off_unlocked (note);
890 add_note_unlocked (note);
893 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sustained: Appending active note on %1 channel %2\n",
894 (unsigned)(uint8_t)note->note(), note->channel()));
895 _write_notes[note->channel()].insert (note);
897 DEBUG_TRACE(DEBUG::Sequence, "Percussive: NOT appending active note on\n");
901 template<typename Time>
903 Sequence<Time>::append_note_off_unlocked (NotePtr note)
905 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 OFF @ %4 v=%5\n",
906 this, (int)note->channel(),
907 (int)note->note(), note->time(), (int)note->velocity()));
908 assert(note->note() <= 127);
909 assert(note->channel() < 16);
914 DEBUG_TRACE(DEBUG::Sequence, "Sequence Ignoring note off (percussive mode)\n");
918 bool resolved = false;
920 /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
921 matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
922 whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
926 /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
928 for (typename WriteNotes::iterator n = _write_notes[note->channel()].begin(); n != _write_notes[note->channel()].end(); ) {
930 typename WriteNotes::iterator tmp = n;
934 if (note->note() == nn->note() && nn->channel() == note->channel()) {
935 assert(note->time() >= nn->time());
937 nn->set_length (note->time() - nn->time());
938 nn->set_off_velocity (note->velocity());
940 _write_notes[note->channel()].erase(n);
941 DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note @ %2 length: %1\n", nn->length(), nn->time()));
950 cerr << this << " spurious note off chan " << (int)note->channel()
951 << ", note " << (int)note->note() << " @ " << note->time() << endl;
955 template<typename Time>
957 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t /* evid */)
959 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3\t=\t%4 # controls: %5\n",
960 this, _type_map.to_symbol(param), time, value, _controls.size()));
961 boost::shared_ptr<Control> c = control(param, true);
962 c->list()->add (time, value);
963 /* XXX control events should use IDs */
966 template<typename Time>
968 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t /* evid */)
970 #ifdef DEBUG_SEQUENCE
971 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
972 for (size_t i=0; i < ev.size(); ++i) {
973 cerr << int(ev.buffer()[i]) << " ";
974 } cerr << "]" << endl;
977 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
978 /* XXX sysex events should use IDs */
979 _sysexes.push_back(event);
982 template<typename Time>
984 Sequence<Time>::append_patch_change_unlocked (const PatchChange<Time>& ev, event_id_t id)
986 PatchChangePtr p (new PatchChange<Time> (ev));
992 _patch_changes.insert (p);
995 template<typename Time>
997 Sequence<Time>::contains (const NotePtr& note) const
999 ReadLock lock (read_lock());
1000 return contains_unlocked (note);
1003 template<typename Time>
1005 Sequence<Time>::contains_unlocked (const NotePtr& note) const
1007 const Pitches& p (pitches (note->channel()));
1008 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
1010 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
1011 i != p.end() && (*i)->note() == note->note(); ++i) {
1021 template<typename Time>
1023 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
1025 ReadLock lock (read_lock());
1026 return overlaps_unlocked (note, without);
1029 template<typename Time>
1031 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
1033 Time sa = note->time();
1034 Time ea = note->end_time();
1036 const Pitches& p (pitches (note->channel()));
1037 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
1039 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
1040 i != p.end() && (*i)->note() == note->note(); ++i) {
1042 if (without && (**i) == *without) {
1046 Time sb = (*i)->time();
1047 Time eb = (*i)->end_time();
1049 if (((sb > sa) && (eb <= ea)) ||
1050 ((eb >= sa) && (eb <= ea)) ||
1051 ((sb > sa) && (sb <= ea)) ||
1052 ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
1060 template<typename Time>
1062 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
1067 /** Return the earliest note with time >= t */
1068 template<typename Time>
1069 typename Sequence<Time>::Notes::const_iterator
1070 Sequence<Time>::note_lower_bound (Time t) const
1072 NotePtr search_note(new Note<Time>(0, t, 0, 0, 0));
1073 typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
1074 assert(i == _notes.end() || (*i)->time() >= t);
1078 /** Return the earliest patch change with time >= t */
1079 template<typename Time>
1080 typename Sequence<Time>::PatchChanges::const_iterator
1081 Sequence<Time>::patch_change_lower_bound (Time t) const
1083 PatchChangePtr search (new PatchChange<Time> (t, 0, 0, 0));
1084 typename Sequence<Time>::PatchChanges::const_iterator i = _patch_changes.lower_bound (search);
1085 assert (i == _patch_changes.end() || (*i)->time() >= t);
1089 template<typename Time>
1091 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1096 case PitchLessThanOrEqual:
1098 case PitchGreaterThanOrEqual:
1099 get_notes_by_pitch (n, op, val, chan_mask);
1103 case VelocityLessThan:
1104 case VelocityLessThanOrEqual:
1105 case VelocityGreater:
1106 case VelocityGreaterThanOrEqual:
1107 get_notes_by_velocity (n, op, val, chan_mask);
1112 template<typename Time>
1114 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1116 for (uint8_t c = 0; c < 16; ++c) {
1118 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
1122 const Pitches& p (pitches (c));
1123 NotePtr search_note(new Note<Time>(0, 0, 0, val, 0));
1124 typename Pitches::const_iterator i;
1127 i = p.lower_bound (search_note);
1128 while (i != p.end() && (*i)->note() == val) {
1133 i = p.upper_bound (search_note);
1134 while (i != p.end() && (*i)->note() < val) {
1138 case PitchLessThanOrEqual:
1139 i = p.upper_bound (search_note);
1140 while (i != p.end() && (*i)->note() <= val) {
1145 i = p.lower_bound (search_note);
1146 while (i != p.end() && (*i)->note() > val) {
1150 case PitchGreaterThanOrEqual:
1151 i = p.lower_bound (search_note);
1152 while (i != p.end() && (*i)->note() >= val) {
1158 //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1165 template<typename Time>
1167 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1169 ReadLock lock (read_lock());
1171 for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1173 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1179 if ((*i)->velocity() == val) {
1183 case VelocityLessThan:
1184 if ((*i)->velocity() < val) {
1188 case VelocityLessThanOrEqual:
1189 if ((*i)->velocity() <= val) {
1193 case VelocityGreater:
1194 if ((*i)->velocity() > val) {
1198 case VelocityGreaterThanOrEqual:
1199 if ((*i)->velocity() >= val) {
1204 // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1212 template<typename Time>
1214 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1216 _overlap_pitch_resolution = opr;
1218 /* XXX todo: clean up existing overlaps in source data? */
1221 template<typename Time>
1223 Sequence<Time>::control_list_marked_dirty ()
1228 template class Sequence<Evoral::MusicalTime>;
1230 template<typename Time>
1232 Sequence<Time>::dump (ostream& str) const
1234 Sequence<Time>::const_iterator i;
1235 str << "+++ dump\n";
1236 for (i = begin(); i != end(); ++i) {
1239 str << "--- dump\n";
1242 } // namespace Evoral