2 Copyright (C) 2006-2007 Paul Davis
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, nframes_t nframes, nframes_t dst_offset, nframes_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 " << src_offset << " .. "
104 << (nframes + src_offset) << endl;
108 _silent = src.silent();
112 MidiBuffer::merge_from (const Buffer& src, nframes_t /*nframes*/, nframes_t /*dst_offset*/, nframes_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);
162 DEBUG_STR_APPEND(a, string_compose ("midibuffer %1 push event @ %2 sz %3 ", this, time, size));
163 for (size_t i=0; i < size; ++i) {
164 DEBUG_STR_APPEND(a,hex);
165 DEBUG_STR_APPEND(a,"0x");
166 DEBUG_STR_APPEND(a,(int)data[i]);
167 DEBUG_STR_APPEND(a,' ');
169 DEBUG_STR_APPEND(a,'\n');
170 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
173 // cerr << "MidiBuffer: pushing event @ " << time
174 // << " size = " << size << endl;
176 if (_size + stamp_size + size >= _capacity) {
177 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
181 if (!Evoral::midi_event_is_valid(data, size)) {
182 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
186 uint8_t* const write_loc = _data + _size;
187 *((TimeType*)write_loc) = time;
188 memcpy(write_loc + stamp_size, data, size);
190 _size += stamp_size + size;
197 /** Push an event into the buffer.
199 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
200 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
202 * @return false if operation failed (not enough room)
205 MidiBuffer::push_back(const jack_midi_event_t& ev)
207 const size_t stamp_size = sizeof(TimeType);
208 if (_size + stamp_size + ev.size >= _capacity) {
209 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
213 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
214 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
218 uint8_t* const write_loc = _data + _size;
219 *((TimeType*)write_loc) = ev.time;
220 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
222 _size += stamp_size + ev.size;
229 /** Reserve space for a new event in the buffer.
231 * This call is for copying MIDI directly into the buffer, the data location
232 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
233 * This call MUST be immediately followed by a write to the returned data
234 * location, or the buffer will be corrupted and very nasty things will happen.
237 MidiBuffer::reserve(TimeType time, size_t size)
239 const size_t stamp_size = sizeof(TimeType);
240 if (_size + stamp_size + size >= _capacity) {
245 uint8_t* write_loc = _data + _size;
246 *((TimeType*)write_loc) = time;
248 // move write_loc to begin of MIDI buffer data to write to
249 write_loc += stamp_size;
251 _size += stamp_size + size;
259 MidiBuffer::silence (nframes_t /*nframes*/, nframes_t /*offset*/)
261 /* XXX iterate over existing events, find all in range given by offset & nframes,
269 /** Merge \a other into this buffer. Realtime safe. */
271 MidiBuffer::merge_in_place(const MidiBuffer &other)
273 if (other.size() == 0) {
282 if (_size + other.size() > _capacity) {
283 cerr << "MidiBuffer::merge failed (no space)" << endl;
288 size_t test_orig_us_size = _size;
289 size_t test_orig_them_size = other._size;
290 TimeType test_time = 0;
291 size_t test_us_count = 0;
292 size_t test_them_count = 0;
293 for (iterator i = begin(); i != end(); ++i) {
294 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
295 assert((*i).time() >= test_time);
296 test_time = (*i).time();
300 for (const_iterator i = other.begin(); i != other.end(); ++i) {
301 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
302 assert((*i).time() >= test_time);
303 test_time = (*i).time();
308 const_iterator them = other.begin();
309 iterator us = begin();
311 while (them != other.end()) {
316 /* gather up total size of events that are earlier than
317 the event referenced by "us"
320 while (them != other.end() && (*them).time() <= (*us).time()) {
324 sz += sizeof (TimeType) + (*them).size();
330 cerr << "us @ " << (*us).time() << endl;
331 if (them != other.end())
332 cerr << "them @ " << (*them).time() << endl;
338 memmove (_data + us.offset + sz, _data + us.offset, _size - us.offset);
341 assert(_size <= _capacity);
342 /* insert new stuff */
343 memcpy (_data + us.offset, other._data + src, sz);
344 /* update iterator to our own events. this is a miserable hack */
348 /* advance past our own events to get to the correct insertion
349 point for the next event(s) from "other"
352 while (us != end() && (*us).time() < (*them).time()) {
357 if (!(us != end())) {
358 /* just append the rest of other */
359 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
360 _size += other._size - them.offset;
366 assert(_size == test_orig_us_size + test_orig_them_size);
367 size_t test_final_count = 0;
369 for (iterator i = begin(); i != end(); ++i) {
370 // cerr << "CHECK " << test_final_count << " / " << test_us_count + test_them_count << endl;
371 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
372 assert((*i).time() >= test_time);
373 test_time = (*i).time();
376 assert(test_final_count = test_us_count + test_them_count);
382 /** Clear, and merge \a a and \a b into this buffer.
384 * \return true if complete merge was successful
387 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
392 return merge_in_place(b);
393 } else if (this == &b) {
394 return merge_in_place(a);
397 const_iterator ai = a.begin();
398 const_iterator bi = b.begin();
400 resize(a.size() + b.size());
401 while (ai != a.end() && bi != b.end()) {
402 if ((*ai).time() < (*bi).time()) {
403 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
404 _size += (*ai).size();
407 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
408 _size += (*bi).size();
413 while (ai != a.end()) {
414 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
415 _size += (*ai).size();
419 while (bi != b.end()) {
420 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
421 _size += (*bi).size();