Merge branch 'master' into exportvis
[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/libardour_visibility.h"
31 #include "ardour/types.h"
32
33 #if defined VST_SUPPORT || defined LXVST_SUPPORT
34 #include "evoral/MIDIEvent.hpp"
35 struct _VstEvents;
36 typedef struct _VstEvents VstEvents;
37 struct _VstMidiEvent;
38 typedef struct _VstMidiEvent VstMidiEvent;
39 #endif
40
41 #ifdef LV2_SUPPORT
42 typedef struct LV2_Evbuf_Impl LV2_Evbuf;
43 #endif
44
45 namespace ARDOUR {
46
47 class Buffer;
48 class AudioBuffer;
49 class MidiBuffer;
50 class PortSet;
51
52 /** A set of buffers of various types.
53  *
54  * These are mainly accessed from Session and passed around as scratch buffers
55  * (eg as parameters to run() methods) to do in-place signal processing.
56  *
57  * There are two types of counts associated with a BufferSet - available,
58  * and the 'use count'.  Available is the actual number of allocated buffers
59  * (and so is the maximum acceptable value for the use counts).
60  *
61  * The use counts are how things determine the form of their input and inform
62  * others the form of their output (eg what they did to the BufferSet).
63  * Setting the use counts is realtime safe.
64  */
65 class LIBARDOUR_API BufferSet
66 {
67 public:
68         BufferSet();
69         ~BufferSet();
70
71         void clear();
72
73         void attach_buffers (PortSet& ports);
74         void get_backend_port_addresses (PortSet &, framecnt_t);
75
76         /* the capacity here is a size_t and has a different interpretation depending
77            on the DataType of the buffers. for audio, its a frame count. for MIDI
78            its a byte count.
79         */
80
81         void ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity);
82         void ensure_buffers(const ChanCount& chns, size_t buffer_capacity);
83
84         const ChanCount& available() const { return _available; }
85         ChanCount&       available()       { return _available; }
86
87         const ChanCount& count() const { return _count; }
88         ChanCount&       count()       { return _count; }
89
90         void set_is_silent(bool yn);
91         void silence (framecnt_t nframes, framecnt_t offset);
92         bool is_mirror() const { return _is_mirror; }
93
94         void set_count(const ChanCount& count) { assert(count <= _available); _count = count; }
95
96         size_t buffer_capacity(DataType type) const;
97
98         Buffer&       get(DataType type, size_t i);
99         const Buffer& get(DataType type, size_t i) const;
100
101         AudioBuffer& get_audio(size_t i) {
102                 return (AudioBuffer&)get(DataType::AUDIO, i);
103         }
104         const AudioBuffer& get_audio(size_t i) const {
105                 return (const AudioBuffer&)get(DataType::AUDIO, i);
106         }
107
108         MidiBuffer& get_midi(size_t i) {
109                 return (MidiBuffer&)get(DataType::MIDI, i);
110         }
111         const MidiBuffer& get_midi(size_t i) const {
112                 return (const MidiBuffer&)get(DataType::MIDI, i);
113         }
114
115 #ifdef LV2_SUPPORT
116         /** Get a MIDI buffer translated into an LV2 MIDI buffer for use with
117          * plugins.  The index here corresponds directly to MIDI buffer numbers
118          * (i.e. the index passed to get_midi), translation back and forth will
119          * happen as needed.  If old_api is true, the returned buffer will be in
120          * old event format.  Otherwise it will be in new atom sequence format.
121          */
122         LV2_Evbuf* get_lv2_midi(bool input, size_t i, bool old_api);
123
124         /** ensure minimum size of LV2 Atom port buffer */
125         void ensure_lv2_bufsize(bool input, size_t i, size_t buffer_capacity);
126
127         /** Flush modified LV2 event output buffers back to Ardour buffers */
128         void flush_lv2_midi(bool input, size_t i);
129
130         /** Forward plugin MIDI output to to Ardour buffers */
131         void forward_lv2_midi(LV2_Evbuf*, size_t, bool purge_ardour_buffer = true);
132 #endif
133
134 #if defined VST_SUPPORT || defined LXVST_SUPPORT
135         VstEvents* get_vst_midi (size_t);
136 #endif
137
138         void read_from(const BufferSet& in, framecnt_t nframes);
139         void read_from(const BufferSet& in, framecnt_t nframes, DataType);
140         void merge_from(const BufferSet& in, framecnt_t nframes);
141
142         template <typename BS, typename B>
143         class iterator_base {
144         public:
145                 B& operator*()  { return (B&)_set.get(_type, _index); }
146                 B* operator->() { return &(B&)_set.get(_type, _index); }
147                 iterator_base<BS,B>& operator++() { ++_index; return *this; } // yes, prefix only
148                 bool operator==(const iterator_base<BS,B>& other) { return (_index == other._index); }
149                 bool operator!=(const iterator_base<BS,B>& other) { return (_index != other._index); }
150                 iterator_base<BS,B> operator=(const iterator_base<BS,B>& other) {
151                         _set = other._set; _type = other._type; _index = other._index; return *this;
152                 }
153
154         private:
155                 friend class BufferSet;
156
157                 iterator_base(BS& list, DataType type, size_t index)
158                         : _set(list), _type(type), _index(index) {}
159
160                 BS&      _set;
161                 DataType _type;
162                 size_t   _index;
163         };
164
165         typedef iterator_base<BufferSet, Buffer> iterator;
166         iterator begin(DataType type) { return iterator(*this, type, 0); }
167         iterator end(DataType type)   { return iterator(*this, type, _count.get(type)); }
168
169         typedef iterator_base<const BufferSet, const Buffer> const_iterator;
170         const_iterator begin(DataType type) const { return const_iterator(*this, type, 0); }
171         const_iterator end(DataType type)   const { return const_iterator(*this, type, _count.get(type)); }
172
173         typedef iterator_base<BufferSet, AudioBuffer> audio_iterator;
174         audio_iterator audio_begin() { return audio_iterator(*this, DataType::AUDIO, 0); }
175         audio_iterator audio_end()   { return audio_iterator(*this, DataType::AUDIO, _count.n_audio()); }
176
177         typedef iterator_base<BufferSet, MidiBuffer> midi_iterator;
178         midi_iterator midi_begin() { return midi_iterator(*this, DataType::MIDI, 0); }
179         midi_iterator midi_end()   { return midi_iterator(*this, DataType::MIDI, _count.n_midi()); }
180
181 private:
182         typedef std::vector<Buffer*> BufferVec;
183
184         /// Vector of vectors, indexed by DataType
185         std::vector<BufferVec> _buffers;
186
187 #ifdef LV2_SUPPORT
188         /// LV2 MIDI buffers (for conversion to/from MIDI buffers)
189         typedef std::vector< std::pair<bool, LV2_Evbuf*> > LV2Buffers;
190         LV2Buffers _lv2_buffers;
191 #endif
192
193 #if defined VST_SUPPORT || defined LXVST_SUPPORT
194         class VSTBuffer {
195         public:
196                 VSTBuffer (size_t);
197                 ~VSTBuffer ();
198
199                 void clear ();
200                 void push_back (Evoral::MIDIEvent<framepos_t> const &);
201                 VstEvents* events () const {
202                         return _events;
203                 }
204
205         private:
206                 /* prevent copy construction */
207                 VSTBuffer (VSTBuffer const &);
208
209                 VstEvents* _events; /// the parent VSTEvents struct
210                 VstMidiEvent* _midi_events; ///< storage area for VSTMidiEvents
211                 size_t _capacity;
212         };
213
214         typedef std::vector<VSTBuffer*> VSTBuffers;
215         VSTBuffers _vst_buffers;
216 #endif
217
218         /// Use counts (there may be more actual buffers than this)
219         ChanCount _count;
220
221         /// Available counts (number of buffers actually allocated)
222         ChanCount _available;
223
224         /// False if we 'own' the contained buffers, if true we mirror a PortSet)
225         bool _is_mirror;
226 };
227
228
229 } // namespace ARDOUR
230
231 #endif // __ardour_buffer_set_h__