Fix MIDI thru, though why this is any different from audio (monitoring) I don't know.
[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         const_iterator them = other.begin();
265         iterator us = begin();
266
267         while (them != other.end()) {
268
269                 Evoral::MIDIEvent<TimeType> ev_other (*them);
270                 size_t sz = 0;
271                 ssize_t src = -1;
272
273                 /* gather up total size of events that are earlier than
274                    the event referenced by "us"
275                 */
276
277                 while (them != other.end() && ev_other.time() < (*us).time()) {
278                         if (src == -1) {
279                                 src = them.offset;
280                         }
281                         sz += sizeof (TimeType) + ev_other.size();
282                         ++them;
283                 }
284
285                 if (sz) {
286                         assert(src >= 0);
287                         /* move existing */
288                         memmove (_data + us.offset + sz, _data + us.offset, _size - us.offset);
289                         /* increase _size */
290                         _size += sz;
291                         assert(_size <= _capacity);
292                         /* insert new stuff */
293                         memcpy  (_data + us.offset, other._data + src, sz);
294                         /* update iterator to our own events. this is a miserable hack */
295                         us.offset += sz;
296                 } else {
297
298                         /* advance past our own events to get to the correct insertion
299                            point for the next event(s) from "other"
300                         */
301
302                         while (us != end() && (*us).time() < ev_other.time()) {
303                                 ++us;
304                         }
305                 }
306
307                 if (!(us != end())) {
308                         /* just append the rest of other */
309                         memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
310                         break;
311                 }
312         }
313
314         return true;
315 }
316
317 /** Clear, and merge \a a and \a b into this buffer.
318  *
319  * \return true if complete merge was successful
320  */
321 bool
322 MidiBuffer::merge(const MidiBuffer& a, const MidiBuffer& b)
323 {
324         _size = 0;
325
326         if (this == &a) {
327             return merge_in_place(b);
328         } else if (this == &b) {
329             return merge_in_place(a);
330         }
331
332         const_iterator ai = a.begin();
333         const_iterator bi = b.begin();
334
335         resize(a.size() + b.size());
336         while (ai != a.end() && bi != b.end()) {
337                 if ((*ai).time() < (*bi).time()) {
338                         memcpy(_data + _size, (*ai).buffer(), (*ai).size());
339                         _size += (*ai).size();
340                         ++ai;
341                 } else {
342                         memcpy(_data + _size, (*bi).buffer(), (*bi).size());
343                         _size += (*bi).size();
344                         ++bi;
345                 }
346         }
347
348         while (ai != a.end()) {
349                 memcpy(_data + _size, (*ai).buffer(), (*ai).size());
350                 _size += (*ai).size();
351                 ++ai;
352         }
353
354         while (bi != b.end()) {
355                 memcpy(_data + _size, (*bi).buffer(), (*bi).size());
356                 _size += (*bi).size();
357                 ++bi;
358         }
359
360         return true;
361 }
362