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