2 Copyright (C) 2006-2009 Paul Davis
3 Author: David Robillard
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)
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
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.
20 #ifndef __ardour_midi_buffer_h__
21 #define __ardour_midi_buffer_h__
23 #include "evoral/EventSink.hpp"
24 #include "evoral/midi_util.h"
25 #include "evoral/types.hpp"
27 #include "midi++/event.h"
29 #include "ardour/buffer.h"
30 #include "ardour/parameter_types.h"
35 /** Buffer containing 8-bit unsigned char (MIDI) data. */
36 class LIBARDOUR_API MidiBuffer : public Buffer, public Evoral::EventSink<samplepos_t>
39 typedef samplepos_t TimeType;
41 MidiBuffer(size_t capacity);
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);
48 void copy(const MidiBuffer& copy);
49 void copy(MidiBuffer const * const);
51 bool push_back(const Evoral::Event<TimeType>& event);
52 bool push_back(TimeType time, size_t size, const uint8_t* data);
54 uint8_t* reserve(TimeType time, size_t size);
57 size_t size() const { return _size; }
58 bool empty() const { return _size == 0; }
60 bool insert_event(const Evoral::Event<TimeType>& event);
61 bool merge_in_place(const MidiBuffer &other);
63 /** EventSink interface for non-RT use (export, bounce). */
64 uint32_t write(TimeType time, Evoral::EventType type, uint32_t size, const uint8_t* buf);
66 template<typename BufferType, typename EventType>
70 iterator_base<BufferType, EventType>(BufferType& b, samplecnt_t o)
71 : buffer(&b), offset(o) {}
73 iterator_base<BufferType, EventType>(const iterator_base<BufferType,EventType>& o)
74 : buffer (o.buffer), offset(o.offset) {}
76 inline iterator_base<BufferType,EventType> operator= (const iterator_base<BufferType,EventType>& o) {
84 inline EventType operator*() const {
85 uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
86 int event_size = Evoral::midi_event_size(ev_start);
87 assert(event_size >= 0);
88 return EventType(midi_parameter_type(*ev_start),
89 *((TimeType*)(buffer->_data + offset)),
90 event_size, ev_start);
93 inline EventType operator*() {
94 uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
95 int event_size = Evoral::midi_event_size(ev_start);
96 assert(event_size >= 0);
97 return EventType(Evoral::MIDI_EVENT,
98 *(reinterpret_cast<TimeType*>((uintptr_t)(buffer->_data + offset))),
99 event_size, ev_start);
102 inline TimeType * timeptr() {
103 return reinterpret_cast<TimeType*>((uintptr_t)(buffer->_data + offset));
106 inline iterator_base<BufferType, EventType>& operator++() {
107 uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
108 int event_size = Evoral::midi_event_size(ev_start);
109 assert(event_size >= 0);
110 offset += sizeof(TimeType) + event_size;
114 inline bool operator!=(const iterator_base<BufferType, EventType>& other) const {
115 return (buffer != other.buffer) || (offset != other.offset);
118 inline bool operator==(const iterator_base<BufferType, EventType>& other) const {
119 return (buffer == other.buffer) && (offset == other.offset);
126 typedef iterator_base< MidiBuffer, Evoral::Event<TimeType> > iterator;
127 typedef iterator_base< const MidiBuffer, const Evoral::Event<TimeType> > const_iterator;
129 iterator begin() { return iterator(*this, 0); }
130 iterator end() { return iterator(*this, _size); }
132 const_iterator begin() const { return const_iterator(*this, 0); }
133 const_iterator end() const { return const_iterator(*this, _size); }
135 iterator erase(const iterator& i) {
136 assert (i.buffer == this);
137 uint8_t* ev_start = _data + i.offset + sizeof (TimeType);
138 int event_size = Evoral::midi_event_size (ev_start);
140 if (event_size < 0) {
141 /* unknown size, sysex: return end() */
145 size_t total_data_deleted = sizeof(TimeType) + event_size;
147 if (i.offset + total_data_deleted > _size) {
152 /* we need to avoid the temporary malloc that memmove would do,
153 so copy by hand. remember: this is small amounts of data ...
156 for (a = i.offset, b = i.offset + total_data_deleted; b < _size; ++b, ++a) {
160 _size -= total_data_deleted;
162 /* all subsequent iterators are now invalid, and the one we
163 * return should refer to the event we copied, which was after
164 * the one we just erased.
167 return iterator (*this, i.offset);
171 * returns true if the message with the second argument as its MIDI
172 * status byte should preceed the message with the first argument as
173 * its MIDI status byte.
175 static bool second_simultaneous_midi_byte_is_first (uint8_t, uint8_t);
178 friend class iterator_base< MidiBuffer, Evoral::Event<TimeType> >;
179 friend class iterator_base< const MidiBuffer, const Evoral::Event<TimeType> >;
181 uint8_t* _data; ///< timestamp, event, timestamp, event, ...
185 } // namespace ARDOUR
187 #endif // __ardour_midi_buffer_h__