7793f7e0bd83946564f3e1925793398b1414faaa
[ardour.git] / libs / ardour / ardour / midi_buffer.h
1 /*
2     Copyright (C) 2006-2009 Paul Davis
3     Author: David 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 #ifndef __ardour_midi_buffer_h__
21 #define __ardour_midi_buffer_h__
22
23 #include "evoral/EventSink.hpp"
24 #include "evoral/midi_util.h"
25 #include "evoral/types.hpp"
26
27 #include "midi++/event.h"
28
29 #include "ardour/buffer.h"
30 #include "ardour/parameter_types.h"
31
32 namespace ARDOUR {
33
34
35 /** Buffer containing 8-bit unsigned char (MIDI) data. */
36 class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<samplepos_t>
37 {
38 public:
39         typedef samplepos_t TimeType;
40
41         MidiBuffer(size_t capacity);
42         ~MidiBuffer();
43
44         void silence (samplecnt_t nframes, samplecnt_t offset = 0);
45         void read_from (const Buffer& src, samplecnt_t nframes, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0);
46         void merge_from (const Buffer& src, samplecnt_t nframes, sampleoffset_t dst_offset = 0, sampleoffset_t src_offset = 0);
47
48         void copy(const MidiBuffer& copy);
49         void copy(MidiBuffer const * const);
50
51         void skip_to (TimeType when);
52
53         bool     push_back(const Evoral::Event<TimeType>& event);
54         bool     push_back(TimeType time, size_t size, const uint8_t* data);
55
56         uint8_t* reserve(TimeType time, size_t size);
57
58         void resize(size_t);
59         size_t size() const { return _size; }
60         bool empty() const { return _size == 0; }
61
62         bool insert_event(const Evoral::Event<TimeType>& event);
63         bool merge_in_place(const MidiBuffer &other);
64
65         /** EventSink interface for non-RT use (export, bounce). */
66         uint32_t write(TimeType time, Evoral::EventType type, uint32_t size, const uint8_t* buf);
67
68         template<typename BufferType, typename EventType>
69                 class iterator_base
70         {
71         public:
72                 iterator_base<BufferType, EventType>(BufferType& b, samplecnt_t o)
73                         : buffer(&b), offset(o) {}
74
75                 iterator_base<BufferType, EventType>(const iterator_base<BufferType,EventType>& o)
76                         : buffer (o.buffer), offset(o.offset) {}
77
78                 inline iterator_base<BufferType,EventType> operator= (const iterator_base<BufferType,EventType>& o) {
79                         if (&o != this) {
80                                 buffer = o.buffer;
81                                 offset = o.offset;
82                         }
83                         return *this;
84                 }
85
86                 inline EventType operator*() const {
87                         uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
88                         int event_size = Evoral::midi_event_size(ev_start);
89                         assert(event_size >= 0);
90                         return EventType(midi_parameter_type(*ev_start),
91                                         *((TimeType*)(buffer->_data + offset)),
92                                         event_size, ev_start);
93                 }
94
95                 inline EventType operator*() {
96                         uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
97                         int event_size = Evoral::midi_event_size(ev_start);
98                         assert(event_size >= 0);
99                         return EventType(Evoral::MIDI_EVENT,
100                                         *(reinterpret_cast<TimeType*>((uintptr_t)(buffer->_data + offset))),
101                                         event_size, ev_start);
102                 }
103
104                 inline TimeType * timeptr() {
105                         return reinterpret_cast<TimeType*>((uintptr_t)(buffer->_data + offset));
106                 }
107
108                 inline iterator_base<BufferType, EventType>& operator++() {
109                         uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
110                         int event_size = Evoral::midi_event_size(ev_start);
111                         assert(event_size >= 0);
112                         offset += sizeof(TimeType) + event_size;
113                         return *this;
114                 }
115
116                 inline bool operator!=(const iterator_base<BufferType, EventType>& other) const {
117                         return (buffer != other.buffer) || (offset != other.offset);
118                 }
119
120                 inline bool operator==(const iterator_base<BufferType, EventType>& other) const {
121                         return (buffer == other.buffer) && (offset == other.offset);
122                 }
123
124                 BufferType*     buffer;
125                 size_t          offset;
126         };
127
128         typedef iterator_base< MidiBuffer, Evoral::Event<TimeType> >             iterator;
129         typedef iterator_base< const MidiBuffer, const Evoral::Event<TimeType> > const_iterator;
130
131         iterator begin() { return iterator(*this, 0); }
132         iterator end()   { return iterator(*this, _size); }
133
134         const_iterator begin() const { return const_iterator(*this, 0); }
135         const_iterator end()   const { return const_iterator(*this, _size); }
136
137         iterator erase(const iterator& i) {
138                 assert (i.buffer == this);
139                 uint8_t* ev_start = _data + i.offset + sizeof (TimeType);
140                 int event_size = Evoral::midi_event_size (ev_start);
141
142                 if (event_size < 0) {
143                         /* unknown size, sysex: return end() */
144                         return end();
145                 }
146
147                 size_t total_data_deleted = sizeof(TimeType) + event_size;
148
149                 if (i.offset + total_data_deleted > _size) {
150                         _size = 0;
151                         return end();
152                 }
153
154                 /* we need to avoid the temporary malloc that memmove would do,
155                    so copy by hand. remember: this is small amounts of data ...
156                 */
157                 size_t a, b;
158                 for (a = i.offset, b = i.offset + total_data_deleted; b < _size; ++b, ++a) {
159                         _data[a] = _data[b];
160                 }
161
162                 _size -= total_data_deleted;
163
164                 /* all subsequent iterators are now invalid, and the one we
165                  * return should refer to the event we copied, which was after
166                  * the one we just erased.
167                  */
168
169                 return iterator (*this, i.offset);
170         }
171
172         /**
173          * returns true if the message with the second argument as its MIDI
174          * status byte should preceed the message with the first argument as
175          * its MIDI status byte.
176          */
177         static bool second_simultaneous_midi_byte_is_first (uint8_t, uint8_t);
178
179 private:
180         friend class iterator_base< MidiBuffer, Evoral::Event<TimeType> >;
181         friend class iterator_base< const MidiBuffer, const Evoral::Event<TimeType> >;
182
183         uint8_t* _data; ///< timestamp, event, timestamp, event, ...
184         pframes_t _size;
185 };
186
187 } // namespace ARDOUR
188
189 #endif // __ardour_midi_buffer_h__