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)
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));
79 _lock = seq.read_lock();
84 typename Sequence<Time>::ReadLock lock(seq.read_lock());
86 // Find first note which begins at or after t
87 _note_iter = seq.note_lower_bound(t);
89 // Find first sysex event at or after t
90 for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
91 i != seq.sysexes().end(); ++i) {
92 if ((*i)->time() >= t) {
97 assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
99 // Find first patch event at or after t
100 for (typename Sequence<Time>::PatchChanges::const_iterator i = seq.patch_changes().begin(); i != seq.patch_changes().end(); ++i) {
101 if ((*i)->time() >= t) {
102 _patch_change_iter = i;
106 assert (_patch_change_iter == seq.patch_changes().end() || (*_patch_change_iter)->time() >= t);
108 // Find first control event after t
109 ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
110 _control_iters.reserve(seq._controls.size());
112 size_t earliest_control_index = 0;
113 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
115 if (filtered.find (i->first) != filtered.end()) {
116 /* this parameter is filtered, so don't bother setting up an iterator for it */
120 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
123 if (_force_discrete) {
124 ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t, x, y, true);
126 ret = i->second->list()->rt_safe_earliest_event_unlocked(t, x, y, true);
129 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
130 i->first.id(), i->second->list()->size(), t));
136 if (y < i->first.min() || y > i->first.max()) {
137 cerr << "ERROR: Controller value " << y
138 << " out of range [" << i->first.min() << "," << i->first.max()
139 << "], event ignored" << endl;
143 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
145 const ControlIterator new_iter(i->second->list(), x, y);
146 _control_iters.push_back(new_iter);
148 // Found a new earliest_control
149 if (x < earliest_control.x) {
150 earliest_control = new_iter;
151 earliest_control_index = _control_iters.size() - 1;
157 _control_iter = _control_iters.begin() + earliest_control_index;
158 assert(_control_iter != _control_iters.end());
160 _control_iter = _control_iters.end();
163 // Now find the earliest event overall and point to it
166 if (_note_iter != seq.notes().end()) {
168 earliest_t = (*_note_iter)->time();
171 if (_sysex_iter != seq.sysexes().end()
172 && ((*_sysex_iter)->time() < earliest_t || _type == NIL)) {
174 earliest_t = (*_sysex_iter)->time();
177 if (_patch_change_iter != seq.patch_changes().end() && ((*_patch_change_iter)->time() < earliest_t || _type == NIL)) {
178 _type = PATCH_CHANGE;
179 earliest_t = (*_patch_change_iter)->time ();
182 if (_control_iter != _control_iters.end()
183 && earliest_control.list && earliest_control.x >= t
184 && (earliest_control.x < earliest_t || _type == NIL)) {
186 earliest_t = earliest_control.x;
191 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at note on event @ %1\n", earliest_t));
192 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_note_iter)->on_event(), true));
193 _active_notes.push(*_note_iter);
196 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at sysex event @ %1\n", earliest_t));
197 _event = boost::shared_ptr< Event<Time> >(
198 new Event<Time>(*(*_sysex_iter), true));
201 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at control event @ %1\n", earliest_t));
202 seq.control_to_midi_event(_event, earliest_control);
205 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at patch change event @ %1\n", earliest_t));
206 _event = boost::shared_ptr<Event<Time> > (new Event<Time> ((*_patch_change_iter)->message (_active_patch_change_message), true));
212 if (_type == NIL || !_event || _event->size() == 0) {
213 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Starting at end @ %1\n", t));
217 DEBUG_TRACE (DEBUG::Sequence, string_compose ("New iterator = 0x%1 : 0x%2 @ %3\n",
218 (int)_event->event_type(),
219 (int)((MIDIEvent<Time>*)_event.get())->type(),
222 assert(midi_event_is_valid(_event->buffer(), _event->size()));
227 template<typename Time>
228 Sequence<Time>::const_iterator::~const_iterator()
232 template<typename Time>
234 Sequence<Time>::const_iterator::invalidate()
236 while (!_active_notes.empty()) {
242 _note_iter = _seq->notes().end();
243 _sysex_iter = _seq->sysexes().end();
244 _patch_change_iter = _seq->patch_changes().end();
245 _active_patch_change_message = 0;
247 _control_iter = _control_iters.end();
251 template<typename Time>
252 const typename Sequence<Time>::const_iterator&
253 Sequence<Time>::const_iterator::operator++()
256 throw std::logic_error("Attempt to iterate past end of Sequence");
259 DEBUG_TRACE(DEBUG::Sequence, "Sequence::const_iterator++\n");
260 assert(_event && _event->buffer() && _event->size() > 0);
262 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
266 || ev.is_pgm_change()
267 || ev.is_pitch_bender()
268 || ev.is_channel_pressure()
269 || ev.is_sysex()) ) {
270 cerr << "WARNING: Unknown event (type " << _type << "): " << hex
271 << int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
278 // Increment past current event
286 // Increment current controller iterator
287 if (_force_discrete || _control_iter->list->interpolation() == ControlList::Discrete) {
288 ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (
289 _control_iter->x, x, y, false
292 ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
293 _control_iter->x + time_between_interpolated_controller_outputs, x, y, false
296 assert(!ret || x > _control_iter->x);
298 _control_iter->x = x;
299 _control_iter->y = y;
301 _control_iter->list.reset();
302 _control_iter->x = DBL_MAX;
303 _control_iter->y = DBL_MAX;
306 // Find the controller with the next earliest event time
307 _control_iter = _control_iters.begin();
308 for (ControlIterators::iterator i = _control_iters.begin();
309 i != _control_iters.end(); ++i) {
310 if (i->x < _control_iter->x) {
319 ++_active_patch_change_message;
320 if (_active_patch_change_message == (*_patch_change_iter)->messages()) {
321 ++_patch_change_iter;
322 _active_patch_change_message = 0;
329 // Now find the earliest event overall and point to it
331 Time earliest_t = std::numeric_limits<Time>::max();
333 // Next earliest note on
334 if (_note_iter != _seq->notes().end()) {
336 earliest_t = (*_note_iter)->time();
339 // Use the next note off iff it's earlier or the same time as the note on
340 if (!_seq->percussive() && (!_active_notes.empty())) {
341 if (_type == NIL || _active_notes.top()->end_time() <= earliest_t) {
343 earliest_t = _active_notes.top()->end_time();
347 // Use the next earliest controller iff it's earlier than the note event
348 if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
349 if (_type == NIL || _control_iter->x < earliest_t) {
351 earliest_t = _control_iter->x;
355 // Use the next earliest SysEx iff it's earlier than the controller
356 if (_sysex_iter != _seq->sysexes().end()) {
357 if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
359 earliest_t = (*_sysex_iter)->time();
363 // Use the next earliest patch change iff it's earlier than the SysEx
364 if (_patch_change_iter != _seq->patch_changes().end()) {
365 if (_type == NIL || (*_patch_change_iter)->time() < earliest_t) {
366 _type = PATCH_CHANGE;
367 earliest_t = (*_patch_change_iter)->time();
371 // Set event to reflect new position
374 DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
375 *_event = (*_note_iter)->on_event();
376 _active_notes.push(*_note_iter);
379 DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
380 assert(!_active_notes.empty());
381 *_event = _active_notes.top()->off_event();
385 DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
386 _seq->control_to_midi_event(_event, *_control_iter);
389 DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
390 *_event = *(*_sysex_iter);
393 DEBUG_TRACE(DEBUG::Sequence, "iterator = patch change\n");
394 *_event = (*_patch_change_iter)->message (_active_patch_change_message);
397 DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
401 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
406 template<typename Time>
408 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
410 if (_seq != other._seq) {
412 } else if (_is_end || other._is_end) {
413 return (_is_end == other._is_end);
414 } else if (_type != other._type) {
417 return (_event == other._event);
421 template<typename Time>
422 typename Sequence<Time>::const_iterator&
423 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
426 _event = other._event;
427 _active_notes = other._active_notes;
429 _is_end = other._is_end;
430 _note_iter = other._note_iter;
431 _sysex_iter = other._sysex_iter;
432 _patch_change_iter = other._patch_change_iter;
433 _control_iters = other._control_iters;
434 _force_discrete = other._force_discrete;
435 _active_patch_change_message = other._active_patch_change_message;
438 _lock = _seq->read_lock();
442 if (other._control_iter == other._control_iters.end()) {
443 _control_iter = _control_iters.end();
445 const size_t index = other._control_iter - other._control_iters.begin();
446 _control_iter = _control_iters.begin() + index;
454 template<typename Time>
455 Sequence<Time>::Sequence(const TypeMap& type_map)
457 , _overlapping_pitches_accepted (true)
458 , _overlap_pitch_resolution (FirstOnFirstOff)
460 , _type_map(type_map)
461 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
466 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
467 assert(_end_iter._is_end);
468 assert( ! _end_iter._lock);
470 for (int i = 0; i < 16; ++i) {
475 template<typename Time>
476 Sequence<Time>::Sequence(const Sequence<Time>& other)
479 , _overlapping_pitches_accepted (other._overlapping_pitches_accepted)
480 , _overlap_pitch_resolution (other._overlap_pitch_resolution)
482 , _type_map(other._type_map)
483 , _end_iter(*this, DBL_MAX, false, std::set<Evoral::Parameter> ())
484 , _percussive(other._percussive)
485 , _lowest_note(other._lowest_note)
486 , _highest_note(other._highest_note)
488 for (typename Notes::const_iterator i = other._notes.begin(); i != other._notes.end(); ++i) {
489 NotePtr n (new Note<Time> (**i));
493 for (typename SysExes::const_iterator i = other._sysexes.begin(); i != other._sysexes.end(); ++i) {
494 boost::shared_ptr<Event<Time> > n (new Event<Time> (**i, true));
495 _sysexes.push_back (n);
498 for (typename PatchChanges::const_iterator i = other._patch_changes.begin(); i != other._patch_changes.end(); ++i) {
499 PatchChangePtr n (new PatchChange<Time> (**i));
500 _patch_changes.insert (n);
503 for (int i = 0; i < 16; ++i) {
504 _bank[i] = other._bank[i];
507 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
508 assert(_end_iter._is_end);
509 assert(! _end_iter._lock);
512 /** Write the controller event pointed to by \a iter to \a ev.
513 * The buffer of \a ev will be allocated or resized as necessary.
514 * The event_type of \a ev should be set to the expected output type.
515 * \return true on success
517 template<typename Time>
519 Sequence<Time>::control_to_midi_event(
520 boost::shared_ptr< Event<Time> >& ev,
521 const ControlIterator& iter) const
523 assert(iter.list.get());
524 const uint32_t event_type = iter.list->parameter().type();
526 // initialize the event pointer with a new event, if necessary
528 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
531 uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
532 ev->set_event_type(_type_map.midi_event_type(midi_type));
534 case MIDI_CMD_CONTROL:
535 assert(iter.list.get());
536 assert(iter.list->parameter().channel() < 16);
537 assert(iter.list->parameter().id() <= INT8_MAX);
538 assert(iter.y <= INT8_MAX);
542 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
543 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
544 ev->buffer()[2] = (uint8_t)iter.y;
547 case MIDI_CMD_PGM_CHANGE:
548 assert(iter.list.get());
549 assert(iter.list->parameter().channel() < 16);
550 assert(iter.y <= INT8_MAX);
554 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
555 ev->buffer()[1] = (uint8_t)iter.y;
558 case MIDI_CMD_BENDER:
559 assert(iter.list.get());
560 assert(iter.list->parameter().channel() < 16);
561 assert(iter.y < (1<<14));
565 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
566 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
567 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
570 case MIDI_CMD_CHANNEL_PRESSURE:
571 assert(iter.list.get());
572 assert(iter.list->parameter().channel() < 16);
573 assert(iter.y <= INT8_MAX);
577 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
578 ev->buffer()[1] = (uint8_t)iter.y;
588 /** Clear all events from the model.
590 template<typename Time>
592 Sequence<Time>::clear()
594 WriteLock lock(write_lock());
596 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
597 li->second->list()->clear();
600 /** Begin a write of events to the model.
602 * If \a mode is Sustained, complete notes with length are constructed as note
603 * on/off events are received. Otherwise (Percussive), only note on events are
604 * stored; note off events are discarded entirely and all contained notes will
607 template<typename Time>
609 Sequence<Time>::start_write()
611 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : start_write (percussive = %2)\n", this, _percussive));
612 WriteLock lock(write_lock());
614 for (int i = 0; i < 16; ++i) {
615 _write_notes[i].clear();
619 /** Finish a write of events to the model.
621 * If \a delete_stuck is true and the current mode is Sustained, note on events
622 * that were never resolved with a corresonding note off will be deleted.
623 * Otherwise they will remain as notes with length 0.
625 template<typename Time>
627 Sequence<Time>::end_write (bool delete_stuck)
629 WriteLock lock(write_lock());
635 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 : end_write (%2 notes)\n", this, _notes.size()));
637 if (!_percussive && delete_stuck) {
638 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
639 typename Notes::iterator next = n;
642 if ((*n)->length() == 0) {
643 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
651 for (int i = 0; i < 16; ++i) {
652 if (!_write_notes[i].empty()) {
653 cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
654 << _write_notes[i].size() << " stuck notes" << endl;
656 _write_notes[i].clear();
663 template<typename Time>
665 Sequence<Time>::add_note_unlocked(const NotePtr note, void* arg)
667 /* This is the core method to add notes to a Sequence
670 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 add note %2 @ %3\n", this, (int)note->note(), note->time()));
672 if (resolve_overlaps_unlocked (note, arg)) {
673 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 DISALLOWED: note %2 @ %3\n", this, (int)note->note(), note->time()));
677 if (note->id() < 0) {
678 note->set_id (Evoral::next_event_id());
681 if (note->note() < _lowest_note)
682 _lowest_note = note->note();
683 if (note->note() > _highest_note)
684 _highest_note = note->note();
686 _notes.insert (note);
687 _pitches[note->channel()].insert (note);
694 template<typename Time>
696 Sequence<Time>::remove_note_unlocked(const constNotePtr note)
702 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 remove note %2 @ %3\n", this, (int)note->note(), note->time()));
704 typename Sequence<Time>::Notes::iterator i = note_lower_bound(note->time());
705 while (i != _notes.end() && (*i)->time() == note->time()) {
707 typename Sequence<Time>::Notes::iterator tmp = i;
714 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing note %2 @ %3\n", this, (int)(*i)->note(), (*i)->time()));
717 if (n->note() == _lowest_note || n->note() == _highest_note) {
722 for (typename Sequence<Time>::Notes::iterator ii = _notes.begin(); ii != _notes.end(); ++ii) {
723 if ((*ii)->note() < _lowest_note)
724 _lowest_note = (*ii)->note();
725 if ((*ii)->note() > _highest_note)
726 _highest_note = (*ii)->note();
736 Pitches& p (pitches (note->channel()));
738 NotePtr search_note(new Note<Time>(0, 0, 0, note->note(), 0));
740 typename Pitches::iterator j = p.lower_bound (search_note);
741 while (j != p.end() && (*j)->note() == note->note()) {
742 typename Pitches::iterator tmp = j;
746 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1\terasing pitch %2 @ %3\n", this, (int)(*j)->note(), (*j)->time()));
754 cerr << "Unable to find note to erase" << endl;
758 template<typename Time>
760 Sequence<Time>::add_patch_change_unlocked (PatchChangePtr p)
762 _patch_changes.insert (p);
764 p->set_id (Evoral::next_event_id ());
768 template<typename Time>
770 Sequence<Time>::remove_patch_change_unlocked (const constPatchChangePtr p)
772 typename Sequence<Time>::PatchChanges::iterator i = patch_change_lower_bound (p->time ());
773 while (i != _patch_changes.end() && (*i)->time() == p->time()) {
775 typename Sequence<Time>::PatchChanges::iterator tmp = i;
779 _patch_changes.erase (i);
786 /** Append \a ev to model. NOT realtime safe.
788 * The timestamp of event is expected to be relative to
789 * the start of this model (t=0) and MUST be monotonically increasing
790 * and MUST be >= the latest event currently in the model.
792 template<typename Time>
794 Sequence<Time>::append(const Event<Time>& event, event_id_t evid)
796 WriteLock lock(write_lock());
798 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
800 assert(_notes.empty() || ev.time() >= (*_notes.rbegin())->time());
803 if (!midi_event_is_valid(ev.buffer(), ev.size())) {
804 cerr << "WARNING: Sequence ignoring illegal MIDI event" << endl;
808 if (ev.is_note_on()) {
809 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
810 append_note_on_unlocked (note, evid);
811 } else if (ev.is_note_off()) {
812 NotePtr note(new Note<Time>(ev.channel(), ev.time(), 0, ev.note(), ev.velocity()));
813 /* XXX note: event ID is discarded because we merge the on+off events into
816 append_note_off_unlocked (note);
817 } else if (ev.is_sysex()) {
818 append_sysex_unlocked(ev, evid);
819 } else if (ev.is_cc() && (ev.cc_number() == MIDI_CTL_MSB_BANK || ev.cc_number() == MIDI_CTL_LSB_BANK)) {
820 /* note bank numbers in our _bank[] array, so that we can write an event when the program change arrives */
821 if (ev.cc_number() == MIDI_CTL_MSB_BANK) {
822 _bank[ev.channel()] &= (0x7f << 7);
823 _bank[ev.channel()] |= ev.cc_value() << 7;
825 _bank[ev.channel()] &= 0x7f;
826 _bank[ev.channel()] |= ev.cc_value();
828 } else if (ev.is_cc()) {
829 append_control_unlocked(
830 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
831 ev.time(), ev.cc_value(), evid);
832 } else if (ev.is_pgm_change()) {
833 /* write a patch change with this program change and any previously set-up bank number */
834 append_patch_change_unlocked (PatchChange<Time> (ev.time(), ev.channel(), ev.pgm_number(), _bank[ev.channel()]), evid);
835 } else if (ev.is_pitch_bender()) {
836 append_control_unlocked(
837 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
838 ev.time(), double ((0x7F & ev.pitch_bender_msb()) << 7
839 | (0x7F & ev.pitch_bender_lsb())),
841 } else if (ev.is_channel_pressure()) {
842 append_control_unlocked(
843 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
844 ev.time(), ev.channel_pressure(), evid);
845 } else if (!_type_map.type_is_midi(ev.event_type())) {
846 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
847 for (size_t i=0; i < ev.size(); ++i) {
848 printf("%X ", ev.buffer()[i]);
852 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
858 template<typename Time>
860 Sequence<Time>::append_note_on_unlocked (NotePtr note, event_id_t evid)
862 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n", this,
863 (int) note->channel(), (int) note->note(),
864 note->time(), (int) note->velocity()));
865 assert(note->note() <= 127);
866 assert(note->channel() < 16);
869 if (note->id() < 0) {
873 if (note->velocity() == 0) {
874 append_note_off_unlocked (note);
878 add_note_unlocked (note);
881 DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sustained: Appending active note on %1 channel %2\n",
882 (unsigned)(uint8_t)note->note(), note->channel()));
883 _write_notes[note->channel()].insert (note);
885 DEBUG_TRACE(DEBUG::Sequence, "Percussive: NOT appending active note on\n");
889 template<typename Time>
891 Sequence<Time>::append_note_off_unlocked (NotePtr note)
893 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 c=%2 note %3 on @ %4 v=%5\n",
894 this, (int)note->channel(),
895 (int)note->note(), note->time(), (int)note->velocity()));
896 assert(note->note() <= 127);
897 assert(note->channel() < 16);
902 DEBUG_TRACE(DEBUG::Sequence, "Sequence Ignoring note off (percussive mode)\n");
906 bool resolved = false;
908 /* _write_notes is sorted earliest-latest, so this will find the first matching note (FIFO) that
909 matches this note (by pitch & channel). the MIDI specification doesn't provide any guidance
910 whether to use FIFO or LIFO for this matching process, so SMF is fundamentally a lossy
914 /* XXX use _overlap_pitch_resolution to determine FIFO/LIFO ... */
916 for (typename WriteNotes::iterator n = _write_notes[note->channel()].begin(); n != _write_notes[note->channel()].end(); ) {
918 typename WriteNotes::iterator tmp = n;
922 if (note->note() == nn->note() && nn->channel() == note->channel()) {
923 assert(note->time() >= nn->time());
925 nn->set_length (note->time() - nn->time());
926 nn->set_off_velocity (note->velocity());
928 _write_notes[note->channel()].erase(n);
929 DEBUG_TRACE (DEBUG::Sequence, string_compose ("resolved note, length: %1\n", note->length()));
938 cerr << this << " spurious note off chan " << (int)note->channel()
939 << ", note " << (int)note->note() << " @ " << note->time() << endl;
943 template<typename Time>
945 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value, event_id_t /* evid */)
947 DEBUG_TRACE (DEBUG::Sequence, string_compose ("%1 %2 @ %3\t=\t%4 # controls: %5\n",
948 this, _type_map.to_symbol(param), time, value, _controls.size()));
949 boost::shared_ptr<Control> c = control(param, true);
950 c->list()->add (time, value);
951 /* XXX control events should use IDs */
954 template<typename Time>
956 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev, event_id_t /* evid */)
958 #ifdef DEBUG_SEQUENCE
959 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
960 for (size_t i=0; i < ev.size(); ++i) {
961 cerr << int(ev.buffer()[i]) << " ";
962 } cerr << "]" << endl;
965 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
966 /* XXX sysex events should use IDs */
967 _sysexes.push_back(event);
970 template<typename Time>
972 Sequence<Time>::append_patch_change_unlocked (const PatchChange<Time>& ev, event_id_t id)
974 PatchChangePtr p (new PatchChange<Time> (ev));
980 _patch_changes.insert (p);
983 template<typename Time>
985 Sequence<Time>::contains (const NotePtr& note) const
987 ReadLock lock (read_lock());
988 return contains_unlocked (note);
991 template<typename Time>
993 Sequence<Time>::contains_unlocked (const NotePtr& note) const
995 const Pitches& p (pitches (note->channel()));
996 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
998 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
999 i != p.end() && (*i)->note() == note->note(); ++i) {
1009 template<typename Time>
1011 Sequence<Time>::overlaps (const NotePtr& note, const NotePtr& without) const
1013 ReadLock lock (read_lock());
1014 return overlaps_unlocked (note, without);
1017 template<typename Time>
1019 Sequence<Time>::overlaps_unlocked (const NotePtr& note, const NotePtr& without) const
1021 Time sa = note->time();
1022 Time ea = note->end_time();
1024 const Pitches& p (pitches (note->channel()));
1025 NotePtr search_note(new Note<Time>(0, 0, 0, note->note()));
1027 for (typename Pitches::const_iterator i = p.lower_bound (search_note);
1028 i != p.end() && (*i)->note() == note->note(); ++i) {
1030 if (without && (**i) == *without) {
1034 Time sb = (*i)->time();
1035 Time eb = (*i)->end_time();
1037 if (((sb > sa) && (eb <= ea)) ||
1038 ((eb >= sa) && (eb <= ea)) ||
1039 ((sb > sa) && (sb <= ea)) ||
1040 ((sa >= sb) && (sa <= eb) && (ea <= eb))) {
1048 template<typename Time>
1050 Sequence<Time>::set_notes (const Sequence<Time>::Notes& n)
1055 /** Return the earliest note with time >= t */
1056 template<typename Time>
1057 typename Sequence<Time>::Notes::const_iterator
1058 Sequence<Time>::note_lower_bound (Time t) const
1060 NotePtr search_note(new Note<Time>(0, t, 0, 0, 0));
1061 typename Sequence<Time>::Notes::const_iterator i = _notes.lower_bound(search_note);
1062 assert(i == _notes.end() || (*i)->time() >= t);
1066 /** Return the earliest patch change with time >= t */
1067 template<typename Time>
1068 typename Sequence<Time>::PatchChanges::const_iterator
1069 Sequence<Time>::patch_change_lower_bound (Time t) const
1071 PatchChangePtr search (new PatchChange<Time> (t, 0, 0, 0));
1072 typename Sequence<Time>::PatchChanges::const_iterator i = _patch_changes.lower_bound (search);
1073 assert (i == _patch_changes.end() || (*i)->time() >= t);
1077 template<typename Time>
1079 Sequence<Time>::get_notes (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1084 case PitchLessThanOrEqual:
1086 case PitchGreaterThanOrEqual:
1087 get_notes_by_pitch (n, op, val, chan_mask);
1091 case VelocityLessThan:
1092 case VelocityLessThanOrEqual:
1093 case VelocityGreater:
1094 case VelocityGreaterThanOrEqual:
1095 get_notes_by_velocity (n, op, val, chan_mask);
1100 template<typename Time>
1102 Sequence<Time>::get_notes_by_pitch (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1104 for (uint8_t c = 0; c < 16; ++c) {
1106 if (chan_mask != 0 && !((1<<c) & chan_mask)) {
1110 const Pitches& p (pitches (c));
1111 NotePtr search_note(new Note<Time>(0, 0, 0, val, 0));
1112 typename Pitches::const_iterator i;
1115 i = p.lower_bound (search_note);
1116 while (i != p.end() && (*i)->note() == val) {
1121 i = p.upper_bound (search_note);
1122 while (i != p.end() && (*i)->note() < val) {
1126 case PitchLessThanOrEqual:
1127 i = p.upper_bound (search_note);
1128 while (i != p.end() && (*i)->note() <= val) {
1133 i = p.lower_bound (search_note);
1134 while (i != p.end() && (*i)->note() > val) {
1138 case PitchGreaterThanOrEqual:
1139 i = p.lower_bound (search_note);
1140 while (i != p.end() && (*i)->note() >= val) {
1146 //fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_pitch() called with illegal operator"), op)) << endmsg;
1153 template<typename Time>
1155 Sequence<Time>::get_notes_by_velocity (Notes& n, NoteOperator op, uint8_t val, int chan_mask) const
1157 ReadLock lock (read_lock());
1159 for (typename Notes::const_iterator i = _notes.begin(); i != _notes.end(); ++i) {
1161 if (chan_mask != 0 && !((1<<((*i)->channel())) & chan_mask)) {
1167 if ((*i)->velocity() == val) {
1171 case VelocityLessThan:
1172 if ((*i)->velocity() < val) {
1176 case VelocityLessThanOrEqual:
1177 if ((*i)->velocity() <= val) {
1181 case VelocityGreater:
1182 if ((*i)->velocity() > val) {
1186 case VelocityGreaterThanOrEqual:
1187 if ((*i)->velocity() >= val) {
1192 // fatal << string_compose (_("programming error: %1 %2", X_("get_notes_by_velocity() called with illegal operator"), op)) << endmsg;
1200 template<typename Time>
1202 Sequence<Time>::set_overlap_pitch_resolution (OverlapPitchResolution opr)
1204 _overlap_pitch_resolution = opr;
1206 /* XXX todo: clean up existing overlaps in source data? */
1209 template<typename Time>
1211 Sequence<Time>::control_list_marked_dirty ()
1216 template class Sequence<Evoral::MusicalTime>;
1218 } // namespace Evoral