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;
202 /** Reserve space for a new event in the buffer.
204 * This call is for copying MIDI directly into the buffer, the data location
205 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
206 * This call MUST be immediately followed by a write to the returned data
207 * location, or the buffer will be corrupted and very nasty things will happen.
210 MidiBuffer::reserve(TimeType time, size_t size)
212 const size_t stamp_size = sizeof(TimeType);
213 if (_size + stamp_size + size >= _capacity) {
218 uint8_t* write_loc = _data + _size;
219 *((TimeType*)write_loc) = time;
221 // move write_loc to begin of MIDI buffer data to write to
222 write_loc += stamp_size;
224 _size += stamp_size + size;
232 MidiBuffer::silence (framecnt_t /*nframes*/, framecnt_t /*offset*/)
234 /* XXX iterate over existing events, find all in range given by offset & nframes,
243 MidiBuffer::second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
245 bool b_first = false;
247 /* two events at identical times. we need to determine
248 the order in which they should occur.
261 if ((a) >= 0xf0 || (b) >= 0xf0 || ((a & 0xf) != (b & 0xf))) {
263 /* if either message is not a channel message, or if the channels are
264 * different, we don't care about the type.
272 case MIDI_CMD_CONTROL:
276 case MIDI_CMD_PGM_CHANGE:
278 case MIDI_CMD_CONTROL:
280 case MIDI_CMD_PGM_CHANGE:
281 case MIDI_CMD_NOTE_OFF:
282 case MIDI_CMD_NOTE_ON:
283 case MIDI_CMD_NOTE_PRESSURE:
284 case MIDI_CMD_CHANNEL_PRESSURE:
285 case MIDI_CMD_BENDER:
290 case MIDI_CMD_NOTE_OFF:
292 case MIDI_CMD_CONTROL:
293 case MIDI_CMD_PGM_CHANGE:
295 case MIDI_CMD_NOTE_OFF:
296 case MIDI_CMD_NOTE_ON:
297 case MIDI_CMD_NOTE_PRESSURE:
298 case MIDI_CMD_CHANNEL_PRESSURE:
299 case MIDI_CMD_BENDER:
304 case MIDI_CMD_NOTE_ON:
306 case MIDI_CMD_CONTROL:
307 case MIDI_CMD_PGM_CHANGE:
308 case MIDI_CMD_NOTE_OFF:
310 case MIDI_CMD_NOTE_ON:
311 case MIDI_CMD_NOTE_PRESSURE:
312 case MIDI_CMD_CHANNEL_PRESSURE:
313 case MIDI_CMD_BENDER:
317 case MIDI_CMD_NOTE_PRESSURE:
319 case MIDI_CMD_CONTROL:
320 case MIDI_CMD_PGM_CHANGE:
321 case MIDI_CMD_NOTE_OFF:
322 case MIDI_CMD_NOTE_ON:
324 case MIDI_CMD_NOTE_PRESSURE:
325 case MIDI_CMD_CHANNEL_PRESSURE:
326 case MIDI_CMD_BENDER:
331 case MIDI_CMD_CHANNEL_PRESSURE:
333 case MIDI_CMD_CONTROL:
334 case MIDI_CMD_PGM_CHANGE:
335 case MIDI_CMD_NOTE_OFF:
336 case MIDI_CMD_NOTE_ON:
337 case MIDI_CMD_NOTE_PRESSURE:
339 case MIDI_CMD_CHANNEL_PRESSURE:
340 case MIDI_CMD_BENDER:
344 case MIDI_CMD_BENDER:
346 case MIDI_CMD_CONTROL:
347 case MIDI_CMD_PGM_CHANGE:
348 case MIDI_CMD_NOTE_OFF:
349 case MIDI_CMD_NOTE_ON:
350 case MIDI_CMD_NOTE_PRESSURE:
351 case MIDI_CMD_CHANNEL_PRESSURE:
353 case MIDI_CMD_BENDER:
363 /** Merge \a other into this buffer. Realtime safe. */
365 MidiBuffer::merge_in_place (const MidiBuffer &other)
367 if (other.size() && size()) {
368 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("merge in place, sizes %1/%2\n", size(), other.size()));
371 if (other.size() == 0) {
380 if (size() + other.size() > _capacity) {
384 const_iterator them = other.begin();
385 iterator us = begin();
387 while (them != other.end()) {
389 size_t bytes_to_merge;
390 ssize_t merge_offset;
392 /* gather up total size of events that are earlier than
393 the event referenced by "us"
399 while (them != other.end() && (*them).time() < (*us).time()) {
400 if (merge_offset == -1) {
401 merge_offset = them.offset;
403 bytes_to_merge += sizeof (TimeType) + (*them).size();
407 /* "them" now points to either:
409 * 1) an event that has the same or later timestamp than the
410 * event pointed to by "us"
414 * 2) the end of the "other" buffer
416 * if "sz" is non-zero, there is data to be merged from "other"
417 * into this buffer before we do anything else, corresponding
418 * to the events from "other" that we skipped while advancing
422 if (bytes_to_merge) {
423 assert(merge_offset >= 0);
425 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
427 _size += bytes_to_merge;
428 assert (_size <= _capacity);
429 /* insert new stuff */
430 memcpy (_data + us.offset, other._data + merge_offset, bytes_to_merge);
431 /* update iterator to our own events. this is a miserable hack */
432 us.offset += bytes_to_merge;
435 /* if we're at the end of the other buffer, we're done */
437 if (them == other.end()) {
441 /* if we have two messages messages with the same timestamp. we
442 * must order them correctly.
445 if ((*us).time() == (*them).time()) {
447 DEBUG_TRACE (DEBUG::MidiIO,
448 string_compose ("simultaneous MIDI events discovered during merge, times %1/%2 status %3/%4\n",
449 (*us).time(), (*them).time(),
450 (int) *(_data + us.offset + sizeof (TimeType)),
451 (int) *(other._data + them.offset + sizeof (TimeType))));
453 uint8_t our_midi_status_byte = *(_data + us.offset + sizeof (TimeType));
454 uint8_t their_midi_status_byte = *(other._data + them.offset + sizeof (TimeType));
455 bool them_first = second_simultaneous_midi_byte_is_first (our_midi_status_byte, their_midi_status_byte);
457 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("other message came first ? %1\n", them_first));
460 /* skip past our own event */
464 bytes_to_merge = sizeof (TimeType) + (*them).size();
466 /* move our remaining events later in the buffer by
467 * enough to fit the one message we're going to merge
470 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
472 _size += bytes_to_merge;
473 assert(_size <= _capacity);
474 /* insert new stuff */
475 memcpy (_data + us.offset, other._data + them.offset, bytes_to_merge);
476 /* update iterator to our own events. this is a miserable hack */
477 us.offset += bytes_to_merge;
478 /* 'us' is now an iterator to the event right after the
479 new ones that we merged
482 /* need to skip the event pointed to by 'us'
483 since its at the same time as 'them'
484 (still), and we'll enter
492 /* we merged one event from the other buffer, so
493 * advance the iterator there.
500 /* advance past our own events to get to the correct insertion
501 point for the next event(s) from "other"
504 while (us != end() && (*us).time() <= (*them).time()) {
509 /* check to see if we reached the end of this buffer while
510 * looking for the insertion point.
515 /* just append the rest of other and we're done*/
517 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
518 _size += other._size - them.offset;
519 assert(_size <= _capacity);