From 728e6027d19d7c8f180187a27c8cc744917dc83f Mon Sep 17 00:00:00 2001 From: "Julien \"_FrnchFrgg_\" RIVAUD" Date: Wed, 20 Jul 2016 01:17:58 +0200 Subject: Make MidiModel::write_section_to able to offset event times MidiModel::write_section_to() only wrote events to the given source if those events had a time in the given range. Make it able to optionally offset event times so that the start of the written range corresponds to time 0 in the source. --- libs/ardour/ardour/midi_model.h | 3 ++- libs/ardour/midi_model.cc | 34 +++++++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/libs/ardour/ardour/midi_model.h b/libs/ardour/ardour/midi_model.h index 254d610077..ea4f178bf3 100644 --- a/libs/ardour/ardour/midi_model.h +++ b/libs/ardour/ardour/midi_model.h @@ -265,7 +265,8 @@ public: bool write_section_to(boost::shared_ptr source, const Glib::Threads::Mutex::Lock& source_lock, Evoral::Beats begin = Evoral::MinBeats, - Evoral::Beats end = Evoral::MaxBeats); + Evoral::Beats end = Evoral::MaxBeats, + bool offset_events = false); // MidiModel doesn't use the normal AutomationList serialisation code // since controller data is stored in the .mid diff --git a/libs/ardour/midi_model.cc b/libs/ardour/midi_model.cc index 17b5b8a1cb..4e895c5a92 100644 --- a/libs/ardour/midi_model.cc +++ b/libs/ardour/midi_model.cc @@ -1483,7 +1483,8 @@ bool MidiModel::write_section_to (boost::shared_ptr source, const Glib::Threads::Mutex::Lock& source_lock, TimeType begin_time, - TimeType end_time) + TimeType end_time, + bool offset_events) { ReadLock lock(read_lock()); MidiStateTracker mst; @@ -1495,21 +1496,17 @@ MidiModel::write_section_to (boost::shared_ptr source, source->mark_streaming_midi_write_started (source_lock, note_mode()); for (Evoral::Sequence::const_iterator i = begin(TimeType(), true); i != end(); ++i) { - const Evoral::Event& ev (*i); + if (i->time() >= begin_time && i->time() < end_time) { - if (ev.time() >= begin_time && ev.time() < end_time) { + Evoral::MIDIEvent mev (*i, true); /* copy the event */ - const Evoral::MIDIEvent* mev = - static_cast* > (&ev); - - if (!mev) { - continue; + if (offset_events) { + mev.set_time(mev.time() - begin_time); } + if (mev.is_note_off()) { - if (mev->is_note_off()) { - - if (!mst.active (mev->note(), mev->channel())) { + if (!mst.active (mev.note(), mev.channel())) { /* the matching note-on was outside the time range we were given, so just ignore this note-off. @@ -1517,18 +1514,21 @@ MidiModel::write_section_to (boost::shared_ptr source, continue; } - source->append_event_beats (source_lock, *i); - mst.remove (mev->note(), mev->channel()); + source->append_event_beats (source_lock, mev); + mst.remove (mev.note(), mev.channel()); - } else if (mev->is_note_on()) { - mst.add (mev->note(), mev->channel()); - source->append_event_beats(source_lock, *i); + } else if (mev.is_note_on()) { + mst.add (mev.note(), mev.channel()); + source->append_event_beats(source_lock, mev); } else { - source->append_event_beats(source_lock, *i); + source->append_event_beats(source_lock, mev); } } } + if (offset_events) { + end_time -= begin_time; + } mst.resolve_notes (*source, source_lock, end_time); set_percussive(old_percussive); -- cgit v1.2.3