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.
21 #include "pbd/malign.h"
22 #include "ardour/midi_buffer.h"
25 using namespace ARDOUR;
27 // FIXME: mirroring for MIDI buffers?
28 MidiBuffer::MidiBuffer(size_t capacity)
29 : Buffer(DataType::MIDI, capacity)
38 MidiBuffer::~MidiBuffer()
44 MidiBuffer::resize(size_t size)
48 if (size < _capacity) {
56 cache_aligned_malloc ((void**) &_data, _capacity);
62 MidiBuffer::copy(const MidiBuffer& copy)
64 assert(_capacity >= copy._size);
66 memcpy(_data, copy._data, copy._size);
70 /** Read events from @a src starting at time @a offset into the START of this buffer, for
71 * time duration @a nframes. Relative time, where 0 = start of buffer.
73 * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
76 MidiBuffer::read_from (const Buffer& src, nframes_t nframes, nframes_t dst_offset, nframes_t src_offset)
78 assert (src.type() == DataType::MIDI);
79 assert (&src != this);
81 const MidiBuffer& msrc = (MidiBuffer&) src;
83 assert (_capacity >= msrc.size());
85 if (dst_offset == 0) {
90 /* XXX use dst_offset somehow */
92 for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
93 const Evoral::MIDIEvent<TimeType> ev(*i, false);
94 if (ev.time() >= src_offset && ev.time() < (nframes+src_offset)) {
99 _silent = src.silent();
103 MidiBuffer::merge_from (const Buffer& src, nframes_t /*nframes*/, nframes_t /*dst_offset*/, nframes_t /*src_offset*/)
105 const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
107 assert (mbuf != this);
109 /* XXX use nframes, and possible offsets */
110 merge_in_place (*mbuf);
113 /** Push an event into the buffer.
115 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
116 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
118 * @return false if operation failed (not enough room)
121 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
123 const size_t stamp_size = sizeof(TimeType);
124 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
125 << " size = " << ev.size() << endl;*/
127 if (_size + stamp_size + ev.size() >= _capacity) {
128 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
132 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
133 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
137 push_back(ev.time(), ev.size(), ev.buffer());
143 /** Push an event into the buffer.
144 * @return false if operation failed (not enough room)
147 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
149 const size_t stamp_size = sizeof(TimeType);
150 /*cerr << "MidiBuffer: pushing event @ " << ev.time()
151 << " size = " << ev.size() << endl;*/
153 if (_size + stamp_size + size >= _capacity) {
154 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
158 if (!Evoral::midi_event_is_valid(data, size)) {
159 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
163 uint8_t* const write_loc = _data + _size;
164 *((TimeType*)write_loc) = time;
165 memcpy(write_loc + stamp_size, data, size);
167 _size += stamp_size + size;
174 /** Push an event into the buffer.
176 * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
177 * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
179 * @return false if operation failed (not enough room)
182 MidiBuffer::push_back(const jack_midi_event_t& ev)
184 const size_t stamp_size = sizeof(TimeType);
185 if (_size + stamp_size + ev.size >= _capacity) {
186 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
190 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
191 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
195 uint8_t* const write_loc = _data + _size;
196 *((TimeType*)write_loc) = ev.time;
197 memcpy(write_loc + stamp_size, ev.buffer, ev.size);
199 _size += stamp_size + ev.size;
206 /** Reserve space for a new event in the buffer.
208 * This call is for copying MIDI directly into the buffer, the data location
209 * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
210 * This call MUST be immediately followed by a write to the returned data
211 * location, or the buffer will be corrupted and very nasty things will happen.
214 MidiBuffer::reserve(TimeType time, size_t size)
216 const size_t stamp_size = sizeof(TimeType);
217 if (_size + stamp_size + size >= _capacity) {
222 uint8_t* write_loc = _data + _size;
223 *((TimeType*)write_loc) = time;
225 // move write_loc to begin of MIDI buffer data to write to
226 write_loc += stamp_size;
228 _size += stamp_size + size;
236 MidiBuffer::silence (nframes_t /*nframes*/, nframes_t /*offset*/)
238 /* XXX iterate over existing events, find all in range given by offset & nframes,
246 /** Merge \a other into this buffer. Realtime safe. */
248 MidiBuffer::merge_in_place(const MidiBuffer &other)
250 if (other.size() == 0) {
259 if (_size + other.size() > _capacity) {
260 cerr << "MidiBuffer::merge failed (no space)" << endl;
265 size_t test_orig_us_size = _size;
266 size_t test_orig_them_size = other._size;
267 TimeType test_time = 0;
268 size_t test_us_count = 0;
269 size_t test_them_count = 0;
270 for (iterator i = begin(); i != end(); ++i) {
271 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
272 assert((*i).time() >= test_time);
273 test_time = (*i).time();
277 for (const_iterator i = other.begin(); i != other.end(); ++i) {
278 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
279 assert((*i).time() >= test_time);
280 test_time = (*i).time();
285 const_iterator them = other.begin();
286 iterator us = begin();
288 while (them != other.end()) {
293 /* gather up total size of events that are earlier than
294 the event referenced by "us"
297 while (them != other.end() && (*them).time() <= (*us).time()) {
301 sz += sizeof (TimeType) + (*them).size();
306 cerr << "us @ " << (*us).time() << endl;
307 if (them != other.end())
308 cerr << "them @ " << (*them).time() << endl;
313 memmove (_data + us.offset + sz, _data + us.offset, _size - us.offset);
316 assert(_size <= _capacity);
317 /* insert new stuff */
318 memcpy (_data + us.offset, other._data + src, sz);
319 /* update iterator to our own events. this is a miserable hack */
323 /* advance past our own events to get to the correct insertion
324 point for the next event(s) from "other"
327 while (us != end() && (*us).time() < (*them).time()) {
332 if (!(us != end())) {
333 /* just append the rest of other */
334 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
335 _size += other._size - them.offset;
341 assert(_size == test_orig_us_size + test_orig_them_size);
342 size_t test_final_count = 0;
344 for (iterator i = begin(); i != end(); ++i) {
345 cerr << "CHECK " << test_final_count << " / " << test_us_count + test_them_count << endl;
346 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
347 assert((*i).time() >= test_time);
348 test_time = (*i).time();
351 assert(test_final_count = test_us_count + test_them_count);
357 /** Clear, and merge \a a and \a b into this buffer.
359 * \return true if complete merge was successful
362 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
367 return merge_in_place(b);
368 } else if (this == &b) {
369 return merge_in_place(a);
372 const_iterator ai = a.begin();
373 const_iterator bi = b.begin();
375 resize(a.size() + b.size());
376 while (ai != a.end() && bi != b.end()) {
377 if ((*ai).time() < (*bi).time()) {
378 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
379 _size += (*ai).size();
382 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
383 _size += (*bi).size();
388 while (ai != a.end()) {
389 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
390 _size += (*ai).size();
394 while (bi != b.end()) {
395 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
396 _size += (*bi).size();