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, capacity)
45 MidiBuffer::~MidiBuffer()
51 MidiBuffer::resize(size_t size)
55 if (size < _capacity) {
63 cache_aligned_malloc ((void**) &_data, _capacity);
69 MidiBuffer::copy(const MidiBuffer& copy)
71 assert(_capacity >= copy._size);
73 memcpy(_data, copy._data, copy._size);
77 /** Read events from @a src starting at time @a offset into the START of this buffer, for
78 * time duration @a nframes. Relative time, where 0 = start of buffer.
80 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
83 MidiBuffer::read_from (const Buffer& src, framecnt_t nframes, framecnt_t dst_offset, framecnt_t src_offset)
85 assert (src.type() == DataType::MIDI);
86 assert (&src != this);
88 const MidiBuffer& msrc = (const MidiBuffer&) src;
90 assert (_capacity >= msrc.size());
92 if (dst_offset == 0) {
97 /* XXX use dst_offset somehow */
99 for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
100 const Evoral::MIDIEvent<TimeType> ev(*i, false);
101 if (ev.time() >= src_offset && ev.time() < (nframes+src_offset)) {
104 cerr << "MIDI event @ " << ev.time() << " skipped, not within range "
105 << src_offset << " .. " << (nframes + src_offset) << endl;
109 _silent = src.silent();
113 MidiBuffer::merge_from (const Buffer& src, framecnt_t /*nframes*/, framecnt_t /*dst_offset*/, framecnt_t /*src_offset*/)
115 const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
117 assert (mbuf != this);
119 /* XXX use nframes, and possible offsets */
120 merge_in_place (*mbuf);
123 /** Push an event into the buffer.
125 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
126 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
128 * @return false if operation failed (not enough room)
131 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
133 const size_t stamp_size = sizeof(TimeType);
135 if (_size + stamp_size + ev.size() >= _capacity) {
136 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
137 PBD::stacktrace (cerr, 20);
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_back2 failed (buffer is full; _size = " << _size << " capacity "
177 << _capacity << " stamp " << stamp_size << " size = " << size << ")" << endl;
178 PBD::stacktrace (cerr, 20);
182 if (!Evoral::midi_event_is_valid(data, size)) {
183 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
187 uint8_t* const write_loc = _data + _size;
188 *((TimeType*)write_loc) = time;
189 memcpy(write_loc + stamp_size, data, size);
191 _size += stamp_size + size;
197 /** Reserve space for a new event in the buffer.
199 * This call is for copying MIDI directly into the buffer, the data location
200 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
201 * This call MUST be immediately followed by a write to the returned data
202 * location, or the buffer will be corrupted and very nasty things will happen.
205 MidiBuffer::reserve(TimeType time, size_t size)
207 const size_t stamp_size = sizeof(TimeType);
208 if (_size + stamp_size + size >= _capacity) {
213 uint8_t* write_loc = _data + _size;
214 *((TimeType*)write_loc) = time;
216 // move write_loc to begin of MIDI buffer data to write to
217 write_loc += stamp_size;
219 _size += stamp_size + size;
227 MidiBuffer::silence (framecnt_t /*nframes*/, framecnt_t /*offset*/)
229 /* XXX iterate over existing events, find all in range given by offset & nframes,
238 MidiBuffer::second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
240 bool b_first = false;
242 /* two events at identical times. we need to determine
243 the order in which they should occur.
256 if ((a) >= 0xf0 || (b) >= 0xf0 || ((a & 0xf) != (b & 0xf))) {
258 /* if either message is not a channel message, or if the channels are
259 * different, we don't care about the type.
267 case MIDI_CMD_CONTROL:
271 case MIDI_CMD_PGM_CHANGE:
273 case MIDI_CMD_CONTROL:
275 case MIDI_CMD_PGM_CHANGE:
276 case MIDI_CMD_NOTE_OFF:
277 case MIDI_CMD_NOTE_ON:
278 case MIDI_CMD_NOTE_PRESSURE:
279 case MIDI_CMD_CHANNEL_PRESSURE:
280 case MIDI_CMD_BENDER:
285 case MIDI_CMD_NOTE_OFF:
287 case MIDI_CMD_CONTROL:
288 case MIDI_CMD_PGM_CHANGE:
290 case MIDI_CMD_NOTE_OFF:
291 case MIDI_CMD_NOTE_ON:
292 case MIDI_CMD_NOTE_PRESSURE:
293 case MIDI_CMD_CHANNEL_PRESSURE:
294 case MIDI_CMD_BENDER:
299 case MIDI_CMD_NOTE_ON:
301 case MIDI_CMD_CONTROL:
302 case MIDI_CMD_PGM_CHANGE:
303 case MIDI_CMD_NOTE_OFF:
305 case MIDI_CMD_NOTE_ON:
306 case MIDI_CMD_NOTE_PRESSURE:
307 case MIDI_CMD_CHANNEL_PRESSURE:
308 case MIDI_CMD_BENDER:
312 case MIDI_CMD_NOTE_PRESSURE:
314 case MIDI_CMD_CONTROL:
315 case MIDI_CMD_PGM_CHANGE:
316 case MIDI_CMD_NOTE_OFF:
317 case MIDI_CMD_NOTE_ON:
319 case MIDI_CMD_NOTE_PRESSURE:
320 case MIDI_CMD_CHANNEL_PRESSURE:
321 case MIDI_CMD_BENDER:
326 case MIDI_CMD_CHANNEL_PRESSURE:
328 case MIDI_CMD_CONTROL:
329 case MIDI_CMD_PGM_CHANGE:
330 case MIDI_CMD_NOTE_OFF:
331 case MIDI_CMD_NOTE_ON:
332 case MIDI_CMD_NOTE_PRESSURE:
334 case MIDI_CMD_CHANNEL_PRESSURE:
335 case MIDI_CMD_BENDER:
339 case MIDI_CMD_BENDER:
341 case MIDI_CMD_CONTROL:
342 case MIDI_CMD_PGM_CHANGE:
343 case MIDI_CMD_NOTE_OFF:
344 case MIDI_CMD_NOTE_ON:
345 case MIDI_CMD_NOTE_PRESSURE:
346 case MIDI_CMD_CHANNEL_PRESSURE:
348 case MIDI_CMD_BENDER:
358 /** Merge \a other into this buffer. Realtime safe. */
360 MidiBuffer::merge_in_place (const MidiBuffer &other)
362 if (other.size() && size()) {
363 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("merge in place, sizes %1/%2\n", size(), other.size()));
366 if (other.size() == 0) {
375 if (size() + other.size() > _capacity) {
379 const_iterator them = other.begin();
380 iterator us = begin();
382 while (them != other.end()) {
384 size_t bytes_to_merge;
385 ssize_t merge_offset;
387 /* gather up total size of events that are earlier than
388 the event referenced by "us"
394 while (them != other.end() && (*them).time() < (*us).time()) {
395 if (merge_offset == -1) {
396 merge_offset = them.offset;
398 bytes_to_merge += sizeof (TimeType) + (*them).size();
402 /* "them" now points to either:
404 * 1) an event that has the same or later timestamp than the
405 * event pointed to by "us"
409 * 2) the end of the "other" buffer
411 * if "sz" is non-zero, there is data to be merged from "other"
412 * into this buffer before we do anything else, corresponding
413 * to the events from "other" that we skipped while advancing
417 if (bytes_to_merge) {
418 assert(merge_offset >= 0);
420 memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
422 _size += bytes_to_merge;
423 assert (_size <= _capacity);
424 /* insert new stuff */
425 memcpy (_data + us.offset, other._data + merge_offset, bytes_to_merge);
426 /* update iterator to our own events. this is a miserable hack */
427 us.offset += bytes_to_merge;
430 /* if we're at the end of the other buffer, we're done */
432 if (them == other.end()) {
436 /* if we have two messages messages with the same timestamp. we
437 * must order them correctly.
440 if ((*us).time() == (*them).time()) {
442 DEBUG_TRACE (DEBUG::MidiIO,
443 string_compose ("simultaneous MIDI events discovered during merge, times %1/%2 status %3/%4\n",
444 (*us).time(), (*them).time(),
445 (int) *(_data + us.offset + sizeof (TimeType)),
446 (int) *(other._data + them.offset + sizeof (TimeType))));
448 uint8_t our_midi_status_byte = *(_data + us.offset + sizeof (TimeType));
449 uint8_t their_midi_status_byte = *(other._data + them.offset + sizeof (TimeType));
450 bool them_first = second_simultaneous_midi_byte_is_first (our_midi_status_byte, their_midi_status_byte);
452 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("other message came first ? %1\n", them_first));
455 /* skip past our own event */
459 bytes_to_merge = sizeof (TimeType) + (*them).size();
461 /* move our remaining events later in the buffer by
462 * enough to fit the one message we're going to merge
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 + them.offset, bytes_to_merge);
471 /* update iterator to our own events. this is a miserable hack */
472 us.offset += bytes_to_merge;
473 /* 'us' is now an iterator to the event right after the
474 new ones that we merged
477 /* need to skip the event pointed to by 'us'
478 since its at the same time as 'them'
479 (still), and we'll enter
487 /* we merged one event from the other buffer, so
488 * advance the iterator there.
495 /* advance past our own events to get to the correct insertion
496 point for the next event(s) from "other"
499 while (us != end() && (*us).time() <= (*them).time()) {
504 /* check to see if we reached the end of this buffer while
505 * looking for the insertion point.
510 /* just append the rest of other and we're done*/
512 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
513 _size += other._size - them.offset;
514 assert(_size <= _capacity);