Removed fixed/maximum event size assumption/limitation from MIDI buffer.
[ardour.git] / libs / ardour / ardour / midi_buffer.h
1 /*
2     Copyright (C) 2006-2009 Paul Davis 
3     Author: Dave 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 MidiBuffer : public Buffer
33 {
34 public:
35         MidiBuffer(size_t capacity);
36         ~MidiBuffer();
37
38         void silence(nframes_t dur, nframes_t offset=0);
39         
40         void read_from(const Buffer& src, nframes_t nframes, nframes_t offset);
41         
42         void copy(const MidiBuffer& copy);
43
44         bool     push_back(const Evoral::MIDIEvent& event);
45         bool     push_back(const jack_midi_event_t& event);
46         uint8_t* reserve(double time, size_t size);
47
48         void resize(size_t);
49
50         bool merge(const MidiBuffer& a, const MidiBuffer& b);
51         bool merge_in_place(const MidiBuffer &other);
52         
53         template<typename B, typename E>
54         struct iterator_base {
55                 iterator_base<B,E>(B& b, size_t o) : buffer(b), offset(o) {}
56                 inline E operator*() const {
57                         uint8_t* ev_start = buffer._data + offset + sizeof(Evoral::EventTime);
58                         return E(EventTypeMap::instance().midi_event_type(*ev_start),
59                                         *(Evoral::EventTime*)(buffer._data + offset),
60                                         Evoral::midi_event_size(*ev_start) + 1, ev_start);
61                 }
62                 inline iterator_base<B,E>& operator++() {
63                         uint8_t* ev_start = buffer._data + offset + sizeof(Evoral::EventTime);
64                         offset += sizeof(Evoral::EventTime) + Evoral::midi_event_size(*ev_start) + 1;
65                         return *this;
66                 }
67                 inline bool operator!=(const iterator_base<B,E>& other) const {
68                         return (&buffer != &other.buffer) || (offset != other.offset);
69                 }
70                 B&     buffer;
71                 size_t offset;
72         };
73         
74         typedef iterator_base<MidiBuffer, Evoral::MIDIEvent>             iterator;
75         typedef iterator_base<const MidiBuffer, const Evoral::MIDIEvent> const_iterator;
76
77         iterator begin() { return iterator(*this, 0); }
78         iterator end()   { return iterator(*this, _size); }
79
80         const_iterator begin() const { return const_iterator(*this, 0); }
81         const_iterator end()   const { return const_iterator(*this, _size); }
82
83 private:
84         friend class iterator_base<MidiBuffer, Evoral::MIDIEvent>;
85         friend class iterator_base<const MidiBuffer, const Evoral::MIDIEvent>;
86         
87         size_t   _size; ///< Size in bytes of used portion of _data
88         uint8_t* _data; ///< timestamp, event, timestamp, event, ...
89 };
90
91
92 } // namespace ARDOUR
93
94 #endif // __ardour_midi_buffer_h__