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
25 #include "evoral/Control.hpp"
26 #include "evoral/ControlList.hpp"
27 #include "evoral/ControlSet.hpp"
28 #include "evoral/EventSink.hpp"
29 #include "evoral/MIDIParameters.hpp"
30 #include "evoral/Sequence.hpp"
31 #include "evoral/TypeMap.hpp"
37 template<typename Time>
38 void Sequence<Time>::write_lock() {
43 template<typename Time>
44 void Sequence<Time>::write_unlock() {
45 _lock.writer_unlock();
46 _control_lock.unlock();
49 template<typename Time>
50 void Sequence<Time>::read_lock() const {
54 template<typename Time>
55 void Sequence<Time>::read_unlock() const {
56 _lock.reader_unlock();
60 // Read iterator (const_iterator)
62 template<typename Time>
63 Sequence<Time>::const_iterator::const_iterator()
68 _event = boost::shared_ptr< Event<Time> >(new Event<Time>());
71 template<typename Time>
72 Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq, Time t)
74 , _is_end((t == DBL_MAX) || seq.empty())
78 cerr << "Created Iterator @ " << t << " (is end: " << _is_end << ")" << endl;
87 // find first note which begins after t
88 _note_iter = seq.notes().end();
89 for (typename Sequence<Time>::Notes::const_iterator i = seq.notes().begin();
90 i != seq.notes().end(); ++i) {
91 if ((*i)->time() >= t) {
96 assert(_note_iter == seq.notes().end() || (*_note_iter)->time() >= t);
98 // find first sysex which begins after t
99 _sysex_iter = seq.sysexes().end();
100 for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
101 i != seq.sysexes().end(); ++i) {
102 if ((*i)->time() >= t) {
107 assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
109 ControlIterator earliest_control(boost::shared_ptr<ControlList>(), DBL_MAX, 0.0);
111 _control_iters.reserve(seq._controls.size());
113 // find the earliest control event available
114 for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
115 #ifdef DEBUG_SEQUENCE
116 cerr << "Iterator: control: " << seq._type_map.to_symbol(i->first) << endl;
119 bool ret = i->second->list()->rt_safe_earliest_event_unlocked(t, DBL_MAX, x, y);
121 #ifdef DEBUG_SEQUENCE
122 cerr << "Iterator: CC " << i->first.id() << " (size " << i->second->list()->size()
123 << ") has no events past " << t << endl;
130 #ifdef DEBUG_SEQUENCE
131 if (y < i->first.min() || y > i->first.max()) {
132 cerr << "ERROR: Controller " << i->first.symbol() << " value " << y
133 << " out of range [" << i->first.min() << "," << i->first.max()
134 << "], event ignored" << endl;
139 const ControlIterator new_iter(i->second->list(), x, y);
141 #ifdef DEBUG_SEQUENCE
142 cerr << "Iterator: CC " << i->first.id() << " added (" << x << ", " << y << ")" << endl;
144 _control_iters.push_back(new_iter);
146 // if the x of the current control is less than earliest_control
147 // we have a new earliest_control
148 if (x < earliest_control.x) {
149 earliest_control = new_iter;
150 _control_iter = _control_iters.end();
152 // now _control_iter points to the last Element in _control_iters
156 #define ENSURE_SYSEX_SANITY 1
157 #if ENSURE_SYSEX_SANITY
158 MIDIMessageType original_type = NIL;
159 assert(!earliest_control.list || earliest_control.x >= t);
160 if (_note_iter != seq.notes().end()
161 && (*_note_iter)->on_event().time() >= t
162 && (!earliest_control.list
163 || (*_note_iter)->on_event().time() < earliest_control.x)) {
164 original_type = NOTE_ON;
166 original_type = CONTROL;
170 MIDIMessageType type = NIL;
173 // if the note comes before anything else set the iterator to the note
174 if (_note_iter != seq.notes().end() && (*_note_iter)->on_event().time() >= t) {
176 earliest_t = (*_note_iter)->on_event().time();
179 if (earliest_control.list &&
180 earliest_control.x >= t &&
181 earliest_control.x <= earliest_t) {
183 earliest_t = earliest_control.x;
186 if (_sysex_iter != seq.sysexes().end() &&
187 (*_sysex_iter)->time() >= t &&
188 (*_sysex_iter)->time() <= earliest_t) {
190 earliest_t = (*_sysex_iter)->time();
193 #if ENSURE_SYSEX_SANITY
194 assert (type == original_type || type == SYSEX);
197 if (type == NOTE_ON) {
198 #ifdef DEBUG_SEQUENCE
199 cerr << "Reading note on event @ " << earliest_t << endl;
201 _event = boost::shared_ptr< Event<Time> >(new Event<Time>((*_note_iter)->on_event(), true));
202 _active_notes.push(*_note_iter);
204 _control_iter = _control_iters.end();
205 } else if (type == CONTROL) {
206 #ifdef DEBUG_SEQUENCE
207 cerr << "Reading control event @ " << earliest_t << endl;
209 seq.control_to_midi_event(_event, earliest_control);
210 } else if (type == SYSEX) {
211 #ifdef DEBUG_SEQUENCE
212 cerr << "Reading system exclusive event @ " << earliest_t << endl;
214 _event = boost::shared_ptr< Event<Time> >(new Event<Time>(*(*_sysex_iter), true));
216 _control_iter = _control_iters.end();
219 if ( (! _event.get()) || _event->size() == 0) {
220 #ifdef DEBUG_SEQUENCE
221 cerr << "New iterator @ " << t << " is at end." << endl;
225 // eliminate possible race condition here (ugly)
226 static Glib::Mutex mutex;
227 Glib::Mutex::Lock lock(mutex);
233 #ifdef DEBUG_SEQUENCE
234 cerr << "New Iterator = " << _event->event_type();
235 cerr << " : " << hex << (int)((MIDIEvent<Time>*)_event.get())->type();
236 cerr << " @ " << _event->time() << endl;
240 assert(_event && _event->size() > 0);
243 template<typename Time>
244 Sequence<Time>::const_iterator::~const_iterator()
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 #ifdef DEBUG_SEQUENCE
260 cerr << "Iterator ++" << endl;
262 assert(_event && _event->buffer() && _event->size() > 0);
264 const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
266 if (! (ev.is_note() || ev.is_cc() || ev.is_pgm_change()
267 || ev.is_pitch_bender() || ev.is_channel_pressure() || ev.is_sysex()) ) {
268 cerr << "WARNING: Unknown event type: " << hex << int(ev.buffer()[0])
269 << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
274 || ev.is_pgm_change()
275 || ev.is_pitch_bender()
276 || ev.is_channel_pressure()
279 // Increment past current control event
280 if (!ev.is_note() && _control_iter != _control_iters.end() && _control_iter->list.get()) {
281 double x = 0.0, y = 0.0;
282 const bool ret = _control_iter->list->rt_safe_earliest_event_unlocked(
283 _control_iter->x, DBL_MAX, x, y, false);
285 assert(!ret || x > _control_iter->x);
288 _control_iter->x = x;
289 _control_iter->y = y;
291 _control_iter->list.reset();
292 _control_iter->x = DBL_MAX;
293 _control_iter->y = DBL_MAX;
297 _control_iter = _control_iters.begin();
299 // find the _control_iter with the earliest event time
300 for (ControlIterators::iterator i = _control_iters.begin(); i != _control_iters.end(); ++i) {
301 if (i->x < _control_iter->x) {
306 MIDIMessageType type = NIL;
309 // Next earliest note on
310 if (_note_iter != _seq->notes().end()) {
312 earliest_t = (*_note_iter)->time();
315 // Use the next earliest note off iff it's earlier than the note on
316 if (!_seq->percussive() && (! _active_notes.empty())) {
317 if (type == NIL || _active_notes.top()->end_time() <= earliest_t) {
319 earliest_t = _active_notes.top()->end_time();
323 // Use the next earliest controller iff it's earlier than the note event
324 if (_control_iter != _control_iters.end() && _control_iter->x != DBL_MAX) {
325 if (type == NIL || _control_iter->x <= earliest_t) {
327 earliest_t = _control_iter->x;
331 // Use the next earliest SysEx iff it's earlier than the controller
332 if (_sysex_iter != _seq->sysexes().end()) {
333 if (type == NIL || (*_sysex_iter)->time() <= earliest_t) {
335 earliest_t = (*_sysex_iter)->time();
339 if (type == NOTE_ON) {
340 #ifdef DEBUG_SEQUENCE
341 cerr << "Iterator = note on" << endl;
343 *_event = (*_note_iter)->on_event();
344 _active_notes.push(*_note_iter);
346 } else if (type == NOTE_OFF) {
347 #ifdef DEBUG_SEQUENCE
348 cerr << "Iterator = note off" << endl;
350 *_event = _active_notes.top()->off_event();
352 } else if (type == CONTROL) {
353 #ifdef DEBUG_SEQUENCE
354 cerr << "Iterator = control" << endl;
356 _seq->control_to_midi_event(_event, *_control_iter);
357 } else if (type == SYSEX) {
358 #ifdef DEBUG_SEQUENCE
359 cerr << "Iterator = SysEx" << endl;
361 *_event =*(*_sysex_iter);
364 #ifdef DEBUG_SEQUENCE
365 cerr << "Iterator = End" << endl;
370 assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
375 template<typename Time>
377 Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
379 if (_is_end || other._is_end) {
380 return (_is_end == other._is_end);
382 return (_event == other._event);
386 template<typename Time>
387 typename Sequence<Time>::const_iterator&
388 Sequence<Time>::const_iterator::operator=(const const_iterator& other)
390 if (_seq != other._seq) {
395 other._seq->read_lock();
400 _active_notes = other._active_notes;
401 _is_end = other._is_end;
402 _locked = other._locked;
403 _note_iter = other._note_iter;
404 _sysex_iter = other._sysex_iter;
405 _control_iters = other._control_iters;
406 size_t index = other._control_iter - other._control_iters.begin();
407 _control_iter = _control_iters.begin() + index;
409 if (!_is_end && other._event) {
411 *_event = *other._event.get();
413 _event = boost::shared_ptr< Event<Time> >(new Event<Time>(*other._event, true));
426 template<typename Time>
427 Sequence<Time>::Sequence(const TypeMap& type_map, size_t size)
428 : _read_iter(*this, DBL_MAX)
430 , _type_map(type_map)
433 , _end_iter(*this, DBL_MAX)
438 #ifdef DEBUG_SEQUENCE
439 cerr << "Sequence (size " << size << ") constructed: " << this << endl;
441 assert(_end_iter._is_end);
442 assert( ! _end_iter._locked);
445 /** Write the controller event pointed to by \a iter to \a ev.
446 * The buffer of \a ev will be allocated or resized as necessary.
447 * The event_type of \a ev should be set to the expected output type.
448 * \return true on success
450 template<typename Time>
452 Sequence<Time>::control_to_midi_event(boost::shared_ptr< Event<Time> >& ev, const ControlIterator& iter) const
454 assert(iter.list.get());
455 const uint32_t event_type = iter.list->parameter().type();
457 // initialize the event pointer with a new event, if necessary
459 ev = boost::shared_ptr< Event<Time> >(new Event<Time>(event_type, 0, 3, NULL, true));
462 uint8_t midi_type = _type_map.parameter_midi_type(iter.list->parameter());
463 ev->set_event_type(_type_map.midi_event_type(midi_type));
465 case MIDI_CMD_CONTROL:
466 assert(iter.list.get());
467 assert(iter.list->parameter().channel() < 16);
468 assert(iter.list->parameter().id() <= INT8_MAX);
469 assert(iter.y <= INT8_MAX);
473 ev->buffer()[0] = MIDI_CMD_CONTROL + iter.list->parameter().channel();
474 ev->buffer()[1] = (uint8_t)iter.list->parameter().id();
475 ev->buffer()[2] = (uint8_t)iter.y;
478 case MIDI_CMD_PGM_CHANGE:
479 assert(iter.list.get());
480 assert(iter.list->parameter().channel() < 16);
481 assert(iter.y <= INT8_MAX);
485 ev->buffer()[0] = MIDI_CMD_PGM_CHANGE + iter.list->parameter().channel();
486 ev->buffer()[1] = (uint8_t)iter.y;
489 case MIDI_CMD_BENDER:
490 assert(iter.list.get());
491 assert(iter.list->parameter().channel() < 16);
492 assert(iter.y < (1<<14));
496 ev->buffer()[0] = MIDI_CMD_BENDER + iter.list->parameter().channel();
497 ev->buffer()[1] = uint16_t(iter.y) & 0x7F; // LSB
498 ev->buffer()[2] = (uint16_t(iter.y) >> 7) & 0x7F; // MSB
501 case MIDI_CMD_CHANNEL_PRESSURE:
502 assert(iter.list.get());
503 assert(iter.list->parameter().channel() < 16);
504 assert(iter.y <= INT8_MAX);
508 ev->buffer()[0] = MIDI_CMD_CHANNEL_PRESSURE + iter.list->parameter().channel();
509 ev->buffer()[1] = (uint8_t)iter.y;
519 /** Clear all events from the model.
521 template<typename Time>
523 Sequence<Time>::clear()
527 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
528 li->second->list()->clear();
530 _lock.writer_unlock();
533 /** Begin a write of events to the model.
535 * If \a mode is Sustained, complete notes with length are constructed as note
536 * on/off events are received. Otherwise (Percussive), only note on events are
537 * stored; note off events are discarded entirely and all contained notes will
540 template<typename Time>
542 Sequence<Time>::start_write()
544 #ifdef DEBUG_SEQUENCE
545 cerr << this << " START WRITE, PERCUSSIVE = " << _percussive << endl;
549 for (int i = 0; i < 16; ++i)
550 _write_notes[i].clear();
552 _dirty_controls.clear();
556 /** Finish a write of events to the model.
558 * If \a delete_stuck is true and the current mode is Sustained, note on events
559 * that were never resolved with a corresonding note off will be deleted.
560 * Otherwise they will remain as notes with length 0.
562 template<typename Time>
564 Sequence<Time>::end_write(bool delete_stuck)
569 #ifdef DEBUG_SEQUENCE
570 cerr << this << " END WRITE: " << _notes.size() << " NOTES\n";
573 if (!_percussive && delete_stuck) {
574 for (typename Notes::iterator n = _notes.begin(); n != _notes.end() ;) {
575 if ((*n)->length() == 0) {
576 cerr << "WARNING: Stuck note lost: " << (*n)->note() << endl;
578 // we have to break here because erase invalidates the iterator
586 for (int i = 0; i < 16; ++i) {
587 if (!_write_notes[i].empty()) {
588 cerr << "WARNING: Sequence<Time>::end_write: Channel " << i << " has "
589 << _write_notes[i].size() << " stuck notes" << endl;
591 _write_notes[i].clear();
594 for (ControlLists::const_iterator i = _dirty_controls.begin(); i != _dirty_controls.end(); ++i) {
602 /** Append \a ev to model. NOT realtime safe.
604 * Timestamps of events in \a buf are expected to be relative to
605 * the start of this model (t=0) and MUST be monotonically increasing
606 * and MUST be >= the latest event currently in the model.
608 template<typename Time>
610 Sequence<Time>::append(const Event<Time>& event)
615 const MIDIEvent<Time>& ev = (const MIDIEvent<Time>&)event;
617 assert(_notes.empty() || ev.time() >= _notes.back()->time());
620 if (ev.is_note_on()) {
621 append_note_on_unlocked(ev.channel(), ev.time(), ev.note(), ev.velocity());
622 } else if (ev.is_note_off()) {
623 append_note_off_unlocked(ev.channel(), ev.time(), ev.note());
624 } else if (ev.is_sysex()) {
625 append_sysex_unlocked(ev);
626 } else if (!_type_map.type_is_midi(ev.event_type())) {
627 printf("WARNING: Sequence: Unknown event type %X: ", ev.event_type());
628 for (size_t i=0; i < ev.size(); ++i) {
629 printf("%X ", ev.buffer()[i]);
632 } else if (ev.is_cc()) {
633 append_control_unlocked(
634 Evoral::MIDI::ContinuousController(ev.event_type(), ev.channel(), ev.cc_number()),
635 ev.time(), ev.cc_value());
636 } else if (ev.is_pgm_change()) {
637 append_control_unlocked(
638 Evoral::MIDI::ProgramChange(ev.event_type(), ev.channel()),
639 ev.time(), ev.pgm_number());
640 } else if (ev.is_pitch_bender()) {
641 append_control_unlocked(
642 Evoral::MIDI::PitchBender(ev.event_type(), ev.channel()),
643 ev.time(), double( (0x7F & ev.pitch_bender_msb()) << 7
644 | (0x7F & ev.pitch_bender_lsb()) ));
645 } else if (ev.is_channel_pressure()) {
646 append_control_unlocked(
647 Evoral::MIDI::ChannelPressure(ev.event_type(), ev.channel()),
648 ev.time(), ev.channel_pressure());
650 printf("WARNING: Sequence: Unknown MIDI event type %X\n", ev.type());
656 template<typename Time>
658 Sequence<Time>::append_note_on_unlocked(uint8_t chan, Time time, uint8_t note_num, uint8_t velocity)
660 #ifdef DEBUG_SEQUENCE
661 cerr << this << " c=" << (int)chan << " note " << (int)note_num
662 << " on @ " << time << " v=" << (int)velocity << endl;
664 assert(note_num <= 127);
670 append_note_off_unlocked(chan, time, note_num);
674 if (note_num < _lowest_note)
675 _lowest_note = note_num;
676 if (note_num > _highest_note)
677 _highest_note = note_num;
679 boost::shared_ptr< Note<Time> > new_note(new Note<Time>(chan, time, 0, note_num, velocity));
680 _notes.push_back(new_note);
682 #ifdef DEBUG_SEQUENCE
683 cerr << "Sustained: Appending active note on " << (unsigned)(uint8_t)note_num
684 << " channel " << chan << endl;
686 _write_notes[chan].push_back(_notes.size() - 1);
688 #ifdef DEBUG_SEQUENCE
689 cerr << "Percussive: NOT appending active note on" << endl;
694 template<typename Time>
696 Sequence<Time>::append_note_off_unlocked(uint8_t chan, Time time, uint8_t note_num)
698 #ifdef DEBUG_SEQUENCE
699 cerr << this << " c=" << (int)chan << " note " << (int)note_num
700 << " off @ " << time << endl;
702 assert(note_num <= 127);
708 #ifdef DEBUG_SEQUENCE
709 cerr << "Sequence Ignoring note off (percussive mode)" << endl;
714 /* FIXME: make _write_notes fixed size (127 noted) for speed */
716 /* FIXME: note off velocity for that one guy out there who actually has
717 * keys that send it */
719 bool resolved = false;
721 for (WriteNotes::iterator n = _write_notes[chan].begin(); n
722 != _write_notes[chan].end(); ++n) {
723 Note<Time>& note = *_notes[*n].get();
724 if (note.note() == note_num) {
725 assert(time >= note.time());
726 note.set_length(time - note.time());
727 _write_notes[chan].erase(n);
728 #ifdef DEBUG_SEQUENCE
729 cerr << "resolved note, length: " << note.length() << endl;
737 cerr << this << " spurious note off chan " << (int)chan
738 << ", note " << (int)note_num << " @ " << time << endl;
742 template<typename Time>
744 Sequence<Time>::append_control_unlocked(const Parameter& param, Time time, double value)
746 #ifdef DEBUG_SEQUENCE
747 cerr << this << " " << _type_map.to_symbol(param) << " @ " << time << " \t= \t" << value
748 << " # controls: " << _controls.size() << endl;
750 boost::shared_ptr<Control> c = control(param, true);
751 c->list()->rt_add(time, value);
754 template<typename Time>
756 Sequence<Time>::append_sysex_unlocked(const MIDIEvent<Time>& ev)
758 #ifdef DEBUG_SEQUENCE
759 cerr << this << " SysEx @ " << ev.time() << " \t= \t [ " << hex;
760 for (size_t i=0; i < ev.size(); ++i) {
761 cerr << int(ev.buffer()[i]) << " ";
766 boost::shared_ptr<MIDIEvent<Time> > event(new MIDIEvent<Time>(ev, true));
767 _sysexes.push_back(event);
770 template<typename Time>
772 Sequence<Time>::add_note_unlocked(const boost::shared_ptr< Note<Time> > note)
774 #ifdef DEBUG_SEQUENCE
775 cerr << this << " add note " << (int)note->note() << " @ " << note->time() << endl;
778 typename Notes::iterator i = upper_bound(_notes.begin(), _notes.end(), note,
779 note_time_comparator);
780 _notes.insert(i, note);
783 template<typename Time>
785 Sequence<Time>::remove_note_unlocked(const boost::shared_ptr< const Note<Time> > note)
788 #ifdef DEBUG_SEQUENCE
789 cerr << this << " remove note " << (int)note->note() << " @ " << note->time() << endl;
791 for (typename Notes::iterator n = _notes.begin(); n != _notes.end(); ++n) {
792 if (*(*n) == *note) {
799 template class Sequence<double>;
801 } // namespace Evoral