Fixup prev commit (LV2 X11 UI) -- #7837
[ardour.git] / libs / ardour / midi_buffer.cc
1 /*
2  * Copyright (C) 2007-2016 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2008-2009 Hans Baier <hansfbaier@googlemail.com>
5  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
6  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <iostream>
24
25 #include "pbd/malign.h"
26 #include "pbd/compose.h"
27 #include "pbd/debug.h"
28 #include "pbd/stacktrace.h"
29
30 #include "ardour/debug.h"
31 #include "ardour/midi_buffer.h"
32 #include "ardour/port.h"
33
34 using namespace std;
35 using namespace ARDOUR;
36 using namespace PBD;
37
38 // FIXME: mirroring for MIDI buffers?
39 MidiBuffer::MidiBuffer(size_t capacity)
40         : Buffer (DataType::MIDI)
41         , _data (0)
42         , _size (0)
43 {
44         if (capacity) {
45                 resize (capacity);
46                 silence (capacity);
47         }
48 }
49
50 MidiBuffer::~MidiBuffer()
51 {
52         cache_aligned_free(_data);
53 }
54
55 void
56 MidiBuffer::resize(size_t size)
57 {
58         if (_data && size < _capacity) {
59
60                 if (_size < size) {
61                         /* truncate */
62                         _size = size;
63                 }
64
65                 return;
66         }
67
68         cache_aligned_free (_data);
69
70         cache_aligned_malloc ((void**) &_data, size);
71
72         _size = 0;
73         _capacity = size;
74
75         assert(_data);
76 }
77
78 void
79 MidiBuffer::copy(const MidiBuffer& copy)
80 {
81         assert(_capacity >= copy._size);
82         _size = copy._size;
83         memcpy(_data, copy._data, copy._size);
84 }
85
86 void
87 MidiBuffer::copy(MidiBuffer const * const copy)
88 {
89         assert(_capacity >= copy->size ());
90         _size = copy->size ();
91         memcpy(_data, copy->_data, _size);
92 }
93
94
95 /** Read events from @a src starting at time @a offset into the START of this buffer, for
96  * time duration @a nframes.  Relative time, where 0 = start of buffer.
97  *
98  * Note that offset and nframes refer to sample time, NOT buffer offsets or event counts.
99  */
100 void
101 MidiBuffer::read_from (const Buffer& src, samplecnt_t nframes, sampleoffset_t dst_offset, sampleoffset_t /* src_offset*/)
102 {
103         assert (src.type() == DataType::MIDI);
104         assert (&src != this);
105
106         const MidiBuffer& msrc = (const MidiBuffer&) src;
107
108         assert (_capacity >= msrc.size());
109
110         if (dst_offset == 0) {
111                 clear ();
112                 assert (_size == 0);
113         }
114
115         for (MidiBuffer::const_iterator i = msrc.begin(); i != msrc.end(); ++i) {
116                 const Evoral::Event<TimeType> ev(*i, false);
117
118                 if (dst_offset >= 0) {
119                         /* Positive offset: shifting events from internal
120                            buffer view of time (always relative to to start of
121                            current possibly split cycle) to from global/port
122                            view of time (always relative to start of process
123                            cycle).
124
125                            Check it is within range of this (split) cycle, then shift.
126                         */
127                         if (ev.time() >= 0 && ev.time() < nframes) {
128                                 push_back (ev.time() + dst_offset, ev.size(), ev.buffer());
129                         } else {
130                                 cerr << "\t!!!! MIDI event @ " <<  ev.time() << " skipped, not within range 0 .. " << nframes << ": ";
131                         }
132                 } else {
133                         /* Negative offset: shifting events from global/port
134                            view of time (always relative to start of process
135                            cycle) back to internal buffer view of time (always
136                            relative to to start of current possibly split
137                            cycle.
138
139                            Shift first, then check it is within range of this
140                            (split) cycle.
141                         */
142                         const samplepos_t evtime = ev.time() + dst_offset;
143
144                         if (evtime >= 0 && evtime < nframes) {
145                                 push_back (evtime, ev.size(), ev.buffer());
146                         } else {
147                                 cerr << "\t!!!! MIDI event @ " <<  evtime << " (based on " << ev.time() << " + " << dst_offset << ") skipped, not within range 0 .. " << nframes << ": ";
148                         }
149                 }
150         }
151
152         _silent = src.silent();
153 }
154
155 void
156 MidiBuffer::merge_from (const Buffer& src, samplecnt_t /*nframes*/, sampleoffset_t /*dst_offset*/, sampleoffset_t /*src_offset*/)
157 {
158         const MidiBuffer* mbuf = dynamic_cast<const MidiBuffer*>(&src);
159         assert (mbuf);
160         assert (mbuf != this);
161
162         /* XXX use nframes, and possible offsets */
163         merge_in_place (*mbuf);
164 }
165
166 /** Push an event into the buffer.
167  *
168  * Note that the raw MIDI pointed to by ev will be COPIED and unmodified.
169  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
170  * Realtime safe.
171  * @return false if operation failed (not enough room)
172  */
173 bool
174 MidiBuffer::push_back(const Evoral::Event<TimeType>& ev)
175 {
176         return push_back (ev.time(), ev.size(), ev.buffer());
177 }
178
179
180 /** Push MIDI data into the buffer.
181  *
182  * Note that the raw MIDI pointed to by @param data will be COPIED and unmodified.
183  * That is, the caller still owns it, if it needs freeing it's Not My Problem(TM).
184  * Realtime safe.
185  * @return false if operation failed (not enough room)
186  */
187 bool
188 MidiBuffer::push_back(TimeType time, size_t size, const uint8_t* data)
189 {
190         const size_t stamp_size = sizeof(TimeType);
191
192 #ifndef NDEBUG
193         if (DEBUG_ENABLED(DEBUG::MidiIO)) {
194                 DEBUG_STR_DECL(a);
195                 DEBUG_STR_APPEND(a, string_compose ("midibuffer %1 push event @ %2 sz %3 ", this, time, size));
196                 for (size_t i=0; i < size; ++i) {
197                         DEBUG_STR_APPEND(a,hex);
198                         DEBUG_STR_APPEND(a,"0x");
199                         DEBUG_STR_APPEND(a,(int)data[i]);
200                         DEBUG_STR_APPEND(a,' ');
201                 }
202                 DEBUG_STR_APPEND(a,'\n');
203                 DEBUG_TRACE (DEBUG::MidiIO, DEBUG_STR(a).str());
204         }
205 #endif
206
207         if (_size + stamp_size + size >= _capacity) {
208                 return false;
209         }
210
211         if (!Evoral::midi_event_is_valid(data, size)) {
212                 return false;
213         }
214
215         uint8_t* const write_loc = _data + _size;
216         *(reinterpret_cast<TimeType*>((uintptr_t)write_loc)) = time;
217         memcpy(write_loc + stamp_size, data, size);
218
219         _size += stamp_size + size;
220         _silent = false;
221
222         return true;
223 }
224
225 bool
226 MidiBuffer::insert_event(const Evoral::Event<TimeType>& ev)
227 {
228         if (size() == 0) {
229                 return push_back(ev);
230         }
231
232         const size_t stamp_size = sizeof(TimeType);
233         const size_t bytes_to_merge = stamp_size + ev.size();
234
235         if (_size + bytes_to_merge >= _capacity) {
236                 cerr << "MidiBuffer::push_back failed (buffer is full)" << endl;
237                 PBD::stacktrace (cerr, 20);
238                 return false;
239         }
240
241         TimeType t = ev.time();
242
243         ssize_t insert_offset = -1;
244         for (MidiBuffer::iterator m = begin(); m != end(); ++m) {
245                 if ((*m).time() < t) {
246                         continue;
247                 }
248                 if ((*m).time() == t) {
249                         const uint8_t our_midi_status_byte = *(_data + m.offset + sizeof (TimeType));
250                         if (second_simultaneous_midi_byte_is_first (ev.type(), our_midi_status_byte)) {
251                                 continue;
252                         }
253                 }
254                 insert_offset = m.offset;
255                 break;
256         }
257         if (insert_offset == -1) {
258                 return push_back(ev);
259         }
260
261         // don't use memmove - it may use malloc(!)
262         // memmove (_data + insert_offset + bytes_to_merge, _data + insert_offset, _size - insert_offset);
263         for (ssize_t a = _size + bytes_to_merge - 1, b = _size - 1; b >= insert_offset; --b, --a) {
264                 _data[a] = _data[b];
265         }
266
267         uint8_t* const write_loc = _data + insert_offset;
268         *(reinterpret_cast<TimeType*>((uintptr_t)write_loc)) = t;
269         memcpy(write_loc + stamp_size, ev.buffer(), ev.size());
270
271         _size += bytes_to_merge;
272
273         return true;
274 }
275
276 uint32_t
277 MidiBuffer::write(TimeType time, Evoral::EventType type, uint32_t size, const uint8_t* buf)
278 {
279         insert_event(Evoral::Event<TimeType>(type, time, size, const_cast<uint8_t*>(buf)));
280         return size;
281 }
282
283 /** Reserve space for a new event in the buffer.
284  *
285  * This call is for copying MIDI directly into the buffer, the data location
286  * (of sufficient size to write \a size bytes) is returned, or 0 on failure.
287  * This call MUST be immediately followed by a write to the returned data
288  * location, or the buffer will be corrupted and very nasty things will happen.
289  */
290 uint8_t*
291 MidiBuffer::reserve(TimeType time, size_t size)
292 {
293         const size_t stamp_size = sizeof(TimeType);
294         if (_size + stamp_size + size >= _capacity) {
295                 return 0;
296         }
297
298         // write timestamp
299         uint8_t* write_loc = _data + _size;
300         *(reinterpret_cast<TimeType*>((uintptr_t)write_loc)) = time;
301
302         // move write_loc to begin of MIDI buffer data to write to
303         write_loc += stamp_size;
304
305         _size += stamp_size + size;
306         _silent = false;
307
308         return write_loc;
309 }
310
311
312 void
313 MidiBuffer::silence (samplecnt_t /*nframes*/, samplecnt_t /*offset*/)
314 {
315         /* XXX iterate over existing events, find all in range given by offset & nframes,
316            and delete them.
317         */
318
319         _size = 0;
320         _silent = true;
321 }
322
323 bool
324 MidiBuffer::second_simultaneous_midi_byte_is_first (uint8_t a, uint8_t b)
325 {
326         bool b_first = false;
327
328         /* two events at identical times. we need to determine
329            the order in which they should occur.
330
331            the rule is:
332
333            Controller messages
334            Program Change
335            Note Off
336            Note On
337            Note Pressure
338            Channel Pressure
339            Pitch Bend
340         */
341
342         if ((a) >= 0xf0 || (b) >= 0xf0 || ((a & 0xf) != (b & 0xf))) {
343
344                 /* if either message is not a channel message, or if the channels are
345                  * different, we don't care about the type.
346                  */
347
348                 b_first = true;
349
350         } else {
351
352                 switch (b & 0xf0) {
353                 case MIDI_CMD_CONTROL:
354                         b_first = true;
355                         break;
356
357                 case MIDI_CMD_PGM_CHANGE:
358                         switch (a & 0xf0) {
359                         case MIDI_CMD_CONTROL:
360                                 break;
361                         case MIDI_CMD_PGM_CHANGE:
362                         case MIDI_CMD_NOTE_OFF:
363                         case MIDI_CMD_NOTE_ON:
364                         case MIDI_CMD_NOTE_PRESSURE:
365                         case MIDI_CMD_CHANNEL_PRESSURE:
366                         case MIDI_CMD_BENDER:
367                                 b_first = true;
368                         }
369                         break;
370
371                 case MIDI_CMD_NOTE_OFF:
372                         switch (a & 0xf0) {
373                         case MIDI_CMD_CONTROL:
374                         case MIDI_CMD_PGM_CHANGE:
375                                 break;
376                         case MIDI_CMD_NOTE_OFF:
377                         case MIDI_CMD_NOTE_ON:
378                         case MIDI_CMD_NOTE_PRESSURE:
379                         case MIDI_CMD_CHANNEL_PRESSURE:
380                         case MIDI_CMD_BENDER:
381                                 b_first = true;
382                         }
383                         break;
384
385                 case MIDI_CMD_NOTE_ON:
386                         switch (a & 0xf0) {
387                         case MIDI_CMD_CONTROL:
388                         case MIDI_CMD_PGM_CHANGE:
389                         case MIDI_CMD_NOTE_OFF:
390                                 break;
391                         case MIDI_CMD_NOTE_ON:
392                         case MIDI_CMD_NOTE_PRESSURE:
393                         case MIDI_CMD_CHANNEL_PRESSURE:
394                         case MIDI_CMD_BENDER:
395                                 b_first = true;
396                         }
397                         break;
398                 case MIDI_CMD_NOTE_PRESSURE:
399                         switch (a & 0xf0) {
400                         case MIDI_CMD_CONTROL:
401                         case MIDI_CMD_PGM_CHANGE:
402                         case MIDI_CMD_NOTE_OFF:
403                         case MIDI_CMD_NOTE_ON:
404                                 break;
405                         case MIDI_CMD_NOTE_PRESSURE:
406                         case MIDI_CMD_CHANNEL_PRESSURE:
407                         case MIDI_CMD_BENDER:
408                                 b_first = true;
409                         }
410                         break;
411
412                 case MIDI_CMD_CHANNEL_PRESSURE:
413                         switch (a & 0xf0) {
414                         case MIDI_CMD_CONTROL:
415                         case MIDI_CMD_PGM_CHANGE:
416                         case MIDI_CMD_NOTE_OFF:
417                         case MIDI_CMD_NOTE_ON:
418                         case MIDI_CMD_NOTE_PRESSURE:
419                                 break;
420                         case MIDI_CMD_CHANNEL_PRESSURE:
421                         case MIDI_CMD_BENDER:
422                                 b_first = true;
423                         }
424                         break;
425                 case MIDI_CMD_BENDER:
426                         switch (a & 0xf0) {
427                         case MIDI_CMD_CONTROL:
428                         case MIDI_CMD_PGM_CHANGE:
429                         case MIDI_CMD_NOTE_OFF:
430                         case MIDI_CMD_NOTE_ON:
431                         case MIDI_CMD_NOTE_PRESSURE:
432                         case MIDI_CMD_CHANNEL_PRESSURE:
433                                 break;
434                         case MIDI_CMD_BENDER:
435                                 b_first = true;
436                         }
437                         break;
438                 }
439         }
440
441         return b_first;
442 }
443
444 /** Merge \a other into this buffer.  Realtime safe. */
445 bool
446 MidiBuffer::merge_in_place (const MidiBuffer &other)
447 {
448         if (other.size() && size()) {
449                 DEBUG_TRACE (DEBUG::MidiIO, string_compose ("merge in place, sizes %1/%2\n", size(), other.size()));
450         }
451
452         if (other.size() == 0) {
453                 return true;
454         }
455
456         if (size() == 0) {
457                 copy (other);
458                 return true;
459         }
460
461         if (size() + other.size() > _capacity) {
462                 return false;
463         }
464
465         const_iterator them = other.begin();
466         iterator us = begin();
467
468         while (them != other.end()) {
469
470                 size_t bytes_to_merge;
471                 ssize_t merge_offset;
472
473                 /* gather up total size of events that are earlier than
474                    the event referenced by "us"
475                 */
476
477                 merge_offset = -1;
478                 bytes_to_merge = 0;
479
480                 while (them != other.end() && (*them).time() < (*us).time()) {
481                         if (merge_offset == -1) {
482                                 merge_offset = them.offset;
483                         }
484                         bytes_to_merge += sizeof (TimeType) + (*them).size();
485                         ++them;
486                 }
487
488                 /* "them" now points to either:
489                  *
490                  * 1) an event that has the same or later timestamp than the
491                  *        event pointed to by "us"
492                  *
493                  * OR
494                  *
495                  * 2) the end of the "other" buffer
496                  *
497                  * if "sz" is non-zero, there is data to be merged from "other"
498                  * into this buffer before we do anything else, corresponding
499                  * to the events from "other" that we skipped while advancing
500                  * "them".
501                  */
502
503                 if (bytes_to_merge) {
504                         assert(merge_offset >= 0);
505                         /* move existing */
506                         memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
507                         /* increase _size */
508                         _size += bytes_to_merge;
509                         assert (_size <= _capacity);
510                         /* insert new stuff */
511                         memcpy  (_data + us.offset, other._data + merge_offset, bytes_to_merge);
512                         /* update iterator to our own events. this is a miserable hack */
513                         us.offset += bytes_to_merge;
514                 }
515
516                 /* if we're at the end of the other buffer, we're done */
517
518                 if (them == other.end()) {
519                         break;
520                 }
521
522                 /* if we have two messages messages with the same timestamp. we
523                  * must order them correctly.
524                  */
525
526                 if ((*us).time() == (*them).time()) {
527
528                         DEBUG_TRACE (DEBUG::MidiIO,
529                                      string_compose ("simultaneous MIDI events discovered during merge, times %1/%2 status %3/%4\n",
530                                                      (*us).time(), (*them).time(),
531                                                      (int) *(_data + us.offset + sizeof (TimeType)),
532                                                      (int) *(other._data + them.offset + sizeof (TimeType))));
533
534                         uint8_t our_midi_status_byte = *(_data + us.offset + sizeof (TimeType));
535                         uint8_t their_midi_status_byte = *(other._data + them.offset + sizeof (TimeType));
536                         bool them_first = second_simultaneous_midi_byte_is_first (our_midi_status_byte, their_midi_status_byte);
537
538                         DEBUG_TRACE (DEBUG::MidiIO, string_compose ("other message came first ? %1\n", them_first));
539
540                         if (!them_first) {
541                                 /* skip past our own event */
542                                 ++us;
543                         }
544
545                         bytes_to_merge = sizeof (TimeType) + (*them).size();
546
547                         /* move our remaining events later in the buffer by
548                          * enough to fit the one message we're going to merge
549                          */
550
551                         memmove (_data + us.offset + bytes_to_merge, _data + us.offset, _size - us.offset);
552                         /* increase _size */
553                         _size += bytes_to_merge;
554                         assert(_size <= _capacity);
555                         /* insert new stuff */
556                         memcpy  (_data + us.offset, other._data + them.offset, bytes_to_merge);
557                         /* update iterator to our own events. this is a miserable hack */
558                         us.offset += bytes_to_merge;
559                         /* 'us' is now an iterator to the event right after the
560                            new ones that we merged
561                         */
562                         if (them_first) {
563                                 /* need to skip the event pointed to by 'us'
564                                    since its at the same time as 'them'
565                                    (still), and we'll enter
566                                 */
567
568                                 if (us != end()) {
569                                         ++us;
570                                 }
571                         }
572
573                         /* we merged one event from the other buffer, so
574                          * advance the iterator there.
575                          */
576
577                         ++them;
578
579                 } else {
580
581                         /* advance past our own events to get to the correct insertion
582                            point for the next event(s) from "other"
583                         */
584
585                         while (us != end() && (*us).time() <= (*them).time()) {
586                                 ++us;
587                         }
588                 }
589
590                 /* check to see if we reached the end of this buffer while
591                  * looking for the insertion point.
592                  */
593
594                 if (us == end()) {
595
596                         /* just append the rest of other and we're done*/
597
598                         memcpy (_data + us.offset, other._data + them.offset, other._size - them.offset);
599                         _size += other._size - them.offset;
600                         assert(_size <= _capacity);
601                         break;
602                 }
603         }
604
605         return true;
606 }