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