Optimise BufferSet::attach_buffers code to avoid memory allocation in the RT thread...
[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 #ifdef VST_SUPPORT
37 #include "vestige/aeffectx.h"
38 #endif
39
40 namespace ARDOUR {
41
42 /** Create a new, empty BufferSet */
43 BufferSet::BufferSet()
44         : _is_mirror(false)
45 {
46         for (size_t i=0; i < DataType::num_types; ++i) {
47                 _buffers.push_back(BufferVec());
48         }
49
50         _count.reset();
51         _available.reset();
52 }
53
54 BufferSet::~BufferSet()
55 {
56         clear();
57 }
58
59 /** Destroy all contained buffers.
60  */
61 void
62 BufferSet::clear()
63 {
64         if (!_is_mirror) {
65                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
66                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
67                                 delete *j;
68                         }
69                         (*i).clear();
70                 }
71         }
72         _buffers.clear();
73         _count.reset();
74         _available.reset();
75
76 #ifdef VST_SUPPORT      
77         for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
78                 delete *i;
79         }
80
81         _vst_buffers.clear ();
82 #endif  
83 }
84
85 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
86  *  This is quite expensive and not RT-safe, so it should not be called in a process context;
87  *  get_jack_port_addresses() will fill in a structure set up by this method.
88  */
89 void
90 BufferSet::attach_buffers (PortSet& ports)
91 {
92         clear();
93
94         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
95                 _buffers.push_back(BufferVec());
96                 BufferVec& v = _buffers[*t];
97
98                 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
99                         assert(p->type() == *t);
100                         v.push_back (0);
101                 }
102         }
103
104         _count = ports.count();
105         _available = ports.count();
106
107         _is_mirror = true;
108 }
109
110 /** Write the JACK port addresses from a PortSet into our data structures.  This
111  *  call assumes that attach_buffers() has already been called for the same PortSet.
112  *  Does not allocate, so RT-safe.
113  */
114 void
115 BufferSet::get_jack_port_addresses (PortSet& ports, framecnt_t nframes, framecnt_t offset)
116 {
117         assert (_count == ports.count ());
118         assert (_available == ports.count ());
119         assert (_is_mirror);
120
121         assert (_buffers.size() == DataType::num_types);
122
123         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
124                 BufferVec& v = _buffers[*t];
125
126                 assert (v.size() == ports.num_ports (*t));
127
128                 int i = 0;
129                 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
130                         v[i] = &p->get_buffer (nframes, offset);
131                         ++i;
132                 }
133         }
134 }
135
136 /** Ensure that there are @a num_buffers buffers of type @a type available,
137  * each of size at least @a buffer_size
138  */
139 void
140 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
141 {
142         assert(type != DataType::NIL);
143         assert(type < _buffers.size());
144
145         if (num_buffers == 0) {
146                 return;
147         }
148
149         // The vector of buffers of the type we care about
150         BufferVec& bufs = _buffers[type];
151
152         // If we're a mirror just make sure we're ok
153         if (_is_mirror) {
154                 assert(_count.get(type) >= num_buffers);
155                 assert(bufs[0]->type() == type);
156                 return;
157         }
158
159         // If there's not enough or they're too small, just nuke the whole thing and
160         // rebuild it (so I'm lazy..)
161         if (bufs.size() < num_buffers
162                         || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
163
164                 // Nuke it
165                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
166                         delete (*i);
167                 }
168                 bufs.clear();
169
170                 // Rebuild it
171                 for (size_t i = 0; i < num_buffers; ++i) {
172                         bufs.push_back(Buffer::create(type, buffer_capacity));
173                 }
174
175                 _available.set(type, num_buffers);
176                 _count.set (type, num_buffers);
177         }
178
179 #ifdef HAVE_SLV2
180         // Ensure enough low level MIDI format buffers are available for conversion
181         // in both directions (input & output, out-of-place)
182         if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
183                 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
184                         _lv2_buffers.push_back(std::make_pair(false, new LV2EventBuffer(buffer_capacity)));
185                 }
186         }
187 #endif
188
189 #ifdef VST_SUPPORT
190         // As above but for VST
191         if (type == DataType::MIDI) {
192                 while (_vst_buffers.size() < _buffers[type].size()) {
193                         _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
194                 }
195         }
196 #endif  
197
198         // Post-conditions
199         assert(bufs[0]->type() == type);
200         assert(bufs.size() >= num_buffers);
201         assert(bufs.size() == _available.get(type));
202         assert(bufs[0]->capacity() >= buffer_capacity);
203 }
204
205 /** Ensure that the number of buffers of each type @a type matches @a chns
206  * and each buffer is of size at least @a buffer_capacity
207  */
208 void
209 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
210 {
211         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
212                 ensure_buffers (*i, chns.get (*i), buffer_capacity);
213         }
214 }
215
216 /** Get the capacity (size) of the available buffers of the given type.
217  *
218  * All buffers of a certain type always have the same capacity.
219  */
220 size_t
221 BufferSet::buffer_capacity(DataType type) const
222 {
223         assert(_available.get(type) > 0);
224         return _buffers[type][0]->capacity();
225 }
226
227 Buffer&
228 BufferSet::get(DataType type, size_t i)
229 {
230         assert(i < _available.get(type));
231         return *_buffers[type][i];
232 }
233
234 const Buffer&
235 BufferSet::get(DataType type, size_t i) const
236 {
237         assert(i < _available.get(type));
238         return *_buffers[type][i];
239 }
240
241 #ifdef HAVE_SLV2
242
243 LV2EventBuffer&
244 BufferSet::get_lv2_midi(bool input, size_t i)
245 {
246         assert (count().get(DataType::MIDI) > i);
247         
248         MidiBuffer& mbuf = get_midi(i);
249         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
250         LV2EventBuffer* ebuf = b.second;
251
252         ebuf->reset();
253         if (input) {
254                 for (MidiBuffer::iterator e = mbuf.begin(); e != mbuf.end(); ++e) {
255                         const Evoral::MIDIEvent<framepos_t> ev(*e, false);
256                         uint32_t type = LV2Plugin::midi_event_type();
257                         ebuf->append(ev.time(), 0, type, ev.size(), ev.buffer());
258                 }
259         }
260         return *ebuf;
261 }
262
263 void
264 BufferSet::flush_lv2_midi(bool input, size_t i)
265 {
266         MidiBuffer& mbuf = get_midi(i);
267         LV2Buffers::value_type b = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
268         LV2EventBuffer* ebuf = b.second;
269
270         mbuf.silence(0, 0);
271         for (ebuf->rewind(); ebuf->is_valid(); ebuf->increment()) {
272                 uint32_t frames;
273                 uint32_t subframes;
274                 uint16_t type;
275                 uint16_t size;
276                 uint8_t* data;
277                 ebuf->get_event(&frames, &subframes, &type, &size, &data);
278                 mbuf.push_back(frames, size, data);
279         }
280 }
281
282 #endif /* HAVE_SLV2 */
283
284 #ifdef VST_SUPPORT
285
286 VstEvents*
287 BufferSet::get_vst_midi (size_t b)
288 {
289         MidiBuffer& m = get_midi (b);
290         VSTBuffer* vst = _vst_buffers[b];
291
292         vst->clear ();
293
294         for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
295                 vst->push_back (*i);
296         }
297         
298         return vst->events();
299 }
300
301 BufferSet::VSTBuffer::VSTBuffer (size_t c)
302   : _capacity (c)
303 {
304         _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
305         _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
306
307         if (_events == 0 || _midi_events == 0) {
308                 free (_events);
309                 free (_midi_events);
310                 throw failed_constructor ();
311         }
312
313         _events->numEvents = 0;
314         _events->reserved = 0;
315 }
316
317 BufferSet::VSTBuffer::~VSTBuffer ()
318 {
319         free (_events);
320         free (_midi_events);
321 }
322
323 void
324 BufferSet::VSTBuffer::clear ()
325 {
326         _events->numEvents = 0;
327 }
328
329 void
330 BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
331 {
332         if (ev.size() > 3) {
333                 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
334                    they won't be passed to VST plugins or VSTis
335                 */
336                 return;
337         }
338         int const n = _events->numEvents;
339         assert (n < (int) _capacity);
340
341         _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
342         VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
343         
344         v->type = kVstMidiType;
345         v->byteSize = sizeof (VstMidiEvent);
346         v->deltaFrames = ev.time ();
347
348         v->flags = 0;
349         v->detune = 0;
350         v->noteLength = 0;
351         v->noteOffset = 0;
352         v->reserved1 = 0;
353         v->reserved2 = 0;
354         v->noteOffVelocity = 0;
355         memcpy (v->midiData, ev.buffer(), ev.size());
356         v->midiData[3] = 0;
357
358         _events->numEvents++;
359 }
360
361 #endif /* VST_SUPPORT */
362
363 void
364 BufferSet::read_from (const BufferSet& in, framecnt_t nframes)
365 {
366         assert(available() >= in.count());
367
368         // Copy all buffers 1:1
369         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
370                 BufferSet::iterator o = begin(*t);
371                 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t); ++i, ++o) {
372                         o->read_from (*i, nframes);
373                 }
374         }
375
376         set_count(in.count());
377 }
378
379 void
380 BufferSet::merge_from (const BufferSet& in, framecnt_t nframes)
381 {
382         /* merge all input buffers into out existing buffers.
383
384            NOTE: if "in" contains more buffers than this set,
385            we will drop the extra buffers.
386
387         */
388
389         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
390                 BufferSet::iterator o = begin(*t);
391                 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
392                         o->merge_from (*i, nframes);
393                 }
394         }
395 }
396
397 void
398 BufferSet::silence (framecnt_t nframes, framecnt_t offset)
399 {
400         for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
401                 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
402                         (*b)->silence (nframes, offset);
403                 }
404         }
405 }
406
407 void
408 BufferSet::is_silent (bool yn)
409 {
410         for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
411                 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
412                         (*b)->is_silent (yn);
413                 }
414         }
415                 
416 }
417
418 } // namespace ARDOUR
419