Const-correct BufferSet.
[ardour.git] / libs / ardour / ardour / buffer_set.h
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #ifndef __ardour_buffer_set_h__
20 #define __ardour_buffer_set_h__
21
22 #ifdef WAF_BUILD
23 #include "libardour-config.h"
24 #endif
25
26 #include <cassert>
27 #include <vector>
28 #include "ardour/chan_count.h"
29 #include "ardour/data_type.h"
30 #include "ardour/types.h"
31
32 namespace ARDOUR {
33
34 class Buffer;
35 class AudioBuffer;
36 class MidiBuffer;
37 class PortSet;
38 #ifdef HAVE_SLV2
39 class LV2EventBuffer;
40 #endif
41
42 /** A set of buffers of various types.
43  *
44  * These are mainly accessed from Session and passed around as scratch buffers
45  * (eg as parameters to run() methods) to do in-place signal processing.
46  *
47  * There are two types of counts associated with a BufferSet - available,
48  * and the 'use count'.  Available is the actual number of allocated buffers
49  * (and so is the maximum acceptable value for the use counts).
50  *
51  * The use counts are how things determine the form of their input and inform
52  * others the form of their output (eg what they did to the BufferSet).
53  * Setting the use counts is realtime safe.
54  */
55 class BufferSet
56 {
57 public:
58         BufferSet();
59         ~BufferSet();
60
61         void clear();
62
63         void attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset = 0);
64
65         /* the capacity here is a size_t and has a different interpretation depending
66            on the DataType of the buffers. for audio, its a frame count. for MIDI
67            its a byte count.
68         */
69
70         void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
71         void ensure_buffers(const ChanCount& chns, size_t buffer_capacity);
72
73         const ChanCount& available() const { return _available; }
74         ChanCount&       available()       { return _available; }
75
76         const ChanCount& count() const { return _count; }
77         ChanCount&       count()       { return _count; }
78
79         void is_silent(bool yn) { _is_silent = yn; }
80         bool is_silent() const  { return _is_silent; }
81         void silence (nframes_t nframes, nframes_t offset);
82         bool is_mirror() const { return _is_mirror; }
83
84         void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
85
86         size_t buffer_capacity(DataType type) const;
87
88         Buffer&       get(DataType type, size_t i);
89         const Buffer& get(DataType type, size_t i) const;
90
91         AudioBuffer& get_audio(size_t i) {
92                 return (AudioBuffer&)get(DataType::AUDIO, i);
93         }
94
95         MidiBuffer& get_midi(size_t i) {
96                 return (MidiBuffer&)get(DataType::MIDI, i);
97         }
98
99 #ifdef HAVE_SLV2
100         /** Get a MIDI buffer translated into an LV2 MIDI buffer for use with plugins.
101          * The index here corresponds directly to MIDI buffer numbers (i.e. the index
102          * passed to get_midi), translation back and forth will happen as needed */
103         LV2EventBuffer& get_lv2_midi(bool input, size_t i);
104
105         /** Flush modified LV2 event output buffers back to Ardour buffers */
106         void flush_lv2_midi(bool input, size_t i);
107 #endif
108
109         void read_from(const BufferSet& in, nframes_t nframes);
110         void merge_from(const BufferSet& in, nframes_t nframes);
111
112         // ITERATORS
113         // FIXME: possible to combine these?  templates?
114
115         class audio_iterator {
116         public:
117                 AudioBuffer& operator*()  { return _set.get_audio(_index); }
118                 AudioBuffer* operator->() { return &_set.get_audio(_index); }
119                 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
120                 bool operator==(const audio_iterator& other) { return (_index == other._index); }
121                 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
122
123         private:
124                 friend class BufferSet;
125
126                 audio_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
127
128                 BufferSet& _set;
129                 size_t    _index;
130         };
131
132         audio_iterator audio_begin() { return audio_iterator(*this, 0); }
133         audio_iterator audio_end()   { return audio_iterator(*this, _count.n_audio()); }
134
135         class midi_iterator {
136         public:
137                 MidiBuffer& operator*()  { return _set.get_midi(_index); }
138                 MidiBuffer* operator->() { return &_set.get_midi(_index); }
139                 midi_iterator& operator++() { ++_index; return *this; } // yes, prefix only
140                 bool operator==(const midi_iterator& other) { return (_index == other._index); }
141                 bool operator!=(const midi_iterator& other) { return (_index != other._index); }
142
143         private:
144                 friend class BufferSet;
145
146                 midi_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
147
148                 BufferSet& _set;
149                 size_t    _index;
150         };
151
152         midi_iterator midi_begin() { return midi_iterator(*this, 0); }
153         midi_iterator midi_end()   { return midi_iterator(*this, _count.n_midi()); }
154
155         template <typename BS, typename B>
156         class iterator_base {
157         public:
158                 B& operator*()  { return _set.get(_type, _index); }
159                 B* operator->() { return &_set.get(_type, _index); }
160                 iterator_base<BS,B>& operator++() { ++_index; return *this; } // yes, prefix only
161                 bool operator==(const iterator_base<BS,B>& other) { return (_index == other._index); }
162                 bool operator!=(const iterator_base<BS,B>& other) { return (_index != other._index); }
163                 iterator_base<BS,B> operator=(const iterator_base<BS,B>& other) {
164                         _set = other._set; _type = other._type; _index = other._index; return *this;
165                 }
166
167         private:
168                 friend class BufferSet;
169
170                 iterator_base(BS& list, DataType type, size_t index)
171                         : _set(list), _type(type), _index(index) {}
172
173                 BS&       _set;
174                 DataType _type;
175                 size_t   _index;
176         };
177
178         typedef iterator_base<BufferSet, Buffer>             iterator;
179         typedef iterator_base<const BufferSet, const Buffer> const_iterator;
180
181         iterator       begin(DataType type)       { return iterator(*this, type, 0); }
182         iterator       end(DataType type)         { return iterator(*this, type, _count.get(type)); }
183         const_iterator begin(DataType type) const { return const_iterator(*this, type, 0); }
184         const_iterator end(DataType type)   const { return const_iterator(*this, type, _count.get(type)); }
185
186 private:
187         typedef std::vector<Buffer*> BufferVec;
188
189         /// Vector of vectors, indexed by DataType
190         std::vector<BufferVec> _buffers;
191
192 #ifdef HAVE_SLV2
193         /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
194         typedef std::vector< std::pair<bool, LV2EventBuffer*> > LV2Buffers;
195         LV2Buffers _lv2_buffers;
196 #endif
197
198         /// Use counts (there may be more actual buffers than this)
199         ChanCount _count;
200
201         /// Available counts (number of buffers actually allocated)
202         ChanCount _available;
203
204         /// False if we 'own' the contained buffers, if true we mirror a PortSet)
205         bool _is_mirror;
206
207         /// Whether the buffer set should be considered silent
208         bool _is_silent;
209 };
210
211
212 } // namespace ARDOUR
213
214 #endif // __ardour_buffer_set_h__