Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / midi_buffer.cc
bloba2f078d8a5944cd2e4e40ca89b474414e1f31bfc
1 /*
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)
8 any later version.
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
13 for more details.
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.
20 #include <iostream>
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"
29 using namespace std;
30 using namespace ARDOUR;
31 using namespace PBD;
33 // FIXME: mirroring for MIDI buffers?
34 MidiBuffer::MidiBuffer(size_t capacity)
35 : Buffer(DataType::MIDI, capacity)
36 , _data(0)
38 if (capacity) {
39 resize(_capacity);
40 silence(_capacity);
44 MidiBuffer::~MidiBuffer()
46 free(_data);
49 void
50 MidiBuffer::resize(size_t size)
52 assert(size > 0);
54 if (size < _capacity) {
55 return;
58 free(_data);
60 _size = 0;
61 _capacity = size;
62 cache_aligned_malloc ((void**) &_data, _capacity);
64 assert(_data);
67 void
68 MidiBuffer::copy(const MidiBuffer& copy)
70 assert(_capacity >= copy._size);
71 _size = 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.
81 void
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) {
92 clear ();
93 assert (_size == 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)) {
101 push_back (ev);
102 } else {
103 cerr << "MIDI event @ " << ev.time() << " skipped, not within range "
104 << src_offset << " .. " << (nframes + src_offset) << endl;
108 _silent = src.silent();
111 void
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);
115 assert (mbuf);
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).
126 * Realtime safe.
127 * @return false if operation failed (not enough room)
129 bool
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;
138 return false;
141 if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
142 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
143 return false;
146 push_back(ev.time(), ev.size(), ev.buffer());
148 return true;
152 /** Push an event into the buffer.
153 * @return false if operation failed (not enough room)
155 bool
156 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
158 const size_t stamp_size = sizeof(TimeType);
160 #ifndef NDEBUG
161 if (DEBUG::MidiIO & PBD::debug_bits) {
162 DEBUG_STR_DECL(a);
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());
173 #endif
175 if (_size + stamp_size + size >= _capacity) {
176 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
177 return false;
180 if (!Evoral::midi_event_is_valid(data, size)) {
181 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
182 return false;
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;
190 _silent = false;
192 return true;
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).
200 * Realtime safe.
201 * @return false if operation failed (not enough room)
203 bool
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;
209 return false;
212 if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
213 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
214 return false;
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;
222 _silent = false;
224 return true;
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.
235 uint8_t*
236 MidiBuffer::reserve(TimeType time, size_t size)
238 const size_t stamp_size = sizeof(TimeType);
239 if (_size + stamp_size + size >= _capacity) {
240 return 0;
243 // write timestamp
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;
251 _silent = false;
253 return write_loc;
257 void
258 MidiBuffer::silence (framecnt_t /*nframes*/, framecnt_t /*offset*/)
260 /* XXX iterate over existing events, find all in range given by offset & nframes,
261 and delete them.
264 _size = 0;
265 _silent = true;
268 /** Merge \a other into this buffer. Realtime safe. */
269 bool
270 MidiBuffer::merge_in_place(const MidiBuffer &other)
272 if (other.size() == 0) {
273 return true;
276 if (_size == 0) {
277 copy(other);
278 return true;
281 if (_size + other.size() > _capacity) {
282 cerr << "MidiBuffer::merge failed (no space)" << endl;
283 return false;
286 #ifndef NDEBUG
287 size_t test_orig_us_size = _size;
288 size_t test_orig_them_size = other._size;
289 TimeType test_time = 0;
290 size_t test_us_count = 0;
291 size_t test_them_count = 0;
292 for (iterator i = begin(); i != end(); ++i) {
293 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
294 assert((*i).time() >= test_time);
295 test_time = (*i).time();
296 ++test_us_count;
298 test_time = 0;
299 for (const_iterator i = other.begin(); i != other.end(); ++i) {
300 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
301 assert((*i).time() >= test_time);
302 test_time = (*i).time();
303 ++test_them_count;
305 #endif
307 const_iterator them = other.begin();
308 iterator us = begin();
310 while (them != other.end()) {
312 size_t sz = 0;
313 ssize_t src = -1;
315 /* gather up total size of events that are earlier than
316 the event referenced by "us"
319 while (them != other.end() && (*them).time() <= (*us).time()) {
320 if (src == -1) {
321 src = them.offset;
323 sz += sizeof (TimeType) + (*them).size();
324 ++them;
327 #if 0
328 if (us != end())
329 cerr << "us @ " << (*us).time() << endl;
330 if (them != other.end())
331 cerr << "them @ " << (*them).time() << endl;
332 #endif
334 if (sz) {
335 assert(src >= 0);
336 /* move existing */
337 memmove (_data + us.offset + sz, _data + us.offset, _size - us.offset);
338 /* increase _size */
339 _size += sz;
340 assert(_size <= _capacity);
341 /* insert new stuff */
342 memcpy (_data + us.offset, other._data + src, sz);
343 /* update iterator to our own events. this is a miserable hack */
344 us.offset += sz;
345 } else {
347 /* advance past our own events to get to the correct insertion
348 point for the next event(s) from "other"
351 while (us != end() && (*us).time() < (*them).time()) {
352 ++us;
356 if (!(us != end())) {
357 /* just append the rest of other */
358 memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
359 _size += other._size - them.offset;
360 break;
364 #ifndef NDEBUG
365 assert(_size == test_orig_us_size + test_orig_them_size);
366 size_t test_final_count = 0;
367 test_time = 0;
368 for (iterator i = begin(); i != end(); ++i) {
369 // cerr << "CHECK " << test_final_count << " / " << test_us_count + test_them_count << endl;
370 assert(Evoral::midi_event_is_valid((*i).buffer(), (*i).size()));
371 assert((*i).time() >= test_time);
372 test_time = (*i).time();
373 ++test_final_count;
375 assert(test_final_count = test_us_count + test_them_count);
376 #endif
378 return true;
381 /** Clear, and merge \a a and \a b into this buffer.
383 * \return true if complete merge was successful
385 bool
386 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
388 _size = 0;
390 if (this == &a) {
391 return merge_in_place(b);
392 } else if (this == &b) {
393 return merge_in_place(a);
396 const_iterator ai = a.begin();
397 const_iterator bi = b.begin();
399 resize(a.size() + b.size());
400 while (ai != a.end() && bi != b.end()) {
401 if ((*ai).time() < (*bi).time()) {
402 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
403 _size += (*ai).size();
404 ++ai;
405 } else {
406 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
407 _size += (*bi).size();
408 ++bi;
412 while (ai != a.end()) {
413 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
414 _size += (*ai).size();
415 ++ai;
418 while (bi != b.end()) {
419 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
420 _size += (*bi).size();
421 ++bi;
424 return true;