Optimize plugin-processing for non-automated params
[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 <sstream>
27
28 #include "pbd/compose.h"
29 #include "pbd/failed_constructor.h"
30
31 #include "ardour/buffer.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/debug.h"
34 #include "ardour/midi_buffer.h"
35 #include "ardour/port.h"
36 #include "ardour/port_set.h"
37 #ifdef LV2_SUPPORT
38 #include "ardour/lv2_plugin.h"
39 #include "lv2_evbuf.h"
40 #include "ardour/uri_map.h"
41 #endif
42 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
43 #include "ardour/vestige/vestige.h"
44 #endif
45
46 namespace ARDOUR {
47
48 /** Create a new, empty BufferSet */
49 BufferSet::BufferSet()
50         : _is_mirror(false)
51 {
52         for (size_t i=0; i < DataType::num_types; ++i) {
53                 _buffers.push_back(BufferVec());
54         }
55
56         _count.reset();
57         _available.reset();
58 }
59
60 BufferSet::~BufferSet()
61 {
62         clear();
63 }
64
65 /** Destroy all contained buffers.
66  */
67 void
68 BufferSet::clear()
69 {
70         if (!_is_mirror) {
71                 for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
72                         for (BufferVec::iterator j = (*i).begin(); j != (*i).end(); ++j) {
73                                 delete *j;
74                         }
75                         (*i).clear();
76                 }
77         }
78         _buffers.clear();
79         _count.reset();
80         _available.reset();
81
82 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
83         for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
84                 delete *i;
85         }
86
87         _vst_buffers.clear ();
88 #endif
89
90 #ifdef LV2_SUPPORT
91         for (LV2Buffers::iterator i = _lv2_buffers.begin(); i != _lv2_buffers.end(); ++i) {
92                 free ((*i).second);
93         }
94         _lv2_buffers.clear ();
95 #endif
96
97 }
98
99 /** Set up this BufferSet so that its data structures mirror a PortSet's buffers.
100  *  This is quite expensive and not RT-safe, so it should not be called in a process context;
101  *  get_backend_port_addresses() will fill in a structure set up by this method.
102  *
103  *  XXX: this *is* called in a process context; I'm not sure quite what `should not' means above.
104  */
105 void
106 BufferSet::attach_buffers (PortSet& ports)
107 {
108         const ChanCount& count (ports.count());
109
110         clear ();
111
112         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
113                 _buffers.push_back (BufferVec());
114                 BufferVec& v = _buffers[*t];
115                 v.assign (count.n (*t), (Buffer*) 0);
116         }
117
118         _count = ports.count();
119         _available = ports.count();
120
121
122         _is_mirror = true;
123 }
124
125 /** Write the backend port addresses from a PortSet into our data structures.  This
126  *  call assumes that attach_buffers() has already been called for the same PortSet.
127  *  Does not allocate, so RT-safe BUT you can only call Port::get_buffer() from
128  *  the process() callback tree anyway, so this has to be called in RT context.
129  */
130 void
131 BufferSet::get_backend_port_addresses (PortSet& ports, samplecnt_t nframes)
132 {
133         assert (_count == ports.count ());
134         assert (_available == ports.count ());
135         assert (_is_mirror);
136
137         assert (_buffers.size() == DataType::num_types);
138
139         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
140                 BufferVec& v = _buffers[*t];
141
142                 assert (v.size() == ports.num_ports (*t));
143
144                 int i = 0;
145                 for (PortSet::iterator p = ports.begin(*t); p != ports.end(*t); ++p) {
146                         v[i] = &p->get_buffer (nframes);
147                         ++i;
148                 }
149         }
150 }
151
152 /** Ensure that there are @a num_buffers buffers of type @a type available,
153  * each of size at least @a buffer_size
154  */
155 void
156 BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capacity)
157 {
158         assert(type != DataType::NIL);
159         assert(type < _buffers.size());
160
161         if (num_buffers == 0) {
162                 return;
163         }
164
165         // The vector of buffers of the type we care about
166         BufferVec& bufs = _buffers[type];
167
168         // If we're a mirror just make sure we're ok
169         if (_is_mirror) {
170                 assert(_count.get(type) >= num_buffers);
171                 assert(bufs[0]->type() == type);
172                 return;
173         }
174
175         // If there's not enough or they're too small, just nuke the whole thing and
176         // rebuild it (so I'm lazy..)
177         if (bufs.size() < num_buffers
178             || (bufs.size() > 0 && bufs[0]->capacity() < buffer_capacity)) {
179
180                 // Nuke it
181                 for (BufferVec::iterator i = bufs.begin(); i != bufs.end(); ++i) {
182                         delete (*i);
183                 }
184                 bufs.clear();
185
186                 // Rebuild it
187                 for (size_t i = 0; i < num_buffers; ++i) {
188                         bufs.push_back(Buffer::create(type, buffer_capacity));
189                 }
190
191                 _available.set(type, num_buffers);
192                 _count.set (type, num_buffers);
193         }
194
195 #ifdef LV2_SUPPORT
196         // Ensure enough low level MIDI format buffers are available for conversion
197         // in both directions (input & output, out-of-place)
198         if (type == DataType::MIDI && _lv2_buffers.size() < _buffers[type].size() * 2 + 1) {
199                 while (_lv2_buffers.size() < _buffers[type].size() * 2) {
200                         _lv2_buffers.push_back(
201                                 std::make_pair(false, lv2_evbuf_new(buffer_capacity,
202                                                                     LV2_EVBUF_EVENT,
203                                                                     URIMap::instance().urids.atom_Chunk,
204                                                                     URIMap::instance().urids.atom_Sequence)));
205                 }
206         }
207 #endif
208
209 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
210         // As above but for VST
211         if (type == DataType::MIDI) {
212                 while (_vst_buffers.size() < _buffers[type].size()) {
213                         _vst_buffers.push_back (new VSTBuffer (buffer_capacity));
214                 }
215         }
216 #endif
217
218         // Post-conditions
219         assert(bufs[0]->type() == type);
220         assert(bufs.size() >= num_buffers);
221         assert(bufs.size() == _available.get(type));
222         assert(bufs[0]->capacity() >= buffer_capacity);
223 }
224
225 /** Ensure that the number of buffers of each type @a type matches @a chns
226  * and each buffer is of size at least @a buffer_capacity
227  */
228 void
229 BufferSet::ensure_buffers(const ChanCount& chns, size_t buffer_capacity)
230 {
231         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
232                 ensure_buffers (*i, chns.get (*i), buffer_capacity);
233         }
234 }
235
236 /** Get the capacity (size) of the available buffers of the given type.
237  *
238  * All buffers of a certain type always have the same capacity.
239  */
240 size_t
241 BufferSet::buffer_capacity(DataType type) const
242 {
243         assert(_available.get(type) > 0);
244         return _buffers[type][0]->capacity();
245 }
246
247 Buffer&
248 BufferSet::get(DataType type, size_t i)
249 {
250         assert(i < _available.get(type));
251         return *_buffers[type][i];
252 }
253
254 const Buffer&
255 BufferSet::get(DataType type, size_t i) const
256 {
257         assert(i < _available.get(type));
258         return *_buffers[type][i];
259 }
260
261 #ifdef LV2_SUPPORT
262
263 void
264 BufferSet::ensure_lv2_bufsize(bool input, size_t i, size_t buffer_capacity)
265 {
266         assert(count().get(DataType::MIDI) > i);
267
268         LV2Buffers::value_type b     = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
269         LV2_Evbuf*             evbuf = b.second;
270
271         if (lv2_evbuf_get_capacity(evbuf) >= buffer_capacity) return;
272
273         lv2_evbuf_free(b.second);
274         _lv2_buffers.at(i * 2 + (input ? 0 : 1)) =
275                 std::make_pair(false, lv2_evbuf_new(
276                                         buffer_capacity,
277                                         LV2_EVBUF_EVENT,
278                                         URIMap::instance().urids.atom_Chunk,
279                                         URIMap::instance().urids.atom_Sequence));
280 }
281
282 LV2_Evbuf*
283 BufferSet::get_lv2_midi(bool input, size_t i, bool old_api)
284 {
285         assert(count().get(DataType::MIDI) > i);
286
287         LV2Buffers::value_type b     = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
288         LV2_Evbuf*             evbuf = b.second;
289
290         lv2_evbuf_set_type(evbuf, old_api ? LV2_EVBUF_EVENT : LV2_EVBUF_ATOM);
291         lv2_evbuf_reset(evbuf, input);
292         return evbuf;
293 }
294
295 void
296 BufferSet::forward_lv2_midi(LV2_Evbuf* buf, size_t i, bool purge_ardour_buffer)
297 {
298         MidiBuffer& mbuf  = get_midi(i);
299         if (purge_ardour_buffer) {
300                 mbuf.silence(0, 0);
301         }
302         for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(buf);
303                          lv2_evbuf_is_valid(i);
304                          i = lv2_evbuf_next(i)) {
305                 uint32_t samples, subframes, type, size;
306                 uint8_t* data;
307                 lv2_evbuf_get(i, &samples, &subframes, &type, &size, &data);
308                 if (type == URIMap::instance().urids.midi_MidiEvent) {
309                         mbuf.push_back(samples, size, data);
310                 }
311         }
312 }
313
314 void
315 BufferSet::flush_lv2_midi(bool input, size_t i)
316 {
317         MidiBuffer&            mbuf  = get_midi(i);
318         LV2Buffers::value_type b     = _lv2_buffers.at(i * 2 + (input ? 0 : 1));
319         LV2_Evbuf*             evbuf = b.second;
320
321         mbuf.silence(0, 0);
322         for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(evbuf);
323              lv2_evbuf_is_valid(i);
324              i = lv2_evbuf_next(i)) {
325                 uint32_t samples;
326                 uint32_t subframes;
327                 uint32_t type;
328                 uint32_t size;
329                 uint8_t* data;
330                 lv2_evbuf_get(i, &samples, &subframes, &type, &size, &data);
331 #ifndef NDEBUG
332                 DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("(FLUSH) MIDI event of size %1\n", size));
333                 for (uint16_t x = 0; x < size; ++x) {
334                         DEBUG_TRACE (PBD::DEBUG::LV2, string_compose ("\tByte[%1] = %2\n", x, (int) data[x]));
335                 }
336 #endif
337                 if (type == URIMap::instance().urids.midi_MidiEvent) {
338                         // TODO: Make Ardour event buffers generic so plugins can communicate
339                         mbuf.push_back(samples, size, data);
340                 }
341         }
342 }
343
344 #endif /* LV2_SUPPORT */
345
346 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
347
348 VstEvents*
349 BufferSet::get_vst_midi (size_t b)
350 {
351         MidiBuffer& m = get_midi (b);
352         assert (b <= _vst_buffers.size());
353         VSTBuffer* vst = _vst_buffers[b];
354
355         vst->clear ();
356
357         for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
358                 vst->push_back (*i);
359         }
360
361         return vst->events();
362 }
363
364 BufferSet::VSTBuffer::VSTBuffer (size_t c)
365         : _events (0)
366         , _midi_events (0)
367         , _capacity (c)
368 {
369         if (_capacity > 0) {
370                 /* from `man malloc`: "If size is 0, then malloc() returns either NULL, or a
371                  * unique pointer value that can later be successfully passed to free()."
372                  *
373                  * The latter will cause trouble here.
374                  */
375                 _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
376                 _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
377         }
378
379         if (_events == 0 || _midi_events == 0) {
380                 free (_events);
381                 free (_midi_events);
382                 _events = 0;
383                 _midi_events = 0;
384                 throw failed_constructor ();
385         }
386
387         _events->numEvents = 0;
388         _events->reserved = 0;
389 }
390
391 BufferSet::VSTBuffer::~VSTBuffer ()
392 {
393         free (_events);
394         free (_midi_events);
395 }
396
397 void
398 BufferSet::VSTBuffer::clear ()
399 {
400         _events->numEvents = 0;
401 }
402
403 void
404 BufferSet::VSTBuffer::push_back (Evoral::Event<samplepos_t> const & ev)
405 {
406         if (ev.size() > 3) {
407                 /* XXX: this will silently drop MIDI messages longer than 3 bytes, so
408                    they won't be passed to VST plugins or VSTis
409                 */
410                 return;
411         }
412         uint32_t const n = _events->numEvents;
413         assert (n < _capacity);
414         if (n >= _capacity) {
415                 return;
416         }
417
418         _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
419         VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);
420
421         v->type = kVstMidiType;
422         v->byteSize = sizeof (VstMidiEvent);
423         v->deltaSamples = ev.time ();
424
425         v->flags = 0;
426         v->detune = 0;
427         v->noteLength = 0;
428         v->noteOffset = 0;
429         v->reserved1 = 0;
430         v->reserved2 = 0;
431         v->noteOffVelocity = 0;
432         memcpy (v->midiData, ev.buffer(), ev.size());
433         v->midiData[3] = 0;
434
435         _events->numEvents++;
436 }
437
438 #endif /* WINDOWS_VST_SUPPORT */
439
440 /** Copy buffers of one type from `in' to this BufferSet */
441 void
442 BufferSet::read_from (const BufferSet& in, samplecnt_t nframes, DataType type)
443 {
444         assert (available().get (type) >= in.count().get (type));
445
446         BufferSet::iterator o = begin (type);
447         for (BufferSet::const_iterator i = in.begin (type); i != in.end (type); ++i, ++o) {
448                 o->read_from (*i, nframes);
449         }
450
451         _count.set (type, in.count().get (type));
452 }
453
454 /** Copy buffers of all types from `in' to this BufferSet */
455 void
456 BufferSet::read_from (const BufferSet& in, samplecnt_t nframes)
457 {
458         assert(available() >= in.count());
459
460         // Copy all buffers 1:1
461         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
462                 read_from (in, nframes, *t);
463         }
464 }
465
466 void
467 BufferSet::merge_from (const BufferSet& in, samplecnt_t nframes)
468 {
469         /* merge all input buffers into out existing buffers.
470
471            NOTE: if "in" contains more buffers than this set,
472            we will drop the extra buffers.
473
474         */
475
476         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
477                 BufferSet::iterator o = begin(*t);
478                 for (BufferSet::const_iterator i = in.begin(*t); i != in.end(*t) && o != end (*t); ++i, ++o) {
479                         o->merge_from (*i, nframes);
480                 }
481         }
482 }
483
484 void
485 BufferSet::silence (samplecnt_t nframes, samplecnt_t offset)
486 {
487         for (std::vector<BufferVec>::iterator i = _buffers.begin(); i != _buffers.end(); ++i) {
488                 for (BufferVec::iterator b = i->begin(); b != i->end(); ++b) {
489                         (*b)->silence (nframes, offset);
490                 }
491         }
492 }
493
494 } // namespace ARDOUR
495