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