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