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"
25 #include "pbd/stacktrace.h"
27 #include "ardour/debug.h"
28 #include "ardour/midi_buffer.h"
31 using namespace ARDOUR;
34 // FIXME: mirroring for MIDI buffers?
35 MidiBuffer::MidiBuffer(size_t capacity)
36 : Buffer (DataType::MIDI)
45 MidiBuffer::~MidiBuffer()
51 MidiBuffer::resize(size_t size)
53 if (_data && size < _capacity) {
65 cache_aligned_malloc ((void**) &_data, size);
74 MidiBuffer::copy(const MidiBuffer& copy)
76 assert(_capacity >= copy._size);
78 memcpy(_data, copy._data, copy._size);
82 /** Read events from @a src starting at time @a offset into the START of this buffer, for
83 * time duration @a nframes. Relative time, where 0 = start of buffer.
85 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
88 MidiBuffer::read_from (const Buffer& src, framecnt_t nframes, framecnt_t dst_offset, framecnt_t src_offset)
90 assert (src.type() == DataType::MIDI);
91 assert (&src != this);
93 const MidiBuffer& msrc = (const MidiBuffer&) src;
95 assert (_capacity >= msrc.size());
97 if (dst_offset == 0) {
102 /* XXX use dst_offset somehow */
104 for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
105 const Evoral::MIDIEvent<TimeType> ev(*i, false);
106 if (ev.time() >= src_offset && ev.time() < (nframes+src_offset)) {
109 cerr << "MIDI event @ " << ev.time() << " skipped, not within range "
110 << src_offset << " .. " << (nframes + src_offset) << endl;
114 _silent = src.silent();
118 MidiBuffer::merge_from (const Buffer& src, framecnt_t /*nframes*/, framecnt_t /*dst_offset*/, framecnt_t /*src_offset*/)
120 const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
122 assert (mbuf != this);
124 /* XXX use nframes, and possible offsets */
125 merge_in_place (*mbuf);
128 /** Push an event into the buffer.
130 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
131 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
133 * @return false if operation failed (not enough room)
136 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
138 const size_t stamp_size = sizeof(TimeType);
140 if (_size + stamp_size + ev.size() >= _capacity) {
141 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
142 PBD::stacktrace (cerr, 20);
146 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
147 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
151 push_back(ev.time(), ev.size(), ev.buffer());
157 /** Push an event into the buffer.
158 * @return false if operation failed (not enough room)
161 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
163 const size_t stamp_size = sizeof(TimeType);
166 if (DEBUG::MidiIO & PBD::debug_bits) {
168 DEBUG_STR_APPEND(a, string_compose ("midibuffer %1 push event @ %2 sz %3 ", this, time, size));
169 for (size_t i=0; i < size; ++i) {
170 DEBUG_STR_APPEND(a,hex);
171 DEBUG_STR_APPEND(a,"0x");
172 DEBUG_STR_APPEND(a,(int)data[i]);
173 DEBUG_STR_APPEND(a,' ');
175 DEBUG_STR_APPEND(a,'\n');
176 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
180 if (_size + stamp_size + size >= _capacity) {
181 cerr << "MidiBuffer::push_back2 failed (buffer is full; _size = " << _size << " capacity "
182 << _capacity << " stamp " << stamp_size << " size = " << size << ")" << endl;
183 PBD::stacktrace (cerr, 20);
187 if (!Evoral::midi_event_is_valid(data, size)) {
188 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
192 uint8_t* const write_loc = _data + _size;
193 *((TimeType*)write_loc) = time;
194 memcpy(write_loc + stamp_size, data, size);
196 _size += stamp_size + size;
203 MidiBuffer::insert_event(const Evoral::MIDIEvent<TimeType>& ev)
206 return push_back(ev);
209 const size_t stamp_size = sizeof(TimeType);
210 const size_t bytes_to_merge = stamp_size + ev.size();
212 if (_size + bytes_to_merge >= _capacity) {
213 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
214 PBD::stacktrace (cerr, 20);
218 TimeType t = ev.time();
220 ssize_t insert_offset = -1;
221 for (MidiBuffer::iterator m = begin(); m != end(); ++m) {
222 if ((*m).time() < t) {
225 if ((*m).time() == t) {
226 const uint8_t our_midi_status_byte = *(_data + m.offset + sizeof (TimeType));
227 if (second_simultaneous_midi_byte_is_first (ev.type(), our_midi_status_byte)) {
231 insert_offset = m.offset;
234 if (insert_offset == -1) {
235 return push_back(ev);
238 // don't use memmove - it may use malloc(!)
239 // memmove (_data + insert_offset + bytes_to_merge, _data + insert_offset, _size - insert_offset);
240 for (ssize_t a = _size + bytes_to_merge - 1, b = _size - 1; b >= insert_offset; --b, --a) {
244 uint8_t* const write_loc = _data + insert_offset;
245 *((TimeType*)write_loc) = t;
246 memcpy(write_loc + stamp_size, ev.buffer(), ev.size());
248 _size += bytes_to_merge;
253 /** Reserve space for a new event in the buffer.
255 * This call is for copying MIDI directly into the buffer, the data location
256 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
257 * This call MUST be immediately followed by a write to the returned data
258 * location, or the buffer will be corrupted and very nasty things will happen.
261 MidiBuffer::reserve(TimeType time, size_t size)
263 const size_t stamp_size = sizeof(TimeType);
264 if (_size + stamp_size + size >= _capacity) {
269 uint8_t* write_loc = _data + _size;
270 *((TimeType*)write_loc) = time;
272 // move write_loc to begin of MIDI buffer data to write to
273 write_loc += stamp_size;
275 _size += stamp_size + size;
283 MidiBuffer::silence (framecnt_t /*nframes*/, framecnt_t /*offset*/)
285 /* XXX iterate over existing events, find all in range given by offset & nframes,
294 MidiBuffer::second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
296 bool b_first = false;
298 /* two events at identical times. we need to determine
299 the order in which they should occur.
312 if ((a) >= 0xf0 || (b) >= 0xf0 || ((a & 0xf) != (b & 0xf))) {
314 /* if either message is not a channel message, or if the channels are
315 * different, we don't care about the type.
323 case MIDI_CMD_CONTROL:
327 case MIDI_CMD_PGM_CHANGE:
329 case MIDI_CMD_CONTROL:
331 case MIDI_CMD_PGM_CHANGE:
332 case MIDI_CMD_NOTE_OFF:
333 case MIDI_CMD_NOTE_ON:
334 case MIDI_CMD_NOTE_PRESSURE:
335 case MIDI_CMD_CHANNEL_PRESSURE:
336 case MIDI_CMD_BENDER:
341 case MIDI_CMD_NOTE_OFF:
343 case MIDI_CMD_CONTROL:
344 case MIDI_CMD_PGM_CHANGE:
346 case MIDI_CMD_NOTE_OFF:
347 case MIDI_CMD_NOTE_ON:
348 case MIDI_CMD_NOTE_PRESSURE:
349 case MIDI_CMD_CHANNEL_PRESSURE:
350 case MIDI_CMD_BENDER:
355 case MIDI_CMD_NOTE_ON:
357 case MIDI_CMD_CONTROL:
358 case MIDI_CMD_PGM_CHANGE:
359 case MIDI_CMD_NOTE_OFF:
361 case MIDI_CMD_NOTE_ON:
362 case MIDI_CMD_NOTE_PRESSURE:
363 case MIDI_CMD_CHANNEL_PRESSURE:
364 case MIDI_CMD_BENDER:
368 case MIDI_CMD_NOTE_PRESSURE:
370 case MIDI_CMD_CONTROL:
371 case MIDI_CMD_PGM_CHANGE:
372 case MIDI_CMD_NOTE_OFF:
373 case MIDI_CMD_NOTE_ON:
375 case MIDI_CMD_NOTE_PRESSURE:
376 case MIDI_CMD_CHANNEL_PRESSURE:
377 case MIDI_CMD_BENDER:
382 case MIDI_CMD_CHANNEL_PRESSURE:
384 case MIDI_CMD_CONTROL:
385 case MIDI_CMD_PGM_CHANGE:
386 case MIDI_CMD_NOTE_OFF:
387 case MIDI_CMD_NOTE_ON:
388 case MIDI_CMD_NOTE_PRESSURE:
390 case MIDI_CMD_CHANNEL_PRESSURE:
391 case MIDI_CMD_BENDER:
395 case MIDI_CMD_BENDER:
397 case MIDI_CMD_CONTROL:
398 case MIDI_CMD_PGM_CHANGE:
399 case MIDI_CMD_NOTE_OFF:
400 case MIDI_CMD_NOTE_ON:
401 case MIDI_CMD_NOTE_PRESSURE:
402 case MIDI_CMD_CHANNEL_PRESSURE:
404 case MIDI_CMD_BENDER:
414 /** Merge \a other into this buffer. Realtime safe. */
416 MidiBuffer::merge_in_place (const MidiBuffer &other)
418 if (other.size() && size()) {
419 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("merge in place, sizes %1/%2\n", size(), other.size()));
422 if (other.size() == 0) {
431 if (size() + other.size() > _capacity) {
435 const_iterator them = other.begin();
436 iterator us = begin();
438 while (them != other.end()) {
440 size_t bytes_to_merge;
441 ssize_t merge_offset;
443 /* gather up total size of events that are earlier than
444 the event referenced by "us"
450 while (them != other.end() && (*them).time() < (*us).time()) {
451 if (merge_offset == -1) {
452 merge_offset = them.offset;
454 bytes_to_merge += sizeof (TimeType) + (*them).size();
458 /* "them" now points to either:
460 * 1) an event that has the same or later timestamp than the
461 * event pointed to by "us"
465 * 2) the end of the "other" buffer
467 * if "sz" is non-zero, there is data to be merged from "other"
468 * into this buffer before we do anything else, corresponding
469 * to the events from "other" that we skipped while advancing
473 if (bytes_to_merge) {
474 assert(merge_offset >= 0);
476 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
478 _size += bytes_to_merge;
479 assert (_size <= _capacity);
480 /* insert new stuff */
481 memcpy (_data + us.offset, other._data + merge_offset, bytes_to_merge);
482 /* update iterator to our own events. this is a miserable hack */
483 us.offset += bytes_to_merge;
486 /* if we're at the end of the other buffer, we're done */
488 if (them == other.end()) {
492 /* if we have two messages messages with the same timestamp. we
493 * must order them correctly.
496 if ((*us).time() == (*them).time()) {
498 DEBUG_TRACE (DEBUG::MidiIO,
499 string_compose ("simultaneous MIDI events discovered during merge, times %1/%2 status %3/%4\n",
500 (*us).time(), (*them).time(),
501 (int) *(_data + us.offset + sizeof (TimeType)),
502 (int) *(other._data + them.offset + sizeof (TimeType))));
504 uint8_t our_midi_status_byte = *(_data + us.offset + sizeof (TimeType));
505 uint8_t their_midi_status_byte = *(other._data + them.offset + sizeof (TimeType));
506 bool them_first = second_simultaneous_midi_byte_is_first (our_midi_status_byte, their_midi_status_byte);
508 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("other message came first ? %1\n", them_first));
511 /* skip past our own event */
515 bytes_to_merge = sizeof (TimeType) + (*them).size();
517 /* move our remaining events later in the buffer by
518 * enough to fit the one message we're going to merge
521 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
523 _size += bytes_to_merge;
524 assert(_size <= _capacity);
525 /* insert new stuff */
526 memcpy (_data + us.offset, other._data + them.offset, bytes_to_merge);
527 /* update iterator to our own events. this is a miserable hack */
528 us.offset += bytes_to_merge;
529 /* 'us' is now an iterator to the event right after the
530 new ones that we merged
533 /* need to skip the event pointed to by 'us'
534 since its at the same time as 'them'
535 (still), and we'll enter
543 /* we merged one event from the other buffer, so
544 * advance the iterator there.
551 /* advance past our own events to get to the correct insertion
552 point for the next event(s) from "other"
555 while (us != end() && (*us).time() <= (*them).time()) {
560 /* check to see if we reached the end of this buffer while
561 * looking for the insertion point.
566 /* just append the rest of other and we're done*/
568 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
569 _size += other._size - them.offset;
570 assert(_size <= _capacity);