2 Copyright (C) 2006-2009 Paul Davis
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 nframes_t TimeType;
37 MidiBuffer(size_t capacity);
40 void silence(nframes_t dur, nframes_t offset=0);
42 void read_from(const Buffer& src, nframes_t nframes, nframes_t offset);
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 uint8_t* reserve(TimeType time, size_t size);
52 bool merge(const MidiBuffer& a, const MidiBuffer& b);
53 bool merge_in_place(const MidiBuffer &other);
55 template<typename BufferType, typename MIDIEventType>
56 struct iterator_base {
57 iterator_base<BufferType, MIDIEventType>(BufferType& b, size_t o) : buffer(b), offset(o) {}
58 inline MIDIEventType operator*() const {
59 uint8_t* ev_start = buffer._data + offset + sizeof(TimeType);
60 int event_size = Evoral::midi_event_size(ev_start);
61 assert(event_size >= 0);
62 return MIDIEventType(EventTypeMap::instance().midi_event_type(*ev_start),
63 *((TimeType*)(buffer._data + offset)),
64 event_size, ev_start);
66 inline iterator_base<BufferType, MIDIEventType>& operator++() {
67 uint8_t* ev_start = buffer._data + offset + sizeof(TimeType);
68 int event_size = Evoral::midi_event_size(ev_start);
69 assert(event_size >= 0);
70 offset += sizeof(TimeType) + event_size;
73 inline bool operator!=(const iterator_base<BufferType, MIDIEventType>& other) const {
74 return (&buffer != &other.buffer) || (offset != other.offset);
80 typedef iterator_base< MidiBuffer, Evoral::MIDIEvent<TimeType> > iterator;
81 typedef iterator_base< const MidiBuffer, const Evoral::MIDIEvent<TimeType> > const_iterator;
83 iterator begin() { return iterator(*this, 0); }
84 iterator end() { return iterator(*this, _size); }
86 const_iterator begin() const { return const_iterator(*this, 0); }
87 const_iterator end() const { return const_iterator(*this, _size); }
90 friend class iterator_base< MidiBuffer, Evoral::MIDIEvent<TimeType> >;
91 friend class iterator_base< const MidiBuffer, const Evoral::MIDIEvent<TimeType> >;
93 size_t _size; ///< Size in bytes of used portion of _data
94 uint8_t* _data; ///< timestamp, event, timestamp, event, ...
100 #endif // __ardour_midi_buffer_h__