fix crash -- midi tracker reset when seeking audio
[ardour.git] / libs / ardour / midi_buffer.cc
index 0b0e61000de81cbfd5af13e6e11c26fc3f570761..1a6cb7fa2612d4fc461d8a3b28c88bce2b10728c 100644 (file)
@@ -22,6 +22,7 @@
 #include "pbd/malign.h"
 #include "pbd/compose.h"
 #include "pbd/debug.h"
+#include "pbd/stacktrace.h"
 
 #include "ardour/debug.h"
 #include "ardour/midi_buffer.h"
@@ -32,12 +33,12 @@ using namespace PBD;
 
 // FIXME: mirroring for MIDI buffers?
 MidiBuffer::MidiBuffer(size_t capacity)
-       : Buffer(DataType::MIDI, capacity)
-       , _data(0)
+       : Buffer (DataType::MIDI)
+       , _data (0)
 {
        if (capacity) {
-               resize(_capacity);
-               silence(_capacity);
+               resize (capacity);
+               silence (capacity);
        }
 }
 
@@ -49,17 +50,22 @@ MidiBuffer::~MidiBuffer()
 void
 MidiBuffer::resize(size_t size)
 {
-       assert(size > 0);
+       if (_data && size < _capacity) {
+
+               if (_size < size) {
+                       /* truncate */
+                       _size = size;
+               }
 
-       if (size < _capacity) {
                return;
        }
 
-       free(_data);
+       free (_data);
+
+       cache_aligned_malloc ((void**) &_data, size);
 
        _size = 0;
        _capacity = size;
-       cache_aligned_malloc ((void**) &_data, _capacity);
 
        assert(_data);
 }
@@ -133,6 +139,7 @@ MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
 
        if (_size + stamp_size + ev.size() >= _capacity) {
                cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
+               PBD::stacktrace (cerr, 20);
                return false;
        }
 
@@ -171,7 +178,9 @@ MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
 #endif
 
        if (_size + stamp_size + size >= _capacity) {
-               cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
+               cerr << "MidiBuffer::push_back2 failed (buffer is full; _size = " << _size << " capacity " 
+                    << _capacity << " stamp " << stamp_size << " size = " << size << ")" << endl;
+               PBD::stacktrace (cerr, 20);
                return false;
        }