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