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/midi_util.h"
24 #include "midi++/event.h"
25 #include "ardour/buffer.h"
26 #include "ardour/event_type_map.h"
31 /** Buffer containing 8-bit unsigned char (MIDI) data. */
32 class MidiBuffer : public Buffer
35 typedef framepos_t TimeType;
37 MidiBuffer(size_t capacity);
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);
44 void copy(const MidiBuffer& copy);
46 bool push_back(const Evoral::MIDIEvent<TimeType>& event);
47 bool push_back(const jack_midi_event_t& event);
48 bool push_back(TimeType time, size_t size, const uint8_t* data);
49 uint8_t* reserve(TimeType time, size_t size);
53 bool merge_in_place(const MidiBuffer &other);
55 template<typename BufferType, typename EventType>
58 iterator_base<BufferType, EventType>(BufferType& b, framecnt_t o)
59 : buffer(&b), offset(o) {}
60 iterator_base<BufferType, EventType>(const iterator_base<BufferType,EventType>& o)
61 : buffer (o.buffer), offset(o.offset) {}
63 inline iterator_base<BufferType,EventType> operator= (const iterator_base<BufferType,EventType>& o) {
71 inline EventType operator*() const {
72 uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
73 int event_size = Evoral::midi_event_size(ev_start);
74 assert(event_size >= 0);
75 return EventType(EventTypeMap::instance().midi_event_type(*ev_start),
76 *((TimeType*)(buffer->_data + offset)),
77 event_size, ev_start);
79 inline EventType operator*() {
80 uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
81 int event_size = Evoral::midi_event_size(ev_start);
82 assert(event_size >= 0);
83 return EventType(EventTypeMap::instance().midi_event_type(*ev_start),
84 *((TimeType*)(buffer->_data + offset)),
85 event_size, ev_start);
88 inline iterator_base<BufferType, EventType>& operator++() {
89 uint8_t* ev_start = buffer->_data + offset + sizeof(TimeType);
90 int event_size = Evoral::midi_event_size(ev_start);
91 assert(event_size >= 0);
92 offset += sizeof(TimeType) + event_size;
95 inline bool operator!=(const iterator_base<BufferType, EventType>& other) const {
96 return (buffer != other.buffer) || (offset != other.offset);
98 inline bool operator==(const iterator_base<BufferType, EventType>& other) const {
99 return (buffer == other.buffer) && (offset == other.offset);
105 typedef iterator_base< MidiBuffer, Evoral::MIDIEvent<TimeType> > iterator;
106 typedef iterator_base< const MidiBuffer, const Evoral::MIDIEvent<TimeType> > const_iterator;
108 iterator begin() { return iterator(*this, 0); }
109 iterator end() { return iterator(*this, _size); }
111 const_iterator begin() const { return const_iterator(*this, 0); }
112 const_iterator end() const { return const_iterator(*this, _size); }
114 iterator erase(const iterator& i) {
115 assert (i.buffer == this);
116 uint8_t* ev_start = _data + i.offset + sizeof (TimeType);
117 int event_size = Evoral::midi_event_size (ev_start);
119 if (event_size < 0) {
120 /* unknown size, sysex: return end() */
124 size_t total_data_deleted = sizeof(TimeType) + event_size;
126 if (i.offset + total_data_deleted >= _size) {
131 /* we need to avoid the temporary malloc that memmove would do,
132 so copy by hand. remember: this is small amounts of data ...
135 for (a = i.offset, b = i.offset + total_data_deleted; b < _size; ++b, ++a) {
139 _size -= total_data_deleted;
141 /* all subsequent iterators are now invalid, and the one we
142 * return should refer to the event we copied, which was after
143 * the one we just erased.
146 return iterator (*this, i.offset);
149 uint8_t* data() const { return _data; }
152 * returns true if the message with the second argument as its MIDI
153 * status byte should preceed the message with the first argument as
154 * its MIDI status byte.
156 static bool second_simultaneous_midi_byte_is_first (uint8_t, uint8_t);
159 friend class iterator_base< MidiBuffer, Evoral::MIDIEvent<TimeType> >;
160 friend class iterator_base< const MidiBuffer, const Evoral::MIDIEvent<TimeType> >;
162 uint8_t* _data; ///< timestamp, event, timestamp, event, ...
166 } // namespace ARDOUR
168 #endif // __ardour_midi_buffer_h__