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