make i18n build work ; add mackie dir back to build ; token work on amp for MIDI...
[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         void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
66         void ensure_buffers(const ChanCount& chns, size_t buffer_capacity);
67
68         const ChanCount& available() const { return _available; }
69         ChanCount&       available()       { return _available; }
70
71         const ChanCount& count() const { return _count; }
72         ChanCount&       count()       { return _count; }
73
74         void is_silent(bool yn) { _is_silent = yn; }
75         bool is_silent() const  { return _is_silent; }
76         void silence (nframes_t nframes, nframes_t offset);
77         bool is_mirror() const { return _is_mirror; } 
78
79         void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
80         
81         size_t buffer_capacity(DataType type) const;
82
83         Buffer& get(DataType type, size_t i);
84
85         AudioBuffer& get_audio(size_t i) {
86                 return (AudioBuffer&)get(DataType::AUDIO, i);
87         }
88         
89         MidiBuffer& get_midi(size_t i) {
90                 return (MidiBuffer&)get(DataType::MIDI, i);
91         }
92
93 #ifdef HAVE_SLV2
94         /** Get a MIDI buffer translated into an LV2 MIDI buffer for use with plugins.
95          * The index here corresponds directly to MIDI buffer numbers (i.e. the index
96          * passed to get_midi), translation back and forth will happen as needed */
97         LV2EventBuffer& get_lv2_midi(bool input, size_t i);
98
99         /** Flush modified LV2 event output buffers back to Ardour buffers */
100         void flush_lv2_midi(bool input, size_t i);
101 #endif
102
103         void read_from(BufferSet& in, nframes_t nframes);
104         void merge_from(BufferSet& in, nframes_t nframes);
105
106         // ITERATORS
107         // FIXME: possible to combine these?  templates?
108         
109         class audio_iterator {
110         public:
111                 AudioBuffer& operator*()  { return _set.get_audio(_index); }
112                 AudioBuffer* operator->() { return &_set.get_audio(_index); }
113                 audio_iterator& operator++() { ++_index; return *this; } // yes, prefix only
114                 bool operator==(const audio_iterator& other) { return (_index == other._index); }
115                 bool operator!=(const audio_iterator& other) { return (_index != other._index); }
116
117         private:
118                 friend class BufferSet;
119
120                 audio_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
121
122                 BufferSet& _set;
123                 size_t    _index;
124         };
125
126         audio_iterator audio_begin() { return audio_iterator(*this, 0); }
127         audio_iterator audio_end()   { return audio_iterator(*this, _count.n_audio()); }
128
129         class midi_iterator {
130         public:
131                 MidiBuffer& operator*()  { return _set.get_midi(_index); }
132                 MidiBuffer* operator->() { return &_set.get_midi(_index); }
133                 midi_iterator& operator++() { ++_index; return *this; } // yes, prefix only
134                 bool operator==(const midi_iterator& other) { return (_index == other._index); }
135                 bool operator!=(const midi_iterator& other) { return (_index != other._index); }
136
137         private:
138                 friend class BufferSet;
139
140                 midi_iterator(BufferSet& list, size_t index) : _set(list), _index(index) {}
141
142                 BufferSet& _set;
143                 size_t    _index;
144         };
145
146         midi_iterator midi_begin() { return midi_iterator(*this, 0); }
147         midi_iterator midi_end()   { return midi_iterator(*this, _count.n_midi()); }
148
149         class iterator {
150         public:
151                 Buffer& operator*()  { return _set.get(_type, _index); }
152                 Buffer* operator->() { return &_set.get(_type, _index); }
153                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
154                 bool operator==(const iterator& other) { return (_index == other._index); }
155                 bool operator!=(const iterator& other) { return (_index != other._index); }
156                 iterator operator=(const iterator& other) {
157                         _set = other._set; _type = other._type; _index = other._index; return *this;
158                 }
159
160         private:
161                 friend class BufferSet;
162
163                 iterator(BufferSet& list, DataType type, size_t index)
164                         : _set(list), _type(type), _index(index) {}
165
166                 BufferSet& _set;
167                 DataType   _type;
168                 size_t     _index;
169         };
170
171         iterator begin(DataType type) { return iterator(*this, type, 0); }
172         iterator end(DataType type)   { return iterator(*this, type, _count.get(type)); }
173         
174 private:
175         typedef std::vector<Buffer*> BufferVec;
176
177         /// Vector of vectors, indexed by DataType
178         std::vector<BufferVec> _buffers;
179         
180 #ifdef HAVE_SLV2
181         /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
182         typedef std::vector< std::pair<bool, LV2EventBuffer*> > LV2Buffers;
183         LV2Buffers _lv2_buffers;
184 #endif
185
186         /// Use counts (there may be more actual buffers than this)
187         ChanCount _count;
188
189         /// Available counts (number of buffers actually allocated)
190         ChanCount _available;
191
192         /// False if we 'own' the contained buffers, if true we mirror a PortSet)
193         bool _is_mirror;
194
195         /// Whether the buffer set should be considered silent
196         bool _is_silent;
197 };
198
199
200 } // namespace ARDOUR
201
202 #endif // __ardour_buffer_set_h__