ensure that imported regions have names based on the files they are based on
[ardour.git] / libs / ardour / midi_buffer.cc
1 /*
2     Copyright (C) 2006-2007 Paul Davis
3         Author: Dave Robillard
4
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.
9
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.
14
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.
18 */
19
20 #include <iostream>
21 #include "pbd/malign.h"
22 #include "ardour/midi_buffer.h"
23
24 using namespace std;
25 using namespace ARDOUR;
26
27 // FIXME: mirroring for MIDI buffers?
28 MidiBuffer::MidiBuffer(size_t capacity)
29         : Buffer(DataType::MIDI, capacity)
30         , _data(0)
31 {
32         if (capacity) {
33                 resize(_capacity);
34                 silence(_capacity);
35         }
36 }
37
38 MidiBuffer::~MidiBuffer()
39 {
40         free(_data);
41 }
42
43 void
44 MidiBuffer::resize(size_t size)
45 {
46         assert(size > 0);
47
48         if (size < _capacity) {
49                 return;
50         }
51
52         free(_data);
53
54         _size = 0;
55         _capacity = size;
56         cache_aligned_malloc ((void**) &_data, _capacity);
57
58         assert(_data);
59 }
60
61 void
62 MidiBuffer::copy(const MidiBuffer& copy)
63 {
64         assert(_capacity >= copy._size);
65         _size = copy._size;
66         memcpy(_data, copy._data, copy._size);
67 }
68
69
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.
72  *
73  * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
74  */
75 void
76 MidiBuffer::read_from (const Buffer& src, nframes_t nframes, nframes_t dst_offset, nframes_t src_offset)
77 {
78         assert (src.type() == DataType::MIDI);
79         assert (&src != this);
80
81         const MidiBuffer& msrc = (MidiBuffer&) src;
82
83         assert (_capacity >= msrc.size());
84
85         if (dst_offset == 0) {
86                 clear ();
87                 assert (_size == 0);
88         }
89
90         /* XXX use dst_offset somehow */
91
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)) {
95                         push_back (ev);
96                 }
97         }
98
99         _silent = src.silent();
100 }
101
102 void
103 MidiBuffer::merge_from (const Buffer& src, nframes_t /*nframes*/, nframes_t /*dst_offset*/, nframes_t /*src_offset*/)
104 {
105         const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
106         assert (mbuf);
107         assert (mbuf != this);
108
109         /* XXX use nframes, and possible offsets */
110         merge_in_place (*mbuf);
111 }
112
113 /** Push an event into the buffer.
114  *
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).
117  * Realtime safe.
118  * @return false if operation failed (not enough room)
119  */
120 bool
121 MidiBuffer::push_back(const Evoral::MIDIEvent<TimeType>& ev)
122 {
123         const size_t stamp_size = sizeof(TimeType);
124         /*cerr << "MidiBuffer: pushing event @ " << ev.time()
125                 << " size = " << ev.size() << endl;*/
126
127         if (_size + stamp_size + ev.size() >= _capacity) {
128                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
129                 return false;
130         }
131
132         if (!Evoral::midi_event_is_valid(ev.buffer(), ev.size())) {
133                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
134                 return false;
135         }
136
137         push_back(ev.time(), ev.size(), ev.buffer());
138
139         return true;
140 }
141
142
143 /** Push an event into the buffer.
144  * @return false if operation failed (not enough room)
145  */
146 bool
147 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
148 {
149         const size_t stamp_size = sizeof(TimeType);
150         /*cerr << "MidiBuffer: pushing event @ " << ev.time()
151                 << " size = " << ev.size() << endl;*/
152
153         if (_size + stamp_size + size >= _capacity) {
154                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
155                 return false;
156         }
157
158         if (!Evoral::midi_event_is_valid(data, size)) {
159                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
160                 return false;
161         }
162
163         uint8_t* const write_loc = _data + _size;
164         *((TimeType*)write_loc) = time;
165         memcpy(write_loc + stamp_size, data, size);
166
167         _size += stamp_size + size;
168         _silent = false;
169
170         return true;
171 }
172
173
174 /** Push an event into the buffer.
175  *
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).
178  * Realtime safe.
179  * @return false if operation failed (not enough room)
180  */
181 bool
182 MidiBuffer::push_back(const jack_midi_event_t& ev)
183 {
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;
187                 return false;
188         }
189
190         if (!Evoral::midi_event_is_valid(ev.buffer, ev.size)) {
191                 cerr << "WARNING: MidiBuffer ignoring illegal MIDI event" << endl;
192                 return false;
193         }
194
195         uint8_t* const write_loc = _data + _size;
196         *((TimeType*)write_loc) = ev.time;
197         memcpy(write_loc + stamp_size, ev.buffer, ev.size);
198
199         _size += stamp_size + ev.size;
200         _silent = false;
201
202         return true;
203 }
204
205
206 /** Reserve space for a new event in the buffer.
207  *
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.
212  */
213 uint8_t*
214 MidiBuffer::reserve(TimeType time, size_t size)
215 {
216         const size_t stamp_size = sizeof(TimeType);
217         if (_size + stamp_size + size >= _capacity) {
218                 return 0;
219         }
220
221         // write timestamp
222         uint8_t* write_loc = _data + _size;
223         *((TimeType*)write_loc) = time;
224
225         // move write_loc to begin of MIDI buffer data to write to
226         write_loc += stamp_size;
227
228         _size += stamp_size + size;
229         _silent = false;
230
231         return write_loc;
232 }
233
234
235 void
236 MidiBuffer::silence (nframes_t /*nframes*/, nframes_t /*offset*/)
237 {
238         /* XXX iterate over existing events, find all in range given by offset & nframes,
239            and delete them.
240         */
241
242         _size = 0;
243         _silent = true;
244 }
245
246 /** Merge \a other into this buffer.  Realtime safe. */
247 bool
248 MidiBuffer::merge_in_place(const MidiBuffer &other)
249 {
250         if (other.size() == 0) {
251                 return true;
252         }
253
254         if (_size == 0) {
255                 copy(other);
256                 return true;
257         }
258
259         if (_size + other.size() > _capacity) {
260                 cerr << "MidiBuffer::merge failed (no space)" << endl;
261                 return false;
262         }
263
264 #ifndef NDEBUG
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();
274                 ++test_us_count;
275         }
276         test_time = 0;
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();
281                 ++test_them_count;
282         }
283 #endif
284
285         const_iterator them = other.begin();
286         iterator us = begin();
287
288         while (them != other.end()) {
289
290                 size_t sz = 0;
291                 ssize_t src = -1;
292
293                 /* gather up total size of events that are earlier than
294                    the event referenced by "us"
295                 */
296
297                 while (them != other.end() && (*them).time() <= (*us).time()) {
298                         if (src == -1) {
299                                 src = them.offset;
300                         }
301                         sz += sizeof (TimeType) + (*them).size();
302                         ++them;
303                 }
304
305                 if (us != end())
306                         cerr << "us @ " << (*us).time() << endl;
307                 if (them != other.end())
308                         cerr << "them @ " << (*them).time() << endl;
309
310                 if (sz) {
311                         assert(src >= 0);
312                         /* move existing */
313                         memmove (_data + us.offset + sz, _data + us.offset, _size - us.offset);
314                         /* increase _size */
315                         _size += sz;
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 */
320                         us.offset += sz;
321                 } else {
322
323                         /* advance past our own events to get to the correct insertion
324                            point for the next event(s) from "other"
325                         */
326
327                         while (us != end() && (*us).time() < (*them).time()) {
328                                 ++us;
329                         }
330                 }
331
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;
336                         break;
337                 }
338         }
339
340 #ifndef NDEBUG
341         assert(_size == test_orig_us_size + test_orig_them_size);
342         size_t test_final_count = 0;
343         test_time = 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();
349                 ++test_final_count;
350         }
351         assert(test_final_count = test_us_count + test_them_count);
352 #endif
353
354         return true;
355 }
356
357 /** Clear, and merge \a a and \a b into this buffer.
358  *
359  * \return true if complete merge was successful
360  */
361 bool
362 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
363 {
364         _size = 0;
365
366         if (this == &a) {
367             return merge_in_place(b);
368         } else if (this == &b) {
369             return merge_in_place(a);
370         }
371
372         const_iterator ai = a.begin();
373         const_iterator bi = b.begin();
374
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();
380                         ++ai;
381                 } else {
382                         memcpy(_data + _size, (*bi).buffer(), (*bi).size());
383                         _size += (*bi).size();
384                         ++bi;
385                 }
386         }
387
388         while (ai != a.end()) {
389                 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
390                 _size += (*ai).size();
391                 ++ai;
392         }
393
394         while (bi != b.end()) {
395                 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
396                 _size += (*bi).size();
397                 ++bi;
398         }
399
400         return true;
401 }
402