13
0

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.
This commit is contained in:
Julien "_FrnchFrgg_" RIVAUD 2016-07-20 01:17:58 +02:00
parent 2bad0e5c55
commit 728e6027d1
2 changed files with 19 additions and 18 deletions

View File

@ -265,7 +265,8 @@ public:
bool write_section_to(boost::shared_ptr<MidiSource> 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

View File

@ -1483,7 +1483,8 @@ bool
MidiModel::write_section_to (boost::shared_ptr<MidiSource> 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<MidiSource> source,
source->mark_streaming_midi_write_started (source_lock, note_mode());
for (Evoral::Sequence<TimeType>::const_iterator i = begin(TimeType(), true); i != end(); ++i) {
const Evoral::Event<TimeType>& ev (*i);
if (i->time() >= begin_time && i->time() < end_time) {
if (ev.time() >= begin_time && ev.time() < end_time) {
Evoral::MIDIEvent<TimeType> mev (*i, true); /* copy the event */
const Evoral::MIDIEvent<TimeType>* mev =
static_cast<const Evoral::MIDIEvent<TimeType>* > (&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<MidiSource> 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);