Fix MidiBuffer::merge_in_place and add aggressive correctness checking.
authorDavid Robillard <d@drobilla.net>
Thu, 22 Oct 2009 14:46:47 +0000 (14:46 +0000)
committerDavid Robillard <d@drobilla.net>
Thu, 22 Oct 2009 14:46:47 +0000 (14:46 +0000)
git-svn-id: svn://localhost/ardour2/branches/3.0@5854 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/ardour/midi_source.h
libs/ardour/ardour/region.h
libs/ardour/midi_buffer.cc
libs/ardour/midi_playlist.cc
libs/ardour/midi_region.cc

index 5298ab2f4fe25fb93d40b8281cc96ea3450c3ab1..0192d94e2d02684bd8692cb2e58cc462129d4067 100644 (file)
@@ -132,6 +132,7 @@ class MidiSource : virtual public Source
        bool                         _writing;
 
        mutable Evoral::Sequence<Evoral::MusicalTime>::const_iterator _model_iter;
+       mutable bool                                                  _model_iterator_valid;
 
        mutable double    _length_beats;
        mutable sframes_t _last_read_end;
index 0f1f10eff832b38e35bf43cbf59d957702acc7e3..c1c7ddd5e01c36282ebfee1f7ab249f7569651a0 100644 (file)
@@ -107,8 +107,7 @@ class Region
 
        const DataType& data_type() const { return _type; }
 
-       /**
-        * Thats how the region parameters play together:
+       /** How the region parameters play together:
         * <PRE>
         * |------------------------------------------------------------------- track
         *                    |..........[------------------].....| region
index d14a9b95e9eb534e4651e1fd7f27dc6eb9a622a9..cbf7603c96020f54a80e84cfaa121a4473608de3 100644 (file)
@@ -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<TimeType> 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;
 }
 
index 991e709651855305507892f84e2df612254cf565..49b5ff2c4f6f8282fd967301456586dfe20bc478 100644 (file)
@@ -137,7 +137,7 @@ MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t d
        _read_data_count = 0;
 
        // relevent regions overlapping start <--> end
-       vector<boost::shared_ptr<Region> > regs;
+       vector< boost::shared_ptr<Region> > regs;
 
        for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
                if ((*i)->coverage (start, end) != OverlapNone) {
@@ -164,7 +164,7 @@ MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t d
 
                        NoteTrackers::iterator t = _note_trackers.find ((*i).get());
                        MidiStateTracker* tracker;
-                       
+
                        if (t == _note_trackers.end()) {
                                pair<Region*,MidiStateTracker*> newpair;
                                newpair.first = (*i).get();
@@ -173,7 +173,7 @@ MidiPlaylist::read (MidiRingBuffer<nframes_t>& dst, nframes_t start, nframes_t d
                        } else {
                                tracker = t->second;
                        }
-                               
+
                        mr->read_at (dst, start, dur, chan_n, _note_mode, tracker);
                        _read_data_count += mr->read_data_count();
                }
index afa1c0f8ccfc83d42a6d315ba409120f2a3c814b..9b7a4b87480471c17a01a30d01c4d72d356559f1 100644 (file)
@@ -142,8 +142,9 @@ MidiRegion::master_read_at (MidiRingBuffer<nframes_t>& out, sframes_t position,
 }
 
 nframes_t
-MidiRegion::_read_at (const SourceList& /*srcs*/, MidiRingBuffer<nframes_t>& dst, sframes_t position, nframes_t dur, uint32_t chan_n, 
-                     NoteMode mode, MidiStateTracker* tracker) const
+MidiRegion::_read_at (const SourceList& /*srcs*/,
+               MidiRingBuffer<nframes_t>& dst, sframes_t position, nframes_t dur, uint32_t chan_n,
+               NoteMode mode, MidiStateTracker* tracker) const
 {
        nframes_t internal_offset = 0;
        nframes_t src_offset      = 0;