c97a47e42bbb9ef97a907831946fa93a586b375a
[ardour.git] / libs / ardour / buffer_set.cc
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
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <iostream>
25 #include <algorithm>
26 #include "ardour/buffer.h"
27 #include "ardour/buffer_set.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/port.h"
30 #include "ardour/port_set.h"
31 #include "ardour/audioengine.h"
32 #ifdef HAVE_SLV2
33 #include "ardour/lv2_plugin.h"
34 #include "ardour/lv2_event_buffer.h"
35 #endif
36
37 namespace ARDOUR {
38
39 /** Create a new, empty BufferSet */
40 BufferSet::BufferSet()
41         : _is_mirror(false)
42 {
43         for (size_t i=0; i < DataType::num_types; ++i) {
44                 _buffers.push_back(BufferVec());
45         }
46
47         _count.reset();
48         _available.reset();
49 }
50
51 BufferSet::~BufferSet()
52 {
53         clear();
54 }
55
56 /** Destroy all contained buffers.
57  */
58 void
59 BufferSet::clear()
60 {
61         if (!_is_mirror) {
62                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
63                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
64                                 delete *j;
65                         }
66                         (*i).clear();
67                 }
68         }
69         _buffers.clear();
70         _count.reset();
71         _available.reset();
72 }
73
74 /** Make this BufferSet a direct mirror of a PortSet's buffers.
75  */
76 void
77 BufferSet::attach_buffers(PortSet& ports, nframes_t nframes, nframes_t offset)
78 {
79         clear();
80
81         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
82                 _buffers.push_back(BufferVec());
83                 BufferVec& v = _buffers[*t];
84
85                 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
86                         assert(p->type() == *t);
87                         v.push_back(&(p->get_buffer(nframes, offset)));
88                 }
89         }
90
91         _count = ports.count();
92         _available = ports.count();
93
94         _is_mirror = true;
95 }
96
97 /** Ensure that there are @a num_buffers buffers of type @a type available,
98  * each of size at least @a buffer_size
99  */
100 void
101 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
102 {
103         assert(type != DataType::NIL);
104         assert(type < _buffers.size());
105
106         if (num_buffers == 0) {
107                 return;
108         }
109
110         // The vector of buffers of the type we care about
111         BufferVec& bufs = _buffers[type];
112
113         // If we're a mirror just make sure we're ok
114         if (_is_mirror) {
115                 assert(_count.get(type) >= num_buffers);
116                 assert(bufs[0]->type() == type);
117                 return;
118         }
119
120         // If there's not enough or they're too small, just nuke the whole thing and
121         // rebuild it (so I'm lazy..)
122         if (bufs.size() < num_buffers
123                         || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
124
125                 // Nuke it
126                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
127                         delete (*i);
128                 }
129                 bufs.clear();
130
131                 // Rebuild it
132                 for (size_t i = 0; i < num_buffers; ++i) {
133                         bufs.push_back(Buffer::create(type, buffer_capacity));
134                 }
135
136                 _available.set(type, num_buffers);
137                 _count.set (type, num_buffers);
138         }
139
140 #ifdef HAVE_SLV2
141         // Ensure enough low level MIDI format buffers are available for conversion
142         // in both directions (input & output, out-of-place)
143         if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
144                 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
145                         _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
146                 }
147         }
148 #endif
149
150         // Post-conditions
151         assert(bufs[0]->type() == type);
152         assert(bufs.size() >= num_buffers);
153         assert(bufs.size() == _available.get(type));
154         assert(bufs[0]->capacity() >= buffer_capacity);
155 }
156
157 /** Ensure that the number of buffers of each type @a type matches @a chns
158  * and each buffer is of size at least @a buffer_capacity
159  */
160 void
161 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
162 {
163         if (chns == ChanCount::ZERO) {
164                 return;
165         }
166
167         // If we're a mirror just make sure we're ok
168         if (_is_mirror) {
169                 assert(_count >= chns);
170                 return;
171         }
172
173         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
174
175                 // The vector of buffers of this type
176                 BufferVec& bufs = _buffers[*t];
177
178                 uint32_t nbufs = chns.get (*t);
179
180                 if (nbufs == 0) {
181                         // Nuke it
182                         for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
183                                 delete (*i);
184                         }
185                         bufs.clear();
186                         continue;
187                 }
188
189                 // If there's not enough or they're too small, just nuke the whole thing and
190                 // rebuild it (so I'm lazy..)
191                 if (bufs.size() < nbufs
192                     || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
193
194                         // Nuke it
195                         for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
196                                 delete (*i);
197                         }
198                         bufs.clear();
199
200                         // Rebuild it
201                         for (size_t i = 0; i < nbufs; ++i) {
202                                 bufs.push_back(Buffer::create(*t, buffer_capacity));
203                         }
204
205                         _available.set (*t, nbufs);
206                 }
207
208 #ifdef HAVE_SLV2
209                 // Ensure enough low level MIDI format buffers are available for conversion
210                 // in both directions (input & output, out-of-place)
211                 if (*t == DataType::MIDI && _lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2 + 1) {
212                         while (_lv2_buffers.size() < _buffers[DataType::MIDI].size() * 2) {
213                                 _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
214                         }
215                 }
216 #endif
217
218                 // Post-conditions
219                 assert(bufs[0]->type() == *t);
220                 assert(bufs.size() == _available.get(*t));
221                 assert(bufs[0]->capacity() >= buffer_capacity);
222         }
223
224         assert (available() >= chns);
225 }
226
227 /** Get the capacity (size) of the available buffers of the given type.
228  *
229  * All buffers of a certain type always have the same capacity.
230  */
231 size_t
232 BufferSet::buffer_capacity(DataType type) const
233 {
234         assert(_available.get(type) > 0);
235         return _buffers[type][0]->capacity();
236 }
237
238 Buffer&
239 BufferSet::get(DataType type, size_t i)
240 {
241         assert(i < _available.get(type));
242         return *_buffers[type][i];
243 }
244
245 const Buffer&
246 BufferSet::get(DataType type, size_t i) const
247 {
248         assert(i < _available.get(type));
249         return *_buffers[type][i];
250 }
251
252 #ifdef HAVE_SLV2
253
254 LV2EventBuffer&
255 BufferSet::get_lv2_midi(bool input, size_t i)
256 {
257         MidiBuffer& mbuf = get_midi(i);
258         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
259         LV2EventBuffer* ebuf = b.second;
260
261         ebuf->reset();
262         if (input) {
263                 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
264                         const Evoral::MIDIEvent<nframes_t> ev(*e, false);
265                         uint32_t type = LV2Plugin::midi_event_type();
266                         ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
267                 }
268         }
269         return *ebuf;
270 }
271
272 void
273 BufferSet::flush_lv2_midi(bool input, size_t i)
274 {
275         MidiBuffer& mbuf = get_midi(i);
276         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
277         LV2EventBuffer* ebuf = b.second;
278
279         mbuf.silence(0, 0);
280         for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
281                 uint32_t frames;
282                 uint32_t subframes;
283                 uint16_t type;
284                 uint16_t size;
285                 uint8_t* data;
286                 ebuf->get_event(&frames, &subframes, &type, &size, &data);
287                 mbuf.push_back(frames, size, data);
288         }
289 }
290
291 #endif
292
293 void
294 BufferSet::read_from (const BufferSet& in, nframes_t nframes)
295 {
296         assert(available() >= in.count());
297
298         // Copy all buffers 1:1
299         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
300                 BufferSet::iterator o = begin(*t);
301                 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
302                         o->read_from (*i, nframes);
303                 }
304         }
305
306         set_count(in.count());
307 }
308
309 void
310 BufferSet::merge_from (const BufferSet& in, nframes_t nframes)
311 {
312         /* merge all input buffers into out existing buffers.
313
314            NOTE: if "in" contains more buffers than this set,
315            we will drop the extra buffers.
316
317         */
318
319         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
320                 BufferSet::iterator o = begin(*t);
321                 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
322                         o->merge_from (*i, nframes);
323                 }
324         }
325 }
326
327 void
328 BufferSet::silence (nframes_t nframes, nframes_t offset)
329 {
330         for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
331                 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
332                         (*b)->silence (nframes, offset);
333                 }
334         }
335 }
336
337 void
338 BufferSet::is_silent (bool yn)
339 {
340         for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
341                 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
342                         (*b)->is_silent (yn);
343                 }
344         }
345                 
346 }
347
348 } // namespace ARDOUR
349