1c7791878983be7f3af96398d6c828db3de2ed01
[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
130         class iterator {
131         public:
132                 Buffer& operator*()  { return _set.get(_type, _index); }
133                 Buffer* operator->() { return &_set.get(_type, _index); }
134                 iterator& operator++() { ++_index; return *this; } // yes, prefix only
135                 bool operator==(const iterator& other) { return (_index == other._index); }
136                 bool operator!=(const iterator& other) { return (_index != other._index); }
137                 iterator operator=(const iterator& other) {
138                         _set = other._set; _type = other._type; _index = other._index; return *this;
139                 }
140
141         private:
142                 friend class BufferSet;
143
144                 iterator(BufferSet& list, DataType type, size_t index)
145                         : _set(list), _type(type), _index(index) {}
146
147                 BufferSet& _set;
148                 DataType   _type;
149                 size_t     _index;
150         };
151
152         iterator begin(DataType type) { return iterator(*this, type, 0); }
153         iterator end(DataType type)   { return iterator(*this, type, _count.get(type)); }
154         
155 private:
156         typedef std::vector<Buffer*> BufferVec;
157
158         /// Vector of vectors, indexed by DataType
159         std::vector<BufferVec> _buffers;
160         
161 #ifdef HAVE_SLV2
162         /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
163         typedef std::vector< std::pair<bool, LV2EventBuffer*> > LV2Buffers;
164         LV2Buffers _lv2_buffers;
165 #endif
166
167         /// Use counts (there may be more actual buffers than this)
168         ChanCount _count;
169
170         /// Available counts (number of buffers actually allocated)
171         ChanCount _available;
172
173         /// False if we 'own' the contained buffers, if true we mirror a PortSet)
174         bool _is_mirror;
175
176         /// Whether the buffer set should be considered silent
177         bool _is_silent;
178 };
179
180
181 } // namespace ARDOUR
182
183 #endif // __ardour_buffer_set_h__