2 Copyright (C) 2006-2007 Paul Davis
3 Author: David Robillard
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2 of the License, or (at your option)
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "pbd/malign.h"
23 #include "pbd/compose.h"
24 #include "pbd/debug.h"
26 #include "ardour/debug.h"
27 #include "ardour/midi_buffer.h"
30 using namespace ARDOUR;
33 // FIXME: mirroring for MIDI buffers?
34 MidiBuffer::MidiBuffer(size_t capacity)
35 : Buffer(DataType::MIDI, capacity)
44 MidiBuffer::~MidiBuffer()
50 MidiBuffer::resize(size_t size)
54 if (size < _capacity) {
62 cache_aligned_malloc ((void**) &_data, _capacity);
68 MidiBuffer::copy(const MidiBuffer& copy)
70 assert(_capacity >= copy._size);
72 memcpy(_data, copy._data, copy._size);
76 /** Read events from @a src starting at time @a offset into the START of this buffer, for
77 * time duration @a nframes. Relative time, where 0 = start of buffer.
79 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
82 MidiBuffer::read_from (const Buffer& src, framecnt_t nframes, framecnt_t dst_offset, framecnt_t src_offset)
84 assert (src.type() == DataType::MIDI);
85 assert (&src != this);
87 const MidiBuffer& msrc = (const MidiBuffer&) src;
89 assert (_capacity >= msrc.size());
91 if (dst_offset == 0) {
96 /* XXX use dst_offset somehow */
98 for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
99 const Evoral::MIDIEvent<TimeType> ev(*i, false);
100 if (ev.time() >= src_offset && ev.time() < (nframes+src_offset)) {
103 cerr << "MIDI event @ " << ev.time() << " skipped, not within range "
104 << src_offset << " .. " << (nframes + src_offset) << endl;
108 _silent = src.silent();
112 MidiBuffer::merge_from (const Buffer& src, framecnt_t /*nframes*/, framecnt_t /*dst_offset*/, framecnt_t /*src_offset*/)
114 const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
116 assert (mbuf != this);
118 /* XXX use nframes, and possible offsets */
119 merge_in_place (*mbuf);
122 /** Push an event into the buffer.
124 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
125 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
127 * @return false if operation failed (not enough room)
130 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
132 const size_t stamp_size = sizeof(TimeType);
134 if (_size + stamp_size + ev.size() >= _capacity) {
135 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
139 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
140 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
144 push_back(ev.time(), ev.size(), ev.buffer());
150 /** Push an event into the buffer.
151 * @return false if operation failed (not enough room)
154 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
156 const size_t stamp_size = sizeof(TimeType);
159 if (DEBUG::MidiIO & PBD::debug_bits) {
161 DEBUG_STR_APPEND(a, string_compose ("midibuffer %1 push event @ %2 sz %3 ", this, time, size));
162 for (size_t i=0; i < size; ++i) {
163 DEBUG_STR_APPEND(a,hex);
164 DEBUG_STR_APPEND(a,"0x");
165 DEBUG_STR_APPEND(a,(int)data[i]);
166 DEBUG_STR_APPEND(a,' ');
168 DEBUG_STR_APPEND(a,'\n');
169 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
173 if (_size + stamp_size + size >= _capacity) {
174 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
178 if (!Evoral::midi_event_is_valid(data, size)) {
179 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
183 uint8_t* const write_loc = _data + _size;
184 *((TimeType*)write_loc) = time;
185 memcpy(write_loc + stamp_size, data, size);
187 _size += stamp_size + size;
194 /** Push an event into the buffer.
196 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
197 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
199 * @return false if operation failed (not enough room)
202 MidiBuffer::push_back(const jack_midi_event_t& ev)
204 const size_t stamp_size = sizeof(TimeType);
206 if (_size + stamp_size + ev.size >= _capacity) {
207 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
211 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
212 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
217 if (DEBUG::MidiIO & PBD::debug_bits) {
219 DEBUG_STR_APPEND(a, string_compose ("midibuffer %1 push jack event @ %2 sz %3 ", this, ev.time, ev.size));
220 for (size_t i=0; i < ev.size; ++i) {
221 DEBUG_STR_APPEND(a,hex);
222 DEBUG_STR_APPEND(a,"0x");
223 DEBUG_STR_APPEND(a,(int)ev.buffer[i]);
224 DEBUG_STR_APPEND(a,' ');
226 DEBUG_STR_APPEND(a,'\n');
227 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
231 uint8_t* const write_loc = _data + _size;
232 *((TimeType*)write_loc) = ev.time;
233 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
235 _size += stamp_size + ev.size;
242 /** Reserve space for a new event in the buffer.
244 * This call is for copying MIDI directly into the buffer, the data location
245 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
246 * This call MUST be immediately followed by a write to the returned data
247 * location, or the buffer will be corrupted and very nasty things will happen.
250 MidiBuffer::reserve(TimeType time, size_t size)
252 const size_t stamp_size = sizeof(TimeType);
253 if (_size + stamp_size + size >= _capacity) {
258 uint8_t* write_loc = _data + _size;
259 *((TimeType*)write_loc) = time;
261 // move write_loc to begin of MIDI buffer data to write to
262 write_loc += stamp_size;
264 _size += stamp_size + size;
272 MidiBuffer::silence (framecnt_t /*nframes*/, framecnt_t /*offset*/)
274 /* XXX iterate over existing events, find all in range given by offset & nframes,
283 MidiBuffer::second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
285 bool b_first = false;
287 /* two events at identical times. we need to determine
288 the order in which they should occur.
301 if ((a) >= 0xf0 || (b) >= 0xf0 || ((a & 0xf) != (b & 0xf))) {
303 /* if either message is not a channel message, or if the channels are
304 * different, we don't care about the type.
312 case MIDI_CMD_CONTROL:
316 case MIDI_CMD_PGM_CHANGE:
318 case MIDI_CMD_CONTROL:
320 case MIDI_CMD_PGM_CHANGE:
321 case MIDI_CMD_NOTE_OFF:
322 case MIDI_CMD_NOTE_ON:
323 case MIDI_CMD_NOTE_PRESSURE:
324 case MIDI_CMD_CHANNEL_PRESSURE:
325 case MIDI_CMD_BENDER:
330 case MIDI_CMD_NOTE_OFF:
332 case MIDI_CMD_CONTROL:
333 case MIDI_CMD_PGM_CHANGE:
335 case MIDI_CMD_NOTE_OFF:
336 case MIDI_CMD_NOTE_ON:
337 case MIDI_CMD_NOTE_PRESSURE:
338 case MIDI_CMD_CHANNEL_PRESSURE:
339 case MIDI_CMD_BENDER:
344 case MIDI_CMD_NOTE_ON:
346 case MIDI_CMD_CONTROL:
347 case MIDI_CMD_PGM_CHANGE:
348 case MIDI_CMD_NOTE_OFF:
350 case MIDI_CMD_NOTE_ON:
351 case MIDI_CMD_NOTE_PRESSURE:
352 case MIDI_CMD_CHANNEL_PRESSURE:
353 case MIDI_CMD_BENDER:
357 case MIDI_CMD_NOTE_PRESSURE:
359 case MIDI_CMD_CONTROL:
360 case MIDI_CMD_PGM_CHANGE:
361 case MIDI_CMD_NOTE_OFF:
362 case MIDI_CMD_NOTE_ON:
364 case MIDI_CMD_NOTE_PRESSURE:
365 case MIDI_CMD_CHANNEL_PRESSURE:
366 case MIDI_CMD_BENDER:
371 case MIDI_CMD_CHANNEL_PRESSURE:
373 case MIDI_CMD_CONTROL:
374 case MIDI_CMD_PGM_CHANGE:
375 case MIDI_CMD_NOTE_OFF:
376 case MIDI_CMD_NOTE_ON:
377 case MIDI_CMD_NOTE_PRESSURE:
379 case MIDI_CMD_CHANNEL_PRESSURE:
380 case MIDI_CMD_BENDER:
384 case MIDI_CMD_BENDER:
386 case MIDI_CMD_CONTROL:
387 case MIDI_CMD_PGM_CHANGE:
388 case MIDI_CMD_NOTE_OFF:
389 case MIDI_CMD_NOTE_ON:
390 case MIDI_CMD_NOTE_PRESSURE:
391 case MIDI_CMD_CHANNEL_PRESSURE:
393 case MIDI_CMD_BENDER:
403 /** Merge \a other into this buffer. Realtime safe. */
405 MidiBuffer::merge_in_place (const MidiBuffer &other)
407 if (other.size() && size()) {
408 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("merge in place, sizes %1/%2\n", size(), other.size()));
411 if (other.size() == 0) {
420 if (size() + other.size() > _capacity) {
424 const_iterator them = other.begin();
425 iterator us = begin();
427 while (them != other.end()) {
429 size_t bytes_to_merge;
430 ssize_t merge_offset;
432 /* gather up total size of events that are earlier than
433 the event referenced by "us"
439 while (them != other.end() && (*them).time() < (*us).time()) {
440 if (merge_offset == -1) {
441 merge_offset = them.offset;
443 bytes_to_merge += sizeof (TimeType) + (*them).size();
447 /* "them" now points to either:
449 * 1) an event that has the same or later timestamp than the
450 * event pointed to by "us"
454 * 2) the end of the "other" buffer
456 * if "sz" is non-zero, there is data to be merged from "other"
457 * into this buffer before we do anything else, corresponding
458 * to the events from "other" that we skipped while advancing
462 if (bytes_to_merge) {
463 assert(merge_offset >= 0);
465 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
467 _size += bytes_to_merge;
468 assert (_size <= _capacity);
469 /* insert new stuff */
470 memcpy (_data + us.offset, other._data + merge_offset, bytes_to_merge);
471 /* update iterator to our own events. this is a miserable hack */
472 us.offset += bytes_to_merge;
475 /* if we're at the end of the other buffer, we're done */
477 if (them == other.end()) {
481 /* if we have two messages messages with the same timestamp. we
482 * must order them correctly.
485 if ((*us).time() == (*them).time()) {
487 DEBUG_TRACE (DEBUG::MidiIO,
488 string_compose ("simultaneous MIDI events discovered during merge, times %1/%2 status %3/%4\n",
489 (*us).time(), (*them).time(),
490 (int) *(_data + us.offset + sizeof (TimeType)),
491 (int) *(other._data + them.offset + sizeof (TimeType))));
493 uint8_t our_midi_status_byte = *(_data + us.offset + sizeof (TimeType));
494 uint8_t their_midi_status_byte = *(other._data + them.offset + sizeof (TimeType));
495 bool them_first = second_simultaneous_midi_byte_is_first (our_midi_status_byte, their_midi_status_byte);
497 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("other message came first ? %1\n", them_first));
500 /* skip past our own event */
504 bytes_to_merge = sizeof (TimeType) + (*them).size();
506 /* move our remaining events later in the buffer by
507 * enough to fit the one message we're going to merge
510 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
512 _size += bytes_to_merge;
513 assert(_size <= _capacity);
514 /* insert new stuff */
515 memcpy (_data + us.offset, other._data + them.offset, bytes_to_merge);
516 /* update iterator to our own events. this is a miserable hack */
517 us.offset += bytes_to_merge;
518 /* 'us' is now an iterator to the event right after the
519 new ones that we merged
522 /* need to skip the event pointed to by 'us'
523 since its at the same time as 'them'
524 (still), and we'll enter
532 /* we merged one event from the other buffer, so
533 * advance the iterator there.
540 /* advance past our own events to get to the correct insertion
541 point for the next event(s) from "other"
544 while (us != end() && (*us).time() <= (*them).time()) {
549 /* check to see if we reached the end of this buffer while
550 * looking for the insertion point.
555 /* just append the rest of other and we're done*/
557 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
558 _size += other._size - them.offset;
559 assert(_size <= _capacity);