remove complex iterator from Evoral::Sequence and replace with wrapping a normal std::multiset iterator

This commit is contained in:
Paul Davis 2016-11-03 10:44:17 +00:00
parent e800d595dc
commit cb567dd881
7 changed files with 90 additions and 606 deletions

View file

@ -236,7 +236,6 @@ class LIBARDOUR_API MidiSource : virtual public Source, public boost::enable_sha
mutable bool _model_iter_valid;
mutable Evoral::Beats _length_beats;
mutable framepos_t _last_read_end;
/** The total duration of the current capture. */
framepos_t _capture_length;

View file

@ -1428,7 +1428,7 @@ MidiModel::write_to (boost::shared_ptr<MidiSource> source,
source->drop_model(source_lock);
source->mark_streaming_midi_write_started (source_lock, note_mode());
for (Evoral::Sequence<TimeType>::const_iterator i = begin(TimeType(), true); i != end(); ++i) {
for (Evoral::Sequence<TimeType>::const_iterator i = begin (); i != end(); ++i) {
source->append_event_beats(source_lock, *i);
}
@ -1466,7 +1466,7 @@ MidiModel::sync_to_source (const Glib::Threads::Mutex::Lock& source_lock)
ms->mark_streaming_midi_write_started (source_lock, note_mode());
for (Evoral::Sequence<TimeType>::const_iterator i = begin(TimeType(), true); i != end(); ++i) {
for (Evoral::Sequence<TimeType>::const_iterator i = begin (); i != end(); ++i) {
ms->append_event_beats(source_lock, *i);
}
@ -1501,7 +1501,7 @@ MidiModel::write_section_to (boost::shared_ptr<MidiSource> source,
source->drop_model(source_lock);
source->mark_streaming_midi_write_started (source_lock, note_mode());
for (Evoral::Sequence<TimeType>::const_iterator i = begin(TimeType(), true); i != end(); ++i) {
for (Evoral::Sequence<TimeType>::const_iterator i = begin (); i != end(); ++i) {
if (i->time() >= begin_time && i->time() < end_time) {
Evoral::MIDIEvent<TimeType> mev (*i, true); /* copy the event */

View file

@ -421,8 +421,7 @@ MidiRegion::_read_at (const SourceList& /*srcs*/,
<< " _start = " << _start
<< " intoffset = " << internal_offset
<< " pulse = " << pulse()
<< " start_pulse = " << start_pulse()
<< " start_beat = " << _start_beats
<< " beat = " << _start_beats
<< endl;
#endif
@ -438,8 +437,8 @@ MidiRegion::_read_at (const SourceList& /*srcs*/,
tracker,
filter,
_filtered_parameters,
pulse(),
_start_beats
pulse(), // position of region in musical time
_start_beats // offset into source file in musical time
) != to_read) {
return 0; /* "read nothing" */
}

View file

@ -63,7 +63,6 @@ MidiSource::MidiSource (Session& s, string name, Source::Flag flags)
, _writing(false)
, _model_iter_valid(false)
, _length_beats(0.0)
, _last_read_end(0)
, _capture_length(0)
, _capture_loop_length(0)
{
@ -74,7 +73,6 @@ MidiSource::MidiSource (Session& s, const XMLNode& node)
, _writing(false)
, _model_iter_valid(false)
, _length_beats(0.0)
, _last_read_end(0)
, _capture_length(0)
, _capture_loop_length(0)
{
@ -182,151 +180,91 @@ void
MidiSource::invalidate (const Lock& lock, std::set<Evoral::Sequence<Evoral::Beats>::WeakNotePtr>* notes)
{
_model_iter_valid = false;
_model_iter.invalidate(notes);
//_model_iter.invalidate(notes);
}
framecnt_t
MidiSource::midi_read (const Lock& lm,
Evoral::EventSink<framepos_t>& dst,
framepos_t source_start,
framepos_t start,
framepos_t source_start, // position of source origin (samples)
framepos_t start, // first position to read (samples)
framecnt_t cnt,
Evoral::Range<framepos_t>* loop_range,
MidiStateTracker* tracker,
MidiChannelFilter* filter,
const std::set<Evoral::Parameter>& filtered,
const double pulse,
const double start_beats) const
const double pulse, // position of start of region in musical time
const double start_beats // offset into data in musical time (equivalent to _start above)
) const
{
//BeatsFramesConverter converter(_session.tempo_map(), source_start);
const int32_t tpb = Timecode::BBT_Time::ticks_per_beat;
const double pulse_tick_res = floor ((pulse * 4.0 * tpb) + 0.5) / tpb;
const double start_qn = (pulse * 4.0) - start_beats;
DEBUG_TRACE (DEBUG::MidiSourceIO,
string_compose ("MidiSource::midi_read() %5 sstart %1 start %2 cnt %3 tracker %4\n",
source_start, start, cnt, tracker, name()));
if (!_model) {
return read_unlocked (lm, dst, source_start, start, cnt, loop_range, tracker, filter);
}
// Find appropriate model iterator
Evoral::Sequence<Evoral::Beats>::const_iterator& i = _model_iter;
const bool linear_read = _last_read_end != 0 && start == _last_read_end;
if (!linear_read || !_model_iter_valid) {
/* Events in the model are timestamped relative to the start of the model (zero).
* We're looking for events in the model corresponding to events on a given section of the
* timeline. Unless the start of the model (zero) is at the the origin of the timeline,
* these two time coordinates are displaced from each other. So we need to compute
* the relative displacement, and then look for the correct events.
*
* start on timeline (samples): start
* source start on timeline (musical): start of region - start_offset_of_region_within_file
* convert start
* compute different in musical time
*/
const int32_t tpb = Timecode::BBT_Time::ticks_per_beat;
const double pulse_tick_res = floor ((pulse * 4.0 * tpb) + 0.5) / tpb;
/* musical time position of start of source */
const TimeType start_qn ((pulse * 4.0) - start_beats);
const TimeType read_start_qn (_session.tempo_map().exact_beat_at_frame (start, 0));
const TimeType read_end_qn (_session.tempo_map().exact_beat_at_frame (start+cnt, 0));
const TimeType first_possible_event_time = read_start_qn - start_qn;
const TimeType last_possible_event_time = read_end_qn - start_qn;
DEBUG_TRACE (DEBUG::MidiSourceIO,
string_compose ("MidiSource::midi_read() %5 sstart %1 start %2 (qn = %5) cnt %3 tracker %4\n",
source_start, start, cnt, tracker, name(), start_qn));
for (MidiModel::const_iterator i = _model->lower_bound (first_possible_event_time); i != _model->end() && i->time() < last_possible_event_time; ++i) {
#if 0
// Cached iterator is invalid, search for the first event past start
i = _model->begin(converter.from(start), false, filtered,
linear_read ? &_model->active_notes() : NULL);
_model_iter_valid = true;
if (!linear_read) {
_model->active_notes().clear();
}
#else
/* hot-fix http://tracker.ardour.org/view.php?id=6541
* "parallel playback of linked midi regions -> no note-offs"
*
* A midi source can be used by multiple tracks simultaneously,
* in which case midi_read() may be called from different tracks for
* overlapping time-ranges.
*
* However there is only a single iterator for a given midi-source.
* This results in every midi_read() performing a seek.
*
* If seeking is performed with
* _model->begin(converter.from(start),...)
* the model is used for seeking. That method seeks to the first
* *note-on* event after 'start'.
*
* _model->begin(converter.from( ) ,..) eventually calls
* Sequence<Time>::const_iterator() in libs/evoral/src/Sequence.cpp
* which looks up the note-event via seq.note_lower_bound(t);
* but the sequence 'seq' only contains note-on events(!).
* note-off events are implicit in Sequence<Time>::operator++()
* via _active_notes.pop(); and not part of seq.
*
* see also http://tracker.ardour.org/view.php?id=6287#c16671
*
* The linear search below assures that reading starts at the first
* event for the given time, regardless of its event-type.
*
* The performance of this approach is O(N), while the previous
* implementation is O(log(N)). This needs to be optimized:
* The model-iterator or event-sequence needs to be re-designed in
* some way (maybe keep an iterator per playlist).
*/
for (i = _model->begin(); i != _model->end(); ++i) {
if (floor (((i->time().to_double() + start_qn) * tpb) + 0.5) / tpb >= pulse_tick_res) {
break;
}
}
_model_iter_valid = true;
if (!linear_read) {
_model->active_notes().clear();
}
#endif
}
_last_read_end = start + cnt;
// Copy events in [start, start + cnt) into dst
for (; i != _model->end(); ++i) {
// Offset by source start to convert event time to session time
framecnt_t time_frames = _session.tempo_map().frame_at_quarter_note (i->time().to_double() + start_qn);
if (time_frames < (start + source_start)) {
/* event too early */
continue;
} else if (time_frames >= start + cnt + source_start) {
if (filter && filter->filter(i->buffer(), i->size())) {
DEBUG_TRACE (DEBUG::MidiSourceIO,
string_compose ("%1: reached end with event @ %2 vs. %3\n",
_name, time_frames, start+cnt));
break;
string_compose ("%1: filter event @ %2 type %3 size %4\n",
_name, i->time(), i->event_type(), i->size()));
continue;
}
} else {
if (loop_range) {
// time_frames = loop_range->squish (time_frames);
}
/* in range */
if (filter && filter->filter(i->buffer(), i->size())) {
DEBUG_TRACE (DEBUG::MidiSourceIO,
string_compose ("%1: filter event @ %2 type %3 size %4\n",
_name, time_frames, i->event_type(), i->size()));
continue;
}
if (loop_range) {
time_frames = loop_range->squish (time_frames);
}
dst.write (time_frames, i->event_type(), i->size(), i->buffer());
// dst.write (time_frames, i->event_type(), i->size(), i->buffer());
#ifndef NDEBUG
if (DEBUG_ENABLED(DEBUG::MidiSourceIO)) {
DEBUG_STR_DECL(a);
DEBUG_STR_APPEND(a, string_compose ("%1 added event @ %2 sz %3 within %4 .. %5 ",
_name, time_frames, i->size(),
start + source_start, start + cnt + source_start));
for (size_t n=0; n < i->size(); ++n) {
DEBUG_STR_APPEND(a,hex);
DEBUG_STR_APPEND(a,"0x");
DEBUG_STR_APPEND(a,(int)i->buffer()[n]);
DEBUG_STR_APPEND(a,' ');
}
DEBUG_STR_APPEND(a,'\n');
DEBUG_TRACE (DEBUG::MidiSourceIO, DEBUG_STR(a).str());
if (DEBUG_ENABLED(DEBUG::MidiSourceIO)) {
DEBUG_STR_DECL(a);
DEBUG_STR_APPEND(a, string_compose ("%1 added event @ %2 sz %3 within %4 .. %5 ",
_name, time_frames, i->size(),
start + source_start, start + cnt + source_start));
for (size_t n=0; n < i->size(); ++n) {
DEBUG_STR_APPEND(a,hex);
DEBUG_STR_APPEND(a,"0x");
DEBUG_STR_APPEND(a,(int)i->buffer()[n]);
DEBUG_STR_APPEND(a,' ');
}
DEBUG_STR_APPEND(a,'\n');
DEBUG_TRACE (DEBUG::MidiSourceIO, DEBUG_STR(a).str());
}
#endif
if (tracker) {
tracker->track (*i);
}
#endif
if (tracker) {
tracker->track (*i);
}
}
@ -342,7 +280,6 @@ MidiSource::midi_write (const Lock& lm,
const framecnt_t ret = write_unlocked (lm, source, source_start, cnt);
if (cnt == max_framecnt) {
_last_read_end = 0;
invalidate(lm);
} else {
_capture_length += cnt;

View file

@ -98,7 +98,7 @@ MidiStretch::run (boost::shared_ptr<Region> r, Progress*)
/* Note: pass true into force_discrete for the begin() iterator so that the model doesn't
* do interpolation of controller data when we stretch.
*/
for (Evoral::Sequence<MidiModel::TimeType>::const_iterator i = old_model->begin (MidiModel::TimeType(), true);
for (Evoral::Sequence<MidiModel::TimeType>::const_iterator i = old_model->begin ();
i != old_model->end(); ++i) {
const MidiModel::TimeType new_time = i->time() * (double)_request.time_fraction;

View file

@ -62,11 +62,11 @@ public:
* Controller data is represented as a list of time-stamped float values. */
template<typename Time>
class LIBEVORAL_API Sequence : virtual public ControlSet {
public:
public:
Sequence(const TypeMap& type_map);
Sequence(const Sequence<Time>& other);
protected:
protected:
struct WriteLockImpl {
WriteLockImpl(Glib::Threads::RWLock& s, Glib::Threads::Mutex& c)
: sequence_lock(new Glib::Threads::RWLock::WriterLock(s))
@ -79,7 +79,22 @@ protected:
Glib::Threads::Mutex::Lock* control_lock;
};
public:
public:
struct EventTimeComparator {
bool operator() (Event<Time> const & a, Event<Time> const & b) const {
return a.time() < b.time();
}
};
typedef typename std::multiset<Evoral::Event<Time>,EventTimeComparator> Events;
typedef typename Events::const_iterator const_iterator;
private:
Events _events; // all events, indexed by time
public:
inline size_t n_events () const { return _events.size(); }
inline size_t empty () const { return _events.empty(); }
typedef typename boost::shared_ptr<Evoral::Note<Time> > NotePtr;
typedef typename boost::weak_ptr<Evoral::Note<Time> > WeakNotePtr;
@ -112,7 +127,6 @@ public:
const TypeMap& type_map() const { return _type_map; }
inline size_t n_notes() const { return _notes.size(); }
inline bool empty() const { return _notes.empty() && _sysexes.empty() && _patch_changes.empty() && ControlSet::controls_empty(); }
inline static bool note_time_comparator(const boost::shared_ptr< const Note<Time> >& a,
const boost::shared_ptr< const Note<Time> >& b) {
@ -207,68 +221,10 @@ public:
private:
typedef std::priority_queue<NotePtr, std::deque<NotePtr>, LaterNoteEndComparator> ActiveNotes;
public:
const_iterator begin () const { return _events.begin(); }
const const_iterator end() const { return _events.end(); }
/** Read iterator */
class LIBEVORAL_API const_iterator {
public:
const_iterator();
const_iterator(const Sequence<Time>& seq,
Time t,
bool force_discrete,
const std::set<Evoral::Parameter>& filtered,
const std::set<WeakNotePtr>* active_notes=NULL);
inline bool valid() const { return !_is_end && _event; }
void invalidate(std::set<WeakNotePtr>* notes);
const Event<Time>& operator*() const { return *_event; }
const boost::shared_ptr< Event<Time> > operator->() const { return _event; }
const boost::shared_ptr< Event<Time> > get_event_pointer() { return _event; }
const const_iterator& operator++(); // prefix only
bool operator==(const const_iterator& other) const;
bool operator!=(const const_iterator& other) const { return ! operator==(other); }
const_iterator& operator=(const const_iterator& other);
private:
friend class Sequence<Time>;
Time choose_next(Time earliest_t);
void set_event();
typedef std::vector<ControlIterator> ControlIterators;
enum MIDIMessageType { NIL, NOTE_ON, NOTE_OFF, CONTROL, SYSEX, PATCH_CHANGE };
const Sequence<Time>* _seq;
boost::shared_ptr< Event<Time> > _event;
mutable ActiveNotes _active_notes;
/** If the iterator is pointing at a patch change, this is the index of the
* sub-message within that change.
*/
int _active_patch_change_message;
MIDIMessageType _type;
bool _is_end;
typename Sequence::ReadLock _lock;
typename Notes::const_iterator _note_iter;
typename SysExes::const_iterator _sysex_iter;
typename PatchChanges::const_iterator _patch_change_iter;
ControlIterators _control_iters;
ControlIterators::iterator _control_iter;
bool _force_discrete;
};
const_iterator begin (
Time t = Time(),
bool force_discrete = false,
const std::set<Evoral::Parameter>& f = std::set<Evoral::Parameter>(),
const std::set<WeakNotePtr>* active_notes = NULL) const {
return const_iterator (*this, t, force_discrete, f, active_notes);
}
const const_iterator& end() const { return _end_iter; }
const_iterator lower_bound (Time t) const { Event<Time> ev (0, t, 0, 0, false); return _events.lower_bound (ev); }
// CONST iterator implementations (x3)
typename Notes::const_iterator note_lower_bound (Time t) const;
@ -321,8 +277,6 @@ protected:
virtual void control_list_marked_dirty ();
private:
friend class const_iterator;
bool overlaps_unlocked (const NotePtr& ev, const NotePtr& ignore_this_note) const;
bool contains_unlocked (const NotePtr& ev) const;
@ -338,7 +292,7 @@ private:
const TypeMap& _type_map;
Notes _notes; // notes indexed by time
Pitches _pitches[16]; // notes indexed by channel+pitch
Pitches _pitches[16]; // notes indexed by pitch, but also channel via array index
SysExes _sysexes;
PatchChanges _patch_changes;
@ -351,7 +305,6 @@ private:
*/
int _bank[16];
const const_iterator _end_iter;
bool _percussive;
uint8_t _lowest_note;
@ -365,4 +318,3 @@ template<typename Time> /*LIBEVORAL_API*/ std::ostream& operator<<(std::ostream&
#endif // EVORAL_SEQUENCE_HPP

View file

@ -57,403 +57,6 @@ static double const time_between_interpolated_controller_outputs = 1.0 / 256;
namespace Evoral {
// Read iterator (const_iterator)
template<typename Time>
Sequence<Time>::const_iterator::const_iterator()
: _seq(NULL)
, _event(boost::shared_ptr< Event<Time> >(new Event<Time>()))
, _active_patch_change_message (0)
, _type(NIL)
, _is_end(true)
, _control_iter(_control_iters.end())
, _force_discrete(false)
{
}
/** @param force_discrete true to force ControlLists to use discrete evaluation, otherwise false to get them to use their configured mode */
template<typename Time>
Sequence<Time>::const_iterator::const_iterator(const Sequence<Time>& seq,
Time t,
bool force_discrete,
const std::set<Evoral::Parameter>& filtered,
const std::set<WeakNotePtr>* active_notes)
: _seq(&seq)
, _active_patch_change_message (0)
, _type(NIL)
, _is_end((t == DBL_MAX) || seq.empty())
, _note_iter(seq.notes().end())
, _sysex_iter(seq.sysexes().end())
, _patch_change_iter(seq.patch_changes().end())
, _control_iter(_control_iters.end())
, _force_discrete (force_discrete)
{
DEBUG_TRACE (DEBUG::Sequence, string_compose ("Created Iterator @ %1 (is end: %2)\n)", t, _is_end));
if (_is_end) {
return;
}
_lock = seq.read_lock();
// Add currently active notes, if given
if (active_notes) {
for (typename std::set<WeakNotePtr>::const_iterator i = active_notes->begin();
i != active_notes->end(); ++i) {
NotePtr note = i->lock();
if (note && note->time() <= t && note->end_time() > t) {
_active_notes.push(note);
}
}
}
// Find first note which begins at or after t
_note_iter = seq.note_lower_bound(t);
// Find first sysex event at or after t
for (typename Sequence<Time>::SysExes::const_iterator i = seq.sysexes().begin();
i != seq.sysexes().end(); ++i) {
if ((*i)->time() >= t) {
_sysex_iter = i;
break;
}
}
assert(_sysex_iter == seq.sysexes().end() || (*_sysex_iter)->time() >= t);
// Find first patch event at or after t
for (typename Sequence<Time>::PatchChanges::const_iterator i = seq.patch_changes().begin(); i != seq.patch_changes().end(); ++i) {
if ((*i)->time() >= t) {
_patch_change_iter = i;
break;
}
}
assert (_patch_change_iter == seq.patch_changes().end() || (*_patch_change_iter)->time() >= t);
// Find first control event after t
_control_iters.reserve(seq._controls.size());
bool found = false;
size_t earliest_control_index = 0;
double earliest_control_x = DBL_MAX;
for (Controls::const_iterator i = seq._controls.begin(); i != seq._controls.end(); ++i) {
if (filtered.find (i->first) != filtered.end()) {
/* this parameter is filtered, so don't bother setting up an iterator for it */
continue;
}
DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: control: %1\n", seq._type_map.to_symbol(i->first)));
double x, y;
bool ret;
if (_force_discrete || i->second->list()->interpolation() == ControlList::Discrete) {
ret = i->second->list()->rt_safe_earliest_event_discrete_unlocked (t.to_double(), x, y, true);
} else {
ret = i->second->list()->rt_safe_earliest_event_unlocked(t.to_double(), x, y, true);
}
if (!ret) {
DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 (size %2) has no events past %3\n",
i->first.id(), i->second->list()->size(), t));
continue;
}
assert(x >= 0);
const ParameterDescriptor& desc = seq.type_map().descriptor(i->first);
if (y < desc.lower || y > desc.upper) {
cerr << "ERROR: Controller value " << y
<< " out of range [" << desc.lower << "," << desc.upper
<< "], event ignored" << endl;
continue;
}
DEBUG_TRACE (DEBUG::Sequence, string_compose ("Iterator: CC %1 added (%2, %3)\n", i->first.id(), x, y));
const ControlIterator new_iter(i->second->list(), x, y);
_control_iters.push_back(new_iter);
// Found a new earliest_control
if (x < earliest_control_x) {
earliest_control_x = x;
earliest_control_index = _control_iters.size() - 1;
found = true;
}
}
if (found) {
_control_iter = _control_iters.begin() + earliest_control_index;
assert(_control_iter != _control_iters.end());
assert(_control_iter->list);
} else {
_control_iter = _control_iters.end();
}
// Choose the earliest event overall to point to
choose_next(t);
// Allocate a new event for storing the current event in MIDI format
_event = boost::shared_ptr< Event<Time> >(
new Event<Time>(0, Time(), 4, NULL, true));
// Set event from chosen sub-iterator
set_event();
if (_is_end) {
DEBUG_TRACE(DEBUG::Sequence,
string_compose("Starting at end @ %1\n", t));
} else {
DEBUG_TRACE(DEBUG::Sequence,
string_compose("Starting at type 0x%1 : 0x%2 @ %3\n",
(int)_event->event_type(),
(int)((MIDIEvent<Time>*)_event.get())->type(),
_event->time()));
}
}
template<typename Time>
void
Sequence<Time>::const_iterator::invalidate(std::set< boost::weak_ptr< Note<Time> > >* notes)
{
while (!_active_notes.empty()) {
if (notes) {
notes->insert(_active_notes.top());
}
_active_notes.pop();
}
_type = NIL;
_is_end = true;
if (_seq) {
_note_iter = _seq->notes().end();
_sysex_iter = _seq->sysexes().end();
_patch_change_iter = _seq->patch_changes().end();
_active_patch_change_message = 0;
}
_control_iters.clear();
_control_iter = _control_iters.end();
_lock.reset();
}
template<typename Time>
Time
Sequence<Time>::const_iterator::choose_next(Time earliest_t)
{
_type = NIL;
// Next earliest note on
if (_note_iter != _seq->notes().end()) {
_type = NOTE_ON;
earliest_t = (*_note_iter)->time();
}
// Use the next note off iff it's earlier or the same time as the note on
if ((!_active_notes.empty())) {
if (_type == NIL || _active_notes.top()->end_time().to_double() <= earliest_t.to_double()) {
_type = NOTE_OFF;
earliest_t = _active_notes.top()->end_time();
}
}
// Use the next earliest controller iff it's earlier than the note event
if (_control_iter != _control_iters.end() &&
_control_iter->list && _control_iter->x != DBL_MAX) {
if (_type == NIL || _control_iter->x < earliest_t.to_double()) {
_type = CONTROL;
earliest_t = Time(_control_iter->x);
}
}
// Use the next earliest SysEx iff it's earlier than the controller
if (_sysex_iter != _seq->sysexes().end()) {
if (_type == NIL || (*_sysex_iter)->time() < earliest_t) {
_type = SYSEX;
earliest_t = (*_sysex_iter)->time();
}
}
// Use the next earliest patch change iff it's earlier than the SysEx
if (_patch_change_iter != _seq->patch_changes().end()) {
if (_type == NIL || (*_patch_change_iter)->time() < earliest_t) {
_type = PATCH_CHANGE;
earliest_t = (*_patch_change_iter)->time();
}
}
return earliest_t;
}
template<typename Time>
void
Sequence<Time>::const_iterator::set_event()
{
switch (_type) {
case NOTE_ON:
DEBUG_TRACE(DEBUG::Sequence, "iterator = note on\n");
_event->assign ((*_note_iter)->on_event());
_active_notes.push(*_note_iter);
break;
case NOTE_OFF:
DEBUG_TRACE(DEBUG::Sequence, "iterator = note off\n");
assert(!_active_notes.empty());
_event->assign (_active_notes.top()->off_event());
// We don't pop the active note until we increment past it
break;
case SYSEX:
DEBUG_TRACE(DEBUG::Sequence, "iterator = sysex\n");
_event->assign (*(*_sysex_iter));
break;
case CONTROL:
DEBUG_TRACE(DEBUG::Sequence, "iterator = control\n");
_seq->control_to_midi_event(_event, *_control_iter);
break;
case PATCH_CHANGE:
DEBUG_TRACE(DEBUG::Sequence, "iterator = program change\n");
_event->assign ((*_patch_change_iter)->message (_active_patch_change_message));
break;
default:
_is_end = true;
break;
}
if (_type == NIL || !_event || _event->size() == 0) {
DEBUG_TRACE(DEBUG::Sequence, "iterator = end\n");
_type = NIL;
_is_end = true;
} else {
assert(midi_event_is_valid(_event->buffer(), _event->size()));
}
}
template<typename Time>
const typename Sequence<Time>::const_iterator&
Sequence<Time>::const_iterator::operator++()
{
if (_is_end) {
throw std::logic_error("Attempt to iterate past end of Sequence");
}
assert(_event && _event->buffer() && _event->size() > 0);
const MIDIEvent<Time>& ev = *((MIDIEvent<Time>*)_event.get());
if (!( ev.is_note()
|| ev.is_cc()
|| ev.is_pgm_change()
|| ev.is_pitch_bender()
|| ev.is_channel_pressure()
|| ev.is_sysex()) ) {
cerr << "WARNING: Unknown event (type " << _type << "): " << hex
<< int(ev.buffer()[0]) << int(ev.buffer()[1]) << int(ev.buffer()[2]) << endl;
}
double x = 0.0;
double y = 0.0;
bool ret = false;
// Increment past current event
switch (_type) {
case NOTE_ON:
++_note_iter;
break;
case NOTE_OFF:
_active_notes.pop();
break;
case CONTROL:
// Increment current controller iterator
if (_force_discrete || _control_iter->list->interpolation() == ControlList::Discrete) {
ret = _control_iter->list->rt_safe_earliest_event_discrete_unlocked (
_control_iter->x, x, y, false);
} else {
ret = _control_iter->list->rt_safe_earliest_event_linear_unlocked (
_control_iter->x + time_between_interpolated_controller_outputs, x, y, false);
}
assert(!ret || x > _control_iter->x);
if (ret) {
_control_iter->x = x;
_control_iter->y = y;
} else {
_control_iter->list.reset();
_control_iter->x = DBL_MAX;
_control_iter->y = DBL_MAX;
}
// Find the controller with the next earliest event time
_control_iter = _control_iters.begin();
for (ControlIterators::iterator i = _control_iters.begin();
i != _control_iters.end(); ++i) {
if (i->x < _control_iter->x) {
_control_iter = i;
}
}
break;
case SYSEX:
++_sysex_iter;
break;
case PATCH_CHANGE:
++_active_patch_change_message;
if (_active_patch_change_message == (*_patch_change_iter)->messages()) {
++_patch_change_iter;
_active_patch_change_message = 0;
}
break;
default:
assert(false);
}
// Choose the earliest event overall to point to
choose_next(std::numeric_limits<Time>::max());
// Set event from chosen sub-iterator
set_event();
assert(_is_end || (_event->size() > 0 && _event->buffer() && _event->buffer()[0] != '\0'));
return *this;
}
template<typename Time>
bool
Sequence<Time>::const_iterator::operator==(const const_iterator& other) const
{
if (_seq != other._seq) {
return false;
} else if (_is_end || other._is_end) {
return (_is_end == other._is_end);
} else if (_type != other._type) {
return false;
} else {
return (_event == other._event);
}
}
template<typename Time>
typename Sequence<Time>::const_iterator&
Sequence<Time>::const_iterator::operator=(const const_iterator& other)
{
_seq = other._seq;
_event = other._event;
_active_notes = other._active_notes;
_type = other._type;
_is_end = other._is_end;
_note_iter = other._note_iter;
_sysex_iter = other._sysex_iter;
_patch_change_iter = other._patch_change_iter;
_control_iters = other._control_iters;
_force_discrete = other._force_discrete;
_active_patch_change_message = other._active_patch_change_message;
if (other._lock) {
_lock = _seq->read_lock();
} else {
_lock.reset();
}
if (other._control_iter == other._control_iters.end()) {
_control_iter = _control_iters.end();
} else {
const size_t index = other._control_iter - other._control_iters.begin();
_control_iter = _control_iters.begin() + index;
}
return *this;
}
// Sequence
template<typename Time>
@ -463,14 +66,11 @@ Sequence<Time>::Sequence(const TypeMap& type_map)
, _overlap_pitch_resolution (FirstOnFirstOff)
, _writing(false)
, _type_map(type_map)
, _end_iter(*this, std::numeric_limits<Time>::max(), false, std::set<Evoral::Parameter> ())
, _percussive(false)
, _lowest_note(127)
, _highest_note(0)
{
DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence constructed: %1\n", this));
assert(_end_iter._is_end);
assert( ! _end_iter._lock);
for (int i = 0; i < 16; ++i) {
_bank[i] = 0;
@ -485,7 +85,6 @@ Sequence<Time>::Sequence(const Sequence<Time>& other)
, _overlap_pitch_resolution (other._overlap_pitch_resolution)
, _writing(false)
, _type_map(other._type_map)
, _end_iter(*this, std::numeric_limits<Time>::max(), false, std::set<Evoral::Parameter> ())
, _percussive(other._percussive)
, _lowest_note(other._lowest_note)
, _highest_note(other._highest_note)
@ -510,8 +109,6 @@ Sequence<Time>::Sequence(const Sequence<Time>& other)
}
DEBUG_TRACE (DEBUG::Sequence, string_compose ("Sequence copied: %1\n", this));
assert(_end_iter._is_end);
assert(! _end_iter._lock);
}
/** Write the controller event pointed to by \a iter to \a ev.
@ -1387,7 +984,7 @@ Sequence<Time>::dump (ostream& str) const
{
typename Sequence<Time>::const_iterator i;
str << "+++ dump\n";
for (i = begin(); i != end(); ++i) {
for (i = begin (); i != end(); ++i) {
str << *i << endl;
}
str << "--- dump\n";