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