From 33da74c8e353ac56194956cae8e2b7d74ec1a1b0 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 22 Oct 2009 14:46:47 +0000 Subject: Fix MidiBuffer::merge_in_place and add aggressive correctness checking. git-svn-id: svn://localhost/ardour2/branches/3.0@5854 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/midi_buffer.cc | 48 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'libs/ardour/midi_buffer.cc') diff --git a/libs/ardour/midi_buffer.cc b/libs/ardour/midi_buffer.cc index d14a9b95e9..cbf7603c96 100644 --- a/libs/ardour/midi_buffer.cc +++ b/libs/ardour/midi_buffer.cc @@ -261,12 +261,32 @@ MidiBuffer::merge_in_place(const MidiBuffer &other) return false; } +#ifndef NDEBUG + size_t test_orig_us_size = _size; + size_t test_orig_them_size = other._size; + TimeType test_time = 0; + size_t test_us_count = 0; + size_t test_them_count = 0; + for (iterator i = begin(); i != end(); ++i) { + assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size())); + assert((*i).time() >= test_time); + test_time = (*i).time(); + ++test_us_count; + } + test_time = 0; + for (const_iterator i = other.begin(); i != other.end(); ++i) { + assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size())); + assert((*i).time() >= test_time); + test_time = (*i).time(); + ++test_them_count; + } +#endif + const_iterator them = other.begin(); iterator us = begin(); while (them != other.end()) { - Evoral::MIDIEvent ev_other (*them); size_t sz = 0; ssize_t src = -1; @@ -274,14 +294,19 @@ MidiBuffer::merge_in_place(const MidiBuffer &other) the event referenced by "us" */ - while (them != other.end() && ev_other.time() < (*us).time()) { + while (them != other.end() && (*them).time() <= (*us).time()) { if (src == -1) { src = them.offset; } - sz += sizeof (TimeType) + ev_other.size(); + sz += sizeof (TimeType) + (*them).size(); ++them; } + if (us != end()) + cerr << "us @ " << (*us).time() << endl; + if (them != other.end()) + cerr << "them @ " << (*them).time() << endl; + if (sz) { assert(src >= 0); /* move existing */ @@ -299,7 +324,7 @@ MidiBuffer::merge_in_place(const MidiBuffer &other) point for the next event(s) from "other" */ - while (us != end() && (*us).time() < ev_other.time()) { + while (us != end() && (*us).time() < (*them).time()) { ++us; } } @@ -307,10 +332,25 @@ MidiBuffer::merge_in_place(const MidiBuffer &other) if (!(us != end())) { /* just append the rest of other */ memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset); + _size += other._size - them.offset; break; } } +#ifndef NDEBUG + assert(_size == test_orig_us_size + test_orig_them_size); + size_t test_final_count = 0; + test_time = 0; + for (iterator i = begin(); i != end(); ++i) { + cerr << "CHECK " << test_final_count << " / " << test_us_count + test_them_count << endl; + assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size())); + assert((*i).time() >= test_time); + test_time = (*i).time(); + ++test_final_count; + } + assert(test_final_count = test_us_count + test_them_count); +#endif + return true; } -- cgit v1.2.3