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