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