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