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 = (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);
133 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
134 << " size = " << ev.size() << endl;*/
136 if (_size + stamp_size + ev.size() >= _capacity) {
137 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
141 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
142 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
146 push_back(ev.time(), ev.size(), ev.buffer());
152 /** Push an event into the buffer.
153 * @return false if operation failed (not enough room)
156 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
158 const size_t stamp_size = sizeof(TimeType);
161 if (DEBUG::MidiIO & PBD::debug_bits) {
163 DEBUG_STR_APPEND(a, string_compose ("midibuffer %1 push event @ %2 sz %3 ", this, time, size));
164 for (size_t i=0; i < size; ++i) {
165 DEBUG_STR_APPEND(a,hex);
166 DEBUG_STR_APPEND(a,"0x");
167 DEBUG_STR_APPEND(a,(int)data[i]);
168 DEBUG_STR_APPEND(a,' ');
170 DEBUG_STR_APPEND(a,'\n');
171 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
175 if (_size + stamp_size + size >= _capacity) {
176 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
180 if (!Evoral::midi_event_is_valid(data, size)) {
181 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
185 uint8_t* const write_loc = _data + _size;
186 *((TimeType*)write_loc) = time;
187 memcpy(write_loc + stamp_size, data, size);
189 _size += stamp_size + size;
196 /** Push an event into the buffer.
198 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
199 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
201 * @return false if operation failed (not enough room)
204 MidiBuffer::push_back(const jack_midi_event_t& ev)
206 const size_t stamp_size = sizeof(TimeType);
207 if (_size + stamp_size + ev.size >= _capacity) {
208 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
212 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
213 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
217 uint8_t* const write_loc = _data + _size;
218 *((TimeType*)write_loc) = ev.time;
219 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
221 _size += stamp_size + ev.size;
228 /** Reserve space for a new event in the buffer.
230 * This call is for copying MIDI directly into the buffer, the data location
231 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
232 * This call MUST be immediately followed by a write to the returned data
233 * location, or the buffer will be corrupted and very nasty things will happen.
236 MidiBuffer::reserve(TimeType time, size_t size)
238 const size_t stamp_size = sizeof(TimeType);
239 if (_size + stamp_size + size >= _capacity) {
244 uint8_t* write_loc = _data + _size;
245 *((TimeType*)write_loc) = time;
247 // move write_loc to begin of MIDI buffer data to write to
248 write_loc += stamp_size;
250 _size += stamp_size + size;
258 MidiBuffer::silence (framecnt_t /*nframes*/, framecnt_t /*offset*/)
260 /* XXX iterate over existing events, find all in range given by offset & nframes,
269 MidiBuffer::second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
271 bool b_first = false;
273 /* two events at identical times. we need to determine
274 the order in which they should occur.
287 if ((a) >= 0xf0 || (b) >= 0xf0 || ((a & 0xf) != (b & 0xf))) {
289 /* if either message is not a channel message, or if the channels are
290 * different, we don't care about the type.
298 case MIDI_CMD_CONTROL:
302 case MIDI_CMD_PGM_CHANGE:
304 case MIDI_CMD_CONTROL:
306 case MIDI_CMD_PGM_CHANGE:
307 case MIDI_CMD_NOTE_OFF:
308 case MIDI_CMD_NOTE_ON:
309 case MIDI_CMD_NOTE_PRESSURE:
310 case MIDI_CMD_CHANNEL_PRESSURE:
311 case MIDI_CMD_BENDER:
315 case MIDI_CMD_NOTE_OFF:
317 case MIDI_CMD_CONTROL:
318 case MIDI_CMD_PGM_CHANGE:
320 case MIDI_CMD_NOTE_OFF:
321 case MIDI_CMD_NOTE_ON:
322 case MIDI_CMD_NOTE_PRESSURE:
323 case MIDI_CMD_CHANNEL_PRESSURE:
324 case MIDI_CMD_BENDER:
329 case MIDI_CMD_NOTE_ON:
331 case MIDI_CMD_CONTROL:
332 case MIDI_CMD_PGM_CHANGE:
333 case MIDI_CMD_NOTE_OFF:
335 case MIDI_CMD_NOTE_ON:
336 case MIDI_CMD_NOTE_PRESSURE:
337 case MIDI_CMD_CHANNEL_PRESSURE:
338 case MIDI_CMD_BENDER:
342 case MIDI_CMD_NOTE_PRESSURE:
344 case MIDI_CMD_CONTROL:
345 case MIDI_CMD_PGM_CHANGE:
346 case MIDI_CMD_NOTE_OFF:
347 case MIDI_CMD_NOTE_ON:
349 case MIDI_CMD_NOTE_PRESSURE:
350 case MIDI_CMD_CHANNEL_PRESSURE:
351 case MIDI_CMD_BENDER:
356 case MIDI_CMD_CHANNEL_PRESSURE:
358 case MIDI_CMD_CONTROL:
359 case MIDI_CMD_PGM_CHANGE:
360 case MIDI_CMD_NOTE_OFF:
361 case MIDI_CMD_NOTE_ON:
362 case MIDI_CMD_NOTE_PRESSURE:
364 case MIDI_CMD_CHANNEL_PRESSURE:
365 case MIDI_CMD_BENDER:
369 case MIDI_CMD_BENDER:
371 case MIDI_CMD_CONTROL:
372 case MIDI_CMD_PGM_CHANGE:
373 case MIDI_CMD_NOTE_OFF:
374 case MIDI_CMD_NOTE_ON:
375 case MIDI_CMD_NOTE_PRESSURE:
376 case MIDI_CMD_CHANNEL_PRESSURE:
378 case MIDI_CMD_BENDER:
388 /** Merge \a other into this buffer. Realtime safe. */
390 MidiBuffer::merge_in_place(const MidiBuffer &other)
392 if (other.size() || size()) {
393 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("merge in place, sizes %1/%2\n", size(), other.size()));
396 if (other.size() == 0) {
405 if (size() + other.size() > _capacity) {
409 const_iterator them = other.begin();
410 iterator us = begin();
412 while (them != other.end()) {
414 size_t bytes_to_merge;
415 ssize_t merge_offset;
417 /* gather up total size of events that are earlier than
418 the event referenced by "us"
424 while (them != other.end() && (*them).time() < (*us).time()) {
425 if (merge_offset == -1) {
426 merge_offset = them.offset;
428 bytes_to_merge += sizeof (TimeType) + (*them).size();
432 /* "them" now points to either:
434 * 1) an event that has the same or later timestamp than the
435 * event pointed to by "us"
439 * 2) the end of the "other" buffer
441 * if "sz" is non-zero, there is data to be merged from "other"
442 * into this buffer before we do anything else, corresponding
443 * to the events from "other" that we skipped while advancing
447 if (bytes_to_merge) {
448 assert(merge_offset >= 0);
450 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
452 _size += bytes_to_merge;
453 assert (_size <= _capacity);
454 /* insert new stuff */
455 memcpy (_data + us.offset, other._data + merge_offset, bytes_to_merge);
456 /* update iterator to our own events. this is a miserable hack */
457 us.offset += bytes_to_merge;
460 /* if we're at the end of the other buffer, we're done */
462 if (them == other.end()) {
466 /* if we have two messages messages with the same timestamp. we
467 * must order them correctly.
470 if ((*us).time() == (*them).time()) {
472 DEBUG_TRACE (DEBUG::MidiIO,
473 string_compose ("simultaneous MIDI events discovered during merge, times %1/%2 status %3/%4\n",
474 (*us).time(), (*them).time(),
475 (int) *(_data + us.offset + sizeof (TimeType)),
476 (int) *(other._data + them.offset + sizeof (TimeType))));
478 uint8_t our_midi_status_byte = *(_data + us.offset + sizeof (TimeType));
479 uint8_t their_midi_status_byte = *(other._data + them.offset + sizeof (TimeType));
480 bool them_first = second_simultaneous_midi_byte_is_first (our_midi_status_byte, their_midi_status_byte);
482 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("other message came first ? %1\n", them_first));
485 /* skip past our own event */
489 bytes_to_merge = sizeof (TimeType) + (*them).size();
491 /* move our remaining events later in the buffer by
492 * enough to fit the one message we're going to merge
495 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
497 _size += bytes_to_merge;
498 assert(_size <= _capacity);
499 /* insert new stuff */
500 memcpy (_data + us.offset, other._data + them.offset, bytes_to_merge);
501 /* update iterator to our own events. this is a miserable hack */
502 us.offset += bytes_to_merge;
503 /* 'us' is now an iterator to the event right after the
504 new ones that we merged
507 /* need to skip the event pointed to by 'us'
508 since its at the same time as 'them'
509 (still), and we'll enter
517 /* we merged one event from the other buffer, so
518 * advance the iterator there.
525 /* advance past our own events to get to the correct insertion
526 point for the next event(s) from "other"
529 while (us != end() && (*us).time() <= (*them).time()) {
534 /* check to see if we reached the end of this buffer while
535 * looking for the insertion point.
540 /* just append the rest of other and we're done*/
542 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
543 _size += other._size - them.offset;
544 assert(_size <= _capacity);
552 /** Clear, and merge \a a and \a b into this buffer.
554 * \return true if complete merge was successful
557 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
562 return merge_in_place(b);
563 } else if (this == &b) {
564 return merge_in_place(a);
567 const_iterator ai = a.begin();
568 const_iterator bi = b.begin();
570 resize(a.size() + b.size());
571 while (ai != a.end() && bi != b.end()) {
572 if ((*ai).time() < (*bi).time()) {
573 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
574 _size += (*ai).size();
577 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
578 _size += (*bi).size();
583 while (ai != a.end()) {
584 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
585 _size += (*ai).size();
589 while (bi != b.end()) {
590 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
591 _size += (*bi).size();