X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fmidi_buffer.cc;h=7f79262b2fdd35a5bea3910fe78a767a3fb200cb;hb=9254e80c396265f484adb3b7bf5f0b502d096107;hp=0b0e61000de81cbfd5af13e6e11c26fc3f570761;hpb=616f2a0370a10dcc7372a95f6bca9f5a45698980;p=ardour.git diff --git a/libs/ardour/midi_buffer.cc b/libs/ardour/midi_buffer.cc index 0b0e61000d..7f79262b2f 100644 --- a/libs/ardour/midi_buffer.cc +++ b/libs/ardour/midi_buffer.cc @@ -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& 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; } @@ -181,7 +190,7 @@ MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data) } uint8_t* const write_loc = _data + _size; - *((TimeType*)write_loc) = time; + *(reinterpret_cast((uintptr_t)write_loc)) = time; memcpy(write_loc + stamp_size, data, size); _size += stamp_size + size; @@ -190,6 +199,57 @@ MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data) return true; } +bool +MidiBuffer::insert_event(const Evoral::MIDIEvent& ev) +{ + if (size() == 0) { + return push_back(ev); + } + + const size_t stamp_size = sizeof(TimeType); + const size_t bytes_to_merge = stamp_size + ev.size(); + + if (_size + bytes_to_merge >= _capacity) { + cerr << "MidiBuffer::push_back failed (buffer is full)" << endl; + PBD::stacktrace (cerr, 20); + return false; + } + + TimeType t = ev.time(); + + ssize_t insert_offset = -1; + for (MidiBuffer::iterator m = begin(); m != end(); ++m) { + if ((*m).time() < t) { + continue; + } + if ((*m).time() == t) { + const uint8_t our_midi_status_byte = *(_data + m.offset + sizeof (TimeType)); + if (second_simultaneous_midi_byte_is_first (ev.type(), our_midi_status_byte)) { + continue; + } + } + insert_offset = m.offset; + break; + } + if (insert_offset == -1) { + return push_back(ev); + } + + // don't use memmove - it may use malloc(!) + // memmove (_data + insert_offset + bytes_to_merge, _data + insert_offset, _size - insert_offset); + for (ssize_t a = _size + bytes_to_merge - 1, b = _size - 1; b >= insert_offset; --b, --a) { + _data[a] = _data[b]; + } + + uint8_t* const write_loc = _data + insert_offset; + *(reinterpret_cast((uintptr_t)write_loc)) = t; + memcpy(write_loc + stamp_size, ev.buffer(), ev.size()); + + _size += bytes_to_merge; + + return true; +} + /** Reserve space for a new event in the buffer. * * This call is for copying MIDI directly into the buffer, the data location @@ -207,7 +267,7 @@ MidiBuffer::reserve(TimeType time, size_t size) // write timestamp uint8_t* write_loc = _data + _size; - *((TimeType*)write_loc) = time; + *(reinterpret_cast((uintptr_t)write_loc)) = time; // move write_loc to begin of MIDI buffer data to write to write_loc += stamp_size;