Skip to content

Commit 7c37a18

Browse files
committed
Update MIDI buffer implementation to retain EventType
1 parent 472fe2b commit 7c37a18

File tree

5 files changed

+51
-34
lines changed

5 files changed

+51
-34
lines changed

libs/ardour/ardour/midi_buffer.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<samplep
5353
void skip_to (TimeType when);
5454

5555
bool push_back(const Evoral::Event<TimeType>& event);
56-
bool push_back(TimeType time, size_t size, const uint8_t* data);
56+
bool push_back(TimeType time, size_t size, const uint8_t* data, Evoral::EventType event_type = Evoral::MIDI_EVENT);
5757

58-
uint8_t* reserve(TimeType time, size_t size);
58+
uint8_t* reserve(TimeType time, Evoral::EventType event_type, size_t size);
5959

6060
void resize(size_t);
6161
size_t size() const { return _size; }
@@ -86,19 +86,21 @@ class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<samplep
8686
}
8787

8888
inline EventType operator*() const {
89-
uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
89+
uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType) + sizeof (Evoral::EventType);
9090
int event_size = Evoral::midi_event_size(ev_start);
9191
assert(event_size >= 0);
92-
return EventType(midi_parameter_type(*ev_start),
92+
return EventType(
93+
*((Evoral::EventType*)(buffer->_data + offset + sizeof(TimeType))),
9394
*((TimeType*)(buffer->_data + offset)),
9495
event_size, ev_start);
9596
}
9697

9798
inline EventType operator*() {
98-
uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
99+
uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType) + sizeof (Evoral::EventType);
99100
int event_size = Evoral::midi_event_size(ev_start);
100101
assert(event_size >= 0);
101-
return EventType(Evoral::MIDI_EVENT,
102+
return EventType(
103+
*(reinterpret_cast<Evoral::EventType*>((uintptr_t)(buffer->_data + offset + sizeof(TimeType)))),
102104
*(reinterpret_cast<TimeType*>((uintptr_t)(buffer->_data + offset))),
103105
event_size, ev_start);
104106
}
@@ -107,11 +109,15 @@ class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<samplep
107109
return reinterpret_cast<TimeType*>((uintptr_t)(buffer->_data + offset));
108110
}
109111

112+
inline Evoral::EventType * event_type_ptr() {
113+
return reinterpret_cast<Evoral::EventType*>((uintptr_t)(buffer->_data + offset + sizeof(TimeType)));
114+
}
115+
110116
inline iterator_base<BufferType, EventType>& operator++() {
111-
uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
117+
uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType) + sizeof (Evoral::EventType);
112118
int event_size = Evoral::midi_event_size(ev_start);
113119
assert(event_size >= 0);
114-
offset += sizeof(TimeType) + event_size;
120+
offset += sizeof(TimeType) + sizeof (Evoral::EventType) + event_size;
115121
return *this;
116122
}
117123

@@ -138,15 +144,15 @@ class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<samplep
138144

139145
iterator erase(const iterator& i) {
140146
assert (i.buffer == this);
141-
uint8_t* ev_start = _data + i.offset + sizeof (TimeType);
147+
uint8_t* ev_start = _data + i.offset + sizeof (TimeType) + sizeof (Evoral::EventType);
142148
int event_size = Evoral::midi_event_size (ev_start);
143149

144150
if (event_size < 0) {
145151
/* unknown size, sysex: return end() */
146152
return end();
147153
}
148154

149-
size_t total_data_deleted = sizeof(TimeType) + event_size;
155+
size_t total_data_deleted = sizeof(TimeType) + sizeof (Evoral::EventType) + event_size;
150156

151157
if (i.offset + total_data_deleted > _size) {
152158
_size = 0;
@@ -182,7 +188,7 @@ class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<samplep
182188
friend class iterator_base< MidiBuffer, Evoral::Event<TimeType> >;
183189
friend class iterator_base< const MidiBuffer, const Evoral::Event<TimeType> >;
184190

185-
uint8_t* _data; ///< timestamp, event, timestamp, event, ...
191+
uint8_t* _data; ///< [timestamp, event-type, event]*
186192
pframes_t _size;
187193
};
188194

libs/ardour/async_midi_port.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ AsyncMIDIPort::flush_output_fifo (MIDI::pframes_t nframes)
8383
assert (evp->buffer());
8484

8585
for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
86-
if (mb.push_back (evp->time(), evp->size(), evp->buffer())) {
86+
if (mb.push_back (evp->time(), evp->size(), evp->buffer(), evp->event_type ())) {
8787
written++;
8888
}
8989
}
@@ -96,7 +96,7 @@ AsyncMIDIPort::flush_output_fifo (MIDI::pframes_t nframes)
9696
assert (evp->buffer());
9797

9898
for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
99-
if (mb.push_back (evp->time(), evp->size(), evp->buffer())) {
99+
if (mb.push_back (evp->time(), evp->size(), evp->buffer(), evp->event_type ())) {
100100
written++;
101101
}
102102
}

libs/ardour/luabindings.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,7 @@ LuaBindings::dsp (lua_State* L)
27312731
.addFunction ("resize", &MidiBuffer::resize)
27322732
.addFunction ("copy", (void (MidiBuffer::*)(MidiBuffer const * const))&MidiBuffer::copy)
27332733
.addFunction ("push_event", (bool (MidiBuffer::*)(const Evoral::Event<samplepos_t>&))&MidiBuffer::push_back)
2734-
.addFunction ("push_back", (bool (MidiBuffer::*)(samplepos_t, size_t, const uint8_t*))&MidiBuffer::push_back)
2734+
.addFunction ("push_back", (bool (MidiBuffer::*)(samplepos_t, size_t, const uint8_t*, Evoral::EventType))&MidiBuffer::push_back)
27352735
// TODO iterators..
27362736
.addExtCFunction ("table", &luabridge::CFunc::listToTable<const Evoral::Event<samplepos_t>, MidiBuffer>)
27372737
.endClass()

libs/ardour/midi_buffer.cc

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ MidiBuffer::read_from (const Buffer& src, samplecnt_t nframes, sampleoffset_t ds
115115
const Evoral::Event<TimeType> ev(*i, false);
116116

117117
if (ev.time() >= 0 && ev.time() < nframes) {
118-
push_back (ev.time(), ev.size(), ev.buffer());
118+
push_back (ev.time(), ev.size(), ev.buffer(), ev.event_type ());
119119
} else {
120120
cerr << "\t!!!! MIDI event @ " << ev.time() << " skipped, not within range 0 .. " << nframes << endl;
121121
PBD::stacktrace (cerr, 30);
@@ -146,7 +146,7 @@ MidiBuffer::merge_from (const Buffer& src, samplecnt_t /*nframes*/, sampleoffset
146146
bool
147147
MidiBuffer::push_back(const Evoral::Event<TimeType>& ev)
148148
{
149-
return push_back (ev.time(), ev.size(), ev.buffer());
149+
return push_back (ev.time(), ev.size(), ev.buffer(), ev.event_type ());
150150
}
151151

152152

@@ -158,9 +158,10 @@ MidiBuffer::push_back(const Evoral::Event<TimeType>& ev)
158158
* @return false if operation failed (not enough room)
159159
*/
160160
bool
161-
MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
161+
MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data, Evoral::EventType event_type)
162162
{
163163
const size_t stamp_size = sizeof(TimeType);
164+
const size_t etype_size = sizeof(Evoral::EventType);
164165

165166
#ifndef NDEBUG
166167
if (DEBUG_ENABLED(DEBUG::MidiIO)) {
@@ -177,7 +178,7 @@ MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
177178
}
178179
#endif
179180

180-
if (_size + stamp_size + size >= _capacity) {
181+
if (_size + stamp_size + etype_size + size >= _capacity) {
181182
return false;
182183
}
183184

@@ -187,9 +188,10 @@ MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
187188

188189
uint8_t* const write_loc = _data + _size;
189190
*(reinterpret_cast<TimeType*>((uintptr_t)write_loc)) = time;
190-
memcpy(write_loc + stamp_size, data, size);
191+
*(reinterpret_cast<Evoral::EventType*>((uintptr_t)(write_loc + stamp_size))) = event_type;
192+
memcpy(write_loc + stamp_size + etype_size, data, size);
191193

192-
_size += stamp_size + size;
194+
_size += stamp_size + etype_size + size;
193195
_silent = false;
194196

195197
return true;
@@ -205,7 +207,9 @@ MidiBuffer::insert_event(const Evoral::Event<TimeType>& ev)
205207
}
206208

207209
const size_t stamp_size = sizeof(TimeType);
208-
const size_t bytes_to_merge = stamp_size + ev.size();
210+
const size_t etype_size = sizeof(Evoral::EventType);
211+
212+
const size_t bytes_to_merge = stamp_size + etype_size + ev.size();
209213

210214
if (_size + bytes_to_merge >= _capacity) {
211215
cerr << string_compose ("MidiBuffer::push_back failed (buffer is full: size: %1 capacity %2 new bytes %3)", _size, _capacity, bytes_to_merge) << endl;
@@ -243,7 +247,8 @@ MidiBuffer::insert_event(const Evoral::Event<TimeType>& ev)
243247

244248
uint8_t* const write_loc = _data + insert_offset;
245249
*(reinterpret_cast<TimeType*>((uintptr_t)write_loc)) = t;
246-
memcpy(write_loc + stamp_size, ev.buffer(), ev.size());
250+
*(reinterpret_cast<Evoral::EventType*>((uintptr_t)(write_loc + stamp_size))) = ev.event_type ();
251+
memcpy(write_loc + stamp_size + etype_size, ev.buffer(), ev.size());
247252

248253
_size += bytes_to_merge;
249254

@@ -265,21 +270,23 @@ MidiBuffer::write(TimeType time, Evoral::EventType type, uint32_t size, const ui
265270
* location, or the buffer will be corrupted and very nasty things will happen.
266271
*/
267272
uint8_t*
268-
MidiBuffer::reserve(TimeType time, size_t size)
273+
MidiBuffer::reserve(TimeType time, Evoral::EventType event_type, size_t size)
269274
{
270275
const size_t stamp_size = sizeof(TimeType);
271-
if (_size + stamp_size + size >= _capacity) {
276+
const size_t etype_size = sizeof(Evoral::EventType);
277+
if (_size + stamp_size + etype_size + size >= _capacity) {
272278
return 0;
273279
}
274280

275-
// write timestamp
281+
// write timestamp and event-type
276282
uint8_t* write_loc = _data + _size;
277283
*(reinterpret_cast<TimeType*>((uintptr_t)write_loc)) = time;
284+
*(reinterpret_cast<Evoral::EventType*>((uintptr_t)(write_loc + stamp_size))) = event_type;
278285

279286
// move write_loc to begin of MIDI buffer data to write to
280-
write_loc += stamp_size;
287+
write_loc += stamp_size + etype_size;
281288

282-
_size += stamp_size + size;
289+
_size += stamp_size + etype_size + size;
283290
_silent = false;
284291

285292
return write_loc;
@@ -422,6 +429,8 @@ MidiBuffer::second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
422429
bool
423430
MidiBuffer::merge_in_place (const MidiBuffer &other)
424431
{
432+
const size_t header_size = sizeof(TimeType) + sizeof(Evoral::EventType);
433+
425434
if (other.size() && size()) {
426435
DEBUG_TRACE (DEBUG::MidiIO, string_compose ("merge in place, sizes %1/%2\n", size(), other.size()));
427436
}
@@ -458,7 +467,7 @@ MidiBuffer::merge_in_place (const MidiBuffer &other)
458467
if (merge_offset == -1) {
459468
merge_offset = them.offset;
460469
}
461-
bytes_to_merge += sizeof (TimeType) + (*them).size();
470+
bytes_to_merge += header_size + (*them).size();
462471
++them;
463472
}
464473

@@ -505,11 +514,11 @@ MidiBuffer::merge_in_place (const MidiBuffer &other)
505514
DEBUG_TRACE (DEBUG::MidiIO,
506515
string_compose ("simultaneous MIDI events discovered during merge, times %1/%2 status %3/%4\n",
507516
(*us).time(), (*them).time(),
508-
(int) *(_data + us.offset + sizeof (TimeType)),
509-
(int) *(other._data + them.offset + sizeof (TimeType))));
517+
(int) *(_data + us.offset + header_size),
518+
(int) *(other._data + them.offset + header_size)));
510519

511-
uint8_t our_midi_status_byte = *(_data + us.offset + sizeof (TimeType));
512-
uint8_t their_midi_status_byte = *(other._data + them.offset + sizeof (TimeType));
520+
uint8_t our_midi_status_byte = *(_data + us.offset + header_size);
521+
uint8_t their_midi_status_byte = *(other._data + them.offset + header_size);
513522
bool them_first = second_simultaneous_midi_byte_is_first (our_midi_status_byte, their_midi_status_byte);
514523

515524
DEBUG_TRACE (DEBUG::MidiIO, string_compose ("other message came first ? %1\n", them_first));
@@ -519,7 +528,7 @@ MidiBuffer::merge_in_place (const MidiBuffer &other)
519528
++us;
520529
}
521530

522-
bytes_to_merge = sizeof (TimeType) + (*them).size();
531+
bytes_to_merge = header_size + (*them).size();
523532

524533
/* move our remaining events later in the buffer by
525534
* enough to fit the one message we're going to merge

libs/ardour/midi_ring_buffer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ MidiRingBuffer<T>::read (MidiBuffer& dst, samplepos_t start, samplepos_t end, sa
4848
}
4949

5050
T ev_time;
51+
Evoral::EventType ev_type;
5152
uint32_t ev_size;
5253
size_t count = 0;
5354
const size_t prefix_size = sizeof(T) + sizeof(Evoral::EventType) + sizeof(uint32_t);
@@ -62,6 +63,7 @@ MidiRingBuffer<T>::read (MidiBuffer& dst, samplepos_t start, samplepos_t end, sa
6263
this->peek (peekbuf, prefix_size);
6364

6465
ev_time = *(reinterpret_cast<T*>((uintptr_t)peekbuf));
66+
ev_type = *(reinterpret_cast<Evoral::EventType*>((uintptr_t)(peekbuf + sizeof(T))));
6567
ev_size = *(reinterpret_cast<uint32_t*>((uintptr_t)(peekbuf + sizeof(T) + sizeof (Evoral::EventType))));
6668

6769
if (this->read_space() < ev_size) {
@@ -92,7 +94,7 @@ MidiRingBuffer<T>::read (MidiBuffer& dst, samplepos_t start, samplepos_t end, sa
9294

9395
/* lets see if we are going to be able to write this event into dst.
9496
*/
95-
uint8_t* write_loc = dst.reserve (ev_time, ev_size);
97+
uint8_t* write_loc = dst.reserve (ev_time, ev_type, ev_size);
9698
if (write_loc == 0) {
9799
if (stop_on_overflow_in_dst) {
98100
DEBUG_TRACE (DEBUG::MidiRingBuffer, string_compose ("MidiRingBuffer: overflow in destination MIDI buffer, stopped after %1 events\n", count));

0 commit comments

Comments
 (0)