make i18n build work ; add mackie dir back to build ; token work on amp for MIDI...
[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         typedef nframes_t TimeType;
36
37         MidiBuffer(size_t capacity);
38         ~MidiBuffer();
39
40         void silence (nframes_t nframes, nframes_t offset = 0);
41         void read_from (const Buffer& src, nframes_t nframes, nframes_t dst_offset = 0, nframes_t src_offset = 0);
42         void merge_from (const Buffer& src, nframes_t nframes, nframes_t dst_offset = 0, nframes_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(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);
50
51         void resize(size_t);
52
53         bool merge(const MidiBuffer& a, const MidiBuffer& b);
54         bool merge_in_place(const MidiBuffer &other);
55         
56         template<typename BufferType, typename EventType>
57         struct iterator_base {
58             iterator_base<BufferType, EventType>(BufferType& b, nframes_t o) : buffer(b), offset(o) {}
59                 inline EventType operator*() const {
60                         uint8_t* ev_start = buffer._data + offset + sizeof(TimeType);
61                         int event_size = Evoral::midi_event_size(ev_start);
62                         assert(event_size >= 0);
63                         return EventType(EventTypeMap::instance().midi_event_type(*ev_start),
64                                         *((TimeType*)(buffer._data + offset)),
65                                         event_size, ev_start);
66                 }
67                 inline EventType operator*() {
68                         uint8_t* ev_start = buffer._data + offset + sizeof(TimeType);
69                         int event_size = Evoral::midi_event_size(ev_start);
70                         assert(event_size >= 0);
71                         return EventType(EventTypeMap::instance().midi_event_type(*ev_start),
72                                         *((TimeType*)(buffer._data + offset)),
73                                         event_size, ev_start);
74                 }
75
76                 inline iterator_base<BufferType, EventType>& operator++() {
77                         uint8_t* ev_start = buffer._data + offset + sizeof(TimeType);
78                         int event_size = Evoral::midi_event_size(ev_start);
79                         assert(event_size >= 0);
80                         offset += sizeof(TimeType) + event_size;
81                         return *this;
82                 }
83                 inline bool operator!=(const iterator_base<BufferType, EventType>& other) const {
84                         return (&buffer != &other.buffer) || (offset != other.offset);
85                 }
86                 BufferType&     buffer;
87                 size_t          offset;
88         };
89         
90         typedef iterator_base< MidiBuffer, Evoral::MIDIEvent<TimeType> >             iterator;
91         typedef iterator_base< const MidiBuffer, const Evoral::MIDIEvent<TimeType> > const_iterator;
92
93         iterator begin() { return iterator(*this, 0); }
94         iterator end()   { return iterator(*this, _size); }
95
96         const_iterator begin() const { return const_iterator(*this, 0); }
97         const_iterator end()   const { return const_iterator(*this, _size); }
98
99 private:
100         friend class iterator_base< MidiBuffer, Evoral::MIDIEvent<TimeType> >;
101         friend class iterator_base< const MidiBuffer, const Evoral::MIDIEvent<TimeType> >;
102         
103         uint8_t* _data; ///< timestamp, event, timestamp, event, ...
104 };
105
106
107 } // namespace ARDOUR
108
109 #endif // __ardour_midi_buffer_h__