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